Mbed OS Reference
Loading...
Searching...
No Matches
chachapoly.h
Go to the documentation of this file.
1/**
2 * \file chachapoly.h
3 *
4 * \brief This file contains the AEAD-ChaCha20-Poly1305 definitions and
5 * functions.
6 *
7 * 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/*
16 * Copyright The Mbed TLS Contributors
17 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 */
31
32#ifndef MBEDTLS_CHACHAPOLY_H
33#define MBEDTLS_CHACHAPOLY_H
34
35#if !defined(MBEDTLS_CONFIG_FILE)
36#include "mbedtls/config.h"
37#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
41/* for shared error codes */
42#include "mbedtls/poly1305.h"
43
44#define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 /**< The requested operation is not permitted in the current state. */
45#define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 /**< Authenticated decryption failed: data was not authentic. */
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
52* \addtogroup mbedtls_chacha20_module
53* \{
54*/
55
56typedef enum
57{
58 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
59 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
62
63#if !defined(MBEDTLS_CHACHAPOLY_ALT)
64
65#include "mbedtls/chacha20.h"
66
68{
69 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
70 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
71 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
72 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
73 int state; /**< The current state of the context. */
74 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
75}
77
78#else /* !MBEDTLS_CHACHAPOLY_ALT */
79#include "chachapoly_alt.h"
80#endif /* !MBEDTLS_CHACHAPOLY_ALT */
81
82/**
83 * \brief This function initializes the specified ChaCha20-Poly1305 context.
84 *
85 * It must be the first API called before using
86 * the context. It must be followed by a call to
87 * \c mbedtls_chachapoly_setkey() before any operation can be
88 * done, and to \c mbedtls_chachapoly_free() once all
89 * operations with that context have been finished.
90 *
91 * In order to encrypt or decrypt full messages at once, for
92 * each message you should make a single call to
93 * \c mbedtls_chachapoly_crypt_and_tag() or
94 * \c mbedtls_chachapoly_auth_decrypt().
95 *
96 * In order to encrypt messages piecewise, for each
97 * message you should make a call to
98 * \c mbedtls_chachapoly_starts(), then 0 or more calls to
99 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
100 * \c mbedtls_chachapoly_update(), then one call to
101 * \c mbedtls_chachapoly_finish().
102 *
103 * \warning Decryption with the piecewise API is discouraged! Always
104 * use \c mbedtls_chachapoly_auth_decrypt() when possible!
105 *
106 * If however this is not possible because the data is too
107 * large to fit in memory, you need to:
108 *
109 * - call \c mbedtls_chachapoly_starts() and (if needed)
110 * \c mbedtls_chachapoly_update_aad() as above,
111 * - call \c mbedtls_chachapoly_update() multiple times and
112 * ensure its output (the plaintext) is NOT used in any other
113 * way than placing it in temporary storage at this point,
114 * - call \c mbedtls_chachapoly_finish() to compute the
115 * authentication tag and compared it in constant time to the
116 * tag received with the ciphertext.
117 *
118 * If the tags are not equal, you must immediately discard
119 * all previous outputs of \c mbedtls_chachapoly_update(),
120 * otherwise you can now safely use the plaintext.
121 *
122 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
123 */
125
126/**
127 * \brief This function releases and clears the specified
128 * ChaCha20-Poly1305 context.
129 *
130 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
131 * case this function is a no-op.
132 */
134
135/**
136 * \brief This function sets the ChaCha20-Poly1305
137 * symmetric encryption key.
138 *
139 * \param ctx The ChaCha20-Poly1305 context to which the key should be
140 * bound. This must be initialized.
141 * \param key The \c 256 Bit (\c 32 Bytes) key.
142 *
143 * \return \c 0 on success.
144 * \return A negative error code on failure.
145 */
147 const unsigned char key[32] );
148
149/**
150 * \brief This function starts a ChaCha20-Poly1305 encryption or
151 * decryption operation.
152 *
153 * \warning You must never use the same nonce twice with the same key.
154 * This would void any confidentiality and authenticity
155 * guarantees for the messages encrypted with the same nonce
156 * and key.
157 *
158 * \note If the context is being used for AAD only (no data to
159 * encrypt or decrypt) then \p mode can be set to any value.
160 *
161 * \warning Decryption with the piecewise API is discouraged, see the
162 * warning on \c mbedtls_chachapoly_init().
163 *
164 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
165 * and bound to a key.
166 * \param nonce The nonce/IV to use for the message.
167 * This must be a redable buffer of length \c 12 Bytes.
168 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
169 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
170 *
171 * \return \c 0 on success.
172 * \return A negative error code on failure.
173 */
175 const unsigned char nonce[12],
177
178/**
179 * \brief This function feeds additional data to be authenticated
180 * into an ongoing ChaCha20-Poly1305 operation.
181 *
182 * The Additional Authenticated Data (AAD), also called
183 * Associated Data (AD) is only authenticated but not
184 * encrypted nor included in the encrypted output. It is
185 * usually transmitted separately from the ciphertext or
186 * computed locally by each party.
187 *
188 * \note This function is called before data is encrypted/decrypted.
189 * I.e. call this function to process the AAD before calling
190 * \c mbedtls_chachapoly_update().
191 *
192 * You may call this function multiple times to process
193 * an arbitrary amount of AAD. It is permitted to call
194 * this function 0 times, if no AAD is used.
195 *
196 * This function cannot be called any more if data has
197 * been processed by \c mbedtls_chachapoly_update(),
198 * or if the context has been finished.
199 *
200 * \warning Decryption with the piecewise API is discouraged, see the
201 * warning on \c mbedtls_chachapoly_init().
202 *
203 * \param ctx The ChaCha20-Poly1305 context. This must be initialized
204 * and bound to a key.
205 * \param aad_len The length in Bytes of the AAD. The length has no
206 * restrictions.
207 * \param aad Buffer containing the AAD.
208 * This pointer can be \c NULL if `aad_len == 0`.
209 *
210 * \return \c 0 on success.
211 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
212 * if \p ctx or \p aad are NULL.
213 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
214 * if the operations has not been started or has been
215 * finished, or if the AAD has been finished.
216 */
218 const unsigned char *aad,
219 size_t aad_len );
220
221/**
222 * \brief Thus function feeds data to be encrypted or decrypted
223 * into an on-going ChaCha20-Poly1305
224 * operation.
225 *
226 * The direction (encryption or decryption) depends on the
227 * mode that was given when calling
228 * \c mbedtls_chachapoly_starts().
229 *
230 * You may call this function multiple times to process
231 * an arbitrary amount of data. It is permitted to call
232 * this function 0 times, if no data is to be encrypted
233 * or decrypted.
234 *
235 * \warning Decryption with the piecewise API is discouraged, see the
236 * warning on \c mbedtls_chachapoly_init().
237 *
238 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
239 * \param len The length (in bytes) of the data to encrypt or decrypt.
240 * \param input The buffer containing the data to encrypt or decrypt.
241 * This pointer can be \c NULL if `len == 0`.
242 * \param output The buffer to where the encrypted or decrypted data is
243 * written. This must be able to hold \p len bytes.
244 * This pointer can be \c NULL if `len == 0`.
245 *
246 * \return \c 0 on success.
247 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
248 * if the operation has not been started or has been
249 * finished.
250 * \return Another negative error code on other kinds of failure.
251 */
253 size_t len,
254 const unsigned char *input,
255 unsigned char *output );
256
257/**
258 * \brief This function finished the ChaCha20-Poly1305 operation and
259 * generates the MAC (authentication tag).
260 *
261 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
262 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
263 *
264 * \warning Decryption with the piecewise API is discouraged, see the
265 * warning on \c mbedtls_chachapoly_init().
266 *
267 * \return \c 0 on success.
268 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
269 * if the operation has not been started or has been
270 * finished.
271 * \return Another negative error code on other kinds of failure.
272 */
274 unsigned char mac[16] );
275
276/**
277 * \brief This function performs a complete ChaCha20-Poly1305
278 * authenticated encryption with the previously-set key.
279 *
280 * \note Before using this function, you must set the key with
281 * \c mbedtls_chachapoly_setkey().
282 *
283 * \warning You must never use the same nonce twice with the same key.
284 * This would void any confidentiality and authenticity
285 * guarantees for the messages encrypted with the same nonce
286 * and key.
287 *
288 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
289 * This must be initialized.
290 * \param length The length (in bytes) of the data to encrypt or decrypt.
291 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
292 * \param aad The buffer containing the additional authenticated
293 * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
294 * \param aad_len The length (in bytes) of the AAD data to process.
295 * \param input The buffer containing the data to encrypt or decrypt.
296 * This pointer can be \c NULL if `ilen == 0`.
297 * \param output The buffer to where the encrypted or decrypted data
298 * is written. This pointer can be \c NULL if `ilen == 0`.
299 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
300 * is written. This must not be \c NULL.
301 *
302 * \return \c 0 on success.
303 * \return A negative error code on failure.
304 */
306 size_t length,
307 const unsigned char nonce[12],
308 const unsigned char *aad,
309 size_t aad_len,
310 const unsigned char *input,
311 unsigned char *output,
312 unsigned char tag[16] );
313
314/**
315 * \brief This function performs a complete ChaCha20-Poly1305
316 * authenticated decryption with the previously-set key.
317 *
318 * \note Before using this function, you must set the key with
319 * \c mbedtls_chachapoly_setkey().
320 *
321 * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
322 * \param length The length (in Bytes) of the data to decrypt.
323 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
324 * \param aad The buffer containing the additional authenticated data (AAD).
325 * This pointer can be \c NULL if `aad_len == 0`.
326 * \param aad_len The length (in bytes) of the AAD data to process.
327 * \param tag The buffer holding the authentication tag.
328 * This must be a readable buffer of length \c 16 Bytes.
329 * \param input The buffer containing the data to decrypt.
330 * This pointer can be \c NULL if `ilen == 0`.
331 * \param output The buffer to where the decrypted data is written.
332 * This pointer can be \c NULL if `ilen == 0`.
333 *
334 * \return \c 0 on success.
335 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
336 * if the data was not authentic.
337 * \return Another negative error code on other kinds of failure.
338 */
340 size_t length,
341 const unsigned char nonce[12],
342 const unsigned char *aad,
343 size_t aad_len,
344 const unsigned char tag[16],
345 const unsigned char *input,
346 unsigned char *output );
347
348#if defined(MBEDTLS_SELF_TEST)
349/**
350 * \brief The ChaCha20-Poly1305 checkup routine.
351 *
352 * \return \c 0 on success.
353 * \return \c 1 on failure.
354 */
355int mbedtls_chachapoly_self_test( int verbose );
356#endif /* MBEDTLS_SELF_TEST */
357
358/// \}
359
360#ifdef __cplusplus
361}
362#endif
363
364#endif /* MBEDTLS_CHACHAPOLY_H */
This file contains ChaCha20 definitions and functions.
Configuration options (set of defines)
int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx, unsigned char mac[16])
This function finished the ChaCha20-Poly1305 operation and generates the MAC (authentication tag).
void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx)
This function initializes the specified ChaCha20-Poly1305 context.
int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx, const unsigned char nonce[12], mbedtls_chachapoly_mode_t mode)
This function starts a ChaCha20-Poly1305 encryption or decryption operation.
int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx, const unsigned char key[32])
This function sets the ChaCha20-Poly1305 symmetric encryption key.
int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx, size_t len, const unsigned char *input, unsigned char *output)
Thus function feeds data to be encrypted or decrypted into an on-going ChaCha20-Poly1305 operation.
void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx)
This function releases and clears the specified ChaCha20-Poly1305 context.
int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx, size_t length, const unsigned char nonce[12], const unsigned char *aad, size_t aad_len, const unsigned char *input, unsigned char *output, unsigned char tag[16])
This function performs a complete ChaCha20-Poly1305 authenticated encryption with the previously-set ...
mbedtls_chachapoly_mode_t
Definition: chachapoly.h:57
int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx, size_t length, const unsigned char nonce[12], const unsigned char *aad, size_t aad_len, const unsigned char tag[16], const unsigned char *input, unsigned char *output)
This function performs a complete ChaCha20-Poly1305 authenticated decryption with the previously-set ...
int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx, const unsigned char *aad, size_t aad_len)
This function feeds additional data to be authenticated into an ongoing ChaCha20-Poly1305 operation.
@ MBEDTLS_CHACHAPOLY_ENCRYPT
The mode value for performing encryption.
Definition: chachapoly.h:58
@ MBEDTLS_CHACHAPOLY_DECRYPT
The mode value for performing decryption.
Definition: chachapoly.h:59
This file contains Poly1305 definitions and functions.
mbedtls_chachapoly_mode_t mode
Cipher mode (encrypt or decrypt).
Definition: chachapoly.h:74
uint64_t aad_len
The length (bytes) of the Additional Authenticated Data.
Definition: chachapoly.h:71
mbedtls_poly1305_context poly1305_ctx
The Poly1305 context.
Definition: chachapoly.h:70
uint64_t ciphertext_len
The length (bytes) of the ciphertext.
Definition: chachapoly.h:72
int state
The current state of the context.
Definition: chachapoly.h:73
mbedtls_chacha20_context chacha20_ctx
The ChaCha20 context.
Definition: chachapoly.h:69