blob: 6c236fe5e6ad0f5acb86e26fdcc637a46bf34463 [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
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010044#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020045
Gilles Peskine449bd832023-01-11 14:50:10 +010046typedef struct mbedtls_chachapoly_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020047 mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */
48 mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */
49 uint64_t MBEDTLS_PRIVATE(aad_len); /**< The length (bytes) of the Additional Authenticated Data. */
50 uint64_t MBEDTLS_PRIVATE(ciphertext_len); /**< The length (bytes) of the ciphertext. */
51 int MBEDTLS_PRIVATE(state); /**< The current state of the context. */
52 mbedtls_chachapoly_mode_t MBEDTLS_PRIVATE(mode); /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030053}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020054mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030055
56/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020057 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030058 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020059 * It must be the first API called before using
60 * the context. It must be followed by a call to
61 * \c mbedtls_chachapoly_setkey() before any operation can be
62 * done, and to \c mbedtls_chachapoly_free() once all
63 * operations with that context have been finished.
64 *
65 * In order to encrypt or decrypt full messages at once, for
66 * each message you should make a single call to
67 * \c mbedtls_chachapoly_crypt_and_tag() or
68 * \c mbedtls_chachapoly_auth_decrypt().
69 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020070 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020071 * message you should make a call to
72 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
73 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
74 * \c mbedtls_chachapoly_update(), then one call to
75 * \c mbedtls_chachapoly_finish().
76 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020077 * \warning Decryption with the piecewise API is discouraged! Always
78 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
79 *
80 * If however this is not possible because the data is too
81 * large to fit in memory, you need to:
82 *
83 * - call \c mbedtls_chachapoly_starts() and (if needed)
84 * \c mbedtls_chachapoly_update_aad() as above,
85 * - call \c mbedtls_chachapoly_update() multiple times and
86 * ensure its output (the plaintext) is NOT used in any other
87 * way than placing it in temporary storage at this point,
88 * - call \c mbedtls_chachapoly_finish() to compute the
89 * authentication tag and compared it in constant time to the
90 * tag received with the ciphertext.
91 *
92 * If the tags are not equal, you must immediately discard
93 * all previous outputs of \c mbedtls_chachapoly_update(),
94 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020095 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -030097 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -030099
100/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \brief This function releases and clears the specified
102 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300103 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
105 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx);
Daniel Kingb8025c52016-05-17 14:43:01 -0300108
109/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \brief This function sets the ChaCha20-Poly1305
111 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300112 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200113 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * bound. This must be initialized.
115 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300116 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200117 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx,
121 const unsigned char key[32]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300122
123/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200124 * \brief This function starts a ChaCha20-Poly1305 encryption or
125 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300126 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200127 * \warning You must never use the same nonce twice with the same key.
128 * This would void any confidentiality and authenticity
129 * guarantees for the messages encrypted with the same nonce
130 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300131 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \note If the context is being used for AAD only (no data to
133 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300134 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200135 * \warning Decryption with the piecewise API is discouraged, see the
136 * warning on \c mbedtls_chachapoly_init().
137 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
139 * and bound to a key.
140 * \param nonce The nonce/IV to use for the message.
Tom Cosgrove1e211442022-05-26 11:51:00 +0100141 * This must be a readable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200142 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200143 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200144 *
145 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx,
149 const unsigned char nonce[12],
150 mbedtls_chachapoly_mode_t mode);
Daniel Kingb8025c52016-05-17 14:43:01 -0300151
152/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 * \brief This function feeds additional data to be authenticated
154 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300155 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200156 * The Additional Authenticated Data (AAD), also called
157 * Associated Data (AD) is only authenticated but not
158 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200159 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300161 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \note This function is called before data is encrypted/decrypted.
163 * I.e. call this function to process the AAD before calling
164 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300165 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * You may call this function multiple times to process
167 * an arbitrary amount of AAD. It is permitted to call
168 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300169 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * This function cannot be called any more if data has
171 * been processed by \c mbedtls_chachapoly_update(),
172 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300173 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200174 * \warning Decryption with the piecewise API is discouraged, see the
175 * warning on \c mbedtls_chachapoly_init().
176 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
178 * and bound to a key.
179 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * restrictions.
181 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300183 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200184 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200185 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * if \p ctx or \p aad are NULL.
187 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
188 * if the operations has not been started or has been
189 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx,
192 const unsigned char *aad,
193 size_t aad_len);
Daniel Kingb8025c52016-05-17 14:43:01 -0300194
195/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200196 * \brief Thus function feeds data to be encrypted or decrypted
197 * into an on-going ChaCha20-Poly1305
198 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300199 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200200 * The direction (encryption or decryption) depends on the
201 * mode that was given when calling
202 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300203 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * You may call this function multiple times to process
205 * an arbitrary amount of data. It is permitted to call
206 * this function 0 times, if no data is to be encrypted
207 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300208 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200209 * \warning Decryption with the piecewise API is discouraged, see the
210 * warning on \c mbedtls_chachapoly_init().
211 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200213 * \param len The length (in bytes) of the data to encrypt or decrypt.
214 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 * This pointer can be \c NULL if `len == 0`.
216 * \param output The buffer to where the encrypted or decrypted data is
217 * written. This must be able to hold \p len bytes.
218 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300219 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200220 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200221 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
222 * if the operation has not been started or has been
223 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx,
227 size_t len,
228 const unsigned char *input,
229 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300230
231/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200232 * \brief This function finished the ChaCha20-Poly1305 operation and
233 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300234 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200236 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300237 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200238 * \warning Decryption with the piecewise API is discouraged, see the
239 * warning on \c mbedtls_chachapoly_init().
240 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200241 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200242 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
243 * if the operation has not been started or has been
244 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx,
248 unsigned char mac[16]);
Daniel Kingb8025c52016-05-17 14:43:01 -0300249
Daniel Kingb8025c52016-05-17 14:43:01 -0300250/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200251 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200252 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300253 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200254 * \note Before using this function, you must set the key with
255 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300256 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200257 * \warning You must never use the same nonce twice with the same key.
258 * This would void any confidentiality and authenticity
259 * guarantees for the messages encrypted with the same nonce
260 * and key.
261 *
262 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500263 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200264 * \param length The length (in bytes) of the data to encrypt or decrypt.
265 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 * \param aad The buffer containing the additional authenticated
267 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200268 * \param aad_len The length (in bytes) of the AAD data to process.
269 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * This pointer can be \c NULL if `ilen == 0`.
271 * \param output The buffer to where the encrypted or decrypted data
272 * is written. This pointer can be \c NULL if `ilen == 0`.
273 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
274 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200275 *
276 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx,
280 size_t length,
281 const unsigned char nonce[12],
282 const unsigned char *aad,
283 size_t aad_len,
284 const unsigned char *input,
285 unsigned char *output,
286 unsigned char tag[16]);
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200287
288/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200289 * \brief This function performs a complete ChaCha20-Poly1305
290 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200291 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200292 * \note Before using this function, you must set the key with
293 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200294 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200295 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500296 * \param length The length (in Bytes) of the data to decrypt.
297 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200298 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200300 * \param aad_len The length (in bytes) of the AAD data to process.
301 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500302 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200303 * \param input The buffer containing the data to decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500304 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200305 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500306 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200307 *
308 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200309 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
310 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx,
314 size_t length,
315 const unsigned char nonce[12],
316 const unsigned char *aad,
317 size_t aad_len,
318 const unsigned char tag[16],
319 const unsigned char *input,
320 unsigned char *output);
Daniel Kingb8025c52016-05-17 14:43:01 -0300321
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200322#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300323/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200324 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300325 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200326 * \return \c 0 on success.
327 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300328 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329int mbedtls_chachapoly_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200330#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300331
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200332#ifdef __cplusplus
333}
334#endif
335
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200336#endif /* MBEDTLS_CHACHAPOLY_H */