Path: blob/main/contrib/bearssl/inc/bearssl_block.h
39586 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#ifndef BR_BEARSSL_BLOCK_H__25#define BR_BEARSSL_BLOCK_H__2627#include <stddef.h>28#include <stdint.h>2930#ifdef __cplusplus31extern "C" {32#endif3334/** \file bearssl_block.h35*36* # Block Ciphers and Symmetric Ciphers37*38* This file documents the API for block ciphers and other symmetric39* ciphers.40*41*42* ## Procedural API43*44* For a block cipher implementation, up to three separate sets of45* functions are provided, for CBC encryption, CBC decryption, and CTR46* encryption/decryption. Each set has its own context structure,47* initialised with the encryption key.48*49* For CBC encryption and decryption, the data to encrypt or decrypt is50* referenced as a sequence of blocks. The implementations assume that51* there is no partial block; no padding is applied or removed. The52* caller is responsible for handling any kind of padding.53*54* Function for CTR encryption are defined only for block ciphers with55* blocks of 16 bytes or more (i.e. AES, but not DES/3DES).56*57* Each implemented block cipher is identified by an "internal name"58* from which are derived the names of structures and functions that59* implement the cipher. For the block cipher of internal name "`xxx`",60* the following are defined:61*62* - `br_xxx_BLOCK_SIZE`63*64* A macro that evaluates to the block size (in bytes) of the65* cipher. For all implemented block ciphers, this value is a66* power of two.67*68* - `br_xxx_cbcenc_keys`69*70* Context structure that contains the subkeys resulting from the key71* expansion. These subkeys are appropriate for CBC encryption. The72* structure first field is called `vtable` and points to the73* appropriate OOP structure.74*75* - `br_xxx_cbcenc_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`76*77* Perform key expansion: subkeys for CBC encryption are computed and78* written in the provided context structure. The key length MUST be79* adequate for the implemented block cipher. This function also sets80* the `vtable` field.81*82* - `br_xxx_cbcenc_run(const br_xxx_cbcenc_keys *ctx, void *iv, void *data, size_t len)`83*84* Perform CBC encryption of `len` bytes, in place. The encrypted data85* replaces the cleartext. `len` MUST be a multiple of the block length86* (if it is not, the function may loop forever or overflow a buffer).87* The IV is provided with the `iv` pointer; it is also updated with88* a copy of the last encrypted block.89*90* - `br_xxx_cbcdec_keys`91*92* Context structure that contains the subkeys resulting from the key93* expansion. These subkeys are appropriate for CBC decryption. The94* structure first field is called `vtable` and points to the95* appropriate OOP structure.96*97* - `br_xxx_cbcdec_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)`98*99* Perform key expansion: subkeys for CBC decryption are computed and100* written in the provided context structure. The key length MUST be101* adequate for the implemented block cipher. This function also sets102* the `vtable` field.103*104* - `br_xxx_cbcdec_run(const br_xxx_cbcdec_keys *ctx, void *iv, void *data, size_t num_blocks)`105*106* Perform CBC decryption of `len` bytes, in place. The decrypted data107* replaces the ciphertext. `len` MUST be a multiple of the block length108* (if it is not, the function may loop forever or overflow a buffer).109* The IV is provided with the `iv` pointer; it is also updated with110* a copy of the last _encrypted_ block.111*112* - `br_xxx_ctr_keys`113*114* Context structure that contains the subkeys resulting from the key115* expansion. These subkeys are appropriate for CTR encryption and116* decryption. The structure first field is called `vtable` and117* points to the appropriate OOP structure.118*119* - `br_xxx_ctr_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)`120*121* Perform key expansion: subkeys for CTR encryption and decryption122* are computed and written in the provided context structure. The123* key length MUST be adequate for the implemented block cipher. This124* function also sets the `vtable` field.125*126* - `br_xxx_ctr_run(const br_xxx_ctr_keys *ctx, const void *iv, uint32_t cc, void *data, size_t len)` (returns `uint32_t`)127*128* Perform CTR encryption/decryption of some data. Processing is done129* "in place" (the output data replaces the input data). This function130* implements the "standard incrementing function" from NIST SP800-38A,131* annex B: the IV length shall be 4 bytes less than the block size132* (i.e. 12 bytes for AES) and the counter is the 32-bit value starting133* with `cc`. The data length (`len`) is not necessarily a multiple of134* the block size. The new counter value is returned, which supports135* chunked processing, provided that each chunk length (except possibly136* the last one) is a multiple of the block size.137*138* - `br_xxx_ctrcbc_keys`139*140* Context structure that contains the subkeys resulting from the141* key expansion. These subkeys are appropriate for doing combined142* CTR encryption/decryption and CBC-MAC, as used in the CCM and EAX143* authenticated encryption modes. The structure first field is144* called `vtable` and points to the appropriate OOP structure.145*146* - `br_xxx_ctrcbc_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)`147*148* Perform key expansion: subkeys for combined CTR149* encryption/decryption and CBC-MAC are computed and written in the150* provided context structure. The key length MUST be adequate for151* the implemented block cipher. This function also sets the152* `vtable` field.153*154* - `br_xxx_ctrcbc_encrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)`155*156* Perform CTR encryption of some data, and CBC-MAC. Processing is157* done "in place" (the output data replaces the input data). This158* function applies CTR encryption on the data, using a full159* block-size counter (i.e. for 128-bit blocks, the counter is160* incremented as a 128-bit value). The 'ctr' array contains the161* initial value for the counter (used in the first block) and it is162* updated with the new value after data processing. The 'cbcmac'163* value shall point to a block-sized value which is used as IV for164* CBC-MAC, computed over the encrypted data (output of CTR165* encryption); the resulting CBC-MAC is written over 'cbcmac' on166* output.167*168* The data length MUST be a multiple of the block size.169*170* - `br_xxx_ctrcbc_decrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)`171*172* Perform CTR decryption of some data, and CBC-MAC. Processing is173* done "in place" (the output data replaces the input data). This174* function applies CTR decryption on the data, using a full175* block-size counter (i.e. for 128-bit blocks, the counter is176* incremented as a 128-bit value). The 'ctr' array contains the177* initial value for the counter (used in the first block) and it is178* updated with the new value after data processing. The 'cbcmac'179* value shall point to a block-sized value which is used as IV for180* CBC-MAC, computed over the encrypted data (input of CTR181* encryption); the resulting CBC-MAC is written over 'cbcmac' on182* output.183*184* The data length MUST be a multiple of the block size.185*186* - `br_xxx_ctrcbc_ctr(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *data, size_t len)`187*188* Perform CTR encryption or decryption of the provided data. The189* data is processed "in place" (the output data replaces the input190* data). A full block-sized counter is applied (i.e. for 128-bit191* blocks, the counter is incremented as a 128-bit value). The 'ctr'192* array contains the initial value for the counter (used in the193* first block), and it is updated with the new value after data194* processing.195*196* The data length MUST be a multiple of the block size.197*198* - `br_xxx_ctrcbc_mac(const br_xxx_ctrcbc_keys *ctx, void *cbcmac, const void *data, size_t len)`199*200* Compute CBC-MAC over the provided data. The IV for CBC-MAC is201* provided as 'cbcmac'; the output is written over the same array.202* The data itself is untouched. The data length MUST be a multiple203* of the block size.204*205*206* It shall be noted that the key expansion functions return `void`. If207* the provided key length is not allowed, then there will be no error208* reporting; implementations need not validate the key length, thus an209* invalid key length may result in undefined behaviour (e.g. buffer210* overflow).211*212* Subkey structures contain no interior pointer, and no external213* resources are allocated upon key expansion. They can thus be214* discarded without any explicit deallocation.215*216*217* ## Object-Oriented API218*219* Each context structure begins with a field (called `vtable`) that220* points to an instance of a structure that references the relevant221* functions through pointers. Each such structure contains the222* following:223*224* - `context_size`225*226* The size (in bytes) of the context structure for subkeys.227*228* - `block_size`229*230* The cipher block size (in bytes).231*232* - `log_block_size`233*234* The base-2 logarithm of cipher block size (e.g. 4 for blocks235* of 16 bytes).236*237* - `init`238*239* Pointer to the key expansion function.240*241* - `run`242*243* Pointer to the encryption/decryption function.244*245* For combined CTR/CBC-MAC encryption, the `vtable` has a slightly246* different structure:247*248* - `context_size`249*250* The size (in bytes) of the context structure for subkeys.251*252* - `block_size`253*254* The cipher block size (in bytes).255*256* - `log_block_size`257*258* The base-2 logarithm of cipher block size (e.g. 4 for blocks259* of 16 bytes).260*261* - `init`262*263* Pointer to the key expansion function.264*265* - `encrypt`266*267* Pointer to the CTR encryption + CBC-MAC function.268*269* - `decrypt`270*271* Pointer to the CTR decryption + CBC-MAC function.272*273* - `ctr`274*275* Pointer to the CTR encryption/decryption function.276*277* - `mac`278*279* Pointer to the CBC-MAC function.280*281* For block cipher "`xxx`", static, constant instances of these282* structures are defined, under the names:283*284* - `br_xxx_cbcenc_vtable`285* - `br_xxx_cbcdec_vtable`286* - `br_xxx_ctr_vtable`287* - `br_xxx_ctrcbc_vtable`288*289*290* ## Implemented Block Ciphers291*292* Provided implementations are:293*294* | Name | Function | Block Size (bytes) | Key lengths (bytes) |295* | :-------- | :------- | :----------------: | :-----------------: |296* | aes_big | AES | 16 | 16, 24 and 32 |297* | aes_small | AES | 16 | 16, 24 and 32 |298* | aes_ct | AES | 16 | 16, 24 and 32 |299* | aes_ct64 | AES | 16 | 16, 24 and 32 |300* | aes_x86ni | AES | 16 | 16, 24 and 32 |301* | aes_pwr8 | AES | 16 | 16, 24 and 32 |302* | des_ct | DES/3DES | 8 | 8, 16 and 24 |303* | des_tab | DES/3DES | 8 | 8, 16 and 24 |304*305* **Note:** DES/3DES nominally uses keys of 64, 128 and 192 bits (i.e. 8,306* 16 and 24 bytes), but some of the bits are ignored by the algorithm, so307* the _effective_ key lengths, from a security point of view, are 56,308* 112 and 168 bits, respectively.309*310* `aes_big` is a "classical" AES implementation, using tables. It311* is fast but not constant-time, since it makes data-dependent array312* accesses.313*314* `aes_small` is an AES implementation optimized for code size. It315* is substantially slower than `aes_big`; it is not constant-time316* either.317*318* `aes_ct` is a constant-time implementation of AES; its code is about319* as big as that of `aes_big`, while its performance is comparable to320* that of `aes_small`. However, it is constant-time. This321* implementation should thus be considered to be the "default" AES in322* BearSSL, to be used unless the operational context guarantees that a323* non-constant-time implementation is safe, or an architecture-specific324* constant-time implementation can be used (e.g. using dedicated325* hardware opcodes).326*327* `aes_ct64` is another constant-time implementation of AES. It is328* similar to `aes_ct` but uses 64-bit values. On 32-bit machines,329* `aes_ct64` is not faster than `aes_ct`, often a bit slower, and has330* a larger footprint; however, on 64-bit architectures, `aes_ct64`331* is typically twice faster than `aes_ct` for modes that allow parallel332* operations (i.e. CTR, and CBC decryption, but not CBC encryption).333*334* `aes_x86ni` exists only on x86 architectures (32-bit and 64-bit). It335* uses the AES-NI opcodes when available.336*337* `aes_pwr8` exists only on PowerPC / POWER architectures (32-bit and338* 64-bit, both little-endian and big-endian). It uses the AES opcodes339* present in POWER8 and later.340*341* `des_tab` is a classic, table-based implementation of DES/3DES. It342* is not constant-time.343*344* `des_ct` is an constant-time implementation of DES/3DES. It is345* substantially slower than `des_tab`.346*347* ## ChaCha20 and Poly1305348*349* ChaCha20 is a stream cipher. Poly1305 is a MAC algorithm. They350* are described in [RFC 7539](https://tools.ietf.org/html/rfc7539).351*352* Two function pointer types are defined:353*354* - `br_chacha20_run` describes a function that implements ChaCha20355* only.356*357* - `br_poly1305_run` describes an implementation of Poly1305,358* in the AEAD combination with ChaCha20 specified in RFC 7539359* (the ChaCha20 implementation is provided as a function pointer).360*361* `chacha20_ct` is a straightforward implementation of ChaCha20 in362* plain C; it is constant-time, small, and reasonably fast.363*364* `chacha20_sse2` leverages SSE2 opcodes (on x86 architectures that365* support these opcodes). It is faster than `chacha20_ct`.366*367* `poly1305_ctmul` is an implementation of the ChaCha20+Poly1305 AEAD368* construction, where the Poly1305 part is performed with mixed 32-bit369* multiplications (operands are 32-bit, result is 64-bit).370*371* `poly1305_ctmul32` implements ChaCha20+Poly1305 using pure 32-bit372* multiplications (32-bit operands, 32-bit result). It is slower than373* `poly1305_ctmul`, except on some specific architectures such as374* the ARM Cortex M0+.375*376* `poly1305_ctmulq` implements ChaCha20+Poly1305 with mixed 64-bit377* multiplications (operands are 64-bit, result is 128-bit) on 64-bit378* platforms that support such operations.379*380* `poly1305_i15` implements ChaCha20+Poly1305 with the generic "i15"381* big integer implementation. It is meant mostly for testing purposes,382* although it can help with saving a few hundred bytes of code footprint383* on systems where code size is scarce.384*/385386/**387* \brief Class type for CBC encryption implementations.388*389* A `br_block_cbcenc_class` instance points to the functions implementing390* a specific block cipher, when used in CBC mode for encrypting data.391*/392typedef struct br_block_cbcenc_class_ br_block_cbcenc_class;393struct br_block_cbcenc_class_ {394/**395* \brief Size (in bytes) of the context structure appropriate396* for containing subkeys.397*/398size_t context_size;399400/**401* \brief Size of individual blocks (in bytes).402*/403unsigned block_size;404405/**406* \brief Base-2 logarithm of the size of individual blocks,407* expressed in bytes.408*/409unsigned log_block_size;410411/**412* \brief Initialisation function.413*414* This function sets the `vtable` field in the context structure.415* The key length MUST be one of the key lengths supported by416* the implementation.417*418* \param ctx context structure to initialise.419* \param key secret key.420* \param key_len key length (in bytes).421*/422void (*init)(const br_block_cbcenc_class **ctx,423const void *key, size_t key_len);424425/**426* \brief Run the CBC encryption.427*428* The `iv` parameter points to the IV for this run; it is429* updated with a copy of the last encrypted block. The data430* is encrypted "in place"; its length (`len`) MUST be a431* multiple of the block size.432*433* \param ctx context structure (already initialised).434* \param iv IV for CBC encryption (updated).435* \param data data to encrypt.436* \param len data length (in bytes, multiple of block size).437*/438void (*run)(const br_block_cbcenc_class *const *ctx,439void *iv, void *data, size_t len);440};441442/**443* \brief Class type for CBC decryption implementations.444*445* A `br_block_cbcdec_class` instance points to the functions implementing446* a specific block cipher, when used in CBC mode for decrypting data.447*/448typedef struct br_block_cbcdec_class_ br_block_cbcdec_class;449struct br_block_cbcdec_class_ {450/**451* \brief Size (in bytes) of the context structure appropriate452* for containing subkeys.453*/454size_t context_size;455456/**457* \brief Size of individual blocks (in bytes).458*/459unsigned block_size;460461/**462* \brief Base-2 logarithm of the size of individual blocks,463* expressed in bytes.464*/465unsigned log_block_size;466467/**468* \brief Initialisation function.469*470* This function sets the `vtable` field in the context structure.471* The key length MUST be one of the key lengths supported by472* the implementation.473*474* \param ctx context structure to initialise.475* \param key secret key.476* \param key_len key length (in bytes).477*/478void (*init)(const br_block_cbcdec_class **ctx,479const void *key, size_t key_len);480481/**482* \brief Run the CBC decryption.483*484* The `iv` parameter points to the IV for this run; it is485* updated with a copy of the last encrypted block. The data486* is decrypted "in place"; its length (`len`) MUST be a487* multiple of the block size.488*489* \param ctx context structure (already initialised).490* \param iv IV for CBC decryption (updated).491* \param data data to decrypt.492* \param len data length (in bytes, multiple of block size).493*/494void (*run)(const br_block_cbcdec_class *const *ctx,495void *iv, void *data, size_t len);496};497498/**499* \brief Class type for CTR encryption/decryption implementations.500*501* A `br_block_ctr_class` instance points to the functions implementing502* a specific block cipher, when used in CTR mode for encrypting or503* decrypting data.504*/505typedef struct br_block_ctr_class_ br_block_ctr_class;506struct br_block_ctr_class_ {507/**508* \brief Size (in bytes) of the context structure appropriate509* for containing subkeys.510*/511size_t context_size;512513/**514* \brief Size of individual blocks (in bytes).515*/516unsigned block_size;517518/**519* \brief Base-2 logarithm of the size of individual blocks,520* expressed in bytes.521*/522unsigned log_block_size;523524/**525* \brief Initialisation function.526*527* This function sets the `vtable` field in the context structure.528* The key length MUST be one of the key lengths supported by529* the implementation.530*531* \param ctx context structure to initialise.532* \param key secret key.533* \param key_len key length (in bytes).534*/535void (*init)(const br_block_ctr_class **ctx,536const void *key, size_t key_len);537538/**539* \brief Run the CTR encryption or decryption.540*541* The `iv` parameter points to the IV for this run; its542* length is exactly 4 bytes less than the block size (e.g.543* 12 bytes for AES/CTR). The IV is combined with a 32-bit544* block counter to produce the block value which is processed545* with the block cipher.546*547* The data to encrypt or decrypt is updated "in place". Its548* length (`len` bytes) is not required to be a multiple of549* the block size; if the final block is partial, then the550* corresponding key stream bits are dropped.551*552* The resulting counter value is returned.553*554* \param ctx context structure (already initialised).555* \param iv IV for CTR encryption/decryption.556* \param cc initial value for the block counter.557* \param data data to encrypt or decrypt.558* \param len data length (in bytes).559* \return the new block counter value.560*/561uint32_t (*run)(const br_block_ctr_class *const *ctx,562const void *iv, uint32_t cc, void *data, size_t len);563};564565/**566* \brief Class type for combined CTR and CBC-MAC implementations.567*568* A `br_block_ctrcbc_class` instance points to the functions implementing569* a specific block cipher, when used in CTR mode for encrypting or570* decrypting data, along with CBC-MAC.571*/572typedef struct br_block_ctrcbc_class_ br_block_ctrcbc_class;573struct br_block_ctrcbc_class_ {574/**575* \brief Size (in bytes) of the context structure appropriate576* for containing subkeys.577*/578size_t context_size;579580/**581* \brief Size of individual blocks (in bytes).582*/583unsigned block_size;584585/**586* \brief Base-2 logarithm of the size of individual blocks,587* expressed in bytes.588*/589unsigned log_block_size;590591/**592* \brief Initialisation function.593*594* This function sets the `vtable` field in the context structure.595* The key length MUST be one of the key lengths supported by596* the implementation.597*598* \param ctx context structure to initialise.599* \param key secret key.600* \param key_len key length (in bytes).601*/602void (*init)(const br_block_ctrcbc_class **ctx,603const void *key, size_t key_len);604605/**606* \brief Run the CTR encryption + CBC-MAC.607*608* The `ctr` parameter points to the counter; its length shall609* be equal to the block size. It is updated by this function610* as encryption proceeds.611*612* The `cbcmac` parameter points to the IV for CBC-MAC. The MAC613* is computed over the encrypted data (output of CTR614* encryption). Its length shall be equal to the block size. The615* computed CBC-MAC value is written over the `cbcmac` array.616*617* The data to encrypt is updated "in place". Its length (`len`618* bytes) MUST be a multiple of the block size.619*620* \param ctx context structure (already initialised).621* \param ctr counter for CTR encryption (initial and final).622* \param cbcmac IV and output buffer for CBC-MAC.623* \param data data to encrypt.624* \param len data length (in bytes).625*/626void (*encrypt)(const br_block_ctrcbc_class *const *ctx,627void *ctr, void *cbcmac, void *data, size_t len);628629/**630* \brief Run the CTR decryption + CBC-MAC.631*632* The `ctr` parameter points to the counter; its length shall633* be equal to the block size. It is updated by this function634* as decryption proceeds.635*636* The `cbcmac` parameter points to the IV for CBC-MAC. The MAC637* is computed over the encrypted data (i.e. before CTR638* decryption). Its length shall be equal to the block size. The639* computed CBC-MAC value is written over the `cbcmac` array.640*641* The data to decrypt is updated "in place". Its length (`len`642* bytes) MUST be a multiple of the block size.643*644* \param ctx context structure (already initialised).645* \param ctr counter for CTR encryption (initial and final).646* \param cbcmac IV and output buffer for CBC-MAC.647* \param data data to decrypt.648* \param len data length (in bytes).649*/650void (*decrypt)(const br_block_ctrcbc_class *const *ctx,651void *ctr, void *cbcmac, void *data, size_t len);652653/**654* \brief Run the CTR encryption/decryption only.655*656* The `ctr` parameter points to the counter; its length shall657* be equal to the block size. It is updated by this function658* as decryption proceeds.659*660* The data to decrypt is updated "in place". Its length (`len`661* bytes) MUST be a multiple of the block size.662*663* \param ctx context structure (already initialised).664* \param ctr counter for CTR encryption (initial and final).665* \param data data to decrypt.666* \param len data length (in bytes).667*/668void (*ctr)(const br_block_ctrcbc_class *const *ctx,669void *ctr, void *data, size_t len);670671/**672* \brief Run the CBC-MAC only.673*674* The `cbcmac` parameter points to the IV for CBC-MAC. The MAC675* is computed over the encrypted data (i.e. before CTR676* decryption). Its length shall be equal to the block size. The677* computed CBC-MAC value is written over the `cbcmac` array.678*679* The data is unmodified. Its length (`len` bytes) MUST be a680* multiple of the block size.681*682* \param ctx context structure (already initialised).683* \param cbcmac IV and output buffer for CBC-MAC.684* \param data data to decrypt.685* \param len data length (in bytes).686*/687void (*mac)(const br_block_ctrcbc_class *const *ctx,688void *cbcmac, const void *data, size_t len);689};690691/*692* Traditional, table-based AES implementation. It is fast, but uses693* internal tables (in particular a 1 kB table for encryption, another694* 1 kB table for decryption, and a 256-byte table for key schedule),695* and it is not constant-time. In contexts where cache-timing attacks696* apply, this implementation may leak the secret key.697*/698699/** \brief AES block size (16 bytes). */700#define br_aes_big_BLOCK_SIZE 16701702/**703* \brief Context for AES subkeys (`aes_big` implementation, CBC encryption).704*705* First field is a pointer to the vtable; it is set by the initialisation706* function. Other fields are not supposed to be accessed by user code.707*/708typedef struct {709/** \brief Pointer to vtable for this context. */710const br_block_cbcenc_class *vtable;711#ifndef BR_DOXYGEN_IGNORE712uint32_t skey[60];713unsigned num_rounds;714#endif715} br_aes_big_cbcenc_keys;716717/**718* \brief Context for AES subkeys (`aes_big` implementation, CBC decryption).719*720* First field is a pointer to the vtable; it is set by the initialisation721* function. Other fields are not supposed to be accessed by user code.722*/723typedef struct {724/** \brief Pointer to vtable for this context. */725const br_block_cbcdec_class *vtable;726#ifndef BR_DOXYGEN_IGNORE727uint32_t skey[60];728unsigned num_rounds;729#endif730} br_aes_big_cbcdec_keys;731732/**733* \brief Context for AES subkeys (`aes_big` implementation, CTR encryption734* and decryption).735*736* First field is a pointer to the vtable; it is set by the initialisation737* function. Other fields are not supposed to be accessed by user code.738*/739typedef struct {740/** \brief Pointer to vtable for this context. */741const br_block_ctr_class *vtable;742#ifndef BR_DOXYGEN_IGNORE743uint32_t skey[60];744unsigned num_rounds;745#endif746} br_aes_big_ctr_keys;747748/**749* \brief Context for AES subkeys (`aes_big` implementation, CTR encryption750* and decryption + CBC-MAC).751*752* First field is a pointer to the vtable; it is set by the initialisation753* function. Other fields are not supposed to be accessed by user code.754*/755typedef struct {756/** \brief Pointer to vtable for this context. */757const br_block_ctrcbc_class *vtable;758#ifndef BR_DOXYGEN_IGNORE759uint32_t skey[60];760unsigned num_rounds;761#endif762} br_aes_big_ctrcbc_keys;763764/**765* \brief Class instance for AES CBC encryption (`aes_big` implementation).766*/767extern const br_block_cbcenc_class br_aes_big_cbcenc_vtable;768769/**770* \brief Class instance for AES CBC decryption (`aes_big` implementation).771*/772extern const br_block_cbcdec_class br_aes_big_cbcdec_vtable;773774/**775* \brief Class instance for AES CTR encryption and decryption776* (`aes_big` implementation).777*/778extern const br_block_ctr_class br_aes_big_ctr_vtable;779780/**781* \brief Class instance for AES CTR encryption/decryption + CBC-MAC782* (`aes_big` implementation).783*/784extern const br_block_ctrcbc_class br_aes_big_ctrcbc_vtable;785786/**787* \brief Context initialisation (key schedule) for AES CBC encryption788* (`aes_big` implementation).789*790* \param ctx context to initialise.791* \param key secret key.792* \param len secret key length (in bytes).793*/794void br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx,795const void *key, size_t len);796797/**798* \brief Context initialisation (key schedule) for AES CBC decryption799* (`aes_big` implementation).800*801* \param ctx context to initialise.802* \param key secret key.803* \param len secret key length (in bytes).804*/805void br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx,806const void *key, size_t len);807808/**809* \brief Context initialisation (key schedule) for AES CTR encryption810* and decryption (`aes_big` implementation).811*812* \param ctx context to initialise.813* \param key secret key.814* \param len secret key length (in bytes).815*/816void br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx,817const void *key, size_t len);818819/**820* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC821* (`aes_big` implementation).822*823* \param ctx context to initialise.824* \param key secret key.825* \param len secret key length (in bytes).826*/827void br_aes_big_ctrcbc_init(br_aes_big_ctrcbc_keys *ctx,828const void *key, size_t len);829830/**831* \brief CBC encryption with AES (`aes_big` implementation).832*833* \param ctx context (already initialised).834* \param iv IV (updated).835* \param data data to encrypt (updated).836* \param len data length (in bytes, MUST be multiple of 16).837*/838void br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx, void *iv,839void *data, size_t len);840841/**842* \brief CBC decryption with AES (`aes_big` implementation).843*844* \param ctx context (already initialised).845* \param iv IV (updated).846* \param data data to decrypt (updated).847* \param len data length (in bytes, MUST be multiple of 16).848*/849void br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx, void *iv,850void *data, size_t len);851852/**853* \brief CTR encryption and decryption with AES (`aes_big` implementation).854*855* \param ctx context (already initialised).856* \param iv IV (constant, 12 bytes).857* \param cc initial block counter value.858* \param data data to encrypt or decrypt (updated).859* \param len data length (in bytes).860* \return new block counter value.861*/862uint32_t br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx,863const void *iv, uint32_t cc, void *data, size_t len);864865/**866* \brief CTR encryption + CBC-MAC with AES (`aes_big` implementation).867*868* \param ctx context (already initialised).869* \param ctr counter for CTR (16 bytes, updated).870* \param cbcmac IV for CBC-MAC (updated).871* \param data data to encrypt (updated).872* \param len data length (in bytes, MUST be a multiple of 16).873*/874void br_aes_big_ctrcbc_encrypt(const br_aes_big_ctrcbc_keys *ctx,875void *ctr, void *cbcmac, void *data, size_t len);876877/**878* \brief CTR decryption + CBC-MAC with AES (`aes_big` implementation).879*880* \param ctx context (already initialised).881* \param ctr counter for CTR (16 bytes, updated).882* \param cbcmac IV for CBC-MAC (updated).883* \param data data to decrypt (updated).884* \param len data length (in bytes, MUST be a multiple of 16).885*/886void br_aes_big_ctrcbc_decrypt(const br_aes_big_ctrcbc_keys *ctx,887void *ctr, void *cbcmac, void *data, size_t len);888889/**890* \brief CTR encryption/decryption with AES (`aes_big` implementation).891*892* \param ctx context (already initialised).893* \param ctr counter for CTR (16 bytes, updated).894* \param data data to MAC (updated).895* \param len data length (in bytes, MUST be a multiple of 16).896*/897void br_aes_big_ctrcbc_ctr(const br_aes_big_ctrcbc_keys *ctx,898void *ctr, void *data, size_t len);899900/**901* \brief CBC-MAC with AES (`aes_big` implementation).902*903* \param ctx context (already initialised).904* \param cbcmac IV for CBC-MAC (updated).905* \param data data to MAC (unmodified).906* \param len data length (in bytes, MUST be a multiple of 16).907*/908void br_aes_big_ctrcbc_mac(const br_aes_big_ctrcbc_keys *ctx,909void *cbcmac, const void *data, size_t len);910911/*912* AES implementation optimized for size. It is slower than the913* traditional table-based AES implementation, but requires much less914* code. It still uses data-dependent table accesses (albeit within a915* much smaller 256-byte table), which makes it conceptually vulnerable916* to cache-timing attacks.917*/918919/** \brief AES block size (16 bytes). */920#define br_aes_small_BLOCK_SIZE 16921922/**923* \brief Context for AES subkeys (`aes_small` implementation, CBC encryption).924*925* First field is a pointer to the vtable; it is set by the initialisation926* function. Other fields are not supposed to be accessed by user code.927*/928typedef struct {929/** \brief Pointer to vtable for this context. */930const br_block_cbcenc_class *vtable;931#ifndef BR_DOXYGEN_IGNORE932uint32_t skey[60];933unsigned num_rounds;934#endif935} br_aes_small_cbcenc_keys;936937/**938* \brief Context for AES subkeys (`aes_small` implementation, CBC decryption).939*940* First field is a pointer to the vtable; it is set by the initialisation941* function. Other fields are not supposed to be accessed by user code.942*/943typedef struct {944/** \brief Pointer to vtable for this context. */945const br_block_cbcdec_class *vtable;946#ifndef BR_DOXYGEN_IGNORE947uint32_t skey[60];948unsigned num_rounds;949#endif950} br_aes_small_cbcdec_keys;951952/**953* \brief Context for AES subkeys (`aes_small` implementation, CTR encryption954* and decryption).955*956* First field is a pointer to the vtable; it is set by the initialisation957* function. Other fields are not supposed to be accessed by user code.958*/959typedef struct {960/** \brief Pointer to vtable for this context. */961const br_block_ctr_class *vtable;962#ifndef BR_DOXYGEN_IGNORE963uint32_t skey[60];964unsigned num_rounds;965#endif966} br_aes_small_ctr_keys;967968/**969* \brief Context for AES subkeys (`aes_small` implementation, CTR encryption970* and decryption + CBC-MAC).971*972* First field is a pointer to the vtable; it is set by the initialisation973* function. Other fields are not supposed to be accessed by user code.974*/975typedef struct {976/** \brief Pointer to vtable for this context. */977const br_block_ctrcbc_class *vtable;978#ifndef BR_DOXYGEN_IGNORE979uint32_t skey[60];980unsigned num_rounds;981#endif982} br_aes_small_ctrcbc_keys;983984/**985* \brief Class instance for AES CBC encryption (`aes_small` implementation).986*/987extern const br_block_cbcenc_class br_aes_small_cbcenc_vtable;988989/**990* \brief Class instance for AES CBC decryption (`aes_small` implementation).991*/992extern const br_block_cbcdec_class br_aes_small_cbcdec_vtable;993994/**995* \brief Class instance for AES CTR encryption and decryption996* (`aes_small` implementation).997*/998extern const br_block_ctr_class br_aes_small_ctr_vtable;9991000/**1001* \brief Class instance for AES CTR encryption/decryption + CBC-MAC1002* (`aes_small` implementation).1003*/1004extern const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable;10051006/**1007* \brief Context initialisation (key schedule) for AES CBC encryption1008* (`aes_small` implementation).1009*1010* \param ctx context to initialise.1011* \param key secret key.1012* \param len secret key length (in bytes).1013*/1014void br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,1015const void *key, size_t len);10161017/**1018* \brief Context initialisation (key schedule) for AES CBC decryption1019* (`aes_small` implementation).1020*1021* \param ctx context to initialise.1022* \param key secret key.1023* \param len secret key length (in bytes).1024*/1025void br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,1026const void *key, size_t len);10271028/**1029* \brief Context initialisation (key schedule) for AES CTR encryption1030* and decryption (`aes_small` implementation).1031*1032* \param ctx context to initialise.1033* \param key secret key.1034* \param len secret key length (in bytes).1035*/1036void br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,1037const void *key, size_t len);10381039/**1040* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC1041* (`aes_small` implementation).1042*1043* \param ctx context to initialise.1044* \param key secret key.1045* \param len secret key length (in bytes).1046*/1047void br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx,1048const void *key, size_t len);10491050/**1051* \brief CBC encryption with AES (`aes_small` implementation).1052*1053* \param ctx context (already initialised).1054* \param iv IV (updated).1055* \param data data to encrypt (updated).1056* \param len data length (in bytes, MUST be multiple of 16).1057*/1058void br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx, void *iv,1059void *data, size_t len);10601061/**1062* \brief CBC decryption with AES (`aes_small` implementation).1063*1064* \param ctx context (already initialised).1065* \param iv IV (updated).1066* \param data data to decrypt (updated).1067* \param len data length (in bytes, MUST be multiple of 16).1068*/1069void br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx, void *iv,1070void *data, size_t len);10711072/**1073* \brief CTR encryption and decryption with AES (`aes_small` implementation).1074*1075* \param ctx context (already initialised).1076* \param iv IV (constant, 12 bytes).1077* \param cc initial block counter value.1078* \param data data to decrypt (updated).1079* \param len data length (in bytes).1080* \return new block counter value.1081*/1082uint32_t br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,1083const void *iv, uint32_t cc, void *data, size_t len);10841085/**1086* \brief CTR encryption + CBC-MAC with AES (`aes_small` implementation).1087*1088* \param ctx context (already initialised).1089* \param ctr counter for CTR (16 bytes, updated).1090* \param cbcmac IV for CBC-MAC (updated).1091* \param data data to encrypt (updated).1092* \param len data length (in bytes, MUST be a multiple of 16).1093*/1094void br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx,1095void *ctr, void *cbcmac, void *data, size_t len);10961097/**1098* \brief CTR decryption + CBC-MAC with AES (`aes_small` implementation).1099*1100* \param ctx context (already initialised).1101* \param ctr counter for CTR (16 bytes, updated).1102* \param cbcmac IV for CBC-MAC (updated).1103* \param data data to decrypt (updated).1104* \param len data length (in bytes, MUST be a multiple of 16).1105*/1106void br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx,1107void *ctr, void *cbcmac, void *data, size_t len);11081109/**1110* \brief CTR encryption/decryption with AES (`aes_small` implementation).1111*1112* \param ctx context (already initialised).1113* \param ctr counter for CTR (16 bytes, updated).1114* \param data data to MAC (updated).1115* \param len data length (in bytes, MUST be a multiple of 16).1116*/1117void br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx,1118void *ctr, void *data, size_t len);11191120/**1121* \brief CBC-MAC with AES (`aes_small` implementation).1122*1123* \param ctx context (already initialised).1124* \param cbcmac IV for CBC-MAC (updated).1125* \param data data to MAC (unmodified).1126* \param len data length (in bytes, MUST be a multiple of 16).1127*/1128void br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx,1129void *cbcmac, const void *data, size_t len);11301131/*1132* Constant-time AES implementation. Its size is similar to that of1133* 'aes_big', and its performance is similar to that of 'aes_small' (faster1134* decryption, slower encryption). However, it is constant-time, i.e.1135* immune to cache-timing and similar attacks.1136*/11371138/** \brief AES block size (16 bytes). */1139#define br_aes_ct_BLOCK_SIZE 1611401141/**1142* \brief Context for AES subkeys (`aes_ct` implementation, CBC encryption).1143*1144* First field is a pointer to the vtable; it is set by the initialisation1145* function. Other fields are not supposed to be accessed by user code.1146*/1147typedef struct {1148/** \brief Pointer to vtable for this context. */1149const br_block_cbcenc_class *vtable;1150#ifndef BR_DOXYGEN_IGNORE1151uint32_t skey[60];1152unsigned num_rounds;1153#endif1154} br_aes_ct_cbcenc_keys;11551156/**1157* \brief Context for AES subkeys (`aes_ct` implementation, CBC decryption).1158*1159* First field is a pointer to the vtable; it is set by the initialisation1160* function. Other fields are not supposed to be accessed by user code.1161*/1162typedef struct {1163/** \brief Pointer to vtable for this context. */1164const br_block_cbcdec_class *vtable;1165#ifndef BR_DOXYGEN_IGNORE1166uint32_t skey[60];1167unsigned num_rounds;1168#endif1169} br_aes_ct_cbcdec_keys;11701171/**1172* \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption1173* and decryption).1174*1175* First field is a pointer to the vtable; it is set by the initialisation1176* function. Other fields are not supposed to be accessed by user code.1177*/1178typedef struct {1179/** \brief Pointer to vtable for this context. */1180const br_block_ctr_class *vtable;1181#ifndef BR_DOXYGEN_IGNORE1182uint32_t skey[60];1183unsigned num_rounds;1184#endif1185} br_aes_ct_ctr_keys;11861187/**1188* \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption1189* and decryption + CBC-MAC).1190*1191* First field is a pointer to the vtable; it is set by the initialisation1192* function. Other fields are not supposed to be accessed by user code.1193*/1194typedef struct {1195/** \brief Pointer to vtable for this context. */1196const br_block_ctrcbc_class *vtable;1197#ifndef BR_DOXYGEN_IGNORE1198uint32_t skey[60];1199unsigned num_rounds;1200#endif1201} br_aes_ct_ctrcbc_keys;12021203/**1204* \brief Class instance for AES CBC encryption (`aes_ct` implementation).1205*/1206extern const br_block_cbcenc_class br_aes_ct_cbcenc_vtable;12071208/**1209* \brief Class instance for AES CBC decryption (`aes_ct` implementation).1210*/1211extern const br_block_cbcdec_class br_aes_ct_cbcdec_vtable;12121213/**1214* \brief Class instance for AES CTR encryption and decryption1215* (`aes_ct` implementation).1216*/1217extern const br_block_ctr_class br_aes_ct_ctr_vtable;12181219/**1220* \brief Class instance for AES CTR encryption/decryption + CBC-MAC1221* (`aes_ct` implementation).1222*/1223extern const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable;12241225/**1226* \brief Context initialisation (key schedule) for AES CBC encryption1227* (`aes_ct` implementation).1228*1229* \param ctx context to initialise.1230* \param key secret key.1231* \param len secret key length (in bytes).1232*/1233void br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,1234const void *key, size_t len);12351236/**1237* \brief Context initialisation (key schedule) for AES CBC decryption1238* (`aes_ct` implementation).1239*1240* \param ctx context to initialise.1241* \param key secret key.1242* \param len secret key length (in bytes).1243*/1244void br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,1245const void *key, size_t len);12461247/**1248* \brief Context initialisation (key schedule) for AES CTR encryption1249* and decryption (`aes_ct` implementation).1250*1251* \param ctx context to initialise.1252* \param key secret key.1253* \param len secret key length (in bytes).1254*/1255void br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,1256const void *key, size_t len);12571258/**1259* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC1260* (`aes_ct` implementation).1261*1262* \param ctx context to initialise.1263* \param key secret key.1264* \param len secret key length (in bytes).1265*/1266void br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,1267const void *key, size_t len);12681269/**1270* \brief CBC encryption with AES (`aes_ct` implementation).1271*1272* \param ctx context (already initialised).1273* \param iv IV (updated).1274* \param data data to encrypt (updated).1275* \param len data length (in bytes, MUST be multiple of 16).1276*/1277void br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx, void *iv,1278void *data, size_t len);12791280/**1281* \brief CBC decryption with AES (`aes_ct` implementation).1282*1283* \param ctx context (already initialised).1284* \param iv IV (updated).1285* \param data data to decrypt (updated).1286* \param len data length (in bytes, MUST be multiple of 16).1287*/1288void br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx, void *iv,1289void *data, size_t len);12901291/**1292* \brief CTR encryption and decryption with AES (`aes_ct` implementation).1293*1294* \param ctx context (already initialised).1295* \param iv IV (constant, 12 bytes).1296* \param cc initial block counter value.1297* \param data data to decrypt (updated).1298* \param len data length (in bytes).1299* \return new block counter value.1300*/1301uint32_t br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,1302const void *iv, uint32_t cc, void *data, size_t len);13031304/**1305* \brief CTR encryption + CBC-MAC with AES (`aes_ct` implementation).1306*1307* \param ctx context (already initialised).1308* \param ctr counter for CTR (16 bytes, updated).1309* \param cbcmac IV for CBC-MAC (updated).1310* \param data data to encrypt (updated).1311* \param len data length (in bytes, MUST be a multiple of 16).1312*/1313void br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,1314void *ctr, void *cbcmac, void *data, size_t len);13151316/**1317* \brief CTR decryption + CBC-MAC with AES (`aes_ct` implementation).1318*1319* \param ctx context (already initialised).1320* \param ctr counter for CTR (16 bytes, updated).1321* \param cbcmac IV for CBC-MAC (updated).1322* \param data data to decrypt (updated).1323* \param len data length (in bytes, MUST be a multiple of 16).1324*/1325void br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,1326void *ctr, void *cbcmac, void *data, size_t len);13271328/**1329* \brief CTR encryption/decryption with AES (`aes_ct` implementation).1330*1331* \param ctx context (already initialised).1332* \param ctr counter for CTR (16 bytes, updated).1333* \param data data to MAC (updated).1334* \param len data length (in bytes, MUST be a multiple of 16).1335*/1336void br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,1337void *ctr, void *data, size_t len);13381339/**1340* \brief CBC-MAC with AES (`aes_ct` implementation).1341*1342* \param ctx context (already initialised).1343* \param cbcmac IV for CBC-MAC (updated).1344* \param data data to MAC (unmodified).1345* \param len data length (in bytes, MUST be a multiple of 16).1346*/1347void br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,1348void *cbcmac, const void *data, size_t len);13491350/*1351* 64-bit constant-time AES implementation. It is similar to 'aes_ct'1352* but uses 64-bit registers, making it about twice faster than 'aes_ct'1353* on 64-bit platforms, while remaining constant-time and with a similar1354* code size. (The doubling in performance is only for CBC decryption1355* and CTR mode; CBC encryption is non-parallel and cannot benefit from1356* the larger registers.)1357*/13581359/** \brief AES block size (16 bytes). */1360#define br_aes_ct64_BLOCK_SIZE 1613611362/**1363* \brief Context for AES subkeys (`aes_ct64` implementation, CBC encryption).1364*1365* First field is a pointer to the vtable; it is set by the initialisation1366* function. Other fields are not supposed to be accessed by user code.1367*/1368typedef struct {1369/** \brief Pointer to vtable for this context. */1370const br_block_cbcenc_class *vtable;1371#ifndef BR_DOXYGEN_IGNORE1372uint64_t skey[30];1373unsigned num_rounds;1374#endif1375} br_aes_ct64_cbcenc_keys;13761377/**1378* \brief Context for AES subkeys (`aes_ct64` implementation, CBC decryption).1379*1380* First field is a pointer to the vtable; it is set by the initialisation1381* function. Other fields are not supposed to be accessed by user code.1382*/1383typedef struct {1384/** \brief Pointer to vtable for this context. */1385const br_block_cbcdec_class *vtable;1386#ifndef BR_DOXYGEN_IGNORE1387uint64_t skey[30];1388unsigned num_rounds;1389#endif1390} br_aes_ct64_cbcdec_keys;13911392/**1393* \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption1394* and decryption).1395*1396* First field is a pointer to the vtable; it is set by the initialisation1397* function. Other fields are not supposed to be accessed by user code.1398*/1399typedef struct {1400/** \brief Pointer to vtable for this context. */1401const br_block_ctr_class *vtable;1402#ifndef BR_DOXYGEN_IGNORE1403uint64_t skey[30];1404unsigned num_rounds;1405#endif1406} br_aes_ct64_ctr_keys;14071408/**1409* \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption1410* and decryption + CBC-MAC).1411*1412* First field is a pointer to the vtable; it is set by the initialisation1413* function. Other fields are not supposed to be accessed by user code.1414*/1415typedef struct {1416/** \brief Pointer to vtable for this context. */1417const br_block_ctrcbc_class *vtable;1418#ifndef BR_DOXYGEN_IGNORE1419uint64_t skey[30];1420unsigned num_rounds;1421#endif1422} br_aes_ct64_ctrcbc_keys;14231424/**1425* \brief Class instance for AES CBC encryption (`aes_ct64` implementation).1426*/1427extern const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable;14281429/**1430* \brief Class instance for AES CBC decryption (`aes_ct64` implementation).1431*/1432extern const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable;14331434/**1435* \brief Class instance for AES CTR encryption and decryption1436* (`aes_ct64` implementation).1437*/1438extern const br_block_ctr_class br_aes_ct64_ctr_vtable;14391440/**1441* \brief Class instance for AES CTR encryption/decryption + CBC-MAC1442* (`aes_ct64` implementation).1443*/1444extern const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable;14451446/**1447* \brief Context initialisation (key schedule) for AES CBC encryption1448* (`aes_ct64` implementation).1449*1450* \param ctx context to initialise.1451* \param key secret key.1452* \param len secret key length (in bytes).1453*/1454void br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,1455const void *key, size_t len);14561457/**1458* \brief Context initialisation (key schedule) for AES CBC decryption1459* (`aes_ct64` implementation).1460*1461* \param ctx context to initialise.1462* \param key secret key.1463* \param len secret key length (in bytes).1464*/1465void br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,1466const void *key, size_t len);14671468/**1469* \brief Context initialisation (key schedule) for AES CTR encryption1470* and decryption (`aes_ct64` implementation).1471*1472* \param ctx context to initialise.1473* \param key secret key.1474* \param len secret key length (in bytes).1475*/1476void br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,1477const void *key, size_t len);14781479/**1480* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC1481* (`aes_ct64` implementation).1482*1483* \param ctx context to initialise.1484* \param key secret key.1485* \param len secret key length (in bytes).1486*/1487void br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx,1488const void *key, size_t len);14891490/**1491* \brief CBC encryption with AES (`aes_ct64` implementation).1492*1493* \param ctx context (already initialised).1494* \param iv IV (updated).1495* \param data data to encrypt (updated).1496* \param len data length (in bytes, MUST be multiple of 16).1497*/1498void br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx, void *iv,1499void *data, size_t len);15001501/**1502* \brief CBC decryption with AES (`aes_ct64` implementation).1503*1504* \param ctx context (already initialised).1505* \param iv IV (updated).1506* \param data data to decrypt (updated).1507* \param len data length (in bytes, MUST be multiple of 16).1508*/1509void br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx, void *iv,1510void *data, size_t len);15111512/**1513* \brief CTR encryption and decryption with AES (`aes_ct64` implementation).1514*1515* \param ctx context (already initialised).1516* \param iv IV (constant, 12 bytes).1517* \param cc initial block counter value.1518* \param data data to decrypt (updated).1519* \param len data length (in bytes).1520* \return new block counter value.1521*/1522uint32_t br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,1523const void *iv, uint32_t cc, void *data, size_t len);15241525/**1526* \brief CTR encryption + CBC-MAC with AES (`aes_ct64` implementation).1527*1528* \param ctx context (already initialised).1529* \param ctr counter for CTR (16 bytes, updated).1530* \param cbcmac IV for CBC-MAC (updated).1531* \param data data to encrypt (updated).1532* \param len data length (in bytes, MUST be a multiple of 16).1533*/1534void br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx,1535void *ctr, void *cbcmac, void *data, size_t len);15361537/**1538* \brief CTR decryption + CBC-MAC with AES (`aes_ct64` implementation).1539*1540* \param ctx context (already initialised).1541* \param ctr counter for CTR (16 bytes, updated).1542* \param cbcmac IV for CBC-MAC (updated).1543* \param data data to decrypt (updated).1544* \param len data length (in bytes, MUST be a multiple of 16).1545*/1546void br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx,1547void *ctr, void *cbcmac, void *data, size_t len);15481549/**1550* \brief CTR encryption/decryption with AES (`aes_ct64` implementation).1551*1552* \param ctx context (already initialised).1553* \param ctr counter for CTR (16 bytes, updated).1554* \param data data to MAC (updated).1555* \param len data length (in bytes, MUST be a multiple of 16).1556*/1557void br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx,1558void *ctr, void *data, size_t len);15591560/**1561* \brief CBC-MAC with AES (`aes_ct64` implementation).1562*1563* \param ctx context (already initialised).1564* \param cbcmac IV for CBC-MAC (updated).1565* \param data data to MAC (unmodified).1566* \param len data length (in bytes, MUST be a multiple of 16).1567*/1568void br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx,1569void *cbcmac, const void *data, size_t len);15701571/*1572* AES implementation using AES-NI opcodes (x86 platform).1573*/15741575/** \brief AES block size (16 bytes). */1576#define br_aes_x86ni_BLOCK_SIZE 1615771578/**1579* \brief Context for AES subkeys (`aes_x86ni` implementation, CBC encryption).1580*1581* First field is a pointer to the vtable; it is set by the initialisation1582* function. Other fields are not supposed to be accessed by user code.1583*/1584typedef struct {1585/** \brief Pointer to vtable for this context. */1586const br_block_cbcenc_class *vtable;1587#ifndef BR_DOXYGEN_IGNORE1588union {1589unsigned char skni[16 * 15];1590} skey;1591unsigned num_rounds;1592#endif1593} br_aes_x86ni_cbcenc_keys;15941595/**1596* \brief Context for AES subkeys (`aes_x86ni` implementation, CBC decryption).1597*1598* First field is a pointer to the vtable; it is set by the initialisation1599* function. Other fields are not supposed to be accessed by user code.1600*/1601typedef struct {1602/** \brief Pointer to vtable for this context. */1603const br_block_cbcdec_class *vtable;1604#ifndef BR_DOXYGEN_IGNORE1605union {1606unsigned char skni[16 * 15];1607} skey;1608unsigned num_rounds;1609#endif1610} br_aes_x86ni_cbcdec_keys;16111612/**1613* \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption1614* and decryption).1615*1616* First field is a pointer to the vtable; it is set by the initialisation1617* function. Other fields are not supposed to be accessed by user code.1618*/1619typedef struct {1620/** \brief Pointer to vtable for this context. */1621const br_block_ctr_class *vtable;1622#ifndef BR_DOXYGEN_IGNORE1623union {1624unsigned char skni[16 * 15];1625} skey;1626unsigned num_rounds;1627#endif1628} br_aes_x86ni_ctr_keys;16291630/**1631* \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption1632* and decryption + CBC-MAC).1633*1634* First field is a pointer to the vtable; it is set by the initialisation1635* function. Other fields are not supposed to be accessed by user code.1636*/1637typedef struct {1638/** \brief Pointer to vtable for this context. */1639const br_block_ctrcbc_class *vtable;1640#ifndef BR_DOXYGEN_IGNORE1641union {1642unsigned char skni[16 * 15];1643} skey;1644unsigned num_rounds;1645#endif1646} br_aes_x86ni_ctrcbc_keys;16471648/**1649* \brief Class instance for AES CBC encryption (`aes_x86ni` implementation).1650*1651* Since this implementation might be omitted from the library, or the1652* AES opcode unavailable on the current CPU, a pointer to this class1653* instance should be obtained through `br_aes_x86ni_cbcenc_get_vtable()`.1654*/1655extern const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable;16561657/**1658* \brief Class instance for AES CBC decryption (`aes_x86ni` implementation).1659*1660* Since this implementation might be omitted from the library, or the1661* AES opcode unavailable on the current CPU, a pointer to this class1662* instance should be obtained through `br_aes_x86ni_cbcdec_get_vtable()`.1663*/1664extern const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable;16651666/**1667* \brief Class instance for AES CTR encryption and decryption1668* (`aes_x86ni` implementation).1669*1670* Since this implementation might be omitted from the library, or the1671* AES opcode unavailable on the current CPU, a pointer to this class1672* instance should be obtained through `br_aes_x86ni_ctr_get_vtable()`.1673*/1674extern const br_block_ctr_class br_aes_x86ni_ctr_vtable;16751676/**1677* \brief Class instance for AES CTR encryption/decryption + CBC-MAC1678* (`aes_x86ni` implementation).1679*1680* Since this implementation might be omitted from the library, or the1681* AES opcode unavailable on the current CPU, a pointer to this class1682* instance should be obtained through `br_aes_x86ni_ctrcbc_get_vtable()`.1683*/1684extern const br_block_ctrcbc_class br_aes_x86ni_ctrcbc_vtable;16851686/**1687* \brief Context initialisation (key schedule) for AES CBC encryption1688* (`aes_x86ni` implementation).1689*1690* \param ctx context to initialise.1691* \param key secret key.1692* \param len secret key length (in bytes).1693*/1694void br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx,1695const void *key, size_t len);16961697/**1698* \brief Context initialisation (key schedule) for AES CBC decryption1699* (`aes_x86ni` implementation).1700*1701* \param ctx context to initialise.1702* \param key secret key.1703* \param len secret key length (in bytes).1704*/1705void br_aes_x86ni_cbcdec_init(br_aes_x86ni_cbcdec_keys *ctx,1706const void *key, size_t len);17071708/**1709* \brief Context initialisation (key schedule) for AES CTR encryption1710* and decryption (`aes_x86ni` implementation).1711*1712* \param ctx context to initialise.1713* \param key secret key.1714* \param len secret key length (in bytes).1715*/1716void br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys *ctx,1717const void *key, size_t len);17181719/**1720* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC1721* (`aes_x86ni` implementation).1722*1723* \param ctx context to initialise.1724* \param key secret key.1725* \param len secret key length (in bytes).1726*/1727void br_aes_x86ni_ctrcbc_init(br_aes_x86ni_ctrcbc_keys *ctx,1728const void *key, size_t len);17291730/**1731* \brief CBC encryption with AES (`aes_x86ni` implementation).1732*1733* \param ctx context (already initialised).1734* \param iv IV (updated).1735* \param data data to encrypt (updated).1736* \param len data length (in bytes, MUST be multiple of 16).1737*/1738void br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx, void *iv,1739void *data, size_t len);17401741/**1742* \brief CBC decryption with AES (`aes_x86ni` implementation).1743*1744* \param ctx context (already initialised).1745* \param iv IV (updated).1746* \param data data to decrypt (updated).1747* \param len data length (in bytes, MUST be multiple of 16).1748*/1749void br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys *ctx, void *iv,1750void *data, size_t len);17511752/**1753* \brief CTR encryption and decryption with AES (`aes_x86ni` implementation).1754*1755* \param ctx context (already initialised).1756* \param iv IV (constant, 12 bytes).1757* \param cc initial block counter value.1758* \param data data to decrypt (updated).1759* \param len data length (in bytes).1760* \return new block counter value.1761*/1762uint32_t br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys *ctx,1763const void *iv, uint32_t cc, void *data, size_t len);17641765/**1766* \brief CTR encryption + CBC-MAC with AES (`aes_x86ni` implementation).1767*1768* \param ctx context (already initialised).1769* \param ctr counter for CTR (16 bytes, updated).1770* \param cbcmac IV for CBC-MAC (updated).1771* \param data data to encrypt (updated).1772* \param len data length (in bytes, MUST be a multiple of 16).1773*/1774void br_aes_x86ni_ctrcbc_encrypt(const br_aes_x86ni_ctrcbc_keys *ctx,1775void *ctr, void *cbcmac, void *data, size_t len);17761777/**1778* \brief CTR decryption + CBC-MAC with AES (`aes_x86ni` implementation).1779*1780* \param ctx context (already initialised).1781* \param ctr counter for CTR (16 bytes, updated).1782* \param cbcmac IV for CBC-MAC (updated).1783* \param data data to decrypt (updated).1784* \param len data length (in bytes, MUST be a multiple of 16).1785*/1786void br_aes_x86ni_ctrcbc_decrypt(const br_aes_x86ni_ctrcbc_keys *ctx,1787void *ctr, void *cbcmac, void *data, size_t len);17881789/**1790* \brief CTR encryption/decryption with AES (`aes_x86ni` implementation).1791*1792* \param ctx context (already initialised).1793* \param ctr counter for CTR (16 bytes, updated).1794* \param data data to MAC (updated).1795* \param len data length (in bytes, MUST be a multiple of 16).1796*/1797void br_aes_x86ni_ctrcbc_ctr(const br_aes_x86ni_ctrcbc_keys *ctx,1798void *ctr, void *data, size_t len);17991800/**1801* \brief CBC-MAC with AES (`aes_x86ni` implementation).1802*1803* \param ctx context (already initialised).1804* \param cbcmac IV for CBC-MAC (updated).1805* \param data data to MAC (unmodified).1806* \param len data length (in bytes, MUST be a multiple of 16).1807*/1808void br_aes_x86ni_ctrcbc_mac(const br_aes_x86ni_ctrcbc_keys *ctx,1809void *cbcmac, const void *data, size_t len);18101811/**1812* \brief Obtain the `aes_x86ni` AES-CBC (encryption) implementation, if1813* available.1814*1815* This function returns a pointer to `br_aes_x86ni_cbcenc_vtable`, if1816* that implementation was compiled in the library _and_ the x86 AES1817* opcodes are available on the currently running CPU. If either of1818* these conditions is not met, then this function returns `NULL`.1819*1820* \return the `aes_x86ni` AES-CBC (encryption) implementation, or `NULL`.1821*/1822const br_block_cbcenc_class *br_aes_x86ni_cbcenc_get_vtable(void);18231824/**1825* \brief Obtain the `aes_x86ni` AES-CBC (decryption) implementation, if1826* available.1827*1828* This function returns a pointer to `br_aes_x86ni_cbcdec_vtable`, if1829* that implementation was compiled in the library _and_ the x86 AES1830* opcodes are available on the currently running CPU. If either of1831* these conditions is not met, then this function returns `NULL`.1832*1833* \return the `aes_x86ni` AES-CBC (decryption) implementation, or `NULL`.1834*/1835const br_block_cbcdec_class *br_aes_x86ni_cbcdec_get_vtable(void);18361837/**1838* \brief Obtain the `aes_x86ni` AES-CTR implementation, if available.1839*1840* This function returns a pointer to `br_aes_x86ni_ctr_vtable`, if1841* that implementation was compiled in the library _and_ the x86 AES1842* opcodes are available on the currently running CPU. If either of1843* these conditions is not met, then this function returns `NULL`.1844*1845* \return the `aes_x86ni` AES-CTR implementation, or `NULL`.1846*/1847const br_block_ctr_class *br_aes_x86ni_ctr_get_vtable(void);18481849/**1850* \brief Obtain the `aes_x86ni` AES-CTR + CBC-MAC implementation, if1851* available.1852*1853* This function returns a pointer to `br_aes_x86ni_ctrcbc_vtable`, if1854* that implementation was compiled in the library _and_ the x86 AES1855* opcodes are available on the currently running CPU. If either of1856* these conditions is not met, then this function returns `NULL`.1857*1858* \return the `aes_x86ni` AES-CTR implementation, or `NULL`.1859*/1860const br_block_ctrcbc_class *br_aes_x86ni_ctrcbc_get_vtable(void);18611862/*1863* AES implementation using POWER8 opcodes.1864*/18651866/** \brief AES block size (16 bytes). */1867#define br_aes_pwr8_BLOCK_SIZE 1618681869/**1870* \brief Context for AES subkeys (`aes_pwr8` implementation, CBC encryption).1871*1872* First field is a pointer to the vtable; it is set by the initialisation1873* function. Other fields are not supposed to be accessed by user code.1874*/1875typedef struct {1876/** \brief Pointer to vtable for this context. */1877const br_block_cbcenc_class *vtable;1878#ifndef BR_DOXYGEN_IGNORE1879union {1880unsigned char skni[16 * 15];1881} skey;1882unsigned num_rounds;1883#endif1884} br_aes_pwr8_cbcenc_keys;18851886/**1887* \brief Context for AES subkeys (`aes_pwr8` implementation, CBC decryption).1888*1889* First field is a pointer to the vtable; it is set by the initialisation1890* function. Other fields are not supposed to be accessed by user code.1891*/1892typedef struct {1893/** \brief Pointer to vtable for this context. */1894const br_block_cbcdec_class *vtable;1895#ifndef BR_DOXYGEN_IGNORE1896union {1897unsigned char skni[16 * 15];1898} skey;1899unsigned num_rounds;1900#endif1901} br_aes_pwr8_cbcdec_keys;19021903/**1904* \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption1905* and decryption).1906*1907* First field is a pointer to the vtable; it is set by the initialisation1908* function. Other fields are not supposed to be accessed by user code.1909*/1910typedef struct {1911/** \brief Pointer to vtable for this context. */1912const br_block_ctr_class *vtable;1913#ifndef BR_DOXYGEN_IGNORE1914union {1915unsigned char skni[16 * 15];1916} skey;1917unsigned num_rounds;1918#endif1919} br_aes_pwr8_ctr_keys;19201921/**1922* \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption1923* and decryption + CBC-MAC).1924*1925* First field is a pointer to the vtable; it is set by the initialisation1926* function. Other fields are not supposed to be accessed by user code.1927*/1928typedef struct {1929/** \brief Pointer to vtable for this context. */1930const br_block_ctrcbc_class *vtable;1931#ifndef BR_DOXYGEN_IGNORE1932union {1933unsigned char skni[16 * 15];1934} skey;1935unsigned num_rounds;1936#endif1937} br_aes_pwr8_ctrcbc_keys;19381939/**1940* \brief Class instance for AES CBC encryption (`aes_pwr8` implementation).1941*1942* Since this implementation might be omitted from the library, or the1943* AES opcode unavailable on the current CPU, a pointer to this class1944* instance should be obtained through `br_aes_pwr8_cbcenc_get_vtable()`.1945*/1946extern const br_block_cbcenc_class br_aes_pwr8_cbcenc_vtable;19471948/**1949* \brief Class instance for AES CBC decryption (`aes_pwr8` implementation).1950*1951* Since this implementation might be omitted from the library, or the1952* AES opcode unavailable on the current CPU, a pointer to this class1953* instance should be obtained through `br_aes_pwr8_cbcdec_get_vtable()`.1954*/1955extern const br_block_cbcdec_class br_aes_pwr8_cbcdec_vtable;19561957/**1958* \brief Class instance for AES CTR encryption and decryption1959* (`aes_pwr8` implementation).1960*1961* Since this implementation might be omitted from the library, or the1962* AES opcode unavailable on the current CPU, a pointer to this class1963* instance should be obtained through `br_aes_pwr8_ctr_get_vtable()`.1964*/1965extern const br_block_ctr_class br_aes_pwr8_ctr_vtable;19661967/**1968* \brief Class instance for AES CTR encryption/decryption + CBC-MAC1969* (`aes_pwr8` implementation).1970*1971* Since this implementation might be omitted from the library, or the1972* AES opcode unavailable on the current CPU, a pointer to this class1973* instance should be obtained through `br_aes_pwr8_ctrcbc_get_vtable()`.1974*/1975extern const br_block_ctrcbc_class br_aes_pwr8_ctrcbc_vtable;19761977/**1978* \brief Context initialisation (key schedule) for AES CBC encryption1979* (`aes_pwr8` implementation).1980*1981* \param ctx context to initialise.1982* \param key secret key.1983* \param len secret key length (in bytes).1984*/1985void br_aes_pwr8_cbcenc_init(br_aes_pwr8_cbcenc_keys *ctx,1986const void *key, size_t len);19871988/**1989* \brief Context initialisation (key schedule) for AES CBC decryption1990* (`aes_pwr8` implementation).1991*1992* \param ctx context to initialise.1993* \param key secret key.1994* \param len secret key length (in bytes).1995*/1996void br_aes_pwr8_cbcdec_init(br_aes_pwr8_cbcdec_keys *ctx,1997const void *key, size_t len);19981999/**2000* \brief Context initialisation (key schedule) for AES CTR encryption2001* and decryption (`aes_pwr8` implementation).2002*2003* \param ctx context to initialise.2004* \param key secret key.2005* \param len secret key length (in bytes).2006*/2007void br_aes_pwr8_ctr_init(br_aes_pwr8_ctr_keys *ctx,2008const void *key, size_t len);20092010/**2011* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC2012* (`aes_pwr8` implementation).2013*2014* \param ctx context to initialise.2015* \param key secret key.2016* \param len secret key length (in bytes).2017*/2018void br_aes_pwr8_ctrcbc_init(br_aes_pwr8_ctrcbc_keys *ctx,2019const void *key, size_t len);20202021/**2022* \brief CBC encryption with AES (`aes_pwr8` implementation).2023*2024* \param ctx context (already initialised).2025* \param iv IV (updated).2026* \param data data to encrypt (updated).2027* \param len data length (in bytes, MUST be multiple of 16).2028*/2029void br_aes_pwr8_cbcenc_run(const br_aes_pwr8_cbcenc_keys *ctx, void *iv,2030void *data, size_t len);20312032/**2033* \brief CBC decryption with AES (`aes_pwr8` implementation).2034*2035* \param ctx context (already initialised).2036* \param iv IV (updated).2037* \param data data to decrypt (updated).2038* \param len data length (in bytes, MUST be multiple of 16).2039*/2040void br_aes_pwr8_cbcdec_run(const br_aes_pwr8_cbcdec_keys *ctx, void *iv,2041void *data, size_t len);20422043/**2044* \brief CTR encryption and decryption with AES (`aes_pwr8` implementation).2045*2046* \param ctx context (already initialised).2047* \param iv IV (constant, 12 bytes).2048* \param cc initial block counter value.2049* \param data data to decrypt (updated).2050* \param len data length (in bytes).2051* \return new block counter value.2052*/2053uint32_t br_aes_pwr8_ctr_run(const br_aes_pwr8_ctr_keys *ctx,2054const void *iv, uint32_t cc, void *data, size_t len);20552056/**2057* \brief CTR encryption + CBC-MAC with AES (`aes_pwr8` implementation).2058*2059* \param ctx context (already initialised).2060* \param ctr counter for CTR (16 bytes, updated).2061* \param cbcmac IV for CBC-MAC (updated).2062* \param data data to encrypt (updated).2063* \param len data length (in bytes, MUST be a multiple of 16).2064*/2065void br_aes_pwr8_ctrcbc_encrypt(const br_aes_pwr8_ctrcbc_keys *ctx,2066void *ctr, void *cbcmac, void *data, size_t len);20672068/**2069* \brief CTR decryption + CBC-MAC with AES (`aes_pwr8` implementation).2070*2071* \param ctx context (already initialised).2072* \param ctr counter for CTR (16 bytes, updated).2073* \param cbcmac IV for CBC-MAC (updated).2074* \param data data to decrypt (updated).2075* \param len data length (in bytes, MUST be a multiple of 16).2076*/2077void br_aes_pwr8_ctrcbc_decrypt(const br_aes_pwr8_ctrcbc_keys *ctx,2078void *ctr, void *cbcmac, void *data, size_t len);20792080/**2081* \brief CTR encryption/decryption with AES (`aes_pwr8` implementation).2082*2083* \param ctx context (already initialised).2084* \param ctr counter for CTR (16 bytes, updated).2085* \param data data to MAC (updated).2086* \param len data length (in bytes, MUST be a multiple of 16).2087*/2088void br_aes_pwr8_ctrcbc_ctr(const br_aes_pwr8_ctrcbc_keys *ctx,2089void *ctr, void *data, size_t len);20902091/**2092* \brief CBC-MAC with AES (`aes_pwr8` implementation).2093*2094* \param ctx context (already initialised).2095* \param cbcmac IV for CBC-MAC (updated).2096* \param data data to MAC (unmodified).2097* \param len data length (in bytes, MUST be a multiple of 16).2098*/2099void br_aes_pwr8_ctrcbc_mac(const br_aes_pwr8_ctrcbc_keys *ctx,2100void *cbcmac, const void *data, size_t len);21012102/**2103* \brief Obtain the `aes_pwr8` AES-CBC (encryption) implementation, if2104* available.2105*2106* This function returns a pointer to `br_aes_pwr8_cbcenc_vtable`, if2107* that implementation was compiled in the library _and_ the POWER82108* crypto opcodes are available on the currently running CPU. If either2109* of these conditions is not met, then this function returns `NULL`.2110*2111* \return the `aes_pwr8` AES-CBC (encryption) implementation, or `NULL`.2112*/2113const br_block_cbcenc_class *br_aes_pwr8_cbcenc_get_vtable(void);21142115/**2116* \brief Obtain the `aes_pwr8` AES-CBC (decryption) implementation, if2117* available.2118*2119* This function returns a pointer to `br_aes_pwr8_cbcdec_vtable`, if2120* that implementation was compiled in the library _and_ the POWER82121* crypto opcodes are available on the currently running CPU. If either2122* of these conditions is not met, then this function returns `NULL`.2123*2124* \return the `aes_pwr8` AES-CBC (decryption) implementation, or `NULL`.2125*/2126const br_block_cbcdec_class *br_aes_pwr8_cbcdec_get_vtable(void);21272128/**2129* \brief Obtain the `aes_pwr8` AES-CTR implementation, if available.2130*2131* This function returns a pointer to `br_aes_pwr8_ctr_vtable`, if that2132* implementation was compiled in the library _and_ the POWER8 crypto2133* opcodes are available on the currently running CPU. If either of2134* these conditions is not met, then this function returns `NULL`.2135*2136* \return the `aes_pwr8` AES-CTR implementation, or `NULL`.2137*/2138const br_block_ctr_class *br_aes_pwr8_ctr_get_vtable(void);21392140/**2141* \brief Obtain the `aes_pwr8` AES-CTR + CBC-MAC implementation, if2142* available.2143*2144* This function returns a pointer to `br_aes_pwr8_ctrcbc_vtable`, if2145* that implementation was compiled in the library _and_ the POWER8 AES2146* opcodes are available on the currently running CPU. If either of2147* these conditions is not met, then this function returns `NULL`.2148*2149* \return the `aes_pwr8` AES-CTR implementation, or `NULL`.2150*/2151const br_block_ctrcbc_class *br_aes_pwr8_ctrcbc_get_vtable(void);21522153/**2154* \brief Aggregate structure large enough to be used as context for2155* subkeys (CBC encryption) for all AES implementations.2156*/2157typedef union {2158const br_block_cbcenc_class *vtable;2159br_aes_big_cbcenc_keys c_big;2160br_aes_small_cbcenc_keys c_small;2161br_aes_ct_cbcenc_keys c_ct;2162br_aes_ct64_cbcenc_keys c_ct64;2163br_aes_x86ni_cbcenc_keys c_x86ni;2164br_aes_pwr8_cbcenc_keys c_pwr8;2165} br_aes_gen_cbcenc_keys;21662167/**2168* \brief Aggregate structure large enough to be used as context for2169* subkeys (CBC decryption) for all AES implementations.2170*/2171typedef union {2172const br_block_cbcdec_class *vtable;2173br_aes_big_cbcdec_keys c_big;2174br_aes_small_cbcdec_keys c_small;2175br_aes_ct_cbcdec_keys c_ct;2176br_aes_ct64_cbcdec_keys c_ct64;2177br_aes_x86ni_cbcdec_keys c_x86ni;2178br_aes_pwr8_cbcdec_keys c_pwr8;2179} br_aes_gen_cbcdec_keys;21802181/**2182* \brief Aggregate structure large enough to be used as context for2183* subkeys (CTR encryption and decryption) for all AES implementations.2184*/2185typedef union {2186const br_block_ctr_class *vtable;2187br_aes_big_ctr_keys c_big;2188br_aes_small_ctr_keys c_small;2189br_aes_ct_ctr_keys c_ct;2190br_aes_ct64_ctr_keys c_ct64;2191br_aes_x86ni_ctr_keys c_x86ni;2192br_aes_pwr8_ctr_keys c_pwr8;2193} br_aes_gen_ctr_keys;21942195/**2196* \brief Aggregate structure large enough to be used as context for2197* subkeys (CTR encryption/decryption + CBC-MAC) for all AES implementations.2198*/2199typedef union {2200const br_block_ctrcbc_class *vtable;2201br_aes_big_ctrcbc_keys c_big;2202br_aes_small_ctrcbc_keys c_small;2203br_aes_ct_ctrcbc_keys c_ct;2204br_aes_ct64_ctrcbc_keys c_ct64;2205br_aes_x86ni_ctrcbc_keys c_x86ni;2206br_aes_pwr8_ctrcbc_keys c_pwr8;2207} br_aes_gen_ctrcbc_keys;22082209/*2210* Traditional, table-based implementation for DES/3DES. Since tables are2211* used, cache-timing attacks are conceptually possible.2212*/22132214/** \brief DES/3DES block size (8 bytes). */2215#define br_des_tab_BLOCK_SIZE 822162217/**2218* \brief Context for DES subkeys (`des_tab` implementation, CBC encryption).2219*2220* First field is a pointer to the vtable; it is set by the initialisation2221* function. Other fields are not supposed to be accessed by user code.2222*/2223typedef struct {2224/** \brief Pointer to vtable for this context. */2225const br_block_cbcenc_class *vtable;2226#ifndef BR_DOXYGEN_IGNORE2227uint32_t skey[96];2228unsigned num_rounds;2229#endif2230} br_des_tab_cbcenc_keys;22312232/**2233* \brief Context for DES subkeys (`des_tab` implementation, CBC decryption).2234*2235* First field is a pointer to the vtable; it is set by the initialisation2236* function. Other fields are not supposed to be accessed by user code.2237*/2238typedef struct {2239/** \brief Pointer to vtable for this context. */2240const br_block_cbcdec_class *vtable;2241#ifndef BR_DOXYGEN_IGNORE2242uint32_t skey[96];2243unsigned num_rounds;2244#endif2245} br_des_tab_cbcdec_keys;22462247/**2248* \brief Class instance for DES CBC encryption (`des_tab` implementation).2249*/2250extern const br_block_cbcenc_class br_des_tab_cbcenc_vtable;22512252/**2253* \brief Class instance for DES CBC decryption (`des_tab` implementation).2254*/2255extern const br_block_cbcdec_class br_des_tab_cbcdec_vtable;22562257/**2258* \brief Context initialisation (key schedule) for DES CBC encryption2259* (`des_tab` implementation).2260*2261* \param ctx context to initialise.2262* \param key secret key.2263* \param len secret key length (in bytes).2264*/2265void br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx,2266const void *key, size_t len);22672268/**2269* \brief Context initialisation (key schedule) for DES CBC decryption2270* (`des_tab` implementation).2271*2272* \param ctx context to initialise.2273* \param key secret key.2274* \param len secret key length (in bytes).2275*/2276void br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx,2277const void *key, size_t len);22782279/**2280* \brief CBC encryption with DES (`des_tab` implementation).2281*2282* \param ctx context (already initialised).2283* \param iv IV (updated).2284* \param data data to encrypt (updated).2285* \param len data length (in bytes, MUST be multiple of 8).2286*/2287void br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx, void *iv,2288void *data, size_t len);22892290/**2291* \brief CBC decryption with DES (`des_tab` implementation).2292*2293* \param ctx context (already initialised).2294* \param iv IV (updated).2295* \param data data to decrypt (updated).2296* \param len data length (in bytes, MUST be multiple of 8).2297*/2298void br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx, void *iv,2299void *data, size_t len);23002301/*2302* Constant-time implementation for DES/3DES. It is substantially slower2303* (by a factor of about 4x), but also immune to cache-timing attacks.2304*/23052306/** \brief DES/3DES block size (8 bytes). */2307#define br_des_ct_BLOCK_SIZE 823082309/**2310* \brief Context for DES subkeys (`des_ct` implementation, CBC encryption).2311*2312* First field is a pointer to the vtable; it is set by the initialisation2313* function. Other fields are not supposed to be accessed by user code.2314*/2315typedef struct {2316/** \brief Pointer to vtable for this context. */2317const br_block_cbcenc_class *vtable;2318#ifndef BR_DOXYGEN_IGNORE2319uint32_t skey[96];2320unsigned num_rounds;2321#endif2322} br_des_ct_cbcenc_keys;23232324/**2325* \brief Context for DES subkeys (`des_ct` implementation, CBC decryption).2326*2327* First field is a pointer to the vtable; it is set by the initialisation2328* function. Other fields are not supposed to be accessed by user code.2329*/2330typedef struct {2331/** \brief Pointer to vtable for this context. */2332const br_block_cbcdec_class *vtable;2333#ifndef BR_DOXYGEN_IGNORE2334uint32_t skey[96];2335unsigned num_rounds;2336#endif2337} br_des_ct_cbcdec_keys;23382339/**2340* \brief Class instance for DES CBC encryption (`des_ct` implementation).2341*/2342extern const br_block_cbcenc_class br_des_ct_cbcenc_vtable;23432344/**2345* \brief Class instance for DES CBC decryption (`des_ct` implementation).2346*/2347extern const br_block_cbcdec_class br_des_ct_cbcdec_vtable;23482349/**2350* \brief Context initialisation (key schedule) for DES CBC encryption2351* (`des_ct` implementation).2352*2353* \param ctx context to initialise.2354* \param key secret key.2355* \param len secret key length (in bytes).2356*/2357void br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx,2358const void *key, size_t len);23592360/**2361* \brief Context initialisation (key schedule) for DES CBC decryption2362* (`des_ct` implementation).2363*2364* \param ctx context to initialise.2365* \param key secret key.2366* \param len secret key length (in bytes).2367*/2368void br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx,2369const void *key, size_t len);23702371/**2372* \brief CBC encryption with DES (`des_ct` implementation).2373*2374* \param ctx context (already initialised).2375* \param iv IV (updated).2376* \param data data to encrypt (updated).2377* \param len data length (in bytes, MUST be multiple of 8).2378*/2379void br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx, void *iv,2380void *data, size_t len);23812382/**2383* \brief CBC decryption with DES (`des_ct` implementation).2384*2385* \param ctx context (already initialised).2386* \param iv IV (updated).2387* \param data data to decrypt (updated).2388* \param len data length (in bytes, MUST be multiple of 8).2389*/2390void br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx, void *iv,2391void *data, size_t len);23922393/*2394* These structures are large enough to accommodate subkeys for all2395* DES/3DES implementations.2396*/23972398/**2399* \brief Aggregate structure large enough to be used as context for2400* subkeys (CBC encryption) for all DES implementations.2401*/2402typedef union {2403const br_block_cbcenc_class *vtable;2404br_des_tab_cbcenc_keys tab;2405br_des_ct_cbcenc_keys ct;2406} br_des_gen_cbcenc_keys;24072408/**2409* \brief Aggregate structure large enough to be used as context for2410* subkeys (CBC decryption) for all DES implementations.2411*/2412typedef union {2413const br_block_cbcdec_class *vtable;2414br_des_tab_cbcdec_keys c_tab;2415br_des_ct_cbcdec_keys c_ct;2416} br_des_gen_cbcdec_keys;24172418/**2419* \brief Type for a ChaCha20 implementation.2420*2421* An implementation follows the description in RFC 7539:2422*2423* - Key is 256 bits (`key` points to exactly 32 bytes).2424*2425* - IV is 96 bits (`iv` points to exactly 12 bytes).2426*2427* - Block counter is over 32 bits and starts at value `cc`; the2428* resulting value is returned.2429*2430* Data (pointed to by `data`, of length `len`) is encrypted/decrypted2431* in place. If `len` is not a multiple of 64, then the excess bytes from2432* the last block processing are dropped (therefore, "chunked" processing2433* works only as long as each non-final chunk has a length multiple of 64).2434*2435* \param key secret key (32 bytes).2436* \param iv IV (12 bytes).2437* \param cc initial counter value.2438* \param data data to encrypt or decrypt.2439* \param len data length (in bytes).2440*/2441typedef uint32_t (*br_chacha20_run)(const void *key,2442const void *iv, uint32_t cc, void *data, size_t len);24432444/**2445* \brief ChaCha20 implementation (straightforward C code, constant-time).2446*2447* \see br_chacha20_run2448*2449* \param key secret key (32 bytes).2450* \param iv IV (12 bytes).2451* \param cc initial counter value.2452* \param data data to encrypt or decrypt.2453* \param len data length (in bytes).2454*/2455uint32_t br_chacha20_ct_run(const void *key,2456const void *iv, uint32_t cc, void *data, size_t len);24572458/**2459* \brief ChaCha20 implementation (SSE2 code, constant-time).2460*2461* This implementation is available only on x86 platforms, depending on2462* compiler support. Moreover, in 32-bit mode, it might not actually run,2463* if the underlying hardware does not implement the SSE2 opcode (in2464* 64-bit mode, SSE2 is part of the ABI, so if the code could be compiled2465* at all, then it can run). Use `br_chacha20_sse2_get()` to safely obtain2466* a pointer to that function.2467*2468* \see br_chacha20_run2469*2470* \param key secret key (32 bytes).2471* \param iv IV (12 bytes).2472* \param cc initial counter value.2473* \param data data to encrypt or decrypt.2474* \param len data length (in bytes).2475*/2476uint32_t br_chacha20_sse2_run(const void *key,2477const void *iv, uint32_t cc, void *data, size_t len);24782479/**2480* \brief Obtain the `sse2` ChaCha20 implementation, if available.2481*2482* This function returns a pointer to `br_chacha20_sse2_run`, if2483* that implementation was compiled in the library _and_ the SSE22484* opcodes are available on the currently running CPU. If either of2485* these conditions is not met, then this function returns `0`.2486*2487* \return the `sse2` ChaCha20 implementation, or `0`.2488*/2489br_chacha20_run br_chacha20_sse2_get(void);24902491/**2492* \brief Type for a ChaCha20+Poly1305 AEAD implementation.2493*2494* The provided data is encrypted or decrypted with ChaCha20. The2495* authentication tag is computed on the concatenation of the2496* additional data and the ciphertext, with the padding and lengths2497* as described in RFC 7539 (section 2.8).2498*2499* After decryption, the caller is responsible for checking that the2500* computed tag matches the expected value.2501*2502* \param key secret key (32 bytes).2503* \param iv nonce (12 bytes).2504* \param data data to encrypt or decrypt.2505* \param len data length (in bytes).2506* \param aad additional authenticated data.2507* \param aad_len length of additional authenticated data (in bytes).2508* \param tag output buffer for the authentication tag.2509* \param ichacha implementation of ChaCha20.2510* \param encrypt non-zero for encryption, zero for decryption.2511*/2512typedef void (*br_poly1305_run)(const void *key, const void *iv,2513void *data, size_t len, const void *aad, size_t aad_len,2514void *tag, br_chacha20_run ichacha, int encrypt);25152516/**2517* \brief ChaCha20+Poly1305 AEAD implementation (mixed 32-bit multiplications).2518*2519* \see br_poly1305_run2520*2521* \param key secret key (32 bytes).2522* \param iv nonce (12 bytes).2523* \param data data to encrypt or decrypt.2524* \param len data length (in bytes).2525* \param aad additional authenticated data.2526* \param aad_len length of additional authenticated data (in bytes).2527* \param tag output buffer for the authentication tag.2528* \param ichacha implementation of ChaCha20.2529* \param encrypt non-zero for encryption, zero for decryption.2530*/2531void br_poly1305_ctmul_run(const void *key, const void *iv,2532void *data, size_t len, const void *aad, size_t aad_len,2533void *tag, br_chacha20_run ichacha, int encrypt);25342535/**2536* \brief ChaCha20+Poly1305 AEAD implementation (pure 32-bit multiplications).2537*2538* \see br_poly1305_run2539*2540* \param key secret key (32 bytes).2541* \param iv nonce (12 bytes).2542* \param data data to encrypt or decrypt.2543* \param len data length (in bytes).2544* \param aad additional authenticated data.2545* \param aad_len length of additional authenticated data (in bytes).2546* \param tag output buffer for the authentication tag.2547* \param ichacha implementation of ChaCha20.2548* \param encrypt non-zero for encryption, zero for decryption.2549*/2550void br_poly1305_ctmul32_run(const void *key, const void *iv,2551void *data, size_t len, const void *aad, size_t aad_len,2552void *tag, br_chacha20_run ichacha, int encrypt);25532554/**2555* \brief ChaCha20+Poly1305 AEAD implementation (i15).2556*2557* This implementation relies on the generic big integer code "i15"2558* (which uses pure 32-bit multiplications). As such, it may save a2559* little code footprint in a context where "i15" is already included2560* (e.g. for elliptic curves or for RSA); however, it is also2561* substantially slower than the ctmul and ctmul32 implementations.2562*2563* \see br_poly1305_run2564*2565* \param key secret key (32 bytes).2566* \param iv nonce (12 bytes).2567* \param data data to encrypt or decrypt.2568* \param len data length (in bytes).2569* \param aad additional authenticated data.2570* \param aad_len length of additional authenticated data (in bytes).2571* \param tag output buffer for the authentication tag.2572* \param ichacha implementation of ChaCha20.2573* \param encrypt non-zero for encryption, zero for decryption.2574*/2575void br_poly1305_i15_run(const void *key, const void *iv,2576void *data, size_t len, const void *aad, size_t aad_len,2577void *tag, br_chacha20_run ichacha, int encrypt);25782579/**2580* \brief ChaCha20+Poly1305 AEAD implementation (ctmulq).2581*2582* This implementation uses 64-bit multiplications (result over 128 bits).2583* It is available only on platforms that offer such a primitive (in2584* practice, 64-bit architectures). Use `br_poly1305_ctmulq_get()` to2585* dynamically obtain a pointer to that function, or 0 if not supported.2586*2587* \see br_poly1305_run2588*2589* \param key secret key (32 bytes).2590* \param iv nonce (12 bytes).2591* \param data data to encrypt or decrypt.2592* \param len data length (in bytes).2593* \param aad additional authenticated data.2594* \param aad_len length of additional authenticated data (in bytes).2595* \param tag output buffer for the authentication tag.2596* \param ichacha implementation of ChaCha20.2597* \param encrypt non-zero for encryption, zero for decryption.2598*/2599void br_poly1305_ctmulq_run(const void *key, const void *iv,2600void *data, size_t len, const void *aad, size_t aad_len,2601void *tag, br_chacha20_run ichacha, int encrypt);26022603/**2604* \brief Get the ChaCha20+Poly1305 "ctmulq" implementation, if available.2605*2606* This function returns a pointer to the `br_poly1305_ctmulq_run()`2607* function if supported on the current platform; otherwise, it returns 0.2608*2609* \return the ctmulq ChaCha20+Poly1305 implementation, or 0.2610*/2611br_poly1305_run br_poly1305_ctmulq_get(void);26122613#ifdef __cplusplus2614}2615#endif26162617#endif261826192620