blob: 3d842ef19ecac44cfa64080f80afe03a9ee806f7 [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)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/config.h"
Daniel Kingb8025c52016-05-17 14:43:01 -030038#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020042/* for shared error codes */
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010043#include "mbedtls/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
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010061#include "mbedtls/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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \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/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * \brief This function releases and clears the specified
124 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300125 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 * \param ctx The ChachaPoly context to clear. This 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/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * bound. This must be initialized.
137 * \param key The \c 256 Bit (\c 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.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
161 * and bound to a key.
162 * \param nonce The nonce/IV to use for the message.
163 * This must be a redable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200165 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 *
167 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300169 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200170int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
171 const unsigned char nonce[12],
172 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300173
174/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 * \brief This function feeds additional data to be authenticated
176 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300177 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200178 * The Additional Authenticated Data (AAD), also called
179 * Associated Data (AD) is only authenticated but not
180 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200181 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200182 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300183 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200184 * \note This function is called before data is encrypted/decrypted.
185 * I.e. call this function to process the AAD before calling
186 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300187 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200188 * You may call this function multiple times to process
189 * an arbitrary amount of AAD. It is permitted to call
190 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300191 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * This function cannot be called any more if data has
193 * been processed by \c mbedtls_chachapoly_update(),
194 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300195 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200196 * \warning Decryption with the piecewise API is discouraged, see the
197 * warning on \c mbedtls_chachapoly_init().
198 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
200 * and bound to a key.
201 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200202 * restrictions.
203 * \param aad Buffer containing the AAD.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300205 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200207 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200208 * if \p ctx or \p aad are NULL.
209 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
210 * if the operations has not been started or has been
211 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300212 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200213int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200214 const unsigned char *aad,
215 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300216
217/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200218 * \brief Thus function feeds data to be encrypted or decrypted
219 * into an on-going ChaCha20-Poly1305
220 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300221 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200222 * The direction (encryption or decryption) depends on the
223 * mode that was given when calling
224 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300225 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200226 * You may call this function multiple times to process
227 * an arbitrary amount of data. It is permitted to call
228 * this function 0 times, if no data is to be encrypted
229 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300230 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200231 * \warning Decryption with the piecewise API is discouraged, see the
232 * warning on \c mbedtls_chachapoly_init().
233 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200235 * \param len The length (in bytes) of the data to encrypt or decrypt.
236 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 * This pointer can be \c NULL if `len == 0`.
238 * \param output The buffer to where the encrypted or decrypted data is
239 * written. This must be able to hold \p len bytes.
240 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300241 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200242 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200243 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
244 * if the operation has not been started or has been
245 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300247 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200248int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
249 size_t len,
250 const unsigned char *input,
251 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300252
253/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200254 * \brief This function finished the ChaCha20-Poly1305 operation and
255 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300256 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200258 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300259 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200260 * \warning Decryption with the piecewise API is discouraged, see the
261 * warning on \c mbedtls_chachapoly_init().
262 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200263 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200264 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
265 * if the operation has not been started or has been
266 * finished.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300268 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200269int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
270 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300271
Daniel Kingb8025c52016-05-17 14:43:01 -0300272/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200273 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200274 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300275 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200276 * \note Before using this function, you must set the key with
277 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300278 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200279 * \warning You must never use the same nonce twice with the same key.
280 * This would void any confidentiality and authenticity
281 * guarantees for the messages encrypted with the same nonce
282 * and key.
283 *
284 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200286 * \param length The length (in bytes) of the data to encrypt or decrypt.
287 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * \param aad The buffer containing the additional authenticated
289 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200290 * \param aad_len The length (in bytes) of the AAD data to process.
291 * \param input The buffer containing the data to encrypt or decrypt.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 * This pointer can be \c NULL if `ilen == 0`.
293 * \param output The buffer to where the encrypted or decrypted data
294 * is written. This pointer can be \c NULL if `ilen == 0`.
295 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
296 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200297 *
298 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300300 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200301int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
302 size_t length,
303 const unsigned char nonce[12],
304 const unsigned char *aad,
305 size_t aad_len,
306 const unsigned char *input,
307 unsigned char *output,
308 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200309
310/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200311 * \brief This function performs a complete ChaCha20-Poly1305
312 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200313 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200314 * \note Before using this function, you must set the key with
315 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200316 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200317 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 * \param length The length (in Bytes) of the data to decrypt.
319 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200320 * \param aad The buffer containing the additional authenticated data (AAD).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200322 * \param aad_len The length (in bytes) of the AAD data to process.
323 * \param tag The buffer holding the authentication tag.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200325 * \param input The buffer containing the data to decrypt.
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 * \param output The buffer to where the decrypted data is written.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200329 *
330 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200331 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
332 * if the data was not authentic.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200334 */
335int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
336 size_t length,
337 const unsigned char nonce[12],
338 const unsigned char *aad,
339 size_t aad_len,
340 const unsigned char tag[16],
341 const unsigned char *input,
342 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300343
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200344#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300345/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200346 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300347 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200348 * \return \c 0 on success.
349 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300350 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200351int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200352#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300353
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200354#ifdef __cplusplus
355}
356#endif
357
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200358#endif /* MBEDTLS_CHACHAPOLY_H */