blob: e7413b36f5f0969d8223074a979f9bb80bc14adf [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 *
4 * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539.
5 *
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020023#ifndef MBEDTLS_CHACHAPOLY_H
24#define MBEDTLS_CHACHAPOLY_H
Daniel Kingb8025c52016-05-17 14:43:01 -030025
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020032#define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */
33#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 +020034#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x00049 /**< Authenticated decryption failed: data was not authentic. */
35
Daniel Kingb8025c52016-05-17 14:43:01 -030036
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Daniel Kingb8025c52016-05-17 14:43:01 -030041typedef enum
42{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020043 MBEDTLS_CHACHAPOLY_ENCRYPT,
44 MBEDTLS_CHACHAPOLY_DECRYPT
Daniel Kingb8025c52016-05-17 14:43:01 -030045}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020046mbedtls_chachapoly_mode_t;
Daniel Kingb8025c52016-05-17 14:43:01 -030047
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020048#if !defined(MBEDTLS_CHACHAPOLY_ALT)
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020049
50#include "chacha20.h"
51#include "poly1305.h"
52
Daniel Kingb8025c52016-05-17 14:43:01 -030053typedef struct
54{
55 mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */
56 mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */
57 uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */
58 uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */
59 int state; /** Current state of the context */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020060 mbedtls_chachapoly_mode_t mode; /** Cipher mode (encrypt or decrypt) */
Daniel Kingb8025c52016-05-17 14:43:01 -030061}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020062mbedtls_chachapoly_context;
Daniel Kingb8025c52016-05-17 14:43:01 -030063
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020064#else /* !MBEDTLS_CHACHAPOLY_ALT */
65#include "chachapoly_alt.h"
66#endif /* !MBEDTLS_CHACHAPOLY_ALT */
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020067
Daniel Kingb8025c52016-05-17 14:43:01 -030068/**
69 * \brief Initialize ChaCha20-Poly1305 context
70 *
71 * \param ctx ChaCha20-Poly1305 context to be initialized
72 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020073void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -030074
75/**
76 * \brief Clear ChaCha20-Poly1305 context
77 *
78 * \param ctx ChaCha20-Poly1305 context to be cleared
79 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020080void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -030081
82/**
83 * \brief Set the ChaCha20-Poly1305 symmetric encryption key.
84 *
85 * \param ctx The ChaCha20-Poly1305 context.
86 * \param key The 256-bit (32 bytes) key.
87 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020088 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -030089 * if \p ctx or \p key are NULL.
90 * Otherwise, 0 is returned to indicate success.
91 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020092int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
93 const unsigned char key[32] );
Daniel Kingb8025c52016-05-17 14:43:01 -030094
95/**
96 * \brief Setup ChaCha20-Poly1305 context for encryption or decryption.
97 *
98 * \note If the context is being used for AAD only (no data to
99 * encrypt or decrypt) then \p mode can be set to any value.
100 *
101 * \param ctx The ChaCha20-Poly1305 context.
102 * \param nonce The nonce/IV to use for the message. This must be unique
103 * for every message encrypted under the same key.
104 * \param mode Specifies whether the context is used to encrypt or
105 * decrypt data.
106 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200107 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300108 * if \p ctx or \p mac are NULL.
109 * Otherwise, 0 is returned to indicate success.
110 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200111int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
112 const unsigned char nonce[12],
113 mbedtls_chachapoly_mode_t mode );
Daniel Kingb8025c52016-05-17 14:43:01 -0300114
115/**
116 * \brief Process additional authenticated data (AAD).
117 *
118 * This function processes data that is authenticated, but
119 * not encrypted.
120 *
121 * \note This function is called before data is encrypted/decrypted.
122 * I.e. call this function to process the AAD before calling
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200123 * mbedtls_chachapoly_update.
Daniel Kingb8025c52016-05-17 14:43:01 -0300124 *
125 * You may call this function multiple times to process
126 * an arbitrary amount of AAD. It is permitted to call
127 * this function 0 times, if no AAD is used.
128 *
129 * This function cannot be called any more if data has
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200130 * been processed by mbedtls_chachapoly_update,
Daniel Kingb8025c52016-05-17 14:43:01 -0300131 * or if the context has been finished.
132 *
133 * \param ctx The ChaCha20-Poly1305 context.
134 * \param aad_len The length (in bytes) of the AAD. The length has no
135 * restrictions.
136 * \param aad Buffer containing the AAD.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300137 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300138 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200139 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300140 * if \p ctx or \p aad are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200141 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300142 * the context has not been setup, the context has been
143 * finished, or if the AAD has been finished.
144 * Otherwise, 0 is returned to indicate success.
145 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200146int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
147 size_t aad_len,
148 const unsigned char *aad );
Daniel Kingb8025c52016-05-17 14:43:01 -0300149
150/**
151 * \brief Encrypt/decrypt data.
152 *
153 * The direction (encryption or decryption) depends on the
154 * mode that was given when calling
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200155 * mbedtls_chachapoly_starts.
Daniel Kingb8025c52016-05-17 14:43:01 -0300156 *
157 * You may call this function multiple times to process
158 * an arbitrary amount of data. It is permitted to call
159 * this function 0 times, if no data is to be encrypted
160 * or decrypted.
161 *
162 * \param ctx The ChaCha20-Poly1305 context.
163 * \param len The length (in bytes) of the data to encrypt or decrypt.
164 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300165 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300166 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300167 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300168 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200169 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300170 * if \p ctx, \p input, or \p output are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200171 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300172 * the context has not been setup, or if the context has been
173 * finished.
174 * Otherwise, 0 is returned to indicate success.
175 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200176int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
177 size_t len,
178 const unsigned char *input,
179 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300180
181/**
182 * \brief Compute the ChaCha20-Poly1305 MAC.
183 *
184 * \param ctx The ChaCha20-Poly1305 context.
185 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
186 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200187 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300188 * if \p ctx or \p mac are NULL.
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200189 * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if
Daniel Kingb8025c52016-05-17 14:43:01 -0300190 * the context has not been setup.
191 * Otherwise, 0 is returned to indicate success.
192 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200193int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
194 unsigned char mac[16] );
Daniel Kingb8025c52016-05-17 14:43:01 -0300195
Daniel Kingb8025c52016-05-17 14:43:01 -0300196/**
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200197 * \brief Encrypt or decrypt data, and produce a MAC (tag) with ChaCha20-Poly1305.
Daniel Kingb8025c52016-05-17 14:43:01 -0300198 *
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200199 * \param ctx The ChachaPoly context.
Daniel Kingb8025c52016-05-17 14:43:01 -0300200 * \param mode Specifies whether the data in the \p input buffer is to
201 * be encrypted or decrypted. If there is no data to encrypt
202 * or decrypt (i.e. \p ilen is 0) then the value of this
203 * parameter does not matter.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200204 * \param length The length (in bytes) of the data to encrypt or decrypt.
205 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
Daniel Kingb8025c52016-05-17 14:43:01 -0300206 * \param aad Buffer containing the additional authenticated data (AAD).
Daniel Kinga310c5e2016-05-17 15:56:26 -0300207 * This pointer can be NULL if aad_len == 0.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200208 * \param aad_len The length (in bytes) of the AAD data to process.
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300210 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300211 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300212 * This pointer can be NULL if ilen == 0.
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200213 * \param tag Buffer to where the computed 128-bit (16 bytes) MAC is written.
Daniel Kingb8025c52016-05-17 14:43:01 -0300214 *
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200215 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
Daniel Kingb8025c52016-05-17 14:43:01 -0300216 * if one or more of the required parameters are NULL.
217 * Otherwise, 0 is returned to indicate success.
218 */
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200219int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200220 mbedtls_chachapoly_mode_t mode,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200221 size_t length,
222 const unsigned char nonce[12],
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200223 const unsigned char *aad,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200224 size_t aad_len,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200225 const unsigned char *input,
226 unsigned char *output,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200227 unsigned char tag[16] );
228
229/**
230 * \brief Decrypt data and check a MAC (tag) with ChaCha20-Poly1305.
231 *
232 * \param ctx The ChachaPoly context.
233 * \param length The length of the input and output data.
234 * \param nonce The nonce / initialization vector.
235 * \param aad The buffer holding the additional authenticated data.
236 * \param aad_len The length of the additional authenticated data.
237 * \param tag The buffer holding the tag.
238 * \param input The buffer holding the input data.
239 * \param output The buffer for holding the output data.
240 *
241 * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned
242 * if one or more of the required parameters are NULL.
243 * MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED if the tag does not
244 * match.
245 * Otherwise, 0 is returned to indicate success.
246 */
247int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
248 size_t length,
249 const unsigned char nonce[12],
250 const unsigned char *aad,
251 size_t aad_len,
252 const unsigned char tag[16],
253 const unsigned char *input,
254 unsigned char *output );
Daniel Kingb8025c52016-05-17 14:43:01 -0300255
256/**
257 * \brief Checkup routine
258 *
259 * \return 0 if successful, or 1 if the test failed
260 */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200261int mbedtls_chachapoly_self_test( int verbose );
Daniel Kingb8025c52016-05-17 14:43:01 -0300262
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200263#ifdef __cplusplus
264}
265#endif
266
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200267#endif /* MBEDTLS_CHACHAPOLY_H */