blob: 5f6cb6e03069914003a5f8be45c4dae91cf3e32a [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útif744bd72020-06-05 13:02:18 +020015/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +020016 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18 *
19 * This file is provided under the Apache License 2.0, or the
20 * GNU General Public License v2.0 or later.
21 *
22 * **********
23 * Apache License 2.0:
Daniel Kingb8025c52016-05-17 14:43:01 -030024 *
25 * Licensed under the Apache License, Version 2.0 (the "License"); you may
26 * not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020037 * **********
38 *
39 * **********
40 * GNU General Public License v2.0 or later:
41 *
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License as published by
44 * the Free Software Foundation; either version 2 of the License, or
45 * (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License along
53 * with this program; if not, write to the Free Software Foundation, Inc.,
54 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 *
56 * **********
Daniel Kingb8025c52016-05-17 14:43:01 -030057 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020058
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020059#ifndef MBEDTLS_CHACHAPOLY_H
60#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030061
62#if !defined(MBEDTLS_CONFIG_FILE)
63#include "config.h"
64#else
65#include MBEDTLS_CONFIG_FILE
66#endif
67
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020068/* for shared error codes */
69#include "poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020070
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020071#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 /**< The requested operation is not permitted in the current state. */
72#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 /**< Authenticated decryption failed: data was not authentic. */
Daniel Kingb8025c52016-05-17 14:43:01 -030073
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020074#ifdef __cplusplus
75extern "C" {
76#endif
77
Daniel Kingb8025c52016-05-17 14:43:01 -030078typedef enum
79{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020080 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
81 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030082}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020083mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030084
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020085#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020086
87#include "chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020088
Dawid Drozd428cc522018-07-24 10:02:47 +020089typedef struct mbedtls_chachapoly_context
Daniel Kingb8025c52016-05-17 14:43:01 -030090{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020091 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
92 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
93 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
94 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
95 int state; /**< The current state of the context. */
96 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030097}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020098mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030099
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200100#else /* !MBEDTLS_CHACHAPOLY_ALT */
101#include "chachapoly_alt.h"
102#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +0200103
Daniel Kingb8025c52016-05-17 14:43:01 -0300104/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300106 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * It must be the first API called before using
108 * the context. It must be followed by a call to
109 * \c mbedtls_chachapoly_setkey() before any operation can be
110 * done, and to \c mbedtls_chachapoly_free() once all
111 * operations with that context have been finished.
112 *
113 * In order to encrypt or decrypt full messages at once, for
114 * each message you should make a single call to
115 * \c mbedtls_chachapoly_crypt_and_tag() or
116 * \c mbedtls_chachapoly_auth_decrypt().
117 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200118 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200119 * message you should make a call to
120 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
121 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
122 * \c mbedtls_chachapoly_update(), then one call to
123 * \c mbedtls_chachapoly_finish().
124 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200125 * \warning Decryption with the piecewise API is discouraged! Always
126 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
127 *
128 * If however this is not possible because the data is too
129 * large to fit in memory, you need to:
130 *
131 * - call \c mbedtls_chachapoly_starts() and (if needed)
132 * \c mbedtls_chachapoly_update_aad() as above,
133 * - call \c mbedtls_chachapoly_update() multiple times and
134 * ensure its output (the plaintext) is NOT used in any other
135 * way than placing it in temporary storage at this point,
136 * - call \c mbedtls_chachapoly_finish() to compute the
137 * authentication tag and compared it in constant time to the
138 * tag received with the ciphertext.
139 *
140 * If the tags are not equal, you must immediately discard
141 * all previous outputs of \c mbedtls_chachapoly_update(),
142 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200143 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000144 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300145 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200146void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300147
148/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000149 * \brief This function releases and clears the specified
150 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300151 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000152 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000153 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300154 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200155void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300156
157/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000158 * \brief This function sets the ChaCha20-Poly1305
159 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300160 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200161 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Hanno Beckerad7581f2018-12-17 09:43:21 +0000162 * bound. This must be initialized.
163 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300164 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * \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_setkey( mbedtls_chachapoly_context *ctx,
169 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300170
171/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \brief This function starts a ChaCha20-Poly1305 encryption or
173 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300174 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 * \warning You must never use the same nonce twice with the same key.
176 * This would void any confidentiality and authenticity
177 * guarantees for the messages encrypted with the same nonce
178 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300179 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * \note If the context is being used for AAD only (no data to
181 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300182 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200183 * \warning Decryption with the piecewise API is discouraged, see the
184 * warning on \c mbedtls_chachapoly_init().
185 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000186 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
187 * and bound to a key.
188 * \param nonce The nonce/IV to use for the message.
189 * This must be a redable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200191 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 *
193 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000194 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300195 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200196int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
197 const unsigned char nonce[12],
198 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300199
200/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200201 * \brief This function feeds additional data to be authenticated
202 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300203 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * The Additional Authenticated Data (AAD), also called
205 * Associated Data (AD) is only authenticated but not
206 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200207 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200208 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * \note This function is called before data is encrypted/decrypted.
211 * I.e. call this function to process the AAD before calling
212 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300213 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200214 * You may call this function multiple times to process
215 * an arbitrary amount of AAD. It is permitted to call
216 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300217 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200218 * This function cannot be called any more if data has
219 * been processed by \c mbedtls_chachapoly_update(),
220 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300221 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200222 * \warning Decryption with the piecewise API is discouraged, see the
223 * warning on \c mbedtls_chachapoly_init().
224 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000225 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
226 * and bound to a key.
227 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200228 * restrictions.
229 * \param aad Buffer containing the AAD.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000230 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300231 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200232 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200233 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200234 * if \p ctx or \p aad are NULL.
235 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
236 * if the operations has not been started or has been
237 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300238 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200239int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200240 const unsigned char *aad,
241 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300242
243/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200244 * \brief Thus function feeds data to be encrypted or decrypted
245 * into an on-going ChaCha20-Poly1305
246 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300247 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200248 * The direction (encryption or decryption) depends on the
249 * mode that was given when calling
250 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300251 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200252 * You may call this function multiple times to process
253 * an arbitrary amount of data. It is permitted to call
254 * this function 0 times, if no data is to be encrypted
255 * or decrypted.
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 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000260 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200261 * \param len The length (in bytes) of the data to encrypt or decrypt.
262 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000263 * This pointer can be \c NULL if `len == 0`.
264 * \param output The buffer to where the encrypted or decrypted data is
Hanno Beckerad7581f2018-12-17 09:43:21 +0000265 * written. This must be able to hold \p len bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000266 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300267 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200268 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200269 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
270 * if the operation has not been started or has been
271 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000272 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300273 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200274int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
275 size_t len,
276 const unsigned char *input,
277 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300278
279/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200280 * \brief This function finished the ChaCha20-Poly1305 operation and
281 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300282 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000283 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200284 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300285 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200286 * \warning Decryption with the piecewise API is discouraged, see the
287 * warning on \c mbedtls_chachapoly_init().
288 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200289 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200290 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
291 * if the operation has not been started or has been
292 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000293 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300294 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200295int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
296 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300297
Daniel Kingb8025c52016-05-17 14:43:01 -0300298/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200299 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200300 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300301 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200302 * \note Before using this function, you must set the key with
303 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300304 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200305 * \warning You must never use the same nonce twice with the same key.
306 * This would void any confidentiality and authenticity
307 * guarantees for the messages encrypted with the same nonce
308 * and key.
309 *
310 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000311 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200312 * \param length The length (in bytes) of the data to encrypt or decrypt.
313 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000314 * \param aad The buffer containing the additional authenticated
315 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200316 * \param aad_len The length (in bytes) of the AAD data to process.
317 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000318 * This pointer can be \c NULL if `ilen == 0`.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000319 * \param output The buffer to where the encrypted or decrypted data
320 * is written. This pointer can be \c NULL if `ilen == 0`.
321 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
322 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200323 *
324 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000325 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300326 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200327int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
328 size_t length,
329 const unsigned char nonce[12],
330 const unsigned char *aad,
331 size_t aad_len,
332 const unsigned char *input,
333 unsigned char *output,
334 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200335
336/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200337 * \brief This function performs a complete ChaCha20-Poly1305
338 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200339 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200340 * \note Before using this function, you must set the key with
341 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200342 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200343 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000344 * \param length The length (in Bytes) of the data to decrypt.
345 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200346 * \param aad The buffer containing the additional authenticated data (AAD).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000347 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200348 * \param aad_len The length (in bytes) of the AAD data to process.
349 * \param tag The buffer holding the authentication tag.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000350 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200351 * \param input The buffer containing the data to decrypt.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000352 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200353 * \param output The buffer to where the decrypted data is written.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000354 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200355 *
356 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200357 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
358 * if the data was not authentic.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000359 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200360 */
361int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
362 size_t length,
363 const unsigned char nonce[12],
364 const unsigned char *aad,
365 size_t aad_len,
366 const unsigned char tag[16],
367 const unsigned char *input,
368 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300369
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200370#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300371/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200372 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300373 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200374 * \return \c 0 on success.
375 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300376 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200377int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200378#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300379
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200380#ifdef __cplusplus
381}
382#endif
383
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200384#endif /* MBEDTLS_CHACHAPOLY_H */