blob: c4ec7b5f2a91a61dac6993414db567a01dc1264e [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
Daniel Kingb8025c52016-05-17 14:43:01 -030017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Daniel Kingb8025c52016-05-17 14:43:01 -030030 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020031
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020032#ifndef MBEDTLS_CHACHAPOLY_H
33#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030034
35#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020041/* for shared error codes */
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020043
Gilles Peskinea3974432021-07-26 18:48:10 +020044/** The requested operation is not permitted in the current state. */
45#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054
46/** Authenticated decryption failed: data was not authentic. */
47#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056
Daniel Kingb8025c52016-05-17 14:43:01 -030048
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Daniel Kingb8025c52016-05-17 14:43:01 -030053typedef enum
54{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020055 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
56 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030057}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020058mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030059
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020060#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020061
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010062#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020063
Dawid Drozd428cc522018-07-24 10:02:47 +020064typedef struct mbedtls_chachapoly_context
Daniel Kingb8025c52016-05-17 14:43:01 -030065{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020066 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
67 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
68 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
69 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
70 int state; /**< The current state of the context. */
71 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030072}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020073mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030074
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020075#else /* !MBEDTLS_CHACHAPOLY_ALT */
76#include "chachapoly_alt.h"
77#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020078
Daniel Kingb8025c52016-05-17 14:43:01 -030079/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020080 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030081 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020082 * It must be the first API called before using
83 * the context. It must be followed by a call to
84 * \c mbedtls_chachapoly_setkey() before any operation can be
85 * done, and to \c mbedtls_chachapoly_free() once all
86 * operations with that context have been finished.
87 *
88 * In order to encrypt or decrypt full messages at once, for
89 * each message you should make a single call to
90 * \c mbedtls_chachapoly_crypt_and_tag() or
91 * \c mbedtls_chachapoly_auth_decrypt().
92 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020093 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020094 * message you should make a call to
95 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
96 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
97 * \c mbedtls_chachapoly_update(), then one call to
98 * \c mbedtls_chachapoly_finish().
99 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200100 * \warning Decryption with the piecewise API is discouraged! Always
101 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
102 *
103 * If however this is not possible because the data is too
104 * large to fit in memory, you need to:
105 *
106 * - call \c mbedtls_chachapoly_starts() and (if needed)
107 * \c mbedtls_chachapoly_update_aad() as above,
108 * - call \c mbedtls_chachapoly_update() multiple times and
109 * ensure its output (the plaintext) is NOT used in any other
110 * way than placing it in temporary storage at this point,
111 * - call \c mbedtls_chachapoly_finish() to compute the
112 * authentication tag and compared it in constant time to the
113 * tag received with the ciphertext.
114 *
115 * If the tags are not equal, you must immediately discard
116 * all previous outputs of \c mbedtls_chachapoly_update(),
117 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300120 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200121void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300122
123/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \brief This function releases and clears the specified
125 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300126 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
128 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300129 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200130void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300131
132/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \brief This function sets the ChaCha20-Poly1305
134 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300135 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * bound. This must be initialized.
138 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300139 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200140 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300142 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200143int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
144 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300145
146/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * \brief This function starts a ChaCha20-Poly1305 encryption or
148 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300149 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * \warning You must never use the same nonce twice with the same key.
151 * This would void any confidentiality and authenticity
152 * guarantees for the messages encrypted with the same nonce
153 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300154 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200155 * \note If the context is being used for AAD only (no data to
156 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300157 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200158 * \warning Decryption with the piecewise API is discouraged, see the
159 * warning on \c mbedtls_chachapoly_init().
160 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
162 * and bound to a key.
163 * \param nonce The nonce/IV to use for the message.
164 * This must be a redable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200166 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200167 *
168 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300170 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200171int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
172 const unsigned char nonce[12],
173 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300174
175/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * \brief This function feeds additional data to be authenticated
177 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300178 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200179 * The Additional Authenticated Data (AAD), also called
180 * Associated Data (AD) is only authenticated but not
181 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200182 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300184 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200185 * \note This function is called before data is encrypted/decrypted.
186 * I.e. call this function to process the AAD before calling
187 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300188 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200189 * You may call this function multiple times to process
190 * an arbitrary amount of AAD. It is permitted to call
191 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300192 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200193 * This function cannot be called any more if data has
194 * been processed by \c mbedtls_chachapoly_update(),
195 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300196 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200197 * \warning Decryption with the piecewise API is discouraged, see the
198 * warning on \c mbedtls_chachapoly_init().
199 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
201 * and bound to a key.
202 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * restrictions.
204 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300206 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200207 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200208 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200209 * if \p ctx or \p aad are NULL.
210 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
211 * if the operations has not been started or has been
212 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300213 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200214int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200215 const unsigned char *aad,
216 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300217
218/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200219 * \brief Thus function feeds data to be encrypted or decrypted
220 * into an on-going ChaCha20-Poly1305
221 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300222 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200223 * The direction (encryption or decryption) depends on the
224 * mode that was given when calling
225 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300226 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200227 * You may call this function multiple times to process
228 * an arbitrary amount of data. It is permitted to call
229 * this function 0 times, if no data is to be encrypted
230 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300231 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200232 * \warning Decryption with the piecewise API is discouraged, see the
233 * warning on \c mbedtls_chachapoly_init().
234 *
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 len The length (in bytes) of the data to encrypt or decrypt.
237 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 * This pointer can be \c NULL if `len == 0`.
239 * \param output The buffer to where the encrypted or decrypted data is
240 * written. This must be able to hold \p len bytes.
241 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300242 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200243 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200244 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
245 * if the operation has not been started or has been
246 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300248 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200249int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
250 size_t len,
251 const unsigned char *input,
252 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300253
254/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200255 * \brief This function finished the ChaCha20-Poly1305 operation and
256 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300257 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200259 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300260 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200261 * \warning Decryption with the piecewise API is discouraged, see the
262 * warning on \c mbedtls_chachapoly_init().
263 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200264 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200265 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
266 * if the operation has not been started or has been
267 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300269 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200270int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
271 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300272
Daniel Kingb8025c52016-05-17 14:43:01 -0300273/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200274 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200275 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300276 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200277 * \note Before using this function, you must set the key with
278 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300279 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200280 * \warning You must never use the same nonce twice with the same key.
281 * This would void any confidentiality and authenticity
282 * guarantees for the messages encrypted with the same nonce
283 * and key.
284 *
285 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200287 * \param length The length (in bytes) of the data to encrypt or decrypt.
288 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289 * \param aad The buffer containing the additional authenticated
290 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200291 * \param aad_len The length (in bytes) of the AAD data to process.
292 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500293 * This pointer can be \c NULL if `ilen == 0`.
294 * \param output The buffer to where the encrypted or decrypted data
295 * is written. This pointer can be \c NULL if `ilen == 0`.
296 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
297 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200298 *
299 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500300 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300301 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200302int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
303 size_t length,
304 const unsigned char nonce[12],
305 const unsigned char *aad,
306 size_t aad_len,
307 const unsigned char *input,
308 unsigned char *output,
309 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200310
311/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200312 * \brief This function performs a complete ChaCha20-Poly1305
313 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200314 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200315 * \note Before using this function, you must set the key with
316 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200317 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200318 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 * \param length The length (in Bytes) of the data to decrypt.
320 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200321 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200323 * \param aad_len The length (in bytes) of the AAD data to process.
324 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500325 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200326 * \param input The buffer containing the data to decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200328 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200330 *
331 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200332 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
333 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200335 */
336int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
337 size_t length,
338 const unsigned char nonce[12],
339 const unsigned char *aad,
340 size_t aad_len,
341 const unsigned char tag[16],
342 const unsigned char *input,
343 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300344
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200345#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300346/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200347 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300348 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200349 * \return \c 0 on success.
350 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300351 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200352int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200353#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300354
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200355#ifdef __cplusplus
356}
357#endif
358
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200359#endif /* MBEDTLS_CHACHAPOLY_H */