blob: 80d20e54acbad37f679637de4d8cd60c80e77621 [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
15/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Daniel Kingb8025c52016-05-17 14:43:01 -030016 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020030 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel Kingb8025c52016-05-17 14:43:01 -030031 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020032
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020033#ifndef MBEDTLS_CHACHAPOLY_H
34#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030035
36#if !defined(MBEDTLS_CONFIG_FILE)
37#include "config.h"
38#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020042/* for shared error codes */
43#include "poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020044
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020045#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 /**< The requested operation is not permitted in the current state. */
46#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 /**< Authenticated decryption failed: data was not authentic. */
Daniel Kingb8025c52016-05-17 14:43:01 -030047
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Daniel Kingb8025c52016-05-17 14:43:01 -030052typedef enum
53{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020054 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
55 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030056}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020057mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030058
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020059#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020060
61#include "chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020062
Dawid Drozd428cc522018-07-24 10:02:47 +020063typedef struct mbedtls_chachapoly_context
Daniel Kingb8025c52016-05-17 14:43:01 -030064{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020065 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
66 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
67 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
68 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
69 int state; /**< The current state of the context. */
70 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030071}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020072mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030073
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020074#else /* !MBEDTLS_CHACHAPOLY_ALT */
75#include "chachapoly_alt.h"
76#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020077
Daniel Kingb8025c52016-05-17 14:43:01 -030078/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020079 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -030080 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020081 * It must be the first API called before using
82 * the context. It must be followed by a call to
83 * \c mbedtls_chachapoly_setkey() before any operation can be
84 * done, and to \c mbedtls_chachapoly_free() once all
85 * operations with that context have been finished.
86 *
87 * In order to encrypt or decrypt full messages at once, for
88 * each message you should make a single call to
89 * \c mbedtls_chachapoly_crypt_and_tag() or
90 * \c mbedtls_chachapoly_auth_decrypt().
91 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020092 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020093 * message you should make a call to
94 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
95 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
96 * \c mbedtls_chachapoly_update(), then one call to
97 * \c mbedtls_chachapoly_finish().
98 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +020099 * \warning Decryption with the piecewise API is discouraged! Always
100 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
101 *
102 * If however this is not possible because the data is too
103 * large to fit in memory, you need to:
104 *
105 * - call \c mbedtls_chachapoly_starts() and (if needed)
106 * \c mbedtls_chachapoly_update_aad() as above,
107 * - call \c mbedtls_chachapoly_update() multiple times and
108 * ensure its output (the plaintext) is NOT used in any other
109 * way than placing it in temporary storage at this point,
110 * - call \c mbedtls_chachapoly_finish() to compute the
111 * authentication tag and compared it in constant time to the
112 * tag received with the ciphertext.
113 *
114 * If the tags are not equal, you must immediately discard
115 * all previous outputs of \c mbedtls_chachapoly_update(),
116 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200117 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000118 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300119 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200120void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300121
122/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000123 * \brief This function releases and clears the specified
124 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300125 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000126 * \param ctx The ChachaPoly context to clear. May be \c NULL, in which
127 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300128 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200129void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300130
131/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000132 * \brief This function sets the ChaCha20-Poly1305
133 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300134 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000136 * bound. Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \param key The 256-bit (32 bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300138 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200139 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000140 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300141 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200142int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
143 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300144
145/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200146 * \brief This function starts a ChaCha20-Poly1305 encryption or
147 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300148 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * \warning You must never use the same nonce twice with the same key.
150 * This would void any confidentiality and authenticity
151 * guarantees for the messages encrypted with the same nonce
152 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300153 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \note If the context is being used for AAD only (no data to
155 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300156 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200157 * \warning Decryption with the piecewise API is discouraged, see the
158 * warning on \c mbedtls_chachapoly_init().
159 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000160 * \param ctx The ChaCha20-Poly1305 context. Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200161 * \param nonce The nonce/IV to use for the message. Must be 12 bytes.
162 * \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.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000166 * \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 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200197 * \param ctx The ChaCha20-Poly1305 context to use.
198 * \param aad_len The length (in bytes) of the AAD. The length has no
199 * restrictions.
200 * \param aad Buffer containing the AAD.
201 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300202 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200204 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200205 * if \p ctx or \p aad are NULL.
206 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
207 * if the operations has not been started or has been
208 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200210int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200211 const unsigned char *aad,
212 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300213
214/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200215 * \brief Thus function feeds data to be encrypted or decrypted
216 * into an on-going ChaCha20-Poly1305
217 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300218 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200219 * The direction (encryption or decryption) depends on the
220 * mode that was given when calling
221 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300222 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200223 * You may call this function multiple times to process
224 * an arbitrary amount of data. It is permitted to call
225 * this function 0 times, if no data is to be encrypted
226 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300227 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200228 * \warning Decryption with the piecewise API is discouraged, see the
229 * warning on \c mbedtls_chachapoly_init().
230 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000231 * \param ctx The ChaCha20-Poly1305 context to use. Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200232 * \param len The length (in bytes) of the data to encrypt or decrypt.
233 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000234 * This pointer can be \c NULL if `len == 0`.
235 * \param output The buffer to where the encrypted or decrypted data is
236 * written. Must be able to hold \p len bytes.
237 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300238 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200239 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200240 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
241 * if the operation has not been started or has been
242 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000243 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300244 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200245int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
246 size_t len,
247 const unsigned char *input,
248 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300249
250/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200251 * \brief This function finished the ChaCha20-Poly1305 operation and
252 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300253 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000254 * \param ctx The ChaCha20-Poly1305 context to use. Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200255 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300256 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200257 * \warning Decryption with the piecewise API is discouraged, see the
258 * warning on \c mbedtls_chachapoly_init().
259 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200260 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200261 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
262 * if the operation has not been started or has been
263 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000264 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300265 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200266int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
267 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300268
Daniel Kingb8025c52016-05-17 14:43:01 -0300269/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200270 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200271 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300272 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200273 * \note Before using this function, you must set the key with
274 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300275 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200276 * \warning You must never use the same nonce twice with the same key.
277 * This would void any confidentiality and authenticity
278 * guarantees for the messages encrypted with the same nonce
279 * and key.
280 *
281 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000282 * Must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200283 * \param length The length (in bytes) of the data to encrypt or decrypt.
284 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
285 * \param aad The buffer containing the additional authenticated data (AAD).
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000286 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200287 * \param aad_len The length (in bytes) of the AAD data to process.
288 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000289 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200290 * \param output The buffer to where the encrypted or decrypted data is written.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000291 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200292 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC is written.
293 *
294 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000295 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300296 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200297int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
298 size_t length,
299 const unsigned char nonce[12],
300 const unsigned char *aad,
301 size_t aad_len,
302 const unsigned char *input,
303 unsigned char *output,
304 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200305
306/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200307 * \brief This function performs a complete ChaCha20-Poly1305
308 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200309 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200310 * \note Before using this function, you must set the key with
311 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200312 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200313 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
314 * \param length The length (in bytes) of the data to decrypt.
315 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
316 * \param aad The buffer containing the additional authenticated data (AAD).
317 * This pointer can be NULL if aad_len == 0.
318 * \param aad_len The length (in bytes) of the AAD data to process.
319 * \param tag The buffer holding the authentication tag.
320 * \param input The buffer containing the data to decrypt.
321 * This pointer can be NULL if ilen == 0.
322 * \param output The buffer to where the decrypted data is written.
323 * This pointer can be NULL if ilen == 0.
324 *
325 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200326 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
327 * if the data was not authentic.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000328 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200329 */
330int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
331 size_t length,
332 const unsigned char nonce[12],
333 const unsigned char *aad,
334 size_t aad_len,
335 const unsigned char tag[16],
336 const unsigned char *input,
337 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300338
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200339#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300340/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200341 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300342 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200343 * \return \c 0 on success.
344 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300345 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200346int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200347#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300348
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200349#ifdef __cplusplus
350}
351#endif
352
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200353#endif /* MBEDTLS_CHACHAPOLY_H */