blob: 3f4318fb0dcfe95e56dc97415501219f366a82bd [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020034#include "mbedtls/private_access.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030035
Bence Szépkútic662b362021-05-27 11:25:03 +020036#include "mbedtls/build_info.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030037
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020038/* for shared error codes */
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010039#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020040
Gilles Peskined2971572021-07-26 18:48:10 +020041/** The requested operation is not permitted in the current state. */
42#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054
43/** Authenticated decryption failed: data was not authentic. */
44#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056
Daniel Kingb8025c52016-05-17 14:43:01 -030045
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Daniel Kingb8025c52016-05-17 14:43:01 -030050typedef enum
51{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020052 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
53 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030054}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020055mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030056
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020057#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020058
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010059#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020060
Dawid Drozd428cc522018-07-24 10:02:47 +020061typedef struct mbedtls_chachapoly_context
Daniel Kingb8025c52016-05-17 14:43:01 -030062{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020063 mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */
64 mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */
65 uint64_t MBEDTLS_PRIVATE(aad_len); /**< The length (bytes) of the Additional Authenticated Data. */
66 uint64_t MBEDTLS_PRIVATE(ciphertext_len); /**< The length (bytes) of the ciphertext. */
67 int MBEDTLS_PRIVATE(state); /**< The current state of the context. */
68 mbedtls_chachapoly_mode_t MBEDTLS_PRIVATE(mode); /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030069}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020070mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030071
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020072#else /* !MBEDTLS_CHACHAPOLY_ALT */
73#include "chachapoly_alt.h"
74#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020075
Daniel Kingb8025c52016-05-17 14:43:01 -030076/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020077 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030078 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020079 * It must be the first API called before using
80 * the context. It must be followed by a call to
81 * \c mbedtls_chachapoly_setkey() before any operation can be
82 * done, and to \c mbedtls_chachapoly_free() once all
83 * operations with that context have been finished.
84 *
85 * In order to encrypt or decrypt full messages at once, for
86 * each message you should make a single call to
87 * \c mbedtls_chachapoly_crypt_and_tag() or
88 * \c mbedtls_chachapoly_auth_decrypt().
89 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020090 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020091 * message you should make a call to
92 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
93 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
94 * \c mbedtls_chachapoly_update(), then one call to
95 * \c mbedtls_chachapoly_finish().
96 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020097 * \warning Decryption with the piecewise API is discouraged! Always
98 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
99 *
100 * If however this is not possible because the data is too
101 * large to fit in memory, you need to:
102 *
103 * - call \c mbedtls_chachapoly_starts() and (if needed)
104 * \c mbedtls_chachapoly_update_aad() as above,
105 * - call \c mbedtls_chachapoly_update() multiple times and
106 * ensure its output (the plaintext) is NOT used in any other
107 * way than placing it in temporary storage at this point,
108 * - call \c mbedtls_chachapoly_finish() to compute the
109 * authentication tag and compared it in constant time to the
110 * tag received with the ciphertext.
111 *
112 * If the tags are not equal, you must immediately discard
113 * all previous outputs of \c mbedtls_chachapoly_update(),
114 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200115 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300117 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200118void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300119
120/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \brief This function releases and clears the specified
122 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300123 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
125 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300126 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200127void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300128
129/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 * \brief This function sets the ChaCha20-Poly1305
131 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300132 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * bound. This must be initialized.
135 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300136 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300139 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200140int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
141 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300142
143/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200144 * \brief This function starts a ChaCha20-Poly1305 encryption or
145 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300146 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * \warning You must never use the same nonce twice with the same key.
148 * This would void any confidentiality and authenticity
149 * guarantees for the messages encrypted with the same nonce
150 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300151 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * \note If the context is being used for AAD only (no data to
153 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300154 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200155 * \warning Decryption with the piecewise API is discouraged, see the
156 * warning on \c mbedtls_chachapoly_init().
157 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
159 * and bound to a key.
160 * \param nonce The nonce/IV to use for the message.
161 * This must be a redable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200163 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 *
165 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300167 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200168int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
169 const unsigned char nonce[12],
170 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300171
172/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \brief This function feeds additional data to be authenticated
174 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300175 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * The Additional Authenticated Data (AAD), also called
177 * Associated Data (AD) is only authenticated but not
178 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200179 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300181 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200182 * \note This function is called before data is encrypted/decrypted.
183 * I.e. call this function to process the AAD before calling
184 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300185 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * You may call this function multiple times to process
187 * an arbitrary amount of AAD. It is permitted to call
188 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300189 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * This function cannot be called any more if data has
191 * been processed by \c mbedtls_chachapoly_update(),
192 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300193 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200194 * \warning Decryption with the piecewise API is discouraged, see the
195 * warning on \c mbedtls_chachapoly_init().
196 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
198 * and bound to a key.
199 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200200 * restrictions.
201 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300203 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200205 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * if \p ctx or \p aad are NULL.
207 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
208 * if the operations has not been started or has been
209 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300210 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200211int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200212 const unsigned char *aad,
213 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300214
215/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200216 * \brief Thus function feeds data to be encrypted or decrypted
217 * into an on-going ChaCha20-Poly1305
218 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300219 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200220 * The direction (encryption or decryption) depends on the
221 * mode that was given when calling
222 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300223 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200224 * You may call this function multiple times to process
225 * an arbitrary amount of data. It is permitted to call
226 * this function 0 times, if no data is to be encrypted
227 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300228 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200229 * \warning Decryption with the piecewise API is discouraged, see the
230 * warning on \c mbedtls_chachapoly_init().
231 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200233 * \param len The length (in bytes) of the data to encrypt or decrypt.
234 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 * This pointer can be \c NULL if `len == 0`.
236 * \param output The buffer to where the encrypted or decrypted data is
237 * written. This must be able to hold \p len bytes.
238 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300239 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200240 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200241 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
242 * if the operation has not been started or has been
243 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300245 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200246int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
247 size_t len,
248 const unsigned char *input,
249 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300250
251/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200252 * \brief This function finished the ChaCha20-Poly1305 operation and
253 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300254 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200256 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300257 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200258 * \warning Decryption with the piecewise API is discouraged, see the
259 * warning on \c mbedtls_chachapoly_init().
260 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200261 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200262 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
263 * if the operation has not been started or has been
264 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300266 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200267int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
268 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300269
Daniel Kingb8025c52016-05-17 14:43:01 -0300270/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200271 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200272 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300273 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200274 * \note Before using this function, you must set the key with
275 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300276 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200277 * \warning You must never use the same nonce twice with the same key.
278 * This would void any confidentiality and authenticity
279 * guarantees for the messages encrypted with the same nonce
280 * and key.
281 *
282 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200284 * \param length The length (in bytes) of the data to encrypt or decrypt.
285 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * \param aad The buffer containing the additional authenticated
287 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200288 * \param aad_len The length (in bytes) of the AAD data to process.
289 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 * This pointer can be \c NULL if `ilen == 0`.
291 * \param output The buffer to where the encrypted or decrypted data
292 * is written. This pointer can be \c NULL if `ilen == 0`.
293 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
294 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200295 *
296 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300298 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200299int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
300 size_t length,
301 const unsigned char nonce[12],
302 const unsigned char *aad,
303 size_t aad_len,
304 const unsigned char *input,
305 unsigned char *output,
306 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200307
308/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200309 * \brief This function performs a complete ChaCha20-Poly1305
310 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200311 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200312 * \note Before using this function, you must set the key with
313 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200314 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200315 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 * \param length The length (in Bytes) of the data to decrypt.
317 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200318 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200320 * \param aad_len The length (in bytes) of the AAD data to process.
321 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200323 * \param input The buffer containing the data to decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200325 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200327 *
328 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200329 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
330 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200332 */
333int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
334 size_t length,
335 const unsigned char nonce[12],
336 const unsigned char *aad,
337 size_t aad_len,
338 const unsigned char tag[16],
339 const unsigned char *input,
340 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300341
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200342#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300343/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200344 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300345 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200346 * \return \c 0 on success.
347 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300348 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200349int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200350#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300351
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200352#ifdef __cplusplus
353}
354#endif
355
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200356#endif /* MBEDTLS_CHACHAPOLY_H */