5281dcf10eee7d35a28ef04d1e8dad90fff2f124
[BearSSL] / inc / bearssl_block.h
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifndef BR_BEARSSL_BLOCK_H__
26 #define BR_BEARSSL_BLOCK_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30
31 /** \file bearssl_block.h
32 *
33 * # Block Ciphers and Symmetric Ciphers
34 *
35 * This file documents the API for block ciphers and other symmetric
36 * ciphers.
37 *
38 *
39 * ## Procedural API
40 *
41 * For a block cipher implementation, up to three separate sets of
42 * functions are provided, for CBC encryption, CBC decryption, and CTR
43 * encryption/decryption. Each set has its own context structure,
44 * initialised with the encryption key.
45 *
46 * For CBC encryption and decryption, the data to encrypt or decrypt is
47 * referenced as a sequence of blocks. The implementations assume that
48 * there is no partial block; no padding is applied or removed. The
49 * caller is responsible for handling any kind of padding.
50 *
51 * Function for CTR encryption are defined only for block ciphers with
52 * blocks of 16 bytes or more (i.e. AES, but not DES/3DES).
53 *
54 * Each implemented block cipher is identified by an "internal name"
55 * from which are derived the names of structures and functions that
56 * implement the cipher. For the block cipher of internal name "`xxx`",
57 * the following are defined:
58 *
59 * - `br_xxx_BLOCK_SIZE`
60 *
61 * A macro that evaluates to the block size (in bytes) of the
62 * cipher. For all implemented block ciphers, this value is a
63 * power of two.
64 *
65 * - `br_xxx_cbcenc_keys`
66 *
67 * Context structure that contains the subkeys resulting from the key
68 * expansion. These subkeys are appropriate for CBC encryption. The
69 * structure first field is called `vtable` and points to the
70 * appropriate OOP structure.
71 *
72 * - `br_xxx_cbcenc_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`
73 *
74 * Perform key expansion: subkeys for CBC encryption are computed and
75 * written in the provided context structure. The key length MUST be
76 * adequate for the implemented block cipher. This function also sets
77 * the `vtable` field.
78 *
79 * - `br_xxx_cbcenc_run(const br_xxx_cbcenc_keys *ctx, void *iv, void *data, size_t len)`
80 *
81 * Perform CBC encryption of `len` bytes, in place. The encrypted data
82 * replaces the cleartext. `len` MUST be a multiple of the block length
83 * (if it is not, the function may loop forever or overflow a buffer).
84 * The IV is provided with the `iv` pointer; it is also updated with
85 * a copy of the last encrypted block.
86 *
87 * - `br_xxx_cbcdec_keys`
88 *
89 * Context structure that contains the subkeys resulting from the key
90 * expansion. These subkeys are appropriate for CBC decryption. The
91 * structure first field is called `vtable` and points to the
92 * appropriate OOP structure.
93 *
94 * - `br_xxx_cbcdec_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`
95 *
96 * Perform key expansion: subkeys for CBC decryption are computed and
97 * written in the provided context structure. The key length MUST be
98 * adequate for the implemented block cipher. This function also sets
99 * the `vtable` field.
100 *
101 * - `br_xxx_cbcdec_run(const br_xxx_cbcdec_keys *ctx, void *iv, void *data, size_t num_blocks)`
102 *
103 * Perform CBC decryption of `len` bytes, in place. The decrypted data
104 * replaces the ciphertext. `len` MUST be a multiple of the block length
105 * (if it is not, the function may loop forever or overflow a buffer).
106 * The IV is provided with the `iv` pointer; it is also updated with
107 * a copy of the last _encrypted_ block.
108 *
109 * - `br_xxx_ctr_keys`
110 *
111 * Context structure that contains the subkeys resulting from the key
112 * expansion. These subkeys are appropriate for CTR encryption and
113 * decryption. The structure first field is called `vtable` and
114 * points to the appropriate OOP structure.
115 *
116 * - `br_xxx_ctr_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)`
117 *
118 * Perform key expansion: subkeys for CTR encryption and decryption
119 * are computed and written in the provided context structure. The
120 * key length MUST be adequate for the implemented block cipher. This
121 * function also sets the `vtable` field.
122 *
123 * - `br_xxx_ctr_run(const br_xxx_ctr_keys *ctx, const void *iv, uint32_t cc, void *data, size_t len)` (returns `uint32_t`)
124 *
125 * Perform CTR encryption/decryption of some data. Processing is done
126 * "in place" (the output data replaces the input data). This function
127 * implements the "standard incrementing function" from NIST SP800-38A,
128 * annex B: the IV length shall be 4 bytes less than the block size
129 * (i.e. 12 bytes for AES) and the counter is the 32-bit value starting
130 * with `cc`. The data length (`len`) is not necessarily a multiple of
131 * the block size. The new counter value is returned, which supports
132 * chunked processing, provided that each chunk length (except possibly
133 * the last one) is a multiple of the block size.
134 *
135 *
136 * It shall be noted that the key expansion functions return `void`. If
137 * the provided key length is not allowed, then there will be no error
138 * reporting; implementations need not validate the key length, thus an
139 * invalid key length may result in undefined behaviour (e.g. buffer
140 * overflow).
141 *
142 * Subkey structures contain no interior pointer, and no external
143 * resources are allocated upon key expansion. They can thus be
144 * discarded without any explicit deallocation.
145 *
146 *
147 * ## Object-Oriented API
148 *
149 * Each context structure begins with a field (called `vtable`) that
150 * points to an instance of a structure that references the relevant
151 * functions through pointers. Each such structure contains the
152 * following:
153 *
154 * - `context_size`
155 *
156 * The size (in bytes) of the context structure for subkeys.
157 *
158 * - `block_size`
159 *
160 * The cipher block size (in bytes).
161 *
162 * - `log_block_size`
163 *
164 * The base-2 logarithm of cipher block size (e.g. 4 for blocks
165 * of 16 bytes).
166 *
167 * - `init`
168 *
169 * Pointer to the key expansion function.
170 *
171 * - `run`
172 *
173 * Pointer to the encryption/decryption function.
174 *
175 *
176 * For block cipher "`xxx`", static, constant instances of these
177 * structures are defined, under the names:
178 *
179 * - `br_xxx_cbcenc_vtable`
180 * - `br_xxx_cbcdec_vtable`
181 * - `br_xxx_ctr_vtable`
182 *
183 *
184 * ## Implemented Block Ciphers
185 *
186 * Provided implementations are:
187 *
188 * | Name | Function | Block Size (bytes) | Key lengths (bytes) |
189 * | :-------- | :------- | :----------------: | :-----------------: |
190 * | aes_big | AES | 16 | 16, 24 and 32 |
191 * | aes_small | AES | 16 | 16, 24 and 32 |
192 * | aes_ct | AES | 16 | 16, 24 and 32 |
193 * | aes_ct64 | AES | 16 | 16, 24 and 32 |
194 * | des_ct | DES/3DES | 8 | 8, 16 and 24 |
195 * | des_tab | DES/3DES | 8 | 8, 16 and 24 |
196 *
197 * **Note:** DES/3DES nominally uses keys of 64, 128 and 192 bits (i.e. 8,
198 * 16 and 24 bytes), but some of the bits are ignored by the algorithm, so
199 * the _effective_ key lengths, from a security point of view, are 56,
200 * 112 and 168 bits, respectively.
201 *
202 * `aes_big` is a "classical" AES implementation, using tables. It
203 * is fast but not constant-time, since it makes data-dependent array
204 * accesses.
205 *
206 * `aes_small` is an AES implementation optimized for code size. It
207 * is substantially slower than `aes_big`; it is not constant-time
208 * either.
209 *
210 * `aes_ct` is a constant-time implementation of AES; its code is about
211 * as big as that of `aes_big`, while its performance is comparable to
212 * that of `aes_small`. However, it is constant-time. This
213 * implementation should thus be considered to be the "default" AES in
214 * BearSSL, to be used unless the operational context guarantees that a
215 * non-constant-time implementation is safe, or an architecture-specific
216 * constant-time implementation can be used (e.g. using dedicated
217 * hardware opcodes).
218 *
219 * `aes_ct64` is another constant-time implementation of AES. It is
220 * similar to `aes_ct` but uses 64-bit values. On 32-bit machines,
221 * `aes_ct64` is not faster than `aes_ct`, often a bit slower, and has
222 * a larger footprint; however, on 64-bit architectures, `aes_ct64`
223 * is typically twice faster than `aes_ct` for modes that allow parallel
224 * operations (i.e. CTR, and CBC decryption, but not CBC encryption).
225 *
226 * `des_tab` is a classic, table-based implementation of DES/3DES. It
227 * is not constant-time.
228 *
229 * `des_ct` is an constant-time implementation of DES/3DES. It is
230 * substantially slower than `des_tab`.
231 *
232 * ## ChaCha20 and Poly1305
233 *
234 * ChaCha20 is a stream cipher. Poly1305 is a MAC algorithm. They
235 * are described in [RFC 7539](https://tools.ietf.org/html/rfc7539).
236 *
237 * Two function pointer types are defined:
238 *
239 * - `br_chacha20_run` describes a function that implements ChaCha20
240 * only.
241 *
242 * - `br_poly1305_run` describes an implementation of Poly1305,
243 * in the AEAD combination with ChaCha20 specified in RFC 7539
244 * (the ChaCha20 implementation is provided as a function pointer).
245 *
246 * `chacha20_ct` is a straightforward implementation of ChaCha20 in
247 * plain C; it is constant-time, small, and reasonably fast.
248 *
249 * `poly1305_ctmul` is an implementation of the ChaCha20+Poly1305 AEAD
250 * construction, where the Poly1305 part is performed with mixed 32-bit
251 * multiplications (operands are 32-bit, result is 64-bit).
252 */
253
254 /**
255 * \brief Class type for CBC encryption implementations.
256 *
257 * A `br_block_cbcenc_class` instance points to the functions implementing
258 * a specific block cipher, when used in CBC mode for encrypting data.
259 */
260 typedef struct br_block_cbcenc_class_ br_block_cbcenc_class;
261 struct br_block_cbcenc_class_ {
262 /**
263 * \brief Size (in bytes) of the context structure appropriate
264 * for containing subkeys.
265 */
266 size_t context_size;
267
268 /**
269 * \brief Size of individual blocks (in bytes).
270 */
271 unsigned block_size;
272
273 /**
274 * \brief Base-2 logarithm of the size of individual blocks,
275 * expressed in bytes.
276 */
277 unsigned log_block_size;
278
279 /**
280 * \brief Initialisation function.
281 *
282 * This function sets the `vtable` field in the context structure.
283 * The key length MUST be one of the key lengths supported by
284 * the implementation.
285 *
286 * \param ctx context structure to initialise.
287 * \param key secret key.
288 * \param key_len key length (in bytes).
289 */
290 void (*init)(const br_block_cbcenc_class **ctx,
291 const void *key, size_t key_len);
292
293 /**
294 * \brief Run the CBC encryption.
295 *
296 * The `iv` parameter points to the IV for this run; it is
297 * updated with a copy of the last encrypted block. The data
298 * is encrypted "in place"; its length (`len`) MUST be a
299 * multiple of the block size.
300 *
301 * \param ctx context structure (already initialised).
302 * \param iv IV for CBC encryption (updated).
303 * \param data data to encrypt.
304 * \param len data length (in bytes, multiple of block size).
305 */
306 void (*run)(const br_block_cbcenc_class *const *ctx,
307 void *iv, void *data, size_t len);
308 };
309
310 /**
311 * \brief Class type for CBC decryption implementations.
312 *
313 * A `br_block_cbcdec_class` instance points to the functions implementing
314 * a specific block cipher, when used in CBC mode for decrypting data.
315 */
316 typedef struct br_block_cbcdec_class_ br_block_cbcdec_class;
317 struct br_block_cbcdec_class_ {
318 /**
319 * \brief Size (in bytes) of the context structure appropriate
320 * for containing subkeys.
321 */
322 size_t context_size;
323
324 /**
325 * \brief Size of individual blocks (in bytes).
326 */
327 unsigned block_size;
328
329 /**
330 * \brief Base-2 logarithm of the size of individual blocks,
331 * expressed in bytes.
332 */
333 unsigned log_block_size;
334
335 /**
336 * \brief Initialisation function.
337 *
338 * This function sets the `vtable` field in the context structure.
339 * The key length MUST be one of the key lengths supported by
340 * the implementation.
341 *
342 * \param ctx context structure to initialise.
343 * \param key secret key.
344 * \param key_len key length (in bytes).
345 */
346 void (*init)(const br_block_cbcdec_class **ctx,
347 const void *key, size_t key_len);
348
349 /**
350 * \brief Run the CBC decryption.
351 *
352 * The `iv` parameter points to the IV for this run; it is
353 * updated with a copy of the last encrypted block. The data
354 * is decrypted "in place"; its length (`len`) MUST be a
355 * multiple of the block size.
356 *
357 * \param ctx context structure (already initialised).
358 * \param iv IV for CBC decryption (updated).
359 * \param data data to decrypt.
360 * \param len data length (in bytes, multiple of block size).
361 */
362 void (*run)(const br_block_cbcdec_class *const *ctx,
363 void *iv, void *data, size_t len);
364 };
365
366 /**
367 * \brief Class type for CTR encryption/decryption implementations.
368 *
369 * A `br_block_ctr_class` instance points to the functions implementing
370 * a specific block cipher, when used in CTR mode for encrypting or
371 * decrypting data.
372 */
373 typedef struct br_block_ctr_class_ br_block_ctr_class;
374 struct br_block_ctr_class_ {
375 /**
376 * \brief Size (in bytes) of the context structure appropriate
377 * for containing subkeys.
378 */
379 size_t context_size;
380
381 /**
382 * \brief Size of individual blocks (in bytes).
383 */
384 unsigned block_size;
385
386 /**
387 * \brief Base-2 logarithm of the size of individual blocks,
388 * expressed in bytes.
389 */
390 unsigned log_block_size;
391
392 /**
393 * \brief Initialisation function.
394 *
395 * This function sets the `vtable` field in the context structure.
396 * The key length MUST be one of the key lengths supported by
397 * the implementation.
398 *
399 * \param ctx context structure to initialise.
400 * \param key secret key.
401 * \param key_len key length (in bytes).
402 */
403 void (*init)(const br_block_ctr_class **ctx,
404 const void *key, size_t key_len);
405
406 /**
407 * \brief Run the CTR encryption or decryption.
408 *
409 * The `iv` parameter points to the IV for this run; its
410 * length is exactly 4 bytes less than the block size (e.g.
411 * 12 bytes for AES/CTR). The IV is combined with a 32-bit
412 * block counter to produce the block value which is processed
413 * with the block cipher.
414 *
415 * The data to encrypt or decrypt is updated "in place". Its
416 * length (`len` bytes) is not required to be a multiple of
417 * the block size; if the final block is partial, then the
418 * corresponding key stream bits are dropped.
419 *
420 * The resulting counter value is returned.
421 *
422 * \param ctx context structure (already initialised).
423 * \param iv IV for CTR encryption/decryption.
424 * \param cc initial value for the block counter.
425 * \param data data to encrypt or decrypt.
426 * \param len data length (in bytes).
427 * \return the new block counter value.
428 */
429 uint32_t (*run)(const br_block_ctr_class *const *ctx,
430 const void *iv, uint32_t cc, void *data, size_t len);
431 };
432
433 /*
434 * Traditional, table-based AES implementation. It is fast, but uses
435 * internal tables (in particular a 1 kB table for encryption, another
436 * 1 kB table for decryption, and a 256-byte table for key schedule),
437 * and it is not constant-time. In contexts where cache-timing attacks
438 * apply, this implementation may leak the secret key.
439 */
440
441 /** \brief AES block size (16 bytes). */
442 #define br_aes_big_BLOCK_SIZE 16
443
444 /**
445 * \brief Context for AES subkeys (`aes_big` implementation, CBC encryption).
446 *
447 * First field is a pointer to the vtable; it is set by the initialisation
448 * function. Other fields are not supposed to be accessed by user code.
449 */
450 typedef struct {
451 /** \brief Pointer to vtable for this context. */
452 const br_block_cbcenc_class *vtable;
453 #ifndef BR_DOXYGEN_IGNORE
454 uint32_t skey[60];
455 unsigned num_rounds;
456 #endif
457 } br_aes_big_cbcenc_keys;
458
459 /**
460 * \brief Context for AES subkeys (`aes_big` implementation, CBC decryption).
461 *
462 * First field is a pointer to the vtable; it is set by the initialisation
463 * function. Other fields are not supposed to be accessed by user code.
464 */
465 typedef struct {
466 /** \brief Pointer to vtable for this context. */
467 const br_block_cbcdec_class *vtable;
468 #ifndef BR_DOXYGEN_IGNORE
469 uint32_t skey[60];
470 unsigned num_rounds;
471 #endif
472 } br_aes_big_cbcdec_keys;
473
474 /**
475 * \brief Context for AES subkeys (`aes_big` implementation, CTR encryption
476 * and decryption).
477 *
478 * First field is a pointer to the vtable; it is set by the initialisation
479 * function. Other fields are not supposed to be accessed by user code.
480 */
481 typedef struct {
482 /** \brief Pointer to vtable for this context. */
483 const br_block_ctr_class *vtable;
484 #ifndef BR_DOXYGEN_IGNORE
485 uint32_t skey[60];
486 unsigned num_rounds;
487 #endif
488 } br_aes_big_ctr_keys;
489
490 /**
491 * \brief Class instance for AES CBC encryption (`aes_big` implementation).
492 */
493 extern const br_block_cbcenc_class br_aes_big_cbcenc_vtable;
494
495 /**
496 * \brief Class instance for AES CBC decryption (`aes_big` implementation).
497 */
498 extern const br_block_cbcdec_class br_aes_big_cbcdec_vtable;
499
500 /**
501 * \brief Class instance for AES CTR encryption and decryption
502 * (`aes_big` implementation).
503 */
504 extern const br_block_ctr_class br_aes_big_ctr_vtable;
505
506 /**
507 * \brief Context initialisation (key schedule) for AES CBC encryption
508 * (`aes_big` implementation).
509 *
510 * \param ctx context to initialise.
511 * \param key secret key.
512 * \param len secret key length (in bytes).
513 */
514 void br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx,
515 const void *key, size_t len);
516
517 /**
518 * \brief Context initialisation (key schedule) for AES CBC decryption
519 * (`aes_big` implementation).
520 *
521 * \param ctx context to initialise.
522 * \param key secret key.
523 * \param len secret key length (in bytes).
524 */
525 void br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx,
526 const void *key, size_t len);
527
528 /**
529 * \brief Context initialisation (key schedule) for AES CTR encryption
530 * and decryption (`aes_big` implementation).
531 *
532 * \param ctx context to initialise.
533 * \param key secret key.
534 * \param len secret key length (in bytes).
535 */
536 void br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx,
537 const void *key, size_t len);
538
539 /**
540 * \brief CBC encryption with AES (`aes_big` implementation).
541 *
542 * \param ctx context (already initialised).
543 * \param iv IV (updated).
544 * \param data data to encrypt (updated).
545 * \param len data length (in bytes, MUST be multiple of 16).
546 */
547 void br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx, void *iv,
548 void *data, size_t len);
549
550 /**
551 * \brief CBC decryption with AES (`aes_big` implementation).
552 *
553 * \param ctx context (already initialised).
554 * \param iv IV (updated).
555 * \param data data to decrypt (updated).
556 * \param len data length (in bytes, MUST be multiple of 16).
557 */
558 void br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx, void *iv,
559 void *data, size_t len);
560
561 /**
562 * \brief CTR encryption and decryption with AES (`aes_big` implementation).
563 *
564 * \param ctx context (already initialised).
565 * \param iv IV (constant, 12 bytes).
566 * \param cc initial block counter value.
567 * \param data data to decrypt (updated).
568 * \param len data length (in bytes).
569 * \return new block counter value.
570 */
571 uint32_t br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx,
572 const void *iv, uint32_t cc, void *data, size_t len);
573
574 /*
575 * AES implementation optimized for size. It is slower than the
576 * traditional table-based AES implementation, but requires much less
577 * code. It still uses data-dependent table accesses (albeit within a
578 * much smaller 256-byte table), which makes it conceptually vulnerable
579 * to cache-timing attacks.
580 */
581
582 /** \brief AES block size (16 bytes). */
583 #define br_aes_small_BLOCK_SIZE 16
584
585 /**
586 * \brief Context for AES subkeys (`aes_small` implementation, CBC encryption).
587 *
588 * First field is a pointer to the vtable; it is set by the initialisation
589 * function. Other fields are not supposed to be accessed by user code.
590 */
591 typedef struct {
592 /** \brief Pointer to vtable for this context. */
593 const br_block_cbcenc_class *vtable;
594 #ifndef BR_DOXYGEN_IGNORE
595 uint32_t skey[60];
596 unsigned num_rounds;
597 #endif
598 } br_aes_small_cbcenc_keys;
599
600 /**
601 * \brief Context for AES subkeys (`aes_small` implementation, CBC decryption).
602 *
603 * First field is a pointer to the vtable; it is set by the initialisation
604 * function. Other fields are not supposed to be accessed by user code.
605 */
606 typedef struct {
607 /** \brief Pointer to vtable for this context. */
608 const br_block_cbcdec_class *vtable;
609 #ifndef BR_DOXYGEN_IGNORE
610 uint32_t skey[60];
611 unsigned num_rounds;
612 #endif
613 } br_aes_small_cbcdec_keys;
614
615 /**
616 * \brief Context for AES subkeys (`aes_small` implementation, CTR encryption
617 * and decryption).
618 *
619 * First field is a pointer to the vtable; it is set by the initialisation
620 * function. Other fields are not supposed to be accessed by user code.
621 */
622 typedef struct {
623 /** \brief Pointer to vtable for this context. */
624 const br_block_ctr_class *vtable;
625 #ifndef BR_DOXYGEN_IGNORE
626 uint32_t skey[60];
627 unsigned num_rounds;
628 #endif
629 } br_aes_small_ctr_keys;
630
631 /**
632 * \brief Class instance for AES CBC encryption (`aes_small` implementation).
633 */
634 extern const br_block_cbcenc_class br_aes_small_cbcenc_vtable;
635
636 /**
637 * \brief Class instance for AES CBC decryption (`aes_small` implementation).
638 */
639 extern const br_block_cbcdec_class br_aes_small_cbcdec_vtable;
640
641 /**
642 * \brief Class instance for AES CTR encryption and decryption
643 * (`aes_small` implementation).
644 */
645 extern const br_block_ctr_class br_aes_small_ctr_vtable;
646
647 /**
648 * \brief Context initialisation (key schedule) for AES CBC encryption
649 * (`aes_small` implementation).
650 *
651 * \param ctx context to initialise.
652 * \param key secret key.
653 * \param len secret key length (in bytes).
654 */
655 void br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
656 const void *key, size_t len);
657
658 /**
659 * \brief Context initialisation (key schedule) for AES CBC decryption
660 * (`aes_small` implementation).
661 *
662 * \param ctx context to initialise.
663 * \param key secret key.
664 * \param len secret key length (in bytes).
665 */
666 void br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
667 const void *key, size_t len);
668
669 /**
670 * \brief Context initialisation (key schedule) for AES CTR encryption
671 * and decryption (`aes_small` implementation).
672 *
673 * \param ctx context to initialise.
674 * \param key secret key.
675 * \param len secret key length (in bytes).
676 */
677 void br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
678 const void *key, size_t len);
679
680 /**
681 * \brief CBC encryption with AES (`aes_small` implementation).
682 *
683 * \param ctx context (already initialised).
684 * \param iv IV (updated).
685 * \param data data to encrypt (updated).
686 * \param len data length (in bytes, MUST be multiple of 16).
687 */
688 void br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx, void *iv,
689 void *data, size_t len);
690
691 /**
692 * \brief CBC decryption with AES (`aes_small` implementation).
693 *
694 * \param ctx context (already initialised).
695 * \param iv IV (updated).
696 * \param data data to decrypt (updated).
697 * \param len data length (in bytes, MUST be multiple of 16).
698 */
699 void br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx, void *iv,
700 void *data, size_t len);
701
702 /**
703 * \brief CTR encryption and decryption with AES (`aes_small` implementation).
704 *
705 * \param ctx context (already initialised).
706 * \param iv IV (constant, 12 bytes).
707 * \param cc initial block counter value.
708 * \param data data to decrypt (updated).
709 * \param len data length (in bytes).
710 * \return new block counter value.
711 */
712 uint32_t br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
713 const void *iv, uint32_t cc, void *data, size_t len);
714
715 /*
716 * Constant-time AES implementation. Its size is similar to that of
717 * 'aes_big', and its performance is similar to that of 'aes_small' (faster
718 * decryption, slower encryption). However, it is constant-time, i.e.
719 * immune to cache-timing and similar attacks.
720 */
721
722 /** \brief AES block size (16 bytes). */
723 #define br_aes_ct_BLOCK_SIZE 16
724
725 /**
726 * \brief Context for AES subkeys (`aes_ct` implementation, CBC encryption).
727 *
728 * First field is a pointer to the vtable; it is set by the initialisation
729 * function. Other fields are not supposed to be accessed by user code.
730 */
731 typedef struct {
732 /** \brief Pointer to vtable for this context. */
733 const br_block_cbcenc_class *vtable;
734 #ifndef BR_DOXYGEN_IGNORE
735 uint32_t skey[60];
736 unsigned num_rounds;
737 #endif
738 } br_aes_ct_cbcenc_keys;
739
740 /**
741 * \brief Context for AES subkeys (`aes_ct` implementation, CBC decryption).
742 *
743 * First field is a pointer to the vtable; it is set by the initialisation
744 * function. Other fields are not supposed to be accessed by user code.
745 */
746 typedef struct {
747 /** \brief Pointer to vtable for this context. */
748 const br_block_cbcdec_class *vtable;
749 #ifndef BR_DOXYGEN_IGNORE
750 uint32_t skey[60];
751 unsigned num_rounds;
752 #endif
753 } br_aes_ct_cbcdec_keys;
754
755 /**
756 * \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption
757 * and decryption).
758 *
759 * First field is a pointer to the vtable; it is set by the initialisation
760 * function. Other fields are not supposed to be accessed by user code.
761 */
762 typedef struct {
763 /** \brief Pointer to vtable for this context. */
764 const br_block_ctr_class *vtable;
765 #ifndef BR_DOXYGEN_IGNORE
766 uint32_t skey[60];
767 unsigned num_rounds;
768 #endif
769 } br_aes_ct_ctr_keys;
770
771 /**
772 * \brief Class instance for AES CBC encryption (`aes_ct` implementation).
773 */
774 extern const br_block_cbcenc_class br_aes_ct_cbcenc_vtable;
775
776 /**
777 * \brief Class instance for AES CBC decryption (`aes_ct` implementation).
778 */
779 extern const br_block_cbcdec_class br_aes_ct_cbcdec_vtable;
780
781 /**
782 * \brief Class instance for AES CTR encryption and decryption
783 * (`aes_ct` implementation).
784 */
785 extern const br_block_ctr_class br_aes_ct_ctr_vtable;
786
787 /**
788 * \brief Context initialisation (key schedule) for AES CBC encryption
789 * (`aes_ct` implementation).
790 *
791 * \param ctx context to initialise.
792 * \param key secret key.
793 * \param len secret key length (in bytes).
794 */
795 void br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
796 const void *key, size_t len);
797
798 /**
799 * \brief Context initialisation (key schedule) for AES CBC decryption
800 * (`aes_ct` implementation).
801 *
802 * \param ctx context to initialise.
803 * \param key secret key.
804 * \param len secret key length (in bytes).
805 */
806 void br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
807 const void *key, size_t len);
808
809 /**
810 * \brief Context initialisation (key schedule) for AES CTR encryption
811 * and decryption (`aes_ct` implementation).
812 *
813 * \param ctx context to initialise.
814 * \param key secret key.
815 * \param len secret key length (in bytes).
816 */
817 void br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
818 const void *key, size_t len);
819
820 /**
821 * \brief CBC encryption with AES (`aes_ct` implementation).
822 *
823 * \param ctx context (already initialised).
824 * \param iv IV (updated).
825 * \param data data to encrypt (updated).
826 * \param len data length (in bytes, MUST be multiple of 16).
827 */
828 void br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx, void *iv,
829 void *data, size_t len);
830
831 /**
832 * \brief CBC decryption with AES (`aes_ct` implementation).
833 *
834 * \param ctx context (already initialised).
835 * \param iv IV (updated).
836 * \param data data to decrypt (updated).
837 * \param len data length (in bytes, MUST be multiple of 16).
838 */
839 void br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx, void *iv,
840 void *data, size_t len);
841
842 /**
843 * \brief CTR encryption and decryption with AES (`aes_ct` implementation).
844 *
845 * \param ctx context (already initialised).
846 * \param iv IV (constant, 12 bytes).
847 * \param cc initial block counter value.
848 * \param data data to decrypt (updated).
849 * \param len data length (in bytes).
850 * \return new block counter value.
851 */
852 uint32_t br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
853 const void *iv, uint32_t cc, void *data, size_t len);
854
855 /*
856 * 64-bit constant-time AES implementation. It is similar to 'aes_ct'
857 * but uses 64-bit registers, making it about twice faster than 'aes_ct'
858 * on 64-bit platforms, while remaining constant-time and with a similar
859 * code size. (The doubling in performance is only for CBC decryption
860 * and CTR mode; CBC encryption is non-parallel and cannot benefit from
861 * the larger registers.)
862 */
863
864 /** \brief AES block size (16 bytes). */
865 #define br_aes_ct64_BLOCK_SIZE 16
866
867 /**
868 * \brief Context for AES subkeys (`aes_ct64` implementation, CBC encryption).
869 *
870 * First field is a pointer to the vtable; it is set by the initialisation
871 * function. Other fields are not supposed to be accessed by user code.
872 */
873 typedef struct {
874 /** \brief Pointer to vtable for this context. */
875 const br_block_cbcenc_class *vtable;
876 #ifndef BR_DOXYGEN_IGNORE
877 uint64_t skey[30];
878 unsigned num_rounds;
879 #endif
880 } br_aes_ct64_cbcenc_keys;
881
882 /**
883 * \brief Context for AES subkeys (`aes_ct64` implementation, CBC decryption).
884 *
885 * First field is a pointer to the vtable; it is set by the initialisation
886 * function. Other fields are not supposed to be accessed by user code.
887 */
888 typedef struct {
889 /** \brief Pointer to vtable for this context. */
890 const br_block_cbcdec_class *vtable;
891 #ifndef BR_DOXYGEN_IGNORE
892 uint64_t skey[30];
893 unsigned num_rounds;
894 #endif
895 } br_aes_ct64_cbcdec_keys;
896
897 /**
898 * \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption
899 * and decryption).
900 *
901 * First field is a pointer to the vtable; it is set by the initialisation
902 * function. Other fields are not supposed to be accessed by user code.
903 */
904 typedef struct {
905 /** \brief Pointer to vtable for this context. */
906 const br_block_ctr_class *vtable;
907 #ifndef BR_DOXYGEN_IGNORE
908 uint64_t skey[30];
909 unsigned num_rounds;
910 #endif
911 } br_aes_ct64_ctr_keys;
912
913 /**
914 * \brief Class instance for AES CBC encryption (`aes_ct64` implementation).
915 */
916 extern const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable;
917
918 /**
919 * \brief Class instance for AES CBC decryption (`aes_ct64` implementation).
920 */
921 extern const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable;
922
923 /**
924 * \brief Class instance for AES CTR encryption and decryption
925 * (`aes_ct64` implementation).
926 */
927 extern const br_block_ctr_class br_aes_ct64_ctr_vtable;
928
929 /**
930 * \brief Context initialisation (key schedule) for AES CBC encryption
931 * (`aes_ct64` implementation).
932 *
933 * \param ctx context to initialise.
934 * \param key secret key.
935 * \param len secret key length (in bytes).
936 */
937 void br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
938 const void *key, size_t len);
939
940 /**
941 * \brief Context initialisation (key schedule) for AES CBC decryption
942 * (`aes_ct64` implementation).
943 *
944 * \param ctx context to initialise.
945 * \param key secret key.
946 * \param len secret key length (in bytes).
947 */
948 void br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
949 const void *key, size_t len);
950
951 /**
952 * \brief Context initialisation (key schedule) for AES CTR encryption
953 * and decryption (`aes_ct64` implementation).
954 *
955 * \param ctx context to initialise.
956 * \param key secret key.
957 * \param len secret key length (in bytes).
958 */
959 void br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
960 const void *key, size_t len);
961
962 /**
963 * \brief CBC encryption with AES (`aes_ct64` implementation).
964 *
965 * \param ctx context (already initialised).
966 * \param iv IV (updated).
967 * \param data data to encrypt (updated).
968 * \param len data length (in bytes, MUST be multiple of 16).
969 */
970 void br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx, void *iv,
971 void *data, size_t len);
972
973 /**
974 * \brief CBC decryption with AES (`aes_ct64` implementation).
975 *
976 * \param ctx context (already initialised).
977 * \param iv IV (updated).
978 * \param data data to decrypt (updated).
979 * \param len data length (in bytes, MUST be multiple of 16).
980 */
981 void br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx, void *iv,
982 void *data, size_t len);
983
984 /**
985 * \brief CTR encryption and decryption with AES (`aes_ct64` implementation).
986 *
987 * \param ctx context (already initialised).
988 * \param iv IV (constant, 12 bytes).
989 * \param cc initial block counter value.
990 * \param data data to decrypt (updated).
991 * \param len data length (in bytes).
992 * \return new block counter value.
993 */
994 uint32_t br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
995 const void *iv, uint32_t cc, void *data, size_t len);
996
997 /**
998 * \brief Aggregate structure large enough to be used as context for
999 * subkeys (CBC encryption) for all AES implementations.
1000 */
1001 typedef union {
1002 const br_block_cbcenc_class *vtable;
1003 br_aes_big_cbcenc_keys big;
1004 br_aes_small_cbcenc_keys small;
1005 br_aes_ct_cbcenc_keys ct;
1006 br_aes_ct64_cbcenc_keys ct64;
1007 } br_aes_gen_cbcenc_keys;
1008
1009 /**
1010 * \brief Aggregate structure large enough to be used as context for
1011 * subkeys (CBC decryption) for all AES implementations.
1012 */
1013 typedef union {
1014 const br_block_cbcdec_class *vtable;
1015 br_aes_big_cbcdec_keys big;
1016 br_aes_small_cbcdec_keys small;
1017 br_aes_ct_cbcdec_keys ct;
1018 br_aes_ct64_cbcdec_keys ct64;
1019 } br_aes_gen_cbcdec_keys;
1020
1021 /**
1022 * \brief Aggregate structure large enough to be used as context for
1023 * subkeys (CTR encryption and decryption) for all AES implementations.
1024 */
1025 typedef union {
1026 const br_block_ctr_class *vtable;
1027 br_aes_big_ctr_keys big;
1028 br_aes_small_ctr_keys small;
1029 br_aes_ct_ctr_keys ct;
1030 br_aes_ct64_ctr_keys ct64;
1031 } br_aes_gen_ctr_keys;
1032
1033 /*
1034 * Traditional, table-based implementation for DES/3DES. Since tables are
1035 * used, cache-timing attacks are conceptually possible.
1036 */
1037
1038 /** \brief DES/3DES block size (8 bytes). */
1039 #define br_des_tab_BLOCK_SIZE 8
1040
1041 /**
1042 * \brief Context for DES subkeys (`des_tab` implementation, CBC encryption).
1043 *
1044 * First field is a pointer to the vtable; it is set by the initialisation
1045 * function. Other fields are not supposed to be accessed by user code.
1046 */
1047 typedef struct {
1048 /** \brief Pointer to vtable for this context. */
1049 const br_block_cbcenc_class *vtable;
1050 #ifndef BR_DOXYGEN_IGNORE
1051 uint32_t skey[96];
1052 unsigned num_rounds;
1053 #endif
1054 } br_des_tab_cbcenc_keys;
1055
1056 /**
1057 * \brief Context for DES subkeys (`des_tab` implementation, CBC decryption).
1058 *
1059 * First field is a pointer to the vtable; it is set by the initialisation
1060 * function. Other fields are not supposed to be accessed by user code.
1061 */
1062 typedef struct {
1063 /** \brief Pointer to vtable for this context. */
1064 const br_block_cbcdec_class *vtable;
1065 #ifndef BR_DOXYGEN_IGNORE
1066 uint32_t skey[96];
1067 unsigned num_rounds;
1068 #endif
1069 } br_des_tab_cbcdec_keys;
1070
1071 /**
1072 * \brief Class instance for DES CBC encryption (`des_tab` implementation).
1073 */
1074 extern const br_block_cbcenc_class br_des_tab_cbcenc_vtable;
1075
1076 /**
1077 * \brief Class instance for DES CBC decryption (`des_tab` implementation).
1078 */
1079 extern const br_block_cbcdec_class br_des_tab_cbcdec_vtable;
1080
1081 /**
1082 * \brief Context initialisation (key schedule) for DES CBC encryption
1083 * (`des_tab` implementation).
1084 *
1085 * \param ctx context to initialise.
1086 * \param key secret key.
1087 * \param len secret key length (in bytes).
1088 */
1089 void br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx,
1090 const void *key, size_t len);
1091
1092 /**
1093 * \brief Context initialisation (key schedule) for DES CBC decryption
1094 * (`des_tab` implementation).
1095 *
1096 * \param ctx context to initialise.
1097 * \param key secret key.
1098 * \param len secret key length (in bytes).
1099 */
1100 void br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx,
1101 const void *key, size_t len);
1102
1103 /**
1104 * \brief CBC encryption with DES (`des_tab` implementation).
1105 *
1106 * \param ctx context (already initialised).
1107 * \param iv IV (updated).
1108 * \param data data to encrypt (updated).
1109 * \param len data length (in bytes, MUST be multiple of 8).
1110 */
1111 void br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx, void *iv,
1112 void *data, size_t len);
1113
1114 /**
1115 * \brief CBC decryption with DES (`des_tab` implementation).
1116 *
1117 * \param ctx context (already initialised).
1118 * \param iv IV (updated).
1119 * \param data data to decrypt (updated).
1120 * \param len data length (in bytes, MUST be multiple of 8).
1121 */
1122 void br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx, void *iv,
1123 void *data, size_t len);
1124
1125 /*
1126 * Constant-time implementation for DES/3DES. It is substantially slower
1127 * (by a factor of about 4x), but also immune to cache-timing attacks.
1128 */
1129
1130 /** \brief DES/3DES block size (8 bytes). */
1131 #define br_des_ct_BLOCK_SIZE 8
1132
1133 /**
1134 * \brief Context for DES subkeys (`des_ct` implementation, CBC encryption).
1135 *
1136 * First field is a pointer to the vtable; it is set by the initialisation
1137 * function. Other fields are not supposed to be accessed by user code.
1138 */
1139 typedef struct {
1140 /** \brief Pointer to vtable for this context. */
1141 const br_block_cbcenc_class *vtable;
1142 #ifndef BR_DOXYGEN_IGNORE
1143 uint32_t skey[96];
1144 unsigned num_rounds;
1145 #endif
1146 } br_des_ct_cbcenc_keys;
1147
1148 /**
1149 * \brief Context for DES subkeys (`des_ct` implementation, CBC decryption).
1150 *
1151 * First field is a pointer to the vtable; it is set by the initialisation
1152 * function. Other fields are not supposed to be accessed by user code.
1153 */
1154 typedef struct {
1155 /** \brief Pointer to vtable for this context. */
1156 const br_block_cbcdec_class *vtable;
1157 #ifndef BR_DOXYGEN_IGNORE
1158 uint32_t skey[96];
1159 unsigned num_rounds;
1160 #endif
1161 } br_des_ct_cbcdec_keys;
1162
1163 /**
1164 * \brief Class instance for DES CBC encryption (`des_ct` implementation).
1165 */
1166 extern const br_block_cbcenc_class br_des_ct_cbcenc_vtable;
1167
1168 /**
1169 * \brief Class instance for DES CBC decryption (`des_ct` implementation).
1170 */
1171 extern const br_block_cbcdec_class br_des_ct_cbcdec_vtable;
1172
1173 /**
1174 * \brief Context initialisation (key schedule) for DES CBC encryption
1175 * (`des_ct` implementation).
1176 *
1177 * \param ctx context to initialise.
1178 * \param key secret key.
1179 * \param len secret key length (in bytes).
1180 */
1181 void br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx,
1182 const void *key, size_t len);
1183
1184 /**
1185 * \brief Context initialisation (key schedule) for DES CBC decryption
1186 * (`des_ct` implementation).
1187 *
1188 * \param ctx context to initialise.
1189 * \param key secret key.
1190 * \param len secret key length (in bytes).
1191 */
1192 void br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx,
1193 const void *key, size_t len);
1194
1195 /**
1196 * \brief CBC encryption with DES (`des_ct` implementation).
1197 *
1198 * \param ctx context (already initialised).
1199 * \param iv IV (updated).
1200 * \param data data to encrypt (updated).
1201 * \param len data length (in bytes, MUST be multiple of 8).
1202 */
1203 void br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx, void *iv,
1204 void *data, size_t len);
1205
1206 /**
1207 * \brief CBC decryption with DES (`des_ct` implementation).
1208 *
1209 * \param ctx context (already initialised).
1210 * \param iv IV (updated).
1211 * \param data data to decrypt (updated).
1212 * \param len data length (in bytes, MUST be multiple of 8).
1213 */
1214 void br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx, void *iv,
1215 void *data, size_t len);
1216
1217 /*
1218 * These structures are large enough to accommodate subkeys for all
1219 * DES/3DES implementations.
1220 */
1221
1222 /**
1223 * \brief Aggregate structure large enough to be used as context for
1224 * subkeys (CBC encryption) for all DES implementations.
1225 */
1226 typedef union {
1227 const br_block_cbcenc_class *vtable;
1228 br_des_tab_cbcenc_keys tab;
1229 br_des_ct_cbcenc_keys ct;
1230 } br_des_gen_cbcenc_keys;
1231
1232 /**
1233 * \brief Aggregate structure large enough to be used as context for
1234 * subkeys (CBC decryption) for all DES implementations.
1235 */
1236 typedef union {
1237 const br_block_cbcdec_class *vtable;
1238 br_des_tab_cbcdec_keys tab;
1239 br_des_ct_cbcdec_keys ct;
1240 } br_des_gen_cbcdec_keys;
1241
1242 /**
1243 * \brief Type for a ChaCha20 implementation.
1244 *
1245 * An implementation follows the description in RFC 7539:
1246 *
1247 * - Key is 256 bits (`key` points to exactly 32 bytes).
1248 *
1249 * - IV is 96 bits (`iv` points to exactly 12 bytes).
1250 *
1251 * - Block counter is oveer 32 bits and starts at value `cc`; the
1252 * resulting value is returned.
1253 *
1254 * Data (pointed to by `data`, of length `len`) is encrypted/decrypted
1255 * in place. If `len` is not a multiple of 64, then the excess bytes from
1256 * the last block processing are dropped (therefore, "chunked" processing
1257 * works only as long as each non-final chunk has a length multiple of 64).
1258 *
1259 * \param key secret key (32 bytes).
1260 * \param iv IV (12 bytes).
1261 * \param cc initial counter value.
1262 * \param data data to encrypt or decrypt.
1263 * \param len data length (in bytes).
1264 */
1265 typedef uint32_t (*br_chacha20_run)(const void *key,
1266 const void *iv, uint32_t cc, void *data, size_t len);
1267
1268 /**
1269 * \brief ChaCha20 implementation (straightforward C code, constant-time).
1270 *
1271 * \see br_chacha20_run
1272 *
1273 * \param key secret key (32 bytes).
1274 * \param iv IV (12 bytes).
1275 * \param cc initial counter value.
1276 * \param data data to encrypt or decrypt.
1277 * \param len data length (in bytes).
1278 */
1279 uint32_t br_chacha20_ct_run(const void *key,
1280 const void *iv, uint32_t cc, void *data, size_t len);
1281
1282 /**
1283 * \brief Type for a ChaCha20+Poly1305 AEAD implementation.
1284 *
1285 * The provided data is encrypted or decrypted with ChaCha20. The
1286 * authentication tag is computed on the concatenation of the
1287 * additional data and the ciphertext, with the padding and lengths
1288 * as described in RFC 7539 (section 2.8).
1289 *
1290 * After decryption, the caller is responsible for checking that the
1291 * computed tag matches the expected value.
1292 *
1293 * \param key secret key (32 bytes).
1294 * \param iv nonce (12 bytes).
1295 * \param data data to encrypt or decrypt.
1296 * \param len data length (in bytes).
1297 * \param aad additional authenticated data.
1298 * \param aad_len length of additional authenticated data (in bytes).
1299 * \param tag output buffer for the authentication tag.
1300 * \param ichacha implementation of ChaCha20.
1301 * \param encrypt non-zero for encryption, zero for decryption.
1302 */
1303 typedef void (*br_poly1305_run)(const void *key, const void *iv,
1304 void *data, size_t len, const void *aad, size_t aad_len,
1305 void *tag, br_chacha20_run ichacha, int encrypt);
1306
1307 /**
1308 * \brief ChaCha20+Poly1305 AEAD implementation (mixed 32-bit multiplications).
1309 *
1310 * \see br_poly1305_run
1311 *
1312 * \param key secret key (32 bytes).
1313 * \param iv nonce (12 bytes).
1314 * \param data data to encrypt or decrypt.
1315 * \param len data length (in bytes).
1316 * \param aad additional authenticated data.
1317 * \param aad_len length of additional authenticated data (in bytes).
1318 * \param tag output buffer for the authentication tag.
1319 * \param ichacha implementation of ChaCha20.
1320 * \param encrypt non-zero for encryption, zero for decryption.
1321 */
1322 void br_poly1305_ctmul_run(const void *key, const void *iv,
1323 void *data, size_t len, const void *aad, size_t aad_len,
1324 void *tag, br_chacha20_run ichacha, int encrypt);
1325
1326 /**
1327 * \brief ChaCha20+Poly1305 AEAD implementation (pure 32-bit multiplications).
1328 *
1329 * \see br_poly1305_run
1330 *
1331 * \param key secret key (32 bytes).
1332 * \param iv nonce (12 bytes).
1333 * \param data data to encrypt or decrypt.
1334 * \param len data length (in bytes).
1335 * \param aad additional authenticated data.
1336 * \param aad_len length of additional authenticated data (in bytes).
1337 * \param tag output buffer for the authentication tag.
1338 * \param ichacha implementation of ChaCha20.
1339 * \param encrypt non-zero for encryption, zero for decryption.
1340 */
1341 void br_poly1305_ctmul32_run(const void *key, const void *iv,
1342 void *data, size_t len, const void *aad, size_t aad_len,
1343 void *tag, br_chacha20_run ichacha, int encrypt);
1344
1345 /**
1346 * \brief ChaCha20+Poly1305 AEAD implementation (i15).
1347 *
1348 * This implementation relies on the generic big integer code "i15"
1349 * (which uses pure 32-bit multiplications). As such, it may save a
1350 * little code footprint in a context where "i15" is already included
1351 * (e.g. for elliptic curves or for RSA); however, it is also
1352 * substantially slower than the ctmul and ctmul32 implementations.
1353 *
1354 * \see br_poly1305_run
1355 *
1356 * \param key secret key (32 bytes).
1357 * \param iv nonce (12 bytes).
1358 * \param data data to encrypt or decrypt.
1359 * \param len data length (in bytes).
1360 * \param aad additional authenticated data.
1361 * \param aad_len length of additional authenticated data (in bytes).
1362 * \param tag output buffer for the authentication tag.
1363 * \param ichacha implementation of ChaCha20.
1364 * \param encrypt non-zero for encryption, zero for decryption.
1365 */
1366 void br_poly1305_i15_run(const void *key, const void *iv,
1367 void *data, size_t len, const void *aad, size_t aad_len,
1368 void *tag, br_chacha20_run ichacha, int encrypt);
1369
1370 #endif