blob: ddcd549720d9c02f4500be91a7142b5046bb23c9 [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é-Gonnarddca3a5d2018-05-07 10:43:27 +020042#define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020043#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state. */
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020044#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x00049 /**< Authenticated decryption failed: data was not authentic. */
45
Daniel Kingb8025c52016-05-17 14:43:01 -030046
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Daniel Kingb8025c52016-05-17 14:43:01 -030051typedef enum
52{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020053 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
54 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
Daniel Kingb8025c52016-05-17 14:43:01 -030055}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020056mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030057
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020058#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020059
60#include "chacha20.h"
61#include "poly1305.h"
62
Daniel Kingb8025c52016-05-17 14:43:01 -030063typedef struct
64{
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 *
92 * In order to encrypt or decrypt messages piecewise, for each
93 * 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 *
99 *
100 * \param ctx The ChachaPoly context to initialize.
Daniel Kingb8025c52016-05-17 14:43:01 -0300101 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200102void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300103
104/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \brief This function releases and clears the specified ChaCha20-Poly1305 context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300106 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * \param ctx The ChachaPoly context to clear.
Daniel Kingb8025c52016-05-17 14:43:01 -0300108 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200109void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300110
111/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * \brief This function sets the ChaCha20-Poly1305 symmetric encryption key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300113 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200114 * \param ctx The ChaCha20-Poly1305 context to which the key should be
115 * bound.
116 * \param key The 256-bit (32 bytes) key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300117 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 * \return \c 0 on success.
119 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
120 * if \p ctx or \p key are NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300121 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200122int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
123 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300124
125/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 * \brief This function starts a ChaCha20-Poly1305 encryption or
127 * decryption operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300128 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200129 * \warning You must never use the same nonce twice with the same key.
130 * This would void any confidentiality and authenticity
131 * guarantees for the messages encrypted with the same nonce
132 * and key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300133 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200134 * \note If the context is being used for AAD only (no data to
135 * encrypt or decrypt) then \p mode can be set to any value.
Daniel Kingb8025c52016-05-17 14:43:01 -0300136 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \param ctx The ChaCha20-Poly1305 context.
138 * \param nonce The nonce/IV to use for the message. Must be 12 bytes.
139 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
140 * #MBEDTLS_CHACHAPOLY_DECRYPT.
141 *
142 * \return \c 0 on success.
143 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
144 * if \p ctx or \p mac are NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300145 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200146int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
147 const unsigned char nonce[12],
148 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300149
150/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \brief This function feeds additional data to be authenticated
152 * into an ongoing ChaCha20-Poly1305 operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300153 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * The Additional Authenticated Data (AAD), also called
155 * Associated Data (AD) is only authenticated but not
156 * encrypted nor included in the encrypted output. It is
157 * usually transmitted separately fro mthe ciphertext or
158 * computed locally by each party.
Daniel Kingb8025c52016-05-17 14:43:01 -0300159 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 * \note This function is called before data is encrypted/decrypted.
161 * I.e. call this function to process the AAD before calling
162 * \c mbedtls_chachapoly_update().
Daniel Kingb8025c52016-05-17 14:43:01 -0300163 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * You may call this function multiple times to process
165 * an arbitrary amount of AAD. It is permitted to call
166 * this function 0 times, if no AAD is used.
Daniel Kingb8025c52016-05-17 14:43:01 -0300167 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 * This function cannot be called any more if data has
169 * been processed by \c mbedtls_chachapoly_update(),
170 * or if the context has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300171 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \param ctx The ChaCha20-Poly1305 context to use.
173 * \param aad_len The length (in bytes) of the AAD. The length has no
174 * restrictions.
175 * \param aad Buffer containing the AAD.
176 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300177 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200178 * \return \c 0 on success.
179 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
180 * if \p ctx or \p aad are NULL.
181 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
182 * if the operations has not been started or has been
183 * finished, or if the AAD has been finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300184 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200185int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
186 size_t aad_len,
187 const unsigned char *aad );
Daniel Kingb8025c52016-05-17 14:43:01 -0300188
189/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \brief Thus function feeds data to be encrypted or decrypted
191 * into an on-going ChaCha20-Poly1305
192 * operation.
Daniel Kingb8025c52016-05-17 14:43:01 -0300193 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200194 * The direction (encryption or decryption) depends on the
195 * mode that was given when calling
196 * \c mbedtls_chachapoly_starts().
Daniel Kingb8025c52016-05-17 14:43:01 -0300197 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200198 * You may call this function multiple times to process
199 * an arbitrary amount of data. It is permitted to call
200 * this function 0 times, if no data is to be encrypted
201 * or decrypted.
Daniel Kingb8025c52016-05-17 14:43:01 -0300202 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \param ctx The ChaCha20-Poly1305 context to use.
204 * \param len The length (in bytes) of the data to encrypt or decrypt.
205 * \param input The buffer containing the data to encrypt or decrypt.
206 * This pointer can be NULL if len == 0.
207 * \param output The buffer to where the encrypted or decrypted data is written.
208 * Must be able to hold \p len bytes.
209 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300210 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200211 * \return \c 0 on success.
212 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
213 * if \p ctx, \p input, or \p output are NULL.
214 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
215 * if the operation has not been started or has been
216 * finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300217 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200218int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
219 size_t len,
220 const unsigned char *input,
221 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300222
223/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200224 * \brief This function finished the ChaCha20-Poly1305 operation and
225 * generates the MAC (authentication tag).
Daniel Kingb8025c52016-05-17 14:43:01 -0300226 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200227 * \param ctx The ChaCha20-Poly1305 context to use.
228 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300229 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200230 * \return \c 0 on success.
231 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
232 * if \p ctx or \p mac are NULL.
233 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
234 * if the operation has not been started or has been
235 * finished.
Daniel Kingb8025c52016-05-17 14:43:01 -0300236 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200237int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
238 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300239
Daniel Kingb8025c52016-05-17 14:43:01 -0300240/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200241 * \brief This function performs a complete ChaCha20-Poly1305
242 * operation with the previously-set key.
Daniel Kingb8025c52016-05-17 14:43:01 -0300243 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200244 * \note Before using this function, you must set the key with
245 * \c mbedtls_chachapoly_setkey().
Daniel Kingb8025c52016-05-17 14:43:01 -0300246 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200247 * \warning You must never use the same nonce twice with the same key.
248 * This would void any confidentiality and authenticity
249 * guarantees for the messages encrypted with the same nonce
250 * and key.
251 *
252 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
253 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
254 * #MBEDTLS_CHACHAPOLY_DECRYPT.
255 * \param length The length (in bytes) of the data to encrypt or decrypt.
256 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
257 * \param aad The buffer containing the additional authenticated data (AAD).
258 * This pointer can be NULL if aad_len == 0.
259 * \param aad_len The length (in bytes) of the AAD data to process.
260 * \param input The buffer containing the data to encrypt or decrypt.
261 * This pointer can be NULL if ilen == 0.
262 * \param output The buffer to where the encrypted or decrypted data is written.
263 * This pointer can be NULL if ilen == 0.
264 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC is written.
265 *
266 * \return \c 0 on success.
267 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
268 * if one or more of the required parameters are NULL.
Daniel Kingb8025c52016-05-17 14:43:01 -0300269 */
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200270int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200271 mbedtls_chachapoly_mode_t mode,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200272 size_t length,
273 const unsigned char nonce[12],
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200274 const unsigned char *aad,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200275 size_t aad_len,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200276 const unsigned char *input,
277 unsigned char *output,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200278 unsigned char tag[16] );
279
280/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200281 * \brief This function performs a complete ChaCha20-Poly1305
282 * authenticated decryption with the previously-set key.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200283 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200284 * \note Before using this function, you must set the key with
285 * \c mbedtls_chachapoly_setkey().
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200286 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200287 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
288 * \param length The length (in bytes) of the data to decrypt.
289 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
290 * \param aad The buffer containing the additional authenticated data (AAD).
291 * This pointer can be NULL if aad_len == 0.
292 * \param aad_len The length (in bytes) of the AAD data to process.
293 * \param tag The buffer holding the authentication tag.
294 * \param input The buffer containing the data to decrypt.
295 * This pointer can be NULL if ilen == 0.
296 * \param output The buffer to where the decrypted data is written.
297 * This pointer can be NULL if ilen == 0.
298 *
299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA
301 * if one or more of the required parameters are NULL.
302 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
303 * if the data was not authentic.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200304 */
305int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
306 size_t length,
307 const unsigned char nonce[12],
308 const unsigned char *aad,
309 size_t aad_len,
310 const unsigned char tag[16],
311 const unsigned char *input,
312 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300313
314/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200315 * \brief The ChaCha20-Poly1305 checkup routine.
Daniel Kingb8025c52016-05-17 14:43:01 -0300316 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200317 * \return \c 0 on success.
318 * \return \c 1 on failure.
Daniel Kingb8025c52016-05-17 14:43:01 -0300319 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200320int mbedtls_chachapoly_self_test( int verbose );
Daniel Kingb8025c52016-05-17 14:43:01 -0300321
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200322#ifdef __cplusplus
323}
324#endif
325
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200326#endif /* MBEDTLS_CHACHAPOLY_H */