blob: bee5a3ab036b8bc44c31ec0453d9d5b9aaaae607 [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/*
16 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
17 * 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 * **********
57 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020058 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel Kingb8025c52016-05-17 14:43:01 -030059 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020060
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020061#ifndef MBEDTLS_CHACHAPOLY_H
62#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030063
64#if !defined(MBEDTLS_CONFIG_FILE)
65#include "config.h"
66#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020070/* for shared error codes */
71#include "poly1305.h"
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020072
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020073#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 /**< The requested operation is not permitted in the current state. */
74#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 /**< Authenticated decryption failed: data was not authentic. */
Daniel Kingb8025c52016-05-17 14:43:01 -030075
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Daniel Kingb8025c52016-05-17 14:43:01 -030080typedef enum
81{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020082 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
83 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030084}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020085mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030086
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020087#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020088
89#include "chacha20.h"
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020090
Dawid Drozd428cc522018-07-24 10:02:47 +020091typedef struct mbedtls_chachapoly_context
Daniel Kingb8025c52016-05-17 14:43:01 -030092{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020093 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
94 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
95 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
96 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
97 int state; /**< The current state of the context. */
98 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
Daniel Kingb8025c52016-05-17 14:43:01 -030099}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200100mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -0300101
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200102#else /* !MBEDTLS_CHACHAPOLY_ALT */
103#include "chachapoly_alt.h"
104#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +0200105
Daniel Kingb8025c52016-05-17 14:43:01 -0300106/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * \brief This function initializes the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300108 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200109 * It must be the first API called before using
110 * the context. It must be followed by a call to
111 * \c mbedtls_chachapoly_setkey() before any operation can be
112 * done, and to \c mbedtls_chachapoly_free() once all
113 * operations with that context have been finished.
114 *
115 * In order to encrypt or decrypt full messages at once, for
116 * each message you should make a single call to
117 * \c mbedtls_chachapoly_crypt_and_tag() or
118 * \c mbedtls_chachapoly_auth_decrypt().
119 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200120 * In order to encrypt messages piecewise, for each
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200121 * message you should make a call to
122 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
123 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
124 * \c mbedtls_chachapoly_update(), then one call to
125 * \c mbedtls_chachapoly_finish().
126 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200127 * \warning Decryption with the piecewise API is discouraged! Always
128 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
129 *
130 * If however this is not possible because the data is too
131 * large to fit in memory, you need to:
132 *
133 * - call \c mbedtls_chachapoly_starts() and (if needed)
134 * \c mbedtls_chachapoly_update_aad() as above,
135 * - call \c mbedtls_chachapoly_update() multiple times and
136 * ensure its output (the plaintext) is NOT used in any other
137 * way than placing it in temporary storage at this point,
138 * - call \c mbedtls_chachapoly_finish() to compute the
139 * authentication tag and compared it in constant time to the
140 * tag received with the ciphertext.
141 *
142 * If the tags are not equal, you must immediately discard
143 * all previous outputs of \c mbedtls_chachapoly_update(),
144 * otherwise you can now safely use the plaintext.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 *
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000146 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300147 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200148void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300149
150/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000151 * \brief This function releases and clears the specified
152 * ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300153 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000154 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000155 * case this function is a no-op.
Daniel Kingb8025c52016-05-17 14:43:01 -0300156 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200157void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300158
159/**
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000160 * \brief This function sets the ChaCha20-Poly1305
161 * symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300162 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200163 * \param ctx The ChaCha20-Poly1305 context to which the key should be
Hanno Beckerad7581f2018-12-17 09:43:21 +0000164 * bound. This must be initialized.
165 * \param key The \c 256 Bit (\c 32 Bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300166 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200167 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000168 * \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_setkey( mbedtls_chachapoly_context *ctx,
171 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300172
173/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200174 * \brief This function starts a ChaCha20-Poly1305 encryption or
175 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300176 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200177 * \warning You must never use the same nonce twice with the same key.
178 * This would void any confidentiality and authenticity
179 * guarantees for the messages encrypted with the same nonce
180 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300181 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200182 * \note If the context is being used for AAD only (no data to
183 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300184 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200185 * \warning Decryption with the piecewise API is discouraged, see the
186 * warning on \c mbedtls_chachapoly_init().
187 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000188 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
189 * and bound to a key.
190 * \param nonce The nonce/IV to use for the message.
191 * This must be a redable buffer of length \c 12 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200193 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200194 *
195 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000196 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300197 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200198int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
199 const unsigned char nonce[12],
200 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300201
202/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \brief This function feeds additional data to be authenticated
204 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300205 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * The Additional Authenticated Data (AAD), also called
207 * Associated Data (AD) is only authenticated but not
208 * encrypted nor included in the encrypted output. It is
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200209 * usually transmitted separately from the ciphertext or
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300211 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200212 * \note This function is called before data is encrypted/decrypted.
213 * I.e. call this function to process the AAD before calling
214 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300215 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200216 * You may call this function multiple times to process
217 * an arbitrary amount of AAD. It is permitted to call
218 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300219 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200220 * This function cannot be called any more if data has
221 * been processed by \c mbedtls_chachapoly_update(),
222 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300223 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200224 * \warning Decryption with the piecewise API is discouraged, see the
225 * warning on \c mbedtls_chachapoly_init().
226 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000227 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
228 * and bound to a key.
229 * \param aad_len The length in Bytes of the AAD. The length has no
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200230 * restrictions.
231 * \param aad Buffer containing the AAD.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000232 * This pointer can be \c NULL if `aad_len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300233 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200234 * \return \c 0 on success.
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +0200235 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200236 * if \p ctx or \p aad are NULL.
237 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
238 * if the operations has not been started or has been
239 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300240 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200241int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200242 const unsigned char *aad,
243 size_t aad_len );
Daniel Kingb8025c52016-05-17 14:43:01 -0300244
245/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200246 * \brief Thus function feeds data to be encrypted or decrypted
247 * into an on-going ChaCha20-Poly1305
248 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300249 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200250 * The direction (encryption or decryption) depends on the
251 * mode that was given when calling
252 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300253 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200254 * You may call this function multiple times to process
255 * an arbitrary amount of data. It is permitted to call
256 * this function 0 times, if no data is to be encrypted
257 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300258 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200259 * \warning Decryption with the piecewise API is discouraged, see the
260 * warning on \c mbedtls_chachapoly_init().
261 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000262 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200263 * \param len The length (in bytes) of the data to encrypt or decrypt.
264 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000265 * This pointer can be \c NULL if `len == 0`.
266 * \param output The buffer to where the encrypted or decrypted data is
Hanno Beckerad7581f2018-12-17 09:43:21 +0000267 * written. This must be able to hold \p len bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000268 * This pointer can be \c NULL if `len == 0`.
Daniel Kingb8025c52016-05-17 14:43:01 -0300269 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200270 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200271 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
272 * if the operation has not been started or has been
273 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000274 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300275 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200276int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
277 size_t len,
278 const unsigned char *input,
279 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300280
281/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200282 * \brief This function finished the ChaCha20-Poly1305 operation and
283 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300284 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000285 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200286 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300287 *
Manuel Pégourié-Gonnardbe78b072018-05-24 19:33:59 +0200288 * \warning Decryption with the piecewise API is discouraged, see the
289 * warning on \c mbedtls_chachapoly_init().
290 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200291 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200292 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
293 * if the operation has not been started or has been
294 * finished.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000295 * \return Another negative error code on other kinds of failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300296 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200297int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
298 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300299
Daniel Kingb8025c52016-05-17 14:43:01 -0300300/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200301 * \brief This function performs a complete ChaCha20-Poly1305
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200302 * authenticated encryption with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300303 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200304 * \note Before using this function, you must set the key with
305 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300306 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200307 * \warning You must never use the same nonce twice with the same key.
308 * This would void any confidentiality and authenticity
309 * guarantees for the messages encrypted with the same nonce
310 * and key.
311 *
312 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000313 * This must be initialized.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200314 * \param length The length (in bytes) of the data to encrypt or decrypt.
315 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000316 * \param aad The buffer containing the additional authenticated
317 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200318 * \param aad_len The length (in bytes) of the AAD data to process.
319 * \param input The buffer containing the data to encrypt or decrypt.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000320 * This pointer can be \c NULL if `ilen == 0`.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000321 * \param output The buffer to where the encrypted or decrypted data
322 * is written. This pointer can be \c NULL if `ilen == 0`.
323 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
324 * is written. This must not be \c NULL.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200325 *
326 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000327 * \return A negative error code on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300328 */
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200329int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
330 size_t length,
331 const unsigned char nonce[12],
332 const unsigned char *aad,
333 size_t aad_len,
334 const unsigned char *input,
335 unsigned char *output,
336 unsigned char tag[16] );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200337
338/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200339 * \brief This function performs a complete ChaCha20-Poly1305
340 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200341 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200342 * \note Before using this function, you must set the key with
343 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200344 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200345 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000346 * \param length The length (in Bytes) of the data to decrypt.
347 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200348 * \param aad The buffer containing the additional authenticated data (AAD).
Hanno Beckerad7581f2018-12-17 09:43:21 +0000349 * This pointer can be \c NULL if `aad_len == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200350 * \param aad_len The length (in bytes) of the AAD data to process.
351 * \param tag The buffer holding the authentication tag.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000352 * This must be a readable buffer of length \c 16 Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200353 * \param input The buffer containing the data to decrypt.
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 * \param output The buffer to where the decrypted data is written.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000356 * This pointer can be \c NULL if `ilen == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200357 *
358 * \return \c 0 on success.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200359 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
360 * if the data was not authentic.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000361 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200362 */
363int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
364 size_t length,
365 const unsigned char nonce[12],
366 const unsigned char *aad,
367 size_t aad_len,
368 const unsigned char tag[16],
369 const unsigned char *input,
370 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300371
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200372#if defined(MBEDTLS_SELF_TEST)
Daniel Kingb8025c52016-05-17 14:43:01 -0300373/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200374 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300375 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200376 * \return \c 0 on success.
377 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300378 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200379int mbedtls_chachapoly_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200380#endif /* MBEDTLS_SELF_TEST */
Daniel Kingb8025c52016-05-17 14:43:01 -0300381
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200382#ifdef __cplusplus
383}
384#endif
385
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200386#endif /* MBEDTLS_CHACHAPOLY_H */