blob: 1156d7db81bc2d3c77f3f5be9dce101662e4368d [file] [log] [blame]
Daniel Kingb8025c52016-05-17 14:43:01 -03001/**
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002 * \file chachapoly.h
Daniel Kingb8025c52016-05-17 14:43:01 -03003 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02004 * \brief This file contains the AEAD-ChaCha20-Poly1305 definitions and
5 * functions.
Daniel Kingb8025c52016-05-17 14:43:01 -03006 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02007 * ChaCha20-Poly1305 is an algorithm for Authenticated Encryption
8 * with Associated Data (AEAD) that can be used to encrypt and
9 * authenticate data. It is based on ChaCha20 and Poly1305 by Daniel
10 * Bernstein and was standardized in RFC 7539.
11 *
12 * \author Daniel King <damaki.gh@gmail.com>
13 */
14
Bence Szépkúti86974652020-06-15 11:59:37 +020015/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Daniel Kingb8025c52016-05-17 14:43:01 -030018 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020019
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020020#ifndef MBEDTLS_CHACHAPOLY_H
21#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030022
23#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010024#include "mbedtls/config.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030025#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020029/* for shared error codes */
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020031
Gilles Peskinea3974432021-07-26 18:48:10 +020032/** The requested operation is not permitted in the current state. */
33#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054
34/** Authenticated decryption failed: data was not authentic. */
35#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056
Daniel Kingb8025c52016-05-17 14:43:01 -030036
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041typedef enum {
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020042 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
43 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030044}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020045mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030046
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020047#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020048
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010049#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051typedef struct mbedtls_chachapoly_context {
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020052 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
53 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
54 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
55 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
56 int state; /**< The current state of the context. */
57 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030058}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020059mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030060
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020061#else /* !MBEDTLS_CHACHAPOLY_ALT */
62#include "chachapoly_alt.h"
63#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020064
Daniel Kingb8025c52016-05-17 14:43:01 -030065/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020066 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030067 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020068 * It must be the first API called before using
69 * the context. It must be followed by a call to
70 * \c mbedtls_chachapoly_setkey() before any operation can be
71 * done, and to \c mbedtls_chachapoly_free() once all
72 * operations with that context have been finished.
73 *
74 * In order to encrypt or decrypt full messages at once, for
75 * each message you should make a single call to
76 * \c mbedtls_chachapoly_crypt_and_tag() or
77 * \c mbedtls_chachapoly_auth_decrypt().
78 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020079 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020080 * message you should make a call to
81 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
82 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
83 * \c mbedtls_chachapoly_update(), then one call to
84 * \c mbedtls_chachapoly_finish().
85 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020086 * \warning Decryption with the piecewise API is discouraged! Always
87 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
88 *
89 * If however this is not possible because the data is too
90 * large to fit in memory, you need to:
91 *
92 * - call \c mbedtls_chachapoly_starts() and (if needed)
93 * \c mbedtls_chachapoly_update_aad() as above,
94 * - call \c mbedtls_chachapoly_update() multiple times and
95 * ensure its output (the plaintext) is NOT used in any other
96 * way than placing it in temporary storage at this point,
97 * - call \c mbedtls_chachapoly_finish() to compute the
98 * authentication tag and compared it in constant time to the
99 * tag received with the ciphertext.
100 *
101 * If the tags are not equal, you must immediately discard
102 * all previous outputs of \c mbedtls_chachapoly_update(),
103 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -0300108
109/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \brief This function releases and clears the specified
111 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
114 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -0300117
118/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 * \brief This function sets the ChaCha20-Poly1305
120 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300121 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * bound. This must be initialized.
124 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300125 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300128 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx,
130 const unsigned char key[32]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300131
132/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \brief This function starts a ChaCha20-Poly1305 encryption or
134 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300135 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 * \warning You must never use the same nonce twice with the same key.
137 * This would void any confidentiality and authenticity
138 * guarantees for the messages encrypted with the same nonce
139 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \note If the context is being used for AAD only (no data to
142 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300143 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200144 * \warning Decryption with the piecewise API is discouraged, see the
145 * warning on \c mbedtls_chachapoly_init().
146 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
148 * and bound to a key.
149 * \param nonce The nonce/IV to use for the message.
Tom Cosgrove2b150752022-05-26 11:55:43 +0100150 * This must be a readable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200152 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 *
154 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300156 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx,
158 const unsigned char nonce[12],
159 mbedtls_chachapoly_mode_t mode);
Daniel Kingb8025c52016-05-17 14:43:01 -0300160
161/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \brief This function feeds additional data to be authenticated
163 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300164 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * The Additional Authenticated Data (AAD), also called
166 * Associated Data (AD) is only authenticated but not
167 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200168 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200169 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300170 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200171 * \note This function is called before data is encrypted/decrypted.
172 * I.e. call this function to process the AAD before calling
173 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300174 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 * You may call this function multiple times to process
176 * an arbitrary amount of AAD. It is permitted to call
177 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300178 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200179 * This function cannot be called any more if data has
180 * been processed by \c mbedtls_chachapoly_update(),
181 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300182 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200183 * \warning Decryption with the piecewise API is discouraged, see the
184 * warning on \c mbedtls_chachapoly_init().
185 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
187 * and bound to a key.
188 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200189 * restrictions.
190 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300192 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200193 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200194 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200195 * if \p ctx or \p aad are NULL.
196 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
197 * if the operations has not been started or has been
198 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx,
201 const unsigned char *aad,
202 size_t aad_len);
Daniel Kingb8025c52016-05-17 14:43:01 -0300203
204/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200205 * \brief Thus function feeds data to be encrypted or decrypted
206 * into an on-going ChaCha20-Poly1305
207 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300208 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200209 * The direction (encryption or decryption) depends on the
210 * mode that was given when calling
211 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300212 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200213 * You may call this function multiple times to process
214 * an arbitrary amount of data. It is permitted to call
215 * this function 0 times, if no data is to be encrypted
216 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300217 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200218 * \warning Decryption with the piecewise API is discouraged, see the
219 * warning on \c mbedtls_chachapoly_init().
220 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200222 * \param len The length (in bytes) of the data to encrypt or decrypt.
223 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 * This pointer can be \c NULL if `len == 0`.
225 * \param output The buffer to where the encrypted or decrypted data is
226 * written. This must be able to hold \p len bytes.
227 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300228 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200229 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200230 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
231 * if the operation has not been started or has been
232 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300234 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx,
236 size_t len,
237 const unsigned char *input,
238 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300239
240/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200241 * \brief This function finished the ChaCha20-Poly1305 operation and
242 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300243 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200245 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300246 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200247 * \warning Decryption with the piecewise API is discouraged, see the
248 * warning on \c mbedtls_chachapoly_init().
249 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200250 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200251 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
252 * if the operation has not been started or has been
253 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx,
257 unsigned char mac[16]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300258
Daniel Kingb8025c52016-05-17 14:43:01 -0300259/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200260 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200261 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300262 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200263 * \note Before using this function, you must set the key with
264 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300265 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200266 * \warning You must never use the same nonce twice with the same key.
267 * This would void any confidentiality and authenticity
268 * guarantees for the messages encrypted with the same nonce
269 * and key.
270 *
271 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200273 * \param length The length (in bytes) of the data to encrypt or decrypt.
274 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 * \param aad The buffer containing the additional authenticated
276 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200277 * \param aad_len The length (in bytes) of the AAD data to process.
278 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500279 * This pointer can be \c NULL if `ilen == 0`.
280 * \param output The buffer to where the encrypted or decrypted data
281 * is written. This pointer can be \c NULL if `ilen == 0`.
282 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
283 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200284 *
285 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300287 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx,
289 size_t length,
290 const unsigned char nonce[12],
291 const unsigned char *aad,
292 size_t aad_len,
293 const unsigned char *input,
294 unsigned char *output,
295 unsigned char tag[16]);
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200296
297/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200298 * \brief This function performs a complete ChaCha20-Poly1305
299 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200300 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200301 * \note Before using this function, you must set the key with
302 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200303 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200304 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 * \param length The length (in Bytes) of the data to decrypt.
306 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200307 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200309 * \param aad_len The length (in bytes) of the AAD data to process.
310 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200312 * \param input The buffer containing the data to decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200314 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200316 *
317 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200318 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
319 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200321 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx,
323 size_t length,
324 const unsigned char nonce[12],
325 const unsigned char *aad,
326 size_t aad_len,
327 const unsigned char tag[16],
328 const unsigned char *input,
329 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300330
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200331#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300332/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200333 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300334 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200335 * \return \c 0 on success.
336 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300337 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338int mbedtls_chachapoly_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200339#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300340
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200341#ifdef __cplusplus
342}
343#endif
344
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200345#endif /* MBEDTLS_CHACHAPOLY_H */