blob: 3dc21e380b00718b06f7d176bdd3b82d3aac9bf4 [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 Rodgman16799db2023-11-02 19:47:20 +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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020022#include "mbedtls/private_access.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030023
Bence Szépkútic662b362021-05-27 11:25:03 +020024#include "mbedtls/build_info.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030025
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020026/* for shared error codes */
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020028
Gilles Peskined2971572021-07-26 18:48:10 +020029/** The requested operation is not permitted in the current state. */
30#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054
31/** Authenticated decryption failed: data was not authentic. */
32#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056
Daniel Kingb8025c52016-05-17 14:43:01 -030033
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020034#ifdef __cplusplus
35extern "C" {
36#endif
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038typedef enum {
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020039 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
40 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030041}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020042mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030043
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020044#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020045
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010046#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020047
Gilles Peskine449bd832023-01-11 14:50:10 +010048typedef struct mbedtls_chachapoly_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020049 mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */
50 mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */
51 uint64_t MBEDTLS_PRIVATE(aad_len); /**< The length (bytes) of the Additional Authenticated Data. */
52 uint64_t MBEDTLS_PRIVATE(ciphertext_len); /**< The length (bytes) of the ciphertext. */
53 int MBEDTLS_PRIVATE(state); /**< The current state of the context. */
54 mbedtls_chachapoly_mode_t MBEDTLS_PRIVATE(mode); /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030055}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020056mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030057
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020058#else /* !MBEDTLS_CHACHAPOLY_ALT */
59#include "chachapoly_alt.h"
60#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020061
Daniel Kingb8025c52016-05-17 14:43:01 -030062/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020063 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030064 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020065 * It must be the first API called before using
66 * the context. It must be followed by a call to
67 * \c mbedtls_chachapoly_setkey() before any operation can be
68 * done, and to \c mbedtls_chachapoly_free() once all
69 * operations with that context have been finished.
70 *
71 * In order to encrypt or decrypt full messages at once, for
72 * each message you should make a single call to
73 * \c mbedtls_chachapoly_crypt_and_tag() or
74 * \c mbedtls_chachapoly_auth_decrypt().
75 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020076 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020077 * message you should make a call to
78 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
79 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
80 * \c mbedtls_chachapoly_update(), then one call to
81 * \c mbedtls_chachapoly_finish().
82 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020083 * \warning Decryption with the piecewise API is discouraged! Always
84 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
85 *
86 * If however this is not possible because the data is too
87 * large to fit in memory, you need to:
88 *
89 * - call \c mbedtls_chachapoly_starts() and (if needed)
90 * \c mbedtls_chachapoly_update_aad() as above,
91 * - call \c mbedtls_chachapoly_update() multiple times and
92 * ensure its output (the plaintext) is NOT used in any other
93 * way than placing it in temporary storage at this point,
94 * - call \c mbedtls_chachapoly_finish() to compute the
95 * authentication tag and compared it in constant time to the
96 * tag received with the ciphertext.
97 *
98 * If the tags are not equal, you must immediately discard
99 * all previous outputs of \c mbedtls_chachapoly_update(),
100 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200101 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -0300105
106/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * \brief This function releases and clears the specified
108 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300109 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
111 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -0300114
115/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * \brief This function sets the ChaCha20-Poly1305
117 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300118 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200119 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * bound. This must be initialized.
121 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300122 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200123 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx,
127 const unsigned char key[32]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300128
129/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200130 * \brief This function starts a ChaCha20-Poly1305 encryption or
131 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300132 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \warning You must never use the same nonce twice with the same key.
134 * This would void any confidentiality and authenticity
135 * guarantees for the messages encrypted with the same nonce
136 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300137 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200138 * \note If the context is being used for AAD only (no data to
139 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300140 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200141 * \warning Decryption with the piecewise API is discouraged, see the
142 * warning on \c mbedtls_chachapoly_init().
143 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
145 * and bound to a key.
146 * \param nonce The nonce/IV to use for the message.
Tom Cosgrove1e211442022-05-26 11:51:00 +0100147 * This must be a readable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200148 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200149 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 *
151 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx,
155 const unsigned char nonce[12],
156 mbedtls_chachapoly_mode_t mode);
Daniel Kingb8025c52016-05-17 14:43:01 -0300157
158/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200159 * \brief This function feeds additional data to be authenticated
160 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300161 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * The Additional Authenticated Data (AAD), also called
163 * Associated Data (AD) is only authenticated but not
164 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200165 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300167 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 * \note This function is called before data is encrypted/decrypted.
169 * I.e. call this function to process the AAD before calling
170 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300171 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * You may call this function multiple times to process
173 * an arbitrary amount of AAD. It is permitted to call
174 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300175 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * This function cannot be called any more if data has
177 * been processed by \c mbedtls_chachapoly_update(),
178 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300179 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200180 * \warning Decryption with the piecewise API is discouraged, see the
181 * warning on \c mbedtls_chachapoly_init().
182 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
184 * and bound to a key.
185 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * restrictions.
187 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300189 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200191 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * if \p ctx or \p aad are NULL.
193 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
194 * if the operations has not been started or has been
195 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx,
198 const unsigned char *aad,
199 size_t aad_len);
Daniel Kingb8025c52016-05-17 14:43:01 -0300200
201/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200202 * \brief Thus function feeds data to be encrypted or decrypted
203 * into an on-going ChaCha20-Poly1305
204 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300205 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * The direction (encryption or decryption) depends on the
207 * mode that was given when calling
208 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * You may call this function multiple times to process
211 * an arbitrary amount of data. It is permitted to call
212 * this function 0 times, if no data is to be encrypted
213 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300214 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200215 * \warning Decryption with the piecewise API is discouraged, see the
216 * warning on \c mbedtls_chachapoly_init().
217 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200219 * \param len The length (in bytes) of the data to encrypt or decrypt.
220 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 * This pointer can be \c NULL if `len == 0`.
222 * \param output The buffer to where the encrypted or decrypted data is
223 * written. This must be able to hold \p len bytes.
224 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300225 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200226 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200227 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
228 * if the operation has not been started or has been
229 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx,
233 size_t len,
234 const unsigned char *input,
235 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300236
237/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200238 * \brief This function finished the ChaCha20-Poly1305 operation and
239 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300240 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500241 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200242 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300243 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200244 * \warning Decryption with the piecewise API is discouraged, see the
245 * warning on \c mbedtls_chachapoly_init().
246 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200247 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200248 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
249 * if the operation has not been started or has been
250 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300252 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx,
254 unsigned char mac[16]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300255
Daniel Kingb8025c52016-05-17 14:43:01 -0300256/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200257 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200258 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300259 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200260 * \note Before using this function, you must set the key with
261 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300262 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200263 * \warning You must never use the same nonce twice with the same key.
264 * This would void any confidentiality and authenticity
265 * guarantees for the messages encrypted with the same nonce
266 * and key.
267 *
268 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200270 * \param length The length (in bytes) of the data to encrypt or decrypt.
271 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * \param aad The buffer containing the additional authenticated
273 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200274 * \param aad_len The length (in bytes) of the AAD data to process.
275 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * This pointer can be \c NULL if `ilen == 0`.
277 * \param output The buffer to where the encrypted or decrypted data
278 * is written. This pointer can be \c NULL if `ilen == 0`.
279 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
280 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200281 *
282 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx,
286 size_t length,
287 const unsigned char nonce[12],
288 const unsigned char *aad,
289 size_t aad_len,
290 const unsigned char *input,
291 unsigned char *output,
292 unsigned char tag[16]);
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200293
294/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200295 * \brief This function performs a complete ChaCha20-Poly1305
296 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200297 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200298 * \note Before using this function, you must set the key with
299 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200300 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200301 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500302 * \param length The length (in Bytes) of the data to decrypt.
303 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200304 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200306 * \param aad_len The length (in bytes) of the AAD data to process.
307 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200309 * \param input The buffer containing the data to decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200311 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200313 *
314 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200315 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
316 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500317 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200318 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx,
320 size_t length,
321 const unsigned char nonce[12],
322 const unsigned char *aad,
323 size_t aad_len,
324 const unsigned char tag[16],
325 const unsigned char *input,
326 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300327
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200328#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300329/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200330 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300331 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200332 * \return \c 0 on success.
333 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300334 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335int mbedtls_chachapoly_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200336#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300337
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200338#ifdef __cplusplus
339}
340#endif
341
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200342#endif /* MBEDTLS_CHACHAPOLY_H */