1*0957b409SSimon J. Gerraty /* 2*0957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org> 3*0957b409SSimon J. Gerraty * 4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining 5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the 6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including 7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish, 8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to 9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to 10*0957b409SSimon J. Gerraty * the following conditions: 11*0957b409SSimon J. Gerraty * 12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be 13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software. 14*0957b409SSimon J. Gerraty * 15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*0957b409SSimon J. Gerraty * SOFTWARE. 23*0957b409SSimon J. Gerraty */ 24*0957b409SSimon J. Gerraty 25*0957b409SSimon J. Gerraty #ifndef BR_BEARSSL_BLOCK_H__ 26*0957b409SSimon J. Gerraty #define BR_BEARSSL_BLOCK_H__ 27*0957b409SSimon J. Gerraty 28*0957b409SSimon J. Gerraty #include <stddef.h> 29*0957b409SSimon J. Gerraty #include <stdint.h> 30*0957b409SSimon J. Gerraty 31*0957b409SSimon J. Gerraty #ifdef __cplusplus 32*0957b409SSimon J. Gerraty extern "C" { 33*0957b409SSimon J. Gerraty #endif 34*0957b409SSimon J. Gerraty 35*0957b409SSimon J. Gerraty /** \file bearssl_block.h 36*0957b409SSimon J. Gerraty * 37*0957b409SSimon J. Gerraty * # Block Ciphers and Symmetric Ciphers 38*0957b409SSimon J. Gerraty * 39*0957b409SSimon J. Gerraty * This file documents the API for block ciphers and other symmetric 40*0957b409SSimon J. Gerraty * ciphers. 41*0957b409SSimon J. Gerraty * 42*0957b409SSimon J. Gerraty * 43*0957b409SSimon J. Gerraty * ## Procedural API 44*0957b409SSimon J. Gerraty * 45*0957b409SSimon J. Gerraty * For a block cipher implementation, up to three separate sets of 46*0957b409SSimon J. Gerraty * functions are provided, for CBC encryption, CBC decryption, and CTR 47*0957b409SSimon J. Gerraty * encryption/decryption. Each set has its own context structure, 48*0957b409SSimon J. Gerraty * initialised with the encryption key. 49*0957b409SSimon J. Gerraty * 50*0957b409SSimon J. Gerraty * For CBC encryption and decryption, the data to encrypt or decrypt is 51*0957b409SSimon J. Gerraty * referenced as a sequence of blocks. The implementations assume that 52*0957b409SSimon J. Gerraty * there is no partial block; no padding is applied or removed. The 53*0957b409SSimon J. Gerraty * caller is responsible for handling any kind of padding. 54*0957b409SSimon J. Gerraty * 55*0957b409SSimon J. Gerraty * Function for CTR encryption are defined only for block ciphers with 56*0957b409SSimon J. Gerraty * blocks of 16 bytes or more (i.e. AES, but not DES/3DES). 57*0957b409SSimon J. Gerraty * 58*0957b409SSimon J. Gerraty * Each implemented block cipher is identified by an "internal name" 59*0957b409SSimon J. Gerraty * from which are derived the names of structures and functions that 60*0957b409SSimon J. Gerraty * implement the cipher. For the block cipher of internal name "`xxx`", 61*0957b409SSimon J. Gerraty * the following are defined: 62*0957b409SSimon J. Gerraty * 63*0957b409SSimon J. Gerraty * - `br_xxx_BLOCK_SIZE` 64*0957b409SSimon J. Gerraty * 65*0957b409SSimon J. Gerraty * A macro that evaluates to the block size (in bytes) of the 66*0957b409SSimon J. Gerraty * cipher. For all implemented block ciphers, this value is a 67*0957b409SSimon J. Gerraty * power of two. 68*0957b409SSimon J. Gerraty * 69*0957b409SSimon J. Gerraty * - `br_xxx_cbcenc_keys` 70*0957b409SSimon J. Gerraty * 71*0957b409SSimon J. Gerraty * Context structure that contains the subkeys resulting from the key 72*0957b409SSimon J. Gerraty * expansion. These subkeys are appropriate for CBC encryption. The 73*0957b409SSimon J. Gerraty * structure first field is called `vtable` and points to the 74*0957b409SSimon J. Gerraty * appropriate OOP structure. 75*0957b409SSimon J. Gerraty * 76*0957b409SSimon J. Gerraty * - `br_xxx_cbcenc_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)` 77*0957b409SSimon J. Gerraty * 78*0957b409SSimon J. Gerraty * Perform key expansion: subkeys for CBC encryption are computed and 79*0957b409SSimon J. Gerraty * written in the provided context structure. The key length MUST be 80*0957b409SSimon J. Gerraty * adequate for the implemented block cipher. This function also sets 81*0957b409SSimon J. Gerraty * the `vtable` field. 82*0957b409SSimon J. Gerraty * 83*0957b409SSimon J. Gerraty * - `br_xxx_cbcenc_run(const br_xxx_cbcenc_keys *ctx, void *iv, void *data, size_t len)` 84*0957b409SSimon J. Gerraty * 85*0957b409SSimon J. Gerraty * Perform CBC encryption of `len` bytes, in place. The encrypted data 86*0957b409SSimon J. Gerraty * replaces the cleartext. `len` MUST be a multiple of the block length 87*0957b409SSimon J. Gerraty * (if it is not, the function may loop forever or overflow a buffer). 88*0957b409SSimon J. Gerraty * The IV is provided with the `iv` pointer; it is also updated with 89*0957b409SSimon J. Gerraty * a copy of the last encrypted block. 90*0957b409SSimon J. Gerraty * 91*0957b409SSimon J. Gerraty * - `br_xxx_cbcdec_keys` 92*0957b409SSimon J. Gerraty * 93*0957b409SSimon J. Gerraty * Context structure that contains the subkeys resulting from the key 94*0957b409SSimon J. Gerraty * expansion. These subkeys are appropriate for CBC decryption. The 95*0957b409SSimon J. Gerraty * structure first field is called `vtable` and points to the 96*0957b409SSimon J. Gerraty * appropriate OOP structure. 97*0957b409SSimon J. Gerraty * 98*0957b409SSimon J. Gerraty * - `br_xxx_cbcdec_init(br_xxx_cbcenc_keys *ctx, const void *key, size_t len)` 99*0957b409SSimon J. Gerraty * 100*0957b409SSimon J. Gerraty * Perform key expansion: subkeys for CBC decryption are computed and 101*0957b409SSimon J. Gerraty * written in the provided context structure. The key length MUST be 102*0957b409SSimon J. Gerraty * adequate for the implemented block cipher. This function also sets 103*0957b409SSimon J. Gerraty * the `vtable` field. 104*0957b409SSimon J. Gerraty * 105*0957b409SSimon J. Gerraty * - `br_xxx_cbcdec_run(const br_xxx_cbcdec_keys *ctx, void *iv, void *data, size_t num_blocks)` 106*0957b409SSimon J. Gerraty * 107*0957b409SSimon J. Gerraty * Perform CBC decryption of `len` bytes, in place. The decrypted data 108*0957b409SSimon J. Gerraty * replaces the ciphertext. `len` MUST be a multiple of the block length 109*0957b409SSimon J. Gerraty * (if it is not, the function may loop forever or overflow a buffer). 110*0957b409SSimon J. Gerraty * The IV is provided with the `iv` pointer; it is also updated with 111*0957b409SSimon J. Gerraty * a copy of the last _encrypted_ block. 112*0957b409SSimon J. Gerraty * 113*0957b409SSimon J. Gerraty * - `br_xxx_ctr_keys` 114*0957b409SSimon J. Gerraty * 115*0957b409SSimon J. Gerraty * Context structure that contains the subkeys resulting from the key 116*0957b409SSimon J. Gerraty * expansion. These subkeys are appropriate for CTR encryption and 117*0957b409SSimon J. Gerraty * decryption. The structure first field is called `vtable` and 118*0957b409SSimon J. Gerraty * points to the appropriate OOP structure. 119*0957b409SSimon J. Gerraty * 120*0957b409SSimon J. Gerraty * - `br_xxx_ctr_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)` 121*0957b409SSimon J. Gerraty * 122*0957b409SSimon J. Gerraty * Perform key expansion: subkeys for CTR encryption and decryption 123*0957b409SSimon J. Gerraty * are computed and written in the provided context structure. The 124*0957b409SSimon J. Gerraty * key length MUST be adequate for the implemented block cipher. This 125*0957b409SSimon J. Gerraty * function also sets the `vtable` field. 126*0957b409SSimon J. Gerraty * 127*0957b409SSimon J. Gerraty * - `br_xxx_ctr_run(const br_xxx_ctr_keys *ctx, const void *iv, uint32_t cc, void *data, size_t len)` (returns `uint32_t`) 128*0957b409SSimon J. Gerraty * 129*0957b409SSimon J. Gerraty * Perform CTR encryption/decryption of some data. Processing is done 130*0957b409SSimon J. Gerraty * "in place" (the output data replaces the input data). This function 131*0957b409SSimon J. Gerraty * implements the "standard incrementing function" from NIST SP800-38A, 132*0957b409SSimon J. Gerraty * annex B: the IV length shall be 4 bytes less than the block size 133*0957b409SSimon J. Gerraty * (i.e. 12 bytes for AES) and the counter is the 32-bit value starting 134*0957b409SSimon J. Gerraty * with `cc`. The data length (`len`) is not necessarily a multiple of 135*0957b409SSimon J. Gerraty * the block size. The new counter value is returned, which supports 136*0957b409SSimon J. Gerraty * chunked processing, provided that each chunk length (except possibly 137*0957b409SSimon J. Gerraty * the last one) is a multiple of the block size. 138*0957b409SSimon J. Gerraty * 139*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_keys` 140*0957b409SSimon J. Gerraty * 141*0957b409SSimon J. Gerraty * Context structure that contains the subkeys resulting from the 142*0957b409SSimon J. Gerraty * key expansion. These subkeys are appropriate for doing combined 143*0957b409SSimon J. Gerraty * CTR encryption/decryption and CBC-MAC, as used in the CCM and EAX 144*0957b409SSimon J. Gerraty * authenticated encryption modes. The structure first field is 145*0957b409SSimon J. Gerraty * called `vtable` and points to the appropriate OOP structure. 146*0957b409SSimon J. Gerraty * 147*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_init(br_xxx_ctr_keys *ctx, const void *key, size_t len)` 148*0957b409SSimon J. Gerraty * 149*0957b409SSimon J. Gerraty * Perform key expansion: subkeys for combined CTR 150*0957b409SSimon J. Gerraty * encryption/decryption and CBC-MAC are computed and written in the 151*0957b409SSimon J. Gerraty * provided context structure. The key length MUST be adequate for 152*0957b409SSimon J. Gerraty * the implemented block cipher. This function also sets the 153*0957b409SSimon J. Gerraty * `vtable` field. 154*0957b409SSimon J. Gerraty * 155*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_encrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)` 156*0957b409SSimon J. Gerraty * 157*0957b409SSimon J. Gerraty * Perform CTR encryption of some data, and CBC-MAC. Processing is 158*0957b409SSimon J. Gerraty * done "in place" (the output data replaces the input data). This 159*0957b409SSimon J. Gerraty * function applies CTR encryption on the data, using a full 160*0957b409SSimon J. Gerraty * block-size counter (i.e. for 128-bit blocks, the counter is 161*0957b409SSimon J. Gerraty * incremented as a 128-bit value). The 'ctr' array contains the 162*0957b409SSimon J. Gerraty * initial value for the counter (used in the first block) and it is 163*0957b409SSimon J. Gerraty * updated with the new value after data processing. The 'cbcmac' 164*0957b409SSimon J. Gerraty * value shall point to a block-sized value which is used as IV for 165*0957b409SSimon J. Gerraty * CBC-MAC, computed over the encrypted data (output of CTR 166*0957b409SSimon J. Gerraty * encryption); the resulting CBC-MAC is written over 'cbcmac' on 167*0957b409SSimon J. Gerraty * output. 168*0957b409SSimon J. Gerraty * 169*0957b409SSimon J. Gerraty * The data length MUST be a multiple of the block size. 170*0957b409SSimon J. Gerraty * 171*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_decrypt(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *cbcmac, void *data, size_t len)` 172*0957b409SSimon J. Gerraty * 173*0957b409SSimon J. Gerraty * Perform CTR decryption of some data, and CBC-MAC. Processing is 174*0957b409SSimon J. Gerraty * done "in place" (the output data replaces the input data). This 175*0957b409SSimon J. Gerraty * function applies CTR decryption on the data, using a full 176*0957b409SSimon J. Gerraty * block-size counter (i.e. for 128-bit blocks, the counter is 177*0957b409SSimon J. Gerraty * incremented as a 128-bit value). The 'ctr' array contains the 178*0957b409SSimon J. Gerraty * initial value for the counter (used in the first block) and it is 179*0957b409SSimon J. Gerraty * updated with the new value after data processing. The 'cbcmac' 180*0957b409SSimon J. Gerraty * value shall point to a block-sized value which is used as IV for 181*0957b409SSimon J. Gerraty * CBC-MAC, computed over the encrypted data (input of CTR 182*0957b409SSimon J. Gerraty * encryption); the resulting CBC-MAC is written over 'cbcmac' on 183*0957b409SSimon J. Gerraty * output. 184*0957b409SSimon J. Gerraty * 185*0957b409SSimon J. Gerraty * The data length MUST be a multiple of the block size. 186*0957b409SSimon J. Gerraty * 187*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_ctr(const br_xxx_ctrcbc_keys *ctx, void *ctr, void *data, size_t len)` 188*0957b409SSimon J. Gerraty * 189*0957b409SSimon J. Gerraty * Perform CTR encryption or decryption of the provided data. The 190*0957b409SSimon J. Gerraty * data is processed "in place" (the output data replaces the input 191*0957b409SSimon J. Gerraty * data). A full block-sized counter is applied (i.e. for 128-bit 192*0957b409SSimon J. Gerraty * blocks, the counter is incremented as a 128-bit value). The 'ctr' 193*0957b409SSimon J. Gerraty * array contains the initial value for the counter (used in the 194*0957b409SSimon J. Gerraty * first block), and it is updated with the new value after data 195*0957b409SSimon J. Gerraty * processing. 196*0957b409SSimon J. Gerraty * 197*0957b409SSimon J. Gerraty * The data length MUST be a multiple of the block size. 198*0957b409SSimon J. Gerraty * 199*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_mac(const br_xxx_ctrcbc_keys *ctx, void *cbcmac, const void *data, size_t len)` 200*0957b409SSimon J. Gerraty * 201*0957b409SSimon J. Gerraty * Compute CBC-MAC over the provided data. The IV for CBC-MAC is 202*0957b409SSimon J. Gerraty * provided as 'cbcmac'; the output is written over the same array. 203*0957b409SSimon J. Gerraty * The data itself is untouched. The data length MUST be a multiple 204*0957b409SSimon J. Gerraty * of the block size. 205*0957b409SSimon J. Gerraty * 206*0957b409SSimon J. Gerraty * 207*0957b409SSimon J. Gerraty * It shall be noted that the key expansion functions return `void`. If 208*0957b409SSimon J. Gerraty * the provided key length is not allowed, then there will be no error 209*0957b409SSimon J. Gerraty * reporting; implementations need not validate the key length, thus an 210*0957b409SSimon J. Gerraty * invalid key length may result in undefined behaviour (e.g. buffer 211*0957b409SSimon J. Gerraty * overflow). 212*0957b409SSimon J. Gerraty * 213*0957b409SSimon J. Gerraty * Subkey structures contain no interior pointer, and no external 214*0957b409SSimon J. Gerraty * resources are allocated upon key expansion. They can thus be 215*0957b409SSimon J. Gerraty * discarded without any explicit deallocation. 216*0957b409SSimon J. Gerraty * 217*0957b409SSimon J. Gerraty * 218*0957b409SSimon J. Gerraty * ## Object-Oriented API 219*0957b409SSimon J. Gerraty * 220*0957b409SSimon J. Gerraty * Each context structure begins with a field (called `vtable`) that 221*0957b409SSimon J. Gerraty * points to an instance of a structure that references the relevant 222*0957b409SSimon J. Gerraty * functions through pointers. Each such structure contains the 223*0957b409SSimon J. Gerraty * following: 224*0957b409SSimon J. Gerraty * 225*0957b409SSimon J. Gerraty * - `context_size` 226*0957b409SSimon J. Gerraty * 227*0957b409SSimon J. Gerraty * The size (in bytes) of the context structure for subkeys. 228*0957b409SSimon J. Gerraty * 229*0957b409SSimon J. Gerraty * - `block_size` 230*0957b409SSimon J. Gerraty * 231*0957b409SSimon J. Gerraty * The cipher block size (in bytes). 232*0957b409SSimon J. Gerraty * 233*0957b409SSimon J. Gerraty * - `log_block_size` 234*0957b409SSimon J. Gerraty * 235*0957b409SSimon J. Gerraty * The base-2 logarithm of cipher block size (e.g. 4 for blocks 236*0957b409SSimon J. Gerraty * of 16 bytes). 237*0957b409SSimon J. Gerraty * 238*0957b409SSimon J. Gerraty * - `init` 239*0957b409SSimon J. Gerraty * 240*0957b409SSimon J. Gerraty * Pointer to the key expansion function. 241*0957b409SSimon J. Gerraty * 242*0957b409SSimon J. Gerraty * - `run` 243*0957b409SSimon J. Gerraty * 244*0957b409SSimon J. Gerraty * Pointer to the encryption/decryption function. 245*0957b409SSimon J. Gerraty * 246*0957b409SSimon J. Gerraty * For combined CTR/CBC-MAC encryption, the `vtable` has a slightly 247*0957b409SSimon J. Gerraty * different structure: 248*0957b409SSimon J. Gerraty * 249*0957b409SSimon J. Gerraty * - `context_size` 250*0957b409SSimon J. Gerraty * 251*0957b409SSimon J. Gerraty * The size (in bytes) of the context structure for subkeys. 252*0957b409SSimon J. Gerraty * 253*0957b409SSimon J. Gerraty * - `block_size` 254*0957b409SSimon J. Gerraty * 255*0957b409SSimon J. Gerraty * The cipher block size (in bytes). 256*0957b409SSimon J. Gerraty * 257*0957b409SSimon J. Gerraty * - `log_block_size` 258*0957b409SSimon J. Gerraty * 259*0957b409SSimon J. Gerraty * The base-2 logarithm of cipher block size (e.g. 4 for blocks 260*0957b409SSimon J. Gerraty * of 16 bytes). 261*0957b409SSimon J. Gerraty * 262*0957b409SSimon J. Gerraty * - `init` 263*0957b409SSimon J. Gerraty * 264*0957b409SSimon J. Gerraty * Pointer to the key expansion function. 265*0957b409SSimon J. Gerraty * 266*0957b409SSimon J. Gerraty * - `encrypt` 267*0957b409SSimon J. Gerraty * 268*0957b409SSimon J. Gerraty * Pointer to the CTR encryption + CBC-MAC function. 269*0957b409SSimon J. Gerraty * 270*0957b409SSimon J. Gerraty * - `decrypt` 271*0957b409SSimon J. Gerraty * 272*0957b409SSimon J. Gerraty * Pointer to the CTR decryption + CBC-MAC function. 273*0957b409SSimon J. Gerraty * 274*0957b409SSimon J. Gerraty * - `ctr` 275*0957b409SSimon J. Gerraty * 276*0957b409SSimon J. Gerraty * Pointer to the CTR encryption/decryption function. 277*0957b409SSimon J. Gerraty * 278*0957b409SSimon J. Gerraty * - `mac` 279*0957b409SSimon J. Gerraty * 280*0957b409SSimon J. Gerraty * Pointer to the CBC-MAC function. 281*0957b409SSimon J. Gerraty * 282*0957b409SSimon J. Gerraty * For block cipher "`xxx`", static, constant instances of these 283*0957b409SSimon J. Gerraty * structures are defined, under the names: 284*0957b409SSimon J. Gerraty * 285*0957b409SSimon J. Gerraty * - `br_xxx_cbcenc_vtable` 286*0957b409SSimon J. Gerraty * - `br_xxx_cbcdec_vtable` 287*0957b409SSimon J. Gerraty * - `br_xxx_ctr_vtable` 288*0957b409SSimon J. Gerraty * - `br_xxx_ctrcbc_vtable` 289*0957b409SSimon J. Gerraty * 290*0957b409SSimon J. Gerraty * 291*0957b409SSimon J. Gerraty * ## Implemented Block Ciphers 292*0957b409SSimon J. Gerraty * 293*0957b409SSimon J. Gerraty * Provided implementations are: 294*0957b409SSimon J. Gerraty * 295*0957b409SSimon J. Gerraty * | Name | Function | Block Size (bytes) | Key lengths (bytes) | 296*0957b409SSimon J. Gerraty * | :-------- | :------- | :----------------: | :-----------------: | 297*0957b409SSimon J. Gerraty * | aes_big | AES | 16 | 16, 24 and 32 | 298*0957b409SSimon J. Gerraty * | aes_small | AES | 16 | 16, 24 and 32 | 299*0957b409SSimon J. Gerraty * | aes_ct | AES | 16 | 16, 24 and 32 | 300*0957b409SSimon J. Gerraty * | aes_ct64 | AES | 16 | 16, 24 and 32 | 301*0957b409SSimon J. Gerraty * | aes_x86ni | AES | 16 | 16, 24 and 32 | 302*0957b409SSimon J. Gerraty * | aes_pwr8 | AES | 16 | 16, 24 and 32 | 303*0957b409SSimon J. Gerraty * | des_ct | DES/3DES | 8 | 8, 16 and 24 | 304*0957b409SSimon J. Gerraty * | des_tab | DES/3DES | 8 | 8, 16 and 24 | 305*0957b409SSimon J. Gerraty * 306*0957b409SSimon J. Gerraty * **Note:** DES/3DES nominally uses keys of 64, 128 and 192 bits (i.e. 8, 307*0957b409SSimon J. Gerraty * 16 and 24 bytes), but some of the bits are ignored by the algorithm, so 308*0957b409SSimon J. Gerraty * the _effective_ key lengths, from a security point of view, are 56, 309*0957b409SSimon J. Gerraty * 112 and 168 bits, respectively. 310*0957b409SSimon J. Gerraty * 311*0957b409SSimon J. Gerraty * `aes_big` is a "classical" AES implementation, using tables. It 312*0957b409SSimon J. Gerraty * is fast but not constant-time, since it makes data-dependent array 313*0957b409SSimon J. Gerraty * accesses. 314*0957b409SSimon J. Gerraty * 315*0957b409SSimon J. Gerraty * `aes_small` is an AES implementation optimized for code size. It 316*0957b409SSimon J. Gerraty * is substantially slower than `aes_big`; it is not constant-time 317*0957b409SSimon J. Gerraty * either. 318*0957b409SSimon J. Gerraty * 319*0957b409SSimon J. Gerraty * `aes_ct` is a constant-time implementation of AES; its code is about 320*0957b409SSimon J. Gerraty * as big as that of `aes_big`, while its performance is comparable to 321*0957b409SSimon J. Gerraty * that of `aes_small`. However, it is constant-time. This 322*0957b409SSimon J. Gerraty * implementation should thus be considered to be the "default" AES in 323*0957b409SSimon J. Gerraty * BearSSL, to be used unless the operational context guarantees that a 324*0957b409SSimon J. Gerraty * non-constant-time implementation is safe, or an architecture-specific 325*0957b409SSimon J. Gerraty * constant-time implementation can be used (e.g. using dedicated 326*0957b409SSimon J. Gerraty * hardware opcodes). 327*0957b409SSimon J. Gerraty * 328*0957b409SSimon J. Gerraty * `aes_ct64` is another constant-time implementation of AES. It is 329*0957b409SSimon J. Gerraty * similar to `aes_ct` but uses 64-bit values. On 32-bit machines, 330*0957b409SSimon J. Gerraty * `aes_ct64` is not faster than `aes_ct`, often a bit slower, and has 331*0957b409SSimon J. Gerraty * a larger footprint; however, on 64-bit architectures, `aes_ct64` 332*0957b409SSimon J. Gerraty * is typically twice faster than `aes_ct` for modes that allow parallel 333*0957b409SSimon J. Gerraty * operations (i.e. CTR, and CBC decryption, but not CBC encryption). 334*0957b409SSimon J. Gerraty * 335*0957b409SSimon J. Gerraty * `aes_x86ni` exists only on x86 architectures (32-bit and 64-bit). It 336*0957b409SSimon J. Gerraty * uses the AES-NI opcodes when available. 337*0957b409SSimon J. Gerraty * 338*0957b409SSimon J. Gerraty * `aes_pwr8` exists only on PowerPC / POWER architectures (32-bit and 339*0957b409SSimon J. Gerraty * 64-bit, both little-endian and big-endian). It uses the AES opcodes 340*0957b409SSimon J. Gerraty * present in POWER8 and later. 341*0957b409SSimon J. Gerraty * 342*0957b409SSimon J. Gerraty * `des_tab` is a classic, table-based implementation of DES/3DES. It 343*0957b409SSimon J. Gerraty * is not constant-time. 344*0957b409SSimon J. Gerraty * 345*0957b409SSimon J. Gerraty * `des_ct` is an constant-time implementation of DES/3DES. It is 346*0957b409SSimon J. Gerraty * substantially slower than `des_tab`. 347*0957b409SSimon J. Gerraty * 348*0957b409SSimon J. Gerraty * ## ChaCha20 and Poly1305 349*0957b409SSimon J. Gerraty * 350*0957b409SSimon J. Gerraty * ChaCha20 is a stream cipher. Poly1305 is a MAC algorithm. They 351*0957b409SSimon J. Gerraty * are described in [RFC 7539](https://tools.ietf.org/html/rfc7539). 352*0957b409SSimon J. Gerraty * 353*0957b409SSimon J. Gerraty * Two function pointer types are defined: 354*0957b409SSimon J. Gerraty * 355*0957b409SSimon J. Gerraty * - `br_chacha20_run` describes a function that implements ChaCha20 356*0957b409SSimon J. Gerraty * only. 357*0957b409SSimon J. Gerraty * 358*0957b409SSimon J. Gerraty * - `br_poly1305_run` describes an implementation of Poly1305, 359*0957b409SSimon J. Gerraty * in the AEAD combination with ChaCha20 specified in RFC 7539 360*0957b409SSimon J. Gerraty * (the ChaCha20 implementation is provided as a function pointer). 361*0957b409SSimon J. Gerraty * 362*0957b409SSimon J. Gerraty * `chacha20_ct` is a straightforward implementation of ChaCha20 in 363*0957b409SSimon J. Gerraty * plain C; it is constant-time, small, and reasonably fast. 364*0957b409SSimon J. Gerraty * 365*0957b409SSimon J. Gerraty * `chacha20_sse2` leverages SSE2 opcodes (on x86 architectures that 366*0957b409SSimon J. Gerraty * support these opcodes). It is faster than `chacha20_ct`. 367*0957b409SSimon J. Gerraty * 368*0957b409SSimon J. Gerraty * `poly1305_ctmul` is an implementation of the ChaCha20+Poly1305 AEAD 369*0957b409SSimon J. Gerraty * construction, where the Poly1305 part is performed with mixed 32-bit 370*0957b409SSimon J. Gerraty * multiplications (operands are 32-bit, result is 64-bit). 371*0957b409SSimon J. Gerraty * 372*0957b409SSimon J. Gerraty * `poly1305_ctmul32` implements ChaCha20+Poly1305 using pure 32-bit 373*0957b409SSimon J. Gerraty * multiplications (32-bit operands, 32-bit result). It is slower than 374*0957b409SSimon J. Gerraty * `poly1305_ctmul`, except on some specific architectures such as 375*0957b409SSimon J. Gerraty * the ARM Cortex M0+. 376*0957b409SSimon J. Gerraty * 377*0957b409SSimon J. Gerraty * `poly1305_ctmulq` implements ChaCha20+Poly1305 with mixed 64-bit 378*0957b409SSimon J. Gerraty * multiplications (operands are 64-bit, result is 128-bit) on 64-bit 379*0957b409SSimon J. Gerraty * platforms that support such operations. 380*0957b409SSimon J. Gerraty * 381*0957b409SSimon J. Gerraty * `poly1305_i15` implements ChaCha20+Poly1305 with the generic "i15" 382*0957b409SSimon J. Gerraty * big integer implementation. It is meant mostly for testing purposes, 383*0957b409SSimon J. Gerraty * although it can help with saving a few hundred bytes of code footprint 384*0957b409SSimon J. Gerraty * on systems where code size is scarce. 385*0957b409SSimon J. Gerraty */ 386*0957b409SSimon J. Gerraty 387*0957b409SSimon J. Gerraty /** 388*0957b409SSimon J. Gerraty * \brief Class type for CBC encryption implementations. 389*0957b409SSimon J. Gerraty * 390*0957b409SSimon J. Gerraty * A `br_block_cbcenc_class` instance points to the functions implementing 391*0957b409SSimon J. Gerraty * a specific block cipher, when used in CBC mode for encrypting data. 392*0957b409SSimon J. Gerraty */ 393*0957b409SSimon J. Gerraty typedef struct br_block_cbcenc_class_ br_block_cbcenc_class; 394*0957b409SSimon J. Gerraty struct br_block_cbcenc_class_ { 395*0957b409SSimon J. Gerraty /** 396*0957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate 397*0957b409SSimon J. Gerraty * for containing subkeys. 398*0957b409SSimon J. Gerraty */ 399*0957b409SSimon J. Gerraty size_t context_size; 400*0957b409SSimon J. Gerraty 401*0957b409SSimon J. Gerraty /** 402*0957b409SSimon J. Gerraty * \brief Size of individual blocks (in bytes). 403*0957b409SSimon J. Gerraty */ 404*0957b409SSimon J. Gerraty unsigned block_size; 405*0957b409SSimon J. Gerraty 406*0957b409SSimon J. Gerraty /** 407*0957b409SSimon J. Gerraty * \brief Base-2 logarithm of the size of individual blocks, 408*0957b409SSimon J. Gerraty * expressed in bytes. 409*0957b409SSimon J. Gerraty */ 410*0957b409SSimon J. Gerraty unsigned log_block_size; 411*0957b409SSimon J. Gerraty 412*0957b409SSimon J. Gerraty /** 413*0957b409SSimon J. Gerraty * \brief Initialisation function. 414*0957b409SSimon J. Gerraty * 415*0957b409SSimon J. Gerraty * This function sets the `vtable` field in the context structure. 416*0957b409SSimon J. Gerraty * The key length MUST be one of the key lengths supported by 417*0957b409SSimon J. Gerraty * the implementation. 418*0957b409SSimon J. Gerraty * 419*0957b409SSimon J. Gerraty * \param ctx context structure to initialise. 420*0957b409SSimon J. Gerraty * \param key secret key. 421*0957b409SSimon J. Gerraty * \param key_len key length (in bytes). 422*0957b409SSimon J. Gerraty */ 423*0957b409SSimon J. Gerraty void (*init)(const br_block_cbcenc_class **ctx, 424*0957b409SSimon J. Gerraty const void *key, size_t key_len); 425*0957b409SSimon J. Gerraty 426*0957b409SSimon J. Gerraty /** 427*0957b409SSimon J. Gerraty * \brief Run the CBC encryption. 428*0957b409SSimon J. Gerraty * 429*0957b409SSimon J. Gerraty * The `iv` parameter points to the IV for this run; it is 430*0957b409SSimon J. Gerraty * updated with a copy of the last encrypted block. The data 431*0957b409SSimon J. Gerraty * is encrypted "in place"; its length (`len`) MUST be a 432*0957b409SSimon J. Gerraty * multiple of the block size. 433*0957b409SSimon J. Gerraty * 434*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 435*0957b409SSimon J. Gerraty * \param iv IV for CBC encryption (updated). 436*0957b409SSimon J. Gerraty * \param data data to encrypt. 437*0957b409SSimon J. Gerraty * \param len data length (in bytes, multiple of block size). 438*0957b409SSimon J. Gerraty */ 439*0957b409SSimon J. Gerraty void (*run)(const br_block_cbcenc_class *const *ctx, 440*0957b409SSimon J. Gerraty void *iv, void *data, size_t len); 441*0957b409SSimon J. Gerraty }; 442*0957b409SSimon J. Gerraty 443*0957b409SSimon J. Gerraty /** 444*0957b409SSimon J. Gerraty * \brief Class type for CBC decryption implementations. 445*0957b409SSimon J. Gerraty * 446*0957b409SSimon J. Gerraty * A `br_block_cbcdec_class` instance points to the functions implementing 447*0957b409SSimon J. Gerraty * a specific block cipher, when used in CBC mode for decrypting data. 448*0957b409SSimon J. Gerraty */ 449*0957b409SSimon J. Gerraty typedef struct br_block_cbcdec_class_ br_block_cbcdec_class; 450*0957b409SSimon J. Gerraty struct br_block_cbcdec_class_ { 451*0957b409SSimon J. Gerraty /** 452*0957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate 453*0957b409SSimon J. Gerraty * for containing subkeys. 454*0957b409SSimon J. Gerraty */ 455*0957b409SSimon J. Gerraty size_t context_size; 456*0957b409SSimon J. Gerraty 457*0957b409SSimon J. Gerraty /** 458*0957b409SSimon J. Gerraty * \brief Size of individual blocks (in bytes). 459*0957b409SSimon J. Gerraty */ 460*0957b409SSimon J. Gerraty unsigned block_size; 461*0957b409SSimon J. Gerraty 462*0957b409SSimon J. Gerraty /** 463*0957b409SSimon J. Gerraty * \brief Base-2 logarithm of the size of individual blocks, 464*0957b409SSimon J. Gerraty * expressed in bytes. 465*0957b409SSimon J. Gerraty */ 466*0957b409SSimon J. Gerraty unsigned log_block_size; 467*0957b409SSimon J. Gerraty 468*0957b409SSimon J. Gerraty /** 469*0957b409SSimon J. Gerraty * \brief Initialisation function. 470*0957b409SSimon J. Gerraty * 471*0957b409SSimon J. Gerraty * This function sets the `vtable` field in the context structure. 472*0957b409SSimon J. Gerraty * The key length MUST be one of the key lengths supported by 473*0957b409SSimon J. Gerraty * the implementation. 474*0957b409SSimon J. Gerraty * 475*0957b409SSimon J. Gerraty * \param ctx context structure to initialise. 476*0957b409SSimon J. Gerraty * \param key secret key. 477*0957b409SSimon J. Gerraty * \param key_len key length (in bytes). 478*0957b409SSimon J. Gerraty */ 479*0957b409SSimon J. Gerraty void (*init)(const br_block_cbcdec_class **ctx, 480*0957b409SSimon J. Gerraty const void *key, size_t key_len); 481*0957b409SSimon J. Gerraty 482*0957b409SSimon J. Gerraty /** 483*0957b409SSimon J. Gerraty * \brief Run the CBC decryption. 484*0957b409SSimon J. Gerraty * 485*0957b409SSimon J. Gerraty * The `iv` parameter points to the IV for this run; it is 486*0957b409SSimon J. Gerraty * updated with a copy of the last encrypted block. The data 487*0957b409SSimon J. Gerraty * is decrypted "in place"; its length (`len`) MUST be a 488*0957b409SSimon J. Gerraty * multiple of the block size. 489*0957b409SSimon J. Gerraty * 490*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 491*0957b409SSimon J. Gerraty * \param iv IV for CBC decryption (updated). 492*0957b409SSimon J. Gerraty * \param data data to decrypt. 493*0957b409SSimon J. Gerraty * \param len data length (in bytes, multiple of block size). 494*0957b409SSimon J. Gerraty */ 495*0957b409SSimon J. Gerraty void (*run)(const br_block_cbcdec_class *const *ctx, 496*0957b409SSimon J. Gerraty void *iv, void *data, size_t len); 497*0957b409SSimon J. Gerraty }; 498*0957b409SSimon J. Gerraty 499*0957b409SSimon J. Gerraty /** 500*0957b409SSimon J. Gerraty * \brief Class type for CTR encryption/decryption implementations. 501*0957b409SSimon J. Gerraty * 502*0957b409SSimon J. Gerraty * A `br_block_ctr_class` instance points to the functions implementing 503*0957b409SSimon J. Gerraty * a specific block cipher, when used in CTR mode for encrypting or 504*0957b409SSimon J. Gerraty * decrypting data. 505*0957b409SSimon J. Gerraty */ 506*0957b409SSimon J. Gerraty typedef struct br_block_ctr_class_ br_block_ctr_class; 507*0957b409SSimon J. Gerraty struct br_block_ctr_class_ { 508*0957b409SSimon J. Gerraty /** 509*0957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate 510*0957b409SSimon J. Gerraty * for containing subkeys. 511*0957b409SSimon J. Gerraty */ 512*0957b409SSimon J. Gerraty size_t context_size; 513*0957b409SSimon J. Gerraty 514*0957b409SSimon J. Gerraty /** 515*0957b409SSimon J. Gerraty * \brief Size of individual blocks (in bytes). 516*0957b409SSimon J. Gerraty */ 517*0957b409SSimon J. Gerraty unsigned block_size; 518*0957b409SSimon J. Gerraty 519*0957b409SSimon J. Gerraty /** 520*0957b409SSimon J. Gerraty * \brief Base-2 logarithm of the size of individual blocks, 521*0957b409SSimon J. Gerraty * expressed in bytes. 522*0957b409SSimon J. Gerraty */ 523*0957b409SSimon J. Gerraty unsigned log_block_size; 524*0957b409SSimon J. Gerraty 525*0957b409SSimon J. Gerraty /** 526*0957b409SSimon J. Gerraty * \brief Initialisation function. 527*0957b409SSimon J. Gerraty * 528*0957b409SSimon J. Gerraty * This function sets the `vtable` field in the context structure. 529*0957b409SSimon J. Gerraty * The key length MUST be one of the key lengths supported by 530*0957b409SSimon J. Gerraty * the implementation. 531*0957b409SSimon J. Gerraty * 532*0957b409SSimon J. Gerraty * \param ctx context structure to initialise. 533*0957b409SSimon J. Gerraty * \param key secret key. 534*0957b409SSimon J. Gerraty * \param key_len key length (in bytes). 535*0957b409SSimon J. Gerraty */ 536*0957b409SSimon J. Gerraty void (*init)(const br_block_ctr_class **ctx, 537*0957b409SSimon J. Gerraty const void *key, size_t key_len); 538*0957b409SSimon J. Gerraty 539*0957b409SSimon J. Gerraty /** 540*0957b409SSimon J. Gerraty * \brief Run the CTR encryption or decryption. 541*0957b409SSimon J. Gerraty * 542*0957b409SSimon J. Gerraty * The `iv` parameter points to the IV for this run; its 543*0957b409SSimon J. Gerraty * length is exactly 4 bytes less than the block size (e.g. 544*0957b409SSimon J. Gerraty * 12 bytes for AES/CTR). The IV is combined with a 32-bit 545*0957b409SSimon J. Gerraty * block counter to produce the block value which is processed 546*0957b409SSimon J. Gerraty * with the block cipher. 547*0957b409SSimon J. Gerraty * 548*0957b409SSimon J. Gerraty * The data to encrypt or decrypt is updated "in place". Its 549*0957b409SSimon J. Gerraty * length (`len` bytes) is not required to be a multiple of 550*0957b409SSimon J. Gerraty * the block size; if the final block is partial, then the 551*0957b409SSimon J. Gerraty * corresponding key stream bits are dropped. 552*0957b409SSimon J. Gerraty * 553*0957b409SSimon J. Gerraty * The resulting counter value is returned. 554*0957b409SSimon J. Gerraty * 555*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 556*0957b409SSimon J. Gerraty * \param iv IV for CTR encryption/decryption. 557*0957b409SSimon J. Gerraty * \param cc initial value for the block counter. 558*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 559*0957b409SSimon J. Gerraty * \param len data length (in bytes). 560*0957b409SSimon J. Gerraty * \return the new block counter value. 561*0957b409SSimon J. Gerraty */ 562*0957b409SSimon J. Gerraty uint32_t (*run)(const br_block_ctr_class *const *ctx, 563*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 564*0957b409SSimon J. Gerraty }; 565*0957b409SSimon J. Gerraty 566*0957b409SSimon J. Gerraty /** 567*0957b409SSimon J. Gerraty * \brief Class type for combined CTR and CBC-MAC implementations. 568*0957b409SSimon J. Gerraty * 569*0957b409SSimon J. Gerraty * A `br_block_ctrcbc_class` instance points to the functions implementing 570*0957b409SSimon J. Gerraty * a specific block cipher, when used in CTR mode for encrypting or 571*0957b409SSimon J. Gerraty * decrypting data, along with CBC-MAC. 572*0957b409SSimon J. Gerraty */ 573*0957b409SSimon J. Gerraty typedef struct br_block_ctrcbc_class_ br_block_ctrcbc_class; 574*0957b409SSimon J. Gerraty struct br_block_ctrcbc_class_ { 575*0957b409SSimon J. Gerraty /** 576*0957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate 577*0957b409SSimon J. Gerraty * for containing subkeys. 578*0957b409SSimon J. Gerraty */ 579*0957b409SSimon J. Gerraty size_t context_size; 580*0957b409SSimon J. Gerraty 581*0957b409SSimon J. Gerraty /** 582*0957b409SSimon J. Gerraty * \brief Size of individual blocks (in bytes). 583*0957b409SSimon J. Gerraty */ 584*0957b409SSimon J. Gerraty unsigned block_size; 585*0957b409SSimon J. Gerraty 586*0957b409SSimon J. Gerraty /** 587*0957b409SSimon J. Gerraty * \brief Base-2 logarithm of the size of individual blocks, 588*0957b409SSimon J. Gerraty * expressed in bytes. 589*0957b409SSimon J. Gerraty */ 590*0957b409SSimon J. Gerraty unsigned log_block_size; 591*0957b409SSimon J. Gerraty 592*0957b409SSimon J. Gerraty /** 593*0957b409SSimon J. Gerraty * \brief Initialisation function. 594*0957b409SSimon J. Gerraty * 595*0957b409SSimon J. Gerraty * This function sets the `vtable` field in the context structure. 596*0957b409SSimon J. Gerraty * The key length MUST be one of the key lengths supported by 597*0957b409SSimon J. Gerraty * the implementation. 598*0957b409SSimon J. Gerraty * 599*0957b409SSimon J. Gerraty * \param ctx context structure to initialise. 600*0957b409SSimon J. Gerraty * \param key secret key. 601*0957b409SSimon J. Gerraty * \param key_len key length (in bytes). 602*0957b409SSimon J. Gerraty */ 603*0957b409SSimon J. Gerraty void (*init)(const br_block_ctrcbc_class **ctx, 604*0957b409SSimon J. Gerraty const void *key, size_t key_len); 605*0957b409SSimon J. Gerraty 606*0957b409SSimon J. Gerraty /** 607*0957b409SSimon J. Gerraty * \brief Run the CTR encryption + CBC-MAC. 608*0957b409SSimon J. Gerraty * 609*0957b409SSimon J. Gerraty * The `ctr` parameter points to the counter; its length shall 610*0957b409SSimon J. Gerraty * be equal to the block size. It is updated by this function 611*0957b409SSimon J. Gerraty * as encryption proceeds. 612*0957b409SSimon J. Gerraty * 613*0957b409SSimon J. Gerraty * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC 614*0957b409SSimon J. Gerraty * is computed over the encrypted data (output of CTR 615*0957b409SSimon J. Gerraty * encryption). Its length shall be equal to the block size. The 616*0957b409SSimon J. Gerraty * computed CBC-MAC value is written over the `cbcmac` array. 617*0957b409SSimon J. Gerraty * 618*0957b409SSimon J. Gerraty * The data to encrypt is updated "in place". Its length (`len` 619*0957b409SSimon J. Gerraty * bytes) MUST be a multiple of the block size. 620*0957b409SSimon J. Gerraty * 621*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 622*0957b409SSimon J. Gerraty * \param ctr counter for CTR encryption (initial and final). 623*0957b409SSimon J. Gerraty * \param cbcmac IV and output buffer for CBC-MAC. 624*0957b409SSimon J. Gerraty * \param data data to encrypt. 625*0957b409SSimon J. Gerraty * \param len data length (in bytes). 626*0957b409SSimon J. Gerraty */ 627*0957b409SSimon J. Gerraty void (*encrypt)(const br_block_ctrcbc_class *const *ctx, 628*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 629*0957b409SSimon J. Gerraty 630*0957b409SSimon J. Gerraty /** 631*0957b409SSimon J. Gerraty * \brief Run the CTR decryption + CBC-MAC. 632*0957b409SSimon J. Gerraty * 633*0957b409SSimon J. Gerraty * The `ctr` parameter points to the counter; its length shall 634*0957b409SSimon J. Gerraty * be equal to the block size. It is updated by this function 635*0957b409SSimon J. Gerraty * as decryption proceeds. 636*0957b409SSimon J. Gerraty * 637*0957b409SSimon J. Gerraty * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC 638*0957b409SSimon J. Gerraty * is computed over the encrypted data (i.e. before CTR 639*0957b409SSimon J. Gerraty * decryption). Its length shall be equal to the block size. The 640*0957b409SSimon J. Gerraty * computed CBC-MAC value is written over the `cbcmac` array. 641*0957b409SSimon J. Gerraty * 642*0957b409SSimon J. Gerraty * The data to decrypt is updated "in place". Its length (`len` 643*0957b409SSimon J. Gerraty * bytes) MUST be a multiple of the block size. 644*0957b409SSimon J. Gerraty * 645*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 646*0957b409SSimon J. Gerraty * \param ctr counter for CTR encryption (initial and final). 647*0957b409SSimon J. Gerraty * \param cbcmac IV and output buffer for CBC-MAC. 648*0957b409SSimon J. Gerraty * \param data data to decrypt. 649*0957b409SSimon J. Gerraty * \param len data length (in bytes). 650*0957b409SSimon J. Gerraty */ 651*0957b409SSimon J. Gerraty void (*decrypt)(const br_block_ctrcbc_class *const *ctx, 652*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 653*0957b409SSimon J. Gerraty 654*0957b409SSimon J. Gerraty /** 655*0957b409SSimon J. Gerraty * \brief Run the CTR encryption/decryption only. 656*0957b409SSimon J. Gerraty * 657*0957b409SSimon J. Gerraty * The `ctr` parameter points to the counter; its length shall 658*0957b409SSimon J. Gerraty * be equal to the block size. It is updated by this function 659*0957b409SSimon J. Gerraty * as decryption proceeds. 660*0957b409SSimon J. Gerraty * 661*0957b409SSimon J. Gerraty * The data to decrypt is updated "in place". Its length (`len` 662*0957b409SSimon J. Gerraty * bytes) MUST be a multiple of the block size. 663*0957b409SSimon J. Gerraty * 664*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 665*0957b409SSimon J. Gerraty * \param ctr counter for CTR encryption (initial and final). 666*0957b409SSimon J. Gerraty * \param data data to decrypt. 667*0957b409SSimon J. Gerraty * \param len data length (in bytes). 668*0957b409SSimon J. Gerraty */ 669*0957b409SSimon J. Gerraty void (*ctr)(const br_block_ctrcbc_class *const *ctx, 670*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 671*0957b409SSimon J. Gerraty 672*0957b409SSimon J. Gerraty /** 673*0957b409SSimon J. Gerraty * \brief Run the CBC-MAC only. 674*0957b409SSimon J. Gerraty * 675*0957b409SSimon J. Gerraty * The `cbcmac` parameter points to the IV for CBC-MAC. The MAC 676*0957b409SSimon J. Gerraty * is computed over the encrypted data (i.e. before CTR 677*0957b409SSimon J. Gerraty * decryption). Its length shall be equal to the block size. The 678*0957b409SSimon J. Gerraty * computed CBC-MAC value is written over the `cbcmac` array. 679*0957b409SSimon J. Gerraty * 680*0957b409SSimon J. Gerraty * The data is unmodified. Its length (`len` bytes) MUST be a 681*0957b409SSimon J. Gerraty * multiple of the block size. 682*0957b409SSimon J. Gerraty * 683*0957b409SSimon J. Gerraty * \param ctx context structure (already initialised). 684*0957b409SSimon J. Gerraty * \param cbcmac IV and output buffer for CBC-MAC. 685*0957b409SSimon J. Gerraty * \param data data to decrypt. 686*0957b409SSimon J. Gerraty * \param len data length (in bytes). 687*0957b409SSimon J. Gerraty */ 688*0957b409SSimon J. Gerraty void (*mac)(const br_block_ctrcbc_class *const *ctx, 689*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 690*0957b409SSimon J. Gerraty }; 691*0957b409SSimon J. Gerraty 692*0957b409SSimon J. Gerraty /* 693*0957b409SSimon J. Gerraty * Traditional, table-based AES implementation. It is fast, but uses 694*0957b409SSimon J. Gerraty * internal tables (in particular a 1 kB table for encryption, another 695*0957b409SSimon J. Gerraty * 1 kB table for decryption, and a 256-byte table for key schedule), 696*0957b409SSimon J. Gerraty * and it is not constant-time. In contexts where cache-timing attacks 697*0957b409SSimon J. Gerraty * apply, this implementation may leak the secret key. 698*0957b409SSimon J. Gerraty */ 699*0957b409SSimon J. Gerraty 700*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 701*0957b409SSimon J. Gerraty #define br_aes_big_BLOCK_SIZE 16 702*0957b409SSimon J. Gerraty 703*0957b409SSimon J. Gerraty /** 704*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_big` implementation, CBC encryption). 705*0957b409SSimon J. Gerraty * 706*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 707*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 708*0957b409SSimon J. Gerraty */ 709*0957b409SSimon J. Gerraty typedef struct { 710*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 711*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 712*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 713*0957b409SSimon J. Gerraty uint32_t skey[60]; 714*0957b409SSimon J. Gerraty unsigned num_rounds; 715*0957b409SSimon J. Gerraty #endif 716*0957b409SSimon J. Gerraty } br_aes_big_cbcenc_keys; 717*0957b409SSimon J. Gerraty 718*0957b409SSimon J. Gerraty /** 719*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_big` implementation, CBC decryption). 720*0957b409SSimon J. Gerraty * 721*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 722*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 723*0957b409SSimon J. Gerraty */ 724*0957b409SSimon J. Gerraty typedef struct { 725*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 726*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 727*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 728*0957b409SSimon J. Gerraty uint32_t skey[60]; 729*0957b409SSimon J. Gerraty unsigned num_rounds; 730*0957b409SSimon J. Gerraty #endif 731*0957b409SSimon J. Gerraty } br_aes_big_cbcdec_keys; 732*0957b409SSimon J. Gerraty 733*0957b409SSimon J. Gerraty /** 734*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_big` implementation, CTR encryption 735*0957b409SSimon J. Gerraty * and decryption). 736*0957b409SSimon J. Gerraty * 737*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 738*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 739*0957b409SSimon J. Gerraty */ 740*0957b409SSimon J. Gerraty typedef struct { 741*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 742*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 743*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 744*0957b409SSimon J. Gerraty uint32_t skey[60]; 745*0957b409SSimon J. Gerraty unsigned num_rounds; 746*0957b409SSimon J. Gerraty #endif 747*0957b409SSimon J. Gerraty } br_aes_big_ctr_keys; 748*0957b409SSimon J. Gerraty 749*0957b409SSimon J. Gerraty /** 750*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_big` implementation, CTR encryption 751*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 752*0957b409SSimon J. Gerraty * 753*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 754*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 755*0957b409SSimon J. Gerraty */ 756*0957b409SSimon J. Gerraty typedef struct { 757*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 758*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 759*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 760*0957b409SSimon J. Gerraty uint32_t skey[60]; 761*0957b409SSimon J. Gerraty unsigned num_rounds; 762*0957b409SSimon J. Gerraty #endif 763*0957b409SSimon J. Gerraty } br_aes_big_ctrcbc_keys; 764*0957b409SSimon J. Gerraty 765*0957b409SSimon J. Gerraty /** 766*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_big` implementation). 767*0957b409SSimon J. Gerraty */ 768*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_big_cbcenc_vtable; 769*0957b409SSimon J. Gerraty 770*0957b409SSimon J. Gerraty /** 771*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_big` implementation). 772*0957b409SSimon J. Gerraty */ 773*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_big_cbcdec_vtable; 774*0957b409SSimon J. Gerraty 775*0957b409SSimon J. Gerraty /** 776*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 777*0957b409SSimon J. Gerraty * (`aes_big` implementation). 778*0957b409SSimon J. Gerraty */ 779*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_big_ctr_vtable; 780*0957b409SSimon J. Gerraty 781*0957b409SSimon J. Gerraty /** 782*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 783*0957b409SSimon J. Gerraty * (`aes_big` implementation). 784*0957b409SSimon J. Gerraty */ 785*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_big_ctrcbc_vtable; 786*0957b409SSimon J. Gerraty 787*0957b409SSimon J. Gerraty /** 788*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 789*0957b409SSimon J. Gerraty * (`aes_big` implementation). 790*0957b409SSimon J. Gerraty * 791*0957b409SSimon J. Gerraty * \param ctx context to initialise. 792*0957b409SSimon J. Gerraty * \param key secret key. 793*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 794*0957b409SSimon J. Gerraty */ 795*0957b409SSimon J. Gerraty void br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx, 796*0957b409SSimon J. Gerraty const void *key, size_t len); 797*0957b409SSimon J. Gerraty 798*0957b409SSimon J. Gerraty /** 799*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 800*0957b409SSimon J. Gerraty * (`aes_big` implementation). 801*0957b409SSimon J. Gerraty * 802*0957b409SSimon J. Gerraty * \param ctx context to initialise. 803*0957b409SSimon J. Gerraty * \param key secret key. 804*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 805*0957b409SSimon J. Gerraty */ 806*0957b409SSimon J. Gerraty void br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx, 807*0957b409SSimon J. Gerraty const void *key, size_t len); 808*0957b409SSimon J. Gerraty 809*0957b409SSimon J. Gerraty /** 810*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 811*0957b409SSimon J. Gerraty * and decryption (`aes_big` implementation). 812*0957b409SSimon J. Gerraty * 813*0957b409SSimon J. Gerraty * \param ctx context to initialise. 814*0957b409SSimon J. Gerraty * \param key secret key. 815*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 816*0957b409SSimon J. Gerraty */ 817*0957b409SSimon J. Gerraty void br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx, 818*0957b409SSimon J. Gerraty const void *key, size_t len); 819*0957b409SSimon J. Gerraty 820*0957b409SSimon J. Gerraty /** 821*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 822*0957b409SSimon J. Gerraty * (`aes_big` implementation). 823*0957b409SSimon J. Gerraty * 824*0957b409SSimon J. Gerraty * \param ctx context to initialise. 825*0957b409SSimon J. Gerraty * \param key secret key. 826*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 827*0957b409SSimon J. Gerraty */ 828*0957b409SSimon J. Gerraty void br_aes_big_ctrcbc_init(br_aes_big_ctrcbc_keys *ctx, 829*0957b409SSimon J. Gerraty const void *key, size_t len); 830*0957b409SSimon J. Gerraty 831*0957b409SSimon J. Gerraty /** 832*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_big` implementation). 833*0957b409SSimon J. Gerraty * 834*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 835*0957b409SSimon J. Gerraty * \param iv IV (updated). 836*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 837*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 838*0957b409SSimon J. Gerraty */ 839*0957b409SSimon J. Gerraty void br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx, void *iv, 840*0957b409SSimon J. Gerraty void *data, size_t len); 841*0957b409SSimon J. Gerraty 842*0957b409SSimon J. Gerraty /** 843*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_big` implementation). 844*0957b409SSimon J. Gerraty * 845*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 846*0957b409SSimon J. Gerraty * \param iv IV (updated). 847*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 848*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 849*0957b409SSimon J. Gerraty */ 850*0957b409SSimon J. Gerraty void br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx, void *iv, 851*0957b409SSimon J. Gerraty void *data, size_t len); 852*0957b409SSimon J. Gerraty 853*0957b409SSimon J. Gerraty /** 854*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_big` implementation). 855*0957b409SSimon J. Gerraty * 856*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 857*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 858*0957b409SSimon J. Gerraty * \param cc initial block counter value. 859*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt (updated). 860*0957b409SSimon J. Gerraty * \param len data length (in bytes). 861*0957b409SSimon J. Gerraty * \return new block counter value. 862*0957b409SSimon J. Gerraty */ 863*0957b409SSimon J. Gerraty uint32_t br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx, 864*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 865*0957b409SSimon J. Gerraty 866*0957b409SSimon J. Gerraty /** 867*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_big` implementation). 868*0957b409SSimon J. Gerraty * 869*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 870*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 871*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 872*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 873*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 874*0957b409SSimon J. Gerraty */ 875*0957b409SSimon J. Gerraty void br_aes_big_ctrcbc_encrypt(const br_aes_big_ctrcbc_keys *ctx, 876*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 877*0957b409SSimon J. Gerraty 878*0957b409SSimon J. Gerraty /** 879*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_big` implementation). 880*0957b409SSimon J. Gerraty * 881*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 882*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 883*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 884*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 885*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 886*0957b409SSimon J. Gerraty */ 887*0957b409SSimon J. Gerraty void br_aes_big_ctrcbc_decrypt(const br_aes_big_ctrcbc_keys *ctx, 888*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 889*0957b409SSimon J. Gerraty 890*0957b409SSimon J. Gerraty /** 891*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_big` implementation). 892*0957b409SSimon J. Gerraty * 893*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 894*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 895*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 896*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 897*0957b409SSimon J. Gerraty */ 898*0957b409SSimon J. Gerraty void br_aes_big_ctrcbc_ctr(const br_aes_big_ctrcbc_keys *ctx, 899*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 900*0957b409SSimon J. Gerraty 901*0957b409SSimon J. Gerraty /** 902*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_big` implementation). 903*0957b409SSimon J. Gerraty * 904*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 905*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 906*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 907*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 908*0957b409SSimon J. Gerraty */ 909*0957b409SSimon J. Gerraty void br_aes_big_ctrcbc_mac(const br_aes_big_ctrcbc_keys *ctx, 910*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 911*0957b409SSimon J. Gerraty 912*0957b409SSimon J. Gerraty /* 913*0957b409SSimon J. Gerraty * AES implementation optimized for size. It is slower than the 914*0957b409SSimon J. Gerraty * traditional table-based AES implementation, but requires much less 915*0957b409SSimon J. Gerraty * code. It still uses data-dependent table accesses (albeit within a 916*0957b409SSimon J. Gerraty * much smaller 256-byte table), which makes it conceptually vulnerable 917*0957b409SSimon J. Gerraty * to cache-timing attacks. 918*0957b409SSimon J. Gerraty */ 919*0957b409SSimon J. Gerraty 920*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 921*0957b409SSimon J. Gerraty #define br_aes_small_BLOCK_SIZE 16 922*0957b409SSimon J. Gerraty 923*0957b409SSimon J. Gerraty /** 924*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_small` implementation, CBC encryption). 925*0957b409SSimon J. Gerraty * 926*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 927*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 928*0957b409SSimon J. Gerraty */ 929*0957b409SSimon J. Gerraty typedef struct { 930*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 931*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 932*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 933*0957b409SSimon J. Gerraty uint32_t skey[60]; 934*0957b409SSimon J. Gerraty unsigned num_rounds; 935*0957b409SSimon J. Gerraty #endif 936*0957b409SSimon J. Gerraty } br_aes_small_cbcenc_keys; 937*0957b409SSimon J. Gerraty 938*0957b409SSimon J. Gerraty /** 939*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_small` implementation, CBC decryption). 940*0957b409SSimon J. Gerraty * 941*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 942*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 943*0957b409SSimon J. Gerraty */ 944*0957b409SSimon J. Gerraty typedef struct { 945*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 946*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 947*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 948*0957b409SSimon J. Gerraty uint32_t skey[60]; 949*0957b409SSimon J. Gerraty unsigned num_rounds; 950*0957b409SSimon J. Gerraty #endif 951*0957b409SSimon J. Gerraty } br_aes_small_cbcdec_keys; 952*0957b409SSimon J. Gerraty 953*0957b409SSimon J. Gerraty /** 954*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_small` implementation, CTR encryption 955*0957b409SSimon J. Gerraty * and decryption). 956*0957b409SSimon J. Gerraty * 957*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 958*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 959*0957b409SSimon J. Gerraty */ 960*0957b409SSimon J. Gerraty typedef struct { 961*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 962*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 963*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 964*0957b409SSimon J. Gerraty uint32_t skey[60]; 965*0957b409SSimon J. Gerraty unsigned num_rounds; 966*0957b409SSimon J. Gerraty #endif 967*0957b409SSimon J. Gerraty } br_aes_small_ctr_keys; 968*0957b409SSimon J. Gerraty 969*0957b409SSimon J. Gerraty /** 970*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_small` implementation, CTR encryption 971*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 972*0957b409SSimon J. Gerraty * 973*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 974*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 975*0957b409SSimon J. Gerraty */ 976*0957b409SSimon J. Gerraty typedef struct { 977*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 978*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 979*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 980*0957b409SSimon J. Gerraty uint32_t skey[60]; 981*0957b409SSimon J. Gerraty unsigned num_rounds; 982*0957b409SSimon J. Gerraty #endif 983*0957b409SSimon J. Gerraty } br_aes_small_ctrcbc_keys; 984*0957b409SSimon J. Gerraty 985*0957b409SSimon J. Gerraty /** 986*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_small` implementation). 987*0957b409SSimon J. Gerraty */ 988*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_small_cbcenc_vtable; 989*0957b409SSimon J. Gerraty 990*0957b409SSimon J. Gerraty /** 991*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_small` implementation). 992*0957b409SSimon J. Gerraty */ 993*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_small_cbcdec_vtable; 994*0957b409SSimon J. Gerraty 995*0957b409SSimon J. Gerraty /** 996*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 997*0957b409SSimon J. Gerraty * (`aes_small` implementation). 998*0957b409SSimon J. Gerraty */ 999*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_small_ctr_vtable; 1000*0957b409SSimon J. Gerraty 1001*0957b409SSimon J. Gerraty /** 1002*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 1003*0957b409SSimon J. Gerraty * (`aes_small` implementation). 1004*0957b409SSimon J. Gerraty */ 1005*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable; 1006*0957b409SSimon J. Gerraty 1007*0957b409SSimon J. Gerraty /** 1008*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 1009*0957b409SSimon J. Gerraty * (`aes_small` implementation). 1010*0957b409SSimon J. Gerraty * 1011*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1012*0957b409SSimon J. Gerraty * \param key secret key. 1013*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1014*0957b409SSimon J. Gerraty */ 1015*0957b409SSimon J. Gerraty void br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx, 1016*0957b409SSimon J. Gerraty const void *key, size_t len); 1017*0957b409SSimon J. Gerraty 1018*0957b409SSimon J. Gerraty /** 1019*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 1020*0957b409SSimon J. Gerraty * (`aes_small` implementation). 1021*0957b409SSimon J. Gerraty * 1022*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1023*0957b409SSimon J. Gerraty * \param key secret key. 1024*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1025*0957b409SSimon J. Gerraty */ 1026*0957b409SSimon J. Gerraty void br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx, 1027*0957b409SSimon J. Gerraty const void *key, size_t len); 1028*0957b409SSimon J. Gerraty 1029*0957b409SSimon J. Gerraty /** 1030*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 1031*0957b409SSimon J. Gerraty * and decryption (`aes_small` implementation). 1032*0957b409SSimon J. Gerraty * 1033*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1034*0957b409SSimon J. Gerraty * \param key secret key. 1035*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1036*0957b409SSimon J. Gerraty */ 1037*0957b409SSimon J. Gerraty void br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx, 1038*0957b409SSimon J. Gerraty const void *key, size_t len); 1039*0957b409SSimon J. Gerraty 1040*0957b409SSimon J. Gerraty /** 1041*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 1042*0957b409SSimon J. Gerraty * (`aes_small` implementation). 1043*0957b409SSimon J. Gerraty * 1044*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1045*0957b409SSimon J. Gerraty * \param key secret key. 1046*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1047*0957b409SSimon J. Gerraty */ 1048*0957b409SSimon J. Gerraty void br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx, 1049*0957b409SSimon J. Gerraty const void *key, size_t len); 1050*0957b409SSimon J. Gerraty 1051*0957b409SSimon J. Gerraty /** 1052*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_small` implementation). 1053*0957b409SSimon J. Gerraty * 1054*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1055*0957b409SSimon J. Gerraty * \param iv IV (updated). 1056*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1057*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1058*0957b409SSimon J. Gerraty */ 1059*0957b409SSimon J. Gerraty void br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx, void *iv, 1060*0957b409SSimon J. Gerraty void *data, size_t len); 1061*0957b409SSimon J. Gerraty 1062*0957b409SSimon J. Gerraty /** 1063*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_small` implementation). 1064*0957b409SSimon J. Gerraty * 1065*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1066*0957b409SSimon J. Gerraty * \param iv IV (updated). 1067*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1068*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1069*0957b409SSimon J. Gerraty */ 1070*0957b409SSimon J. Gerraty void br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx, void *iv, 1071*0957b409SSimon J. Gerraty void *data, size_t len); 1072*0957b409SSimon J. Gerraty 1073*0957b409SSimon J. Gerraty /** 1074*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_small` implementation). 1075*0957b409SSimon J. Gerraty * 1076*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1077*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 1078*0957b409SSimon J. Gerraty * \param cc initial block counter value. 1079*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1080*0957b409SSimon J. Gerraty * \param len data length (in bytes). 1081*0957b409SSimon J. Gerraty * \return new block counter value. 1082*0957b409SSimon J. Gerraty */ 1083*0957b409SSimon J. Gerraty uint32_t br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx, 1084*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 1085*0957b409SSimon J. Gerraty 1086*0957b409SSimon J. Gerraty /** 1087*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_small` implementation). 1088*0957b409SSimon J. Gerraty * 1089*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1090*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1091*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1092*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1093*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1094*0957b409SSimon J. Gerraty */ 1095*0957b409SSimon J. Gerraty void br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx, 1096*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1097*0957b409SSimon J. Gerraty 1098*0957b409SSimon J. Gerraty /** 1099*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_small` implementation). 1100*0957b409SSimon J. Gerraty * 1101*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1102*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1103*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1104*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1105*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1106*0957b409SSimon J. Gerraty */ 1107*0957b409SSimon J. Gerraty void br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx, 1108*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1109*0957b409SSimon J. Gerraty 1110*0957b409SSimon J. Gerraty /** 1111*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_small` implementation). 1112*0957b409SSimon J. Gerraty * 1113*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1114*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1115*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 1116*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1117*0957b409SSimon J. Gerraty */ 1118*0957b409SSimon J. Gerraty void br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx, 1119*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 1120*0957b409SSimon J. Gerraty 1121*0957b409SSimon J. Gerraty /** 1122*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_small` implementation). 1123*0957b409SSimon J. Gerraty * 1124*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1125*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1126*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 1127*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1128*0957b409SSimon J. Gerraty */ 1129*0957b409SSimon J. Gerraty void br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx, 1130*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 1131*0957b409SSimon J. Gerraty 1132*0957b409SSimon J. Gerraty /* 1133*0957b409SSimon J. Gerraty * Constant-time AES implementation. Its size is similar to that of 1134*0957b409SSimon J. Gerraty * 'aes_big', and its performance is similar to that of 'aes_small' (faster 1135*0957b409SSimon J. Gerraty * decryption, slower encryption). However, it is constant-time, i.e. 1136*0957b409SSimon J. Gerraty * immune to cache-timing and similar attacks. 1137*0957b409SSimon J. Gerraty */ 1138*0957b409SSimon J. Gerraty 1139*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 1140*0957b409SSimon J. Gerraty #define br_aes_ct_BLOCK_SIZE 16 1141*0957b409SSimon J. Gerraty 1142*0957b409SSimon J. Gerraty /** 1143*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct` implementation, CBC encryption). 1144*0957b409SSimon J. Gerraty * 1145*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1146*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1147*0957b409SSimon J. Gerraty */ 1148*0957b409SSimon J. Gerraty typedef struct { 1149*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1150*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 1151*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1152*0957b409SSimon J. Gerraty uint32_t skey[60]; 1153*0957b409SSimon J. Gerraty unsigned num_rounds; 1154*0957b409SSimon J. Gerraty #endif 1155*0957b409SSimon J. Gerraty } br_aes_ct_cbcenc_keys; 1156*0957b409SSimon J. Gerraty 1157*0957b409SSimon J. Gerraty /** 1158*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct` implementation, CBC decryption). 1159*0957b409SSimon J. Gerraty * 1160*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1161*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1162*0957b409SSimon J. Gerraty */ 1163*0957b409SSimon J. Gerraty typedef struct { 1164*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1165*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 1166*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1167*0957b409SSimon J. Gerraty uint32_t skey[60]; 1168*0957b409SSimon J. Gerraty unsigned num_rounds; 1169*0957b409SSimon J. Gerraty #endif 1170*0957b409SSimon J. Gerraty } br_aes_ct_cbcdec_keys; 1171*0957b409SSimon J. Gerraty 1172*0957b409SSimon J. Gerraty /** 1173*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption 1174*0957b409SSimon J. Gerraty * and decryption). 1175*0957b409SSimon J. Gerraty * 1176*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1177*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1178*0957b409SSimon J. Gerraty */ 1179*0957b409SSimon J. Gerraty typedef struct { 1180*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1181*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 1182*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1183*0957b409SSimon J. Gerraty uint32_t skey[60]; 1184*0957b409SSimon J. Gerraty unsigned num_rounds; 1185*0957b409SSimon J. Gerraty #endif 1186*0957b409SSimon J. Gerraty } br_aes_ct_ctr_keys; 1187*0957b409SSimon J. Gerraty 1188*0957b409SSimon J. Gerraty /** 1189*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption 1190*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 1191*0957b409SSimon J. Gerraty * 1192*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1193*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1194*0957b409SSimon J. Gerraty */ 1195*0957b409SSimon J. Gerraty typedef struct { 1196*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1197*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 1198*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1199*0957b409SSimon J. Gerraty uint32_t skey[60]; 1200*0957b409SSimon J. Gerraty unsigned num_rounds; 1201*0957b409SSimon J. Gerraty #endif 1202*0957b409SSimon J. Gerraty } br_aes_ct_ctrcbc_keys; 1203*0957b409SSimon J. Gerraty 1204*0957b409SSimon J. Gerraty /** 1205*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_ct` implementation). 1206*0957b409SSimon J. Gerraty */ 1207*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_ct_cbcenc_vtable; 1208*0957b409SSimon J. Gerraty 1209*0957b409SSimon J. Gerraty /** 1210*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_ct` implementation). 1211*0957b409SSimon J. Gerraty */ 1212*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_ct_cbcdec_vtable; 1213*0957b409SSimon J. Gerraty 1214*0957b409SSimon J. Gerraty /** 1215*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 1216*0957b409SSimon J. Gerraty * (`aes_ct` implementation). 1217*0957b409SSimon J. Gerraty */ 1218*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_ct_ctr_vtable; 1219*0957b409SSimon J. Gerraty 1220*0957b409SSimon J. Gerraty /** 1221*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 1222*0957b409SSimon J. Gerraty * (`aes_ct` implementation). 1223*0957b409SSimon J. Gerraty */ 1224*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable; 1225*0957b409SSimon J. Gerraty 1226*0957b409SSimon J. Gerraty /** 1227*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 1228*0957b409SSimon J. Gerraty * (`aes_ct` implementation). 1229*0957b409SSimon J. Gerraty * 1230*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1231*0957b409SSimon J. Gerraty * \param key secret key. 1232*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1233*0957b409SSimon J. Gerraty */ 1234*0957b409SSimon J. Gerraty void br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx, 1235*0957b409SSimon J. Gerraty const void *key, size_t len); 1236*0957b409SSimon J. Gerraty 1237*0957b409SSimon J. Gerraty /** 1238*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 1239*0957b409SSimon J. Gerraty * (`aes_ct` implementation). 1240*0957b409SSimon J. Gerraty * 1241*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1242*0957b409SSimon J. Gerraty * \param key secret key. 1243*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1244*0957b409SSimon J. Gerraty */ 1245*0957b409SSimon J. Gerraty void br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx, 1246*0957b409SSimon J. Gerraty const void *key, size_t len); 1247*0957b409SSimon J. Gerraty 1248*0957b409SSimon J. Gerraty /** 1249*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 1250*0957b409SSimon J. Gerraty * and decryption (`aes_ct` implementation). 1251*0957b409SSimon J. Gerraty * 1252*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1253*0957b409SSimon J. Gerraty * \param key secret key. 1254*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1255*0957b409SSimon J. Gerraty */ 1256*0957b409SSimon J. Gerraty void br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx, 1257*0957b409SSimon J. Gerraty const void *key, size_t len); 1258*0957b409SSimon J. Gerraty 1259*0957b409SSimon J. Gerraty /** 1260*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 1261*0957b409SSimon J. Gerraty * (`aes_ct` implementation). 1262*0957b409SSimon J. Gerraty * 1263*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1264*0957b409SSimon J. Gerraty * \param key secret key. 1265*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1266*0957b409SSimon J. Gerraty */ 1267*0957b409SSimon J. Gerraty void br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx, 1268*0957b409SSimon J. Gerraty const void *key, size_t len); 1269*0957b409SSimon J. Gerraty 1270*0957b409SSimon J. Gerraty /** 1271*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_ct` implementation). 1272*0957b409SSimon J. Gerraty * 1273*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1274*0957b409SSimon J. Gerraty * \param iv IV (updated). 1275*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1276*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1277*0957b409SSimon J. Gerraty */ 1278*0957b409SSimon J. Gerraty void br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx, void *iv, 1279*0957b409SSimon J. Gerraty void *data, size_t len); 1280*0957b409SSimon J. Gerraty 1281*0957b409SSimon J. Gerraty /** 1282*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_ct` implementation). 1283*0957b409SSimon J. Gerraty * 1284*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1285*0957b409SSimon J. Gerraty * \param iv IV (updated). 1286*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1287*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1288*0957b409SSimon J. Gerraty */ 1289*0957b409SSimon J. Gerraty void br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx, void *iv, 1290*0957b409SSimon J. Gerraty void *data, size_t len); 1291*0957b409SSimon J. Gerraty 1292*0957b409SSimon J. Gerraty /** 1293*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_ct` implementation). 1294*0957b409SSimon J. Gerraty * 1295*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1296*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 1297*0957b409SSimon J. Gerraty * \param cc initial block counter value. 1298*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1299*0957b409SSimon J. Gerraty * \param len data length (in bytes). 1300*0957b409SSimon J. Gerraty * \return new block counter value. 1301*0957b409SSimon J. Gerraty */ 1302*0957b409SSimon J. Gerraty uint32_t br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx, 1303*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 1304*0957b409SSimon J. Gerraty 1305*0957b409SSimon J. Gerraty /** 1306*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_ct` implementation). 1307*0957b409SSimon J. Gerraty * 1308*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1309*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1310*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1311*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1312*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1313*0957b409SSimon J. Gerraty */ 1314*0957b409SSimon J. Gerraty void br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx, 1315*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1316*0957b409SSimon J. Gerraty 1317*0957b409SSimon J. Gerraty /** 1318*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_ct` implementation). 1319*0957b409SSimon J. Gerraty * 1320*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1321*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1322*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1323*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1324*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1325*0957b409SSimon J. Gerraty */ 1326*0957b409SSimon J. Gerraty void br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx, 1327*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1328*0957b409SSimon J. Gerraty 1329*0957b409SSimon J. Gerraty /** 1330*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_ct` implementation). 1331*0957b409SSimon J. Gerraty * 1332*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1333*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1334*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 1335*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1336*0957b409SSimon J. Gerraty */ 1337*0957b409SSimon J. Gerraty void br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx, 1338*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 1339*0957b409SSimon J. Gerraty 1340*0957b409SSimon J. Gerraty /** 1341*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_ct` implementation). 1342*0957b409SSimon J. Gerraty * 1343*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1344*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1345*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 1346*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1347*0957b409SSimon J. Gerraty */ 1348*0957b409SSimon J. Gerraty void br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx, 1349*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 1350*0957b409SSimon J. Gerraty 1351*0957b409SSimon J. Gerraty /* 1352*0957b409SSimon J. Gerraty * 64-bit constant-time AES implementation. It is similar to 'aes_ct' 1353*0957b409SSimon J. Gerraty * but uses 64-bit registers, making it about twice faster than 'aes_ct' 1354*0957b409SSimon J. Gerraty * on 64-bit platforms, while remaining constant-time and with a similar 1355*0957b409SSimon J. Gerraty * code size. (The doubling in performance is only for CBC decryption 1356*0957b409SSimon J. Gerraty * and CTR mode; CBC encryption is non-parallel and cannot benefit from 1357*0957b409SSimon J. Gerraty * the larger registers.) 1358*0957b409SSimon J. Gerraty */ 1359*0957b409SSimon J. Gerraty 1360*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 1361*0957b409SSimon J. Gerraty #define br_aes_ct64_BLOCK_SIZE 16 1362*0957b409SSimon J. Gerraty 1363*0957b409SSimon J. Gerraty /** 1364*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct64` implementation, CBC encryption). 1365*0957b409SSimon J. Gerraty * 1366*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1367*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1368*0957b409SSimon J. Gerraty */ 1369*0957b409SSimon J. Gerraty typedef struct { 1370*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1371*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 1372*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1373*0957b409SSimon J. Gerraty uint64_t skey[30]; 1374*0957b409SSimon J. Gerraty unsigned num_rounds; 1375*0957b409SSimon J. Gerraty #endif 1376*0957b409SSimon J. Gerraty } br_aes_ct64_cbcenc_keys; 1377*0957b409SSimon J. Gerraty 1378*0957b409SSimon J. Gerraty /** 1379*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct64` implementation, CBC decryption). 1380*0957b409SSimon J. Gerraty * 1381*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1382*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1383*0957b409SSimon J. Gerraty */ 1384*0957b409SSimon J. Gerraty typedef struct { 1385*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1386*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 1387*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1388*0957b409SSimon J. Gerraty uint64_t skey[30]; 1389*0957b409SSimon J. Gerraty unsigned num_rounds; 1390*0957b409SSimon J. Gerraty #endif 1391*0957b409SSimon J. Gerraty } br_aes_ct64_cbcdec_keys; 1392*0957b409SSimon J. Gerraty 1393*0957b409SSimon J. Gerraty /** 1394*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption 1395*0957b409SSimon J. Gerraty * and decryption). 1396*0957b409SSimon J. Gerraty * 1397*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1398*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1399*0957b409SSimon J. Gerraty */ 1400*0957b409SSimon J. Gerraty typedef struct { 1401*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1402*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 1403*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1404*0957b409SSimon J. Gerraty uint64_t skey[30]; 1405*0957b409SSimon J. Gerraty unsigned num_rounds; 1406*0957b409SSimon J. Gerraty #endif 1407*0957b409SSimon J. Gerraty } br_aes_ct64_ctr_keys; 1408*0957b409SSimon J. Gerraty 1409*0957b409SSimon J. Gerraty /** 1410*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption 1411*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 1412*0957b409SSimon J. Gerraty * 1413*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1414*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1415*0957b409SSimon J. Gerraty */ 1416*0957b409SSimon J. Gerraty typedef struct { 1417*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1418*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 1419*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1420*0957b409SSimon J. Gerraty uint64_t skey[30]; 1421*0957b409SSimon J. Gerraty unsigned num_rounds; 1422*0957b409SSimon J. Gerraty #endif 1423*0957b409SSimon J. Gerraty } br_aes_ct64_ctrcbc_keys; 1424*0957b409SSimon J. Gerraty 1425*0957b409SSimon J. Gerraty /** 1426*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_ct64` implementation). 1427*0957b409SSimon J. Gerraty */ 1428*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable; 1429*0957b409SSimon J. Gerraty 1430*0957b409SSimon J. Gerraty /** 1431*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_ct64` implementation). 1432*0957b409SSimon J. Gerraty */ 1433*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable; 1434*0957b409SSimon J. Gerraty 1435*0957b409SSimon J. Gerraty /** 1436*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 1437*0957b409SSimon J. Gerraty * (`aes_ct64` implementation). 1438*0957b409SSimon J. Gerraty */ 1439*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_ct64_ctr_vtable; 1440*0957b409SSimon J. Gerraty 1441*0957b409SSimon J. Gerraty /** 1442*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 1443*0957b409SSimon J. Gerraty * (`aes_ct64` implementation). 1444*0957b409SSimon J. Gerraty */ 1445*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable; 1446*0957b409SSimon J. Gerraty 1447*0957b409SSimon J. Gerraty /** 1448*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 1449*0957b409SSimon J. Gerraty * (`aes_ct64` implementation). 1450*0957b409SSimon J. Gerraty * 1451*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1452*0957b409SSimon J. Gerraty * \param key secret key. 1453*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1454*0957b409SSimon J. Gerraty */ 1455*0957b409SSimon J. Gerraty void br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx, 1456*0957b409SSimon J. Gerraty const void *key, size_t len); 1457*0957b409SSimon J. Gerraty 1458*0957b409SSimon J. Gerraty /** 1459*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 1460*0957b409SSimon J. Gerraty * (`aes_ct64` implementation). 1461*0957b409SSimon J. Gerraty * 1462*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1463*0957b409SSimon J. Gerraty * \param key secret key. 1464*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1465*0957b409SSimon J. Gerraty */ 1466*0957b409SSimon J. Gerraty void br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx, 1467*0957b409SSimon J. Gerraty const void *key, size_t len); 1468*0957b409SSimon J. Gerraty 1469*0957b409SSimon J. Gerraty /** 1470*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 1471*0957b409SSimon J. Gerraty * and decryption (`aes_ct64` implementation). 1472*0957b409SSimon J. Gerraty * 1473*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1474*0957b409SSimon J. Gerraty * \param key secret key. 1475*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1476*0957b409SSimon J. Gerraty */ 1477*0957b409SSimon J. Gerraty void br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx, 1478*0957b409SSimon J. Gerraty const void *key, size_t len); 1479*0957b409SSimon J. Gerraty 1480*0957b409SSimon J. Gerraty /** 1481*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 1482*0957b409SSimon J. Gerraty * (`aes_ct64` implementation). 1483*0957b409SSimon J. Gerraty * 1484*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1485*0957b409SSimon J. Gerraty * \param key secret key. 1486*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1487*0957b409SSimon J. Gerraty */ 1488*0957b409SSimon J. Gerraty void br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx, 1489*0957b409SSimon J. Gerraty const void *key, size_t len); 1490*0957b409SSimon J. Gerraty 1491*0957b409SSimon J. Gerraty /** 1492*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_ct64` implementation). 1493*0957b409SSimon J. Gerraty * 1494*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1495*0957b409SSimon J. Gerraty * \param iv IV (updated). 1496*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1497*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1498*0957b409SSimon J. Gerraty */ 1499*0957b409SSimon J. Gerraty void br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx, void *iv, 1500*0957b409SSimon J. Gerraty void *data, size_t len); 1501*0957b409SSimon J. Gerraty 1502*0957b409SSimon J. Gerraty /** 1503*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_ct64` implementation). 1504*0957b409SSimon J. Gerraty * 1505*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1506*0957b409SSimon J. Gerraty * \param iv IV (updated). 1507*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1508*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1509*0957b409SSimon J. Gerraty */ 1510*0957b409SSimon J. Gerraty void br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx, void *iv, 1511*0957b409SSimon J. Gerraty void *data, size_t len); 1512*0957b409SSimon J. Gerraty 1513*0957b409SSimon J. Gerraty /** 1514*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_ct64` implementation). 1515*0957b409SSimon J. Gerraty * 1516*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1517*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 1518*0957b409SSimon J. Gerraty * \param cc initial block counter value. 1519*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1520*0957b409SSimon J. Gerraty * \param len data length (in bytes). 1521*0957b409SSimon J. Gerraty * \return new block counter value. 1522*0957b409SSimon J. Gerraty */ 1523*0957b409SSimon J. Gerraty uint32_t br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx, 1524*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 1525*0957b409SSimon J. Gerraty 1526*0957b409SSimon J. Gerraty /** 1527*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_ct64` implementation). 1528*0957b409SSimon J. Gerraty * 1529*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1530*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1531*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1532*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1533*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1534*0957b409SSimon J. Gerraty */ 1535*0957b409SSimon J. Gerraty void br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx, 1536*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1537*0957b409SSimon J. Gerraty 1538*0957b409SSimon J. Gerraty /** 1539*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_ct64` implementation). 1540*0957b409SSimon J. Gerraty * 1541*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1542*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1543*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1544*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1545*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1546*0957b409SSimon J. Gerraty */ 1547*0957b409SSimon J. Gerraty void br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx, 1548*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1549*0957b409SSimon J. Gerraty 1550*0957b409SSimon J. Gerraty /** 1551*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_ct64` implementation). 1552*0957b409SSimon J. Gerraty * 1553*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1554*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1555*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 1556*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1557*0957b409SSimon J. Gerraty */ 1558*0957b409SSimon J. Gerraty void br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx, 1559*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 1560*0957b409SSimon J. Gerraty 1561*0957b409SSimon J. Gerraty /** 1562*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_ct64` implementation). 1563*0957b409SSimon J. Gerraty * 1564*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1565*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1566*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 1567*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1568*0957b409SSimon J. Gerraty */ 1569*0957b409SSimon J. Gerraty void br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx, 1570*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 1571*0957b409SSimon J. Gerraty 1572*0957b409SSimon J. Gerraty /* 1573*0957b409SSimon J. Gerraty * AES implementation using AES-NI opcodes (x86 platform). 1574*0957b409SSimon J. Gerraty */ 1575*0957b409SSimon J. Gerraty 1576*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 1577*0957b409SSimon J. Gerraty #define br_aes_x86ni_BLOCK_SIZE 16 1578*0957b409SSimon J. Gerraty 1579*0957b409SSimon J. Gerraty /** 1580*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_x86ni` implementation, CBC encryption). 1581*0957b409SSimon J. Gerraty * 1582*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1583*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1584*0957b409SSimon J. Gerraty */ 1585*0957b409SSimon J. Gerraty typedef struct { 1586*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1587*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 1588*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1589*0957b409SSimon J. Gerraty union { 1590*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1591*0957b409SSimon J. Gerraty } skey; 1592*0957b409SSimon J. Gerraty unsigned num_rounds; 1593*0957b409SSimon J. Gerraty #endif 1594*0957b409SSimon J. Gerraty } br_aes_x86ni_cbcenc_keys; 1595*0957b409SSimon J. Gerraty 1596*0957b409SSimon J. Gerraty /** 1597*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_x86ni` implementation, CBC decryption). 1598*0957b409SSimon J. Gerraty * 1599*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1600*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1601*0957b409SSimon J. Gerraty */ 1602*0957b409SSimon J. Gerraty typedef struct { 1603*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1604*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 1605*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1606*0957b409SSimon J. Gerraty union { 1607*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1608*0957b409SSimon J. Gerraty } skey; 1609*0957b409SSimon J. Gerraty unsigned num_rounds; 1610*0957b409SSimon J. Gerraty #endif 1611*0957b409SSimon J. Gerraty } br_aes_x86ni_cbcdec_keys; 1612*0957b409SSimon J. Gerraty 1613*0957b409SSimon J. Gerraty /** 1614*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption 1615*0957b409SSimon J. Gerraty * and decryption). 1616*0957b409SSimon J. Gerraty * 1617*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1618*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1619*0957b409SSimon J. Gerraty */ 1620*0957b409SSimon J. Gerraty typedef struct { 1621*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1622*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 1623*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1624*0957b409SSimon J. Gerraty union { 1625*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1626*0957b409SSimon J. Gerraty } skey; 1627*0957b409SSimon J. Gerraty unsigned num_rounds; 1628*0957b409SSimon J. Gerraty #endif 1629*0957b409SSimon J. Gerraty } br_aes_x86ni_ctr_keys; 1630*0957b409SSimon J. Gerraty 1631*0957b409SSimon J. Gerraty /** 1632*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_x86ni` implementation, CTR encryption 1633*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 1634*0957b409SSimon J. Gerraty * 1635*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1636*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1637*0957b409SSimon J. Gerraty */ 1638*0957b409SSimon J. Gerraty typedef struct { 1639*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1640*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 1641*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1642*0957b409SSimon J. Gerraty union { 1643*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1644*0957b409SSimon J. Gerraty } skey; 1645*0957b409SSimon J. Gerraty unsigned num_rounds; 1646*0957b409SSimon J. Gerraty #endif 1647*0957b409SSimon J. Gerraty } br_aes_x86ni_ctrcbc_keys; 1648*0957b409SSimon J. Gerraty 1649*0957b409SSimon J. Gerraty /** 1650*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_x86ni` implementation). 1651*0957b409SSimon J. Gerraty * 1652*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1653*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1654*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_x86ni_cbcenc_get_vtable()`. 1655*0957b409SSimon J. Gerraty */ 1656*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable; 1657*0957b409SSimon J. Gerraty 1658*0957b409SSimon J. Gerraty /** 1659*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_x86ni` implementation). 1660*0957b409SSimon J. Gerraty * 1661*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1662*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1663*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_x86ni_cbcdec_get_vtable()`. 1664*0957b409SSimon J. Gerraty */ 1665*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_x86ni_cbcdec_vtable; 1666*0957b409SSimon J. Gerraty 1667*0957b409SSimon J. Gerraty /** 1668*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 1669*0957b409SSimon J. Gerraty * (`aes_x86ni` implementation). 1670*0957b409SSimon J. Gerraty * 1671*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1672*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1673*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_x86ni_ctr_get_vtable()`. 1674*0957b409SSimon J. Gerraty */ 1675*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_x86ni_ctr_vtable; 1676*0957b409SSimon J. Gerraty 1677*0957b409SSimon J. Gerraty /** 1678*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 1679*0957b409SSimon J. Gerraty * (`aes_x86ni` implementation). 1680*0957b409SSimon J. Gerraty * 1681*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1682*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1683*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_x86ni_ctrcbc_get_vtable()`. 1684*0957b409SSimon J. Gerraty */ 1685*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_x86ni_ctrcbc_vtable; 1686*0957b409SSimon J. Gerraty 1687*0957b409SSimon J. Gerraty /** 1688*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 1689*0957b409SSimon J. Gerraty * (`aes_x86ni` implementation). 1690*0957b409SSimon J. Gerraty * 1691*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1692*0957b409SSimon J. Gerraty * \param key secret key. 1693*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1694*0957b409SSimon J. Gerraty */ 1695*0957b409SSimon J. Gerraty void br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx, 1696*0957b409SSimon J. Gerraty const void *key, size_t len); 1697*0957b409SSimon J. Gerraty 1698*0957b409SSimon J. Gerraty /** 1699*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 1700*0957b409SSimon J. Gerraty * (`aes_x86ni` implementation). 1701*0957b409SSimon J. Gerraty * 1702*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1703*0957b409SSimon J. Gerraty * \param key secret key. 1704*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1705*0957b409SSimon J. Gerraty */ 1706*0957b409SSimon J. Gerraty void br_aes_x86ni_cbcdec_init(br_aes_x86ni_cbcdec_keys *ctx, 1707*0957b409SSimon J. Gerraty const void *key, size_t len); 1708*0957b409SSimon J. Gerraty 1709*0957b409SSimon J. Gerraty /** 1710*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 1711*0957b409SSimon J. Gerraty * and decryption (`aes_x86ni` implementation). 1712*0957b409SSimon J. Gerraty * 1713*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1714*0957b409SSimon J. Gerraty * \param key secret key. 1715*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1716*0957b409SSimon J. Gerraty */ 1717*0957b409SSimon J. Gerraty void br_aes_x86ni_ctr_init(br_aes_x86ni_ctr_keys *ctx, 1718*0957b409SSimon J. Gerraty const void *key, size_t len); 1719*0957b409SSimon J. Gerraty 1720*0957b409SSimon J. Gerraty /** 1721*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 1722*0957b409SSimon J. Gerraty * (`aes_x86ni` implementation). 1723*0957b409SSimon J. Gerraty * 1724*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1725*0957b409SSimon J. Gerraty * \param key secret key. 1726*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1727*0957b409SSimon J. Gerraty */ 1728*0957b409SSimon J. Gerraty void br_aes_x86ni_ctrcbc_init(br_aes_x86ni_ctrcbc_keys *ctx, 1729*0957b409SSimon J. Gerraty const void *key, size_t len); 1730*0957b409SSimon J. Gerraty 1731*0957b409SSimon J. Gerraty /** 1732*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_x86ni` implementation). 1733*0957b409SSimon J. Gerraty * 1734*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1735*0957b409SSimon J. Gerraty * \param iv IV (updated). 1736*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1737*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1738*0957b409SSimon J. Gerraty */ 1739*0957b409SSimon J. Gerraty void br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx, void *iv, 1740*0957b409SSimon J. Gerraty void *data, size_t len); 1741*0957b409SSimon J. Gerraty 1742*0957b409SSimon J. Gerraty /** 1743*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_x86ni` implementation). 1744*0957b409SSimon J. Gerraty * 1745*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1746*0957b409SSimon J. Gerraty * \param iv IV (updated). 1747*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1748*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 1749*0957b409SSimon J. Gerraty */ 1750*0957b409SSimon J. Gerraty void br_aes_x86ni_cbcdec_run(const br_aes_x86ni_cbcdec_keys *ctx, void *iv, 1751*0957b409SSimon J. Gerraty void *data, size_t len); 1752*0957b409SSimon J. Gerraty 1753*0957b409SSimon J. Gerraty /** 1754*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_x86ni` implementation). 1755*0957b409SSimon J. Gerraty * 1756*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1757*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 1758*0957b409SSimon J. Gerraty * \param cc initial block counter value. 1759*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1760*0957b409SSimon J. Gerraty * \param len data length (in bytes). 1761*0957b409SSimon J. Gerraty * \return new block counter value. 1762*0957b409SSimon J. Gerraty */ 1763*0957b409SSimon J. Gerraty uint32_t br_aes_x86ni_ctr_run(const br_aes_x86ni_ctr_keys *ctx, 1764*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 1765*0957b409SSimon J. Gerraty 1766*0957b409SSimon J. Gerraty /** 1767*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_x86ni` implementation). 1768*0957b409SSimon J. Gerraty * 1769*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1770*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1771*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1772*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 1773*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1774*0957b409SSimon J. Gerraty */ 1775*0957b409SSimon J. Gerraty void br_aes_x86ni_ctrcbc_encrypt(const br_aes_x86ni_ctrcbc_keys *ctx, 1776*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1777*0957b409SSimon J. Gerraty 1778*0957b409SSimon J. Gerraty /** 1779*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_x86ni` implementation). 1780*0957b409SSimon J. Gerraty * 1781*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1782*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1783*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1784*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 1785*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1786*0957b409SSimon J. Gerraty */ 1787*0957b409SSimon J. Gerraty void br_aes_x86ni_ctrcbc_decrypt(const br_aes_x86ni_ctrcbc_keys *ctx, 1788*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 1789*0957b409SSimon J. Gerraty 1790*0957b409SSimon J. Gerraty /** 1791*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_x86ni` implementation). 1792*0957b409SSimon J. Gerraty * 1793*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1794*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 1795*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 1796*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1797*0957b409SSimon J. Gerraty */ 1798*0957b409SSimon J. Gerraty void br_aes_x86ni_ctrcbc_ctr(const br_aes_x86ni_ctrcbc_keys *ctx, 1799*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 1800*0957b409SSimon J. Gerraty 1801*0957b409SSimon J. Gerraty /** 1802*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_x86ni` implementation). 1803*0957b409SSimon J. Gerraty * 1804*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 1805*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 1806*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 1807*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 1808*0957b409SSimon J. Gerraty */ 1809*0957b409SSimon J. Gerraty void br_aes_x86ni_ctrcbc_mac(const br_aes_x86ni_ctrcbc_keys *ctx, 1810*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 1811*0957b409SSimon J. Gerraty 1812*0957b409SSimon J. Gerraty /** 1813*0957b409SSimon J. Gerraty * \brief Obtain the `aes_x86ni` AES-CBC (encryption) implementation, if 1814*0957b409SSimon J. Gerraty * available. 1815*0957b409SSimon J. Gerraty * 1816*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_x86ni_cbcenc_vtable`, if 1817*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the x86 AES 1818*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 1819*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 1820*0957b409SSimon J. Gerraty * 1821*0957b409SSimon J. Gerraty * \return the `aes_x86ni` AES-CBC (encryption) implementation, or `NULL`. 1822*0957b409SSimon J. Gerraty */ 1823*0957b409SSimon J. Gerraty const br_block_cbcenc_class *br_aes_x86ni_cbcenc_get_vtable(void); 1824*0957b409SSimon J. Gerraty 1825*0957b409SSimon J. Gerraty /** 1826*0957b409SSimon J. Gerraty * \brief Obtain the `aes_x86ni` AES-CBC (decryption) implementation, if 1827*0957b409SSimon J. Gerraty * available. 1828*0957b409SSimon J. Gerraty * 1829*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_x86ni_cbcdec_vtable`, if 1830*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the x86 AES 1831*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 1832*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 1833*0957b409SSimon J. Gerraty * 1834*0957b409SSimon J. Gerraty * \return the `aes_x86ni` AES-CBC (decryption) implementation, or `NULL`. 1835*0957b409SSimon J. Gerraty */ 1836*0957b409SSimon J. Gerraty const br_block_cbcdec_class *br_aes_x86ni_cbcdec_get_vtable(void); 1837*0957b409SSimon J. Gerraty 1838*0957b409SSimon J. Gerraty /** 1839*0957b409SSimon J. Gerraty * \brief Obtain the `aes_x86ni` AES-CTR implementation, if available. 1840*0957b409SSimon J. Gerraty * 1841*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_x86ni_ctr_vtable`, if 1842*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the x86 AES 1843*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 1844*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 1845*0957b409SSimon J. Gerraty * 1846*0957b409SSimon J. Gerraty * \return the `aes_x86ni` AES-CTR implementation, or `NULL`. 1847*0957b409SSimon J. Gerraty */ 1848*0957b409SSimon J. Gerraty const br_block_ctr_class *br_aes_x86ni_ctr_get_vtable(void); 1849*0957b409SSimon J. Gerraty 1850*0957b409SSimon J. Gerraty /** 1851*0957b409SSimon J. Gerraty * \brief Obtain the `aes_x86ni` AES-CTR + CBC-MAC implementation, if 1852*0957b409SSimon J. Gerraty * available. 1853*0957b409SSimon J. Gerraty * 1854*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_x86ni_ctrcbc_vtable`, if 1855*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the x86 AES 1856*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 1857*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 1858*0957b409SSimon J. Gerraty * 1859*0957b409SSimon J. Gerraty * \return the `aes_x86ni` AES-CTR implementation, or `NULL`. 1860*0957b409SSimon J. Gerraty */ 1861*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *br_aes_x86ni_ctrcbc_get_vtable(void); 1862*0957b409SSimon J. Gerraty 1863*0957b409SSimon J. Gerraty /* 1864*0957b409SSimon J. Gerraty * AES implementation using POWER8 opcodes. 1865*0957b409SSimon J. Gerraty */ 1866*0957b409SSimon J. Gerraty 1867*0957b409SSimon J. Gerraty /** \brief AES block size (16 bytes). */ 1868*0957b409SSimon J. Gerraty #define br_aes_pwr8_BLOCK_SIZE 16 1869*0957b409SSimon J. Gerraty 1870*0957b409SSimon J. Gerraty /** 1871*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_pwr8` implementation, CBC encryption). 1872*0957b409SSimon J. Gerraty * 1873*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1874*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1875*0957b409SSimon J. Gerraty */ 1876*0957b409SSimon J. Gerraty typedef struct { 1877*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1878*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 1879*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1880*0957b409SSimon J. Gerraty union { 1881*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1882*0957b409SSimon J. Gerraty } skey; 1883*0957b409SSimon J. Gerraty unsigned num_rounds; 1884*0957b409SSimon J. Gerraty #endif 1885*0957b409SSimon J. Gerraty } br_aes_pwr8_cbcenc_keys; 1886*0957b409SSimon J. Gerraty 1887*0957b409SSimon J. Gerraty /** 1888*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_pwr8` implementation, CBC decryption). 1889*0957b409SSimon J. Gerraty * 1890*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1891*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1892*0957b409SSimon J. Gerraty */ 1893*0957b409SSimon J. Gerraty typedef struct { 1894*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1895*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 1896*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1897*0957b409SSimon J. Gerraty union { 1898*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1899*0957b409SSimon J. Gerraty } skey; 1900*0957b409SSimon J. Gerraty unsigned num_rounds; 1901*0957b409SSimon J. Gerraty #endif 1902*0957b409SSimon J. Gerraty } br_aes_pwr8_cbcdec_keys; 1903*0957b409SSimon J. Gerraty 1904*0957b409SSimon J. Gerraty /** 1905*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption 1906*0957b409SSimon J. Gerraty * and decryption). 1907*0957b409SSimon J. Gerraty * 1908*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1909*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1910*0957b409SSimon J. Gerraty */ 1911*0957b409SSimon J. Gerraty typedef struct { 1912*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1913*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 1914*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1915*0957b409SSimon J. Gerraty union { 1916*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1917*0957b409SSimon J. Gerraty } skey; 1918*0957b409SSimon J. Gerraty unsigned num_rounds; 1919*0957b409SSimon J. Gerraty #endif 1920*0957b409SSimon J. Gerraty } br_aes_pwr8_ctr_keys; 1921*0957b409SSimon J. Gerraty 1922*0957b409SSimon J. Gerraty /** 1923*0957b409SSimon J. Gerraty * \brief Context for AES subkeys (`aes_pwr8` implementation, CTR encryption 1924*0957b409SSimon J. Gerraty * and decryption + CBC-MAC). 1925*0957b409SSimon J. Gerraty * 1926*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 1927*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 1928*0957b409SSimon J. Gerraty */ 1929*0957b409SSimon J. Gerraty typedef struct { 1930*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 1931*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 1932*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 1933*0957b409SSimon J. Gerraty union { 1934*0957b409SSimon J. Gerraty unsigned char skni[16 * 15]; 1935*0957b409SSimon J. Gerraty } skey; 1936*0957b409SSimon J. Gerraty unsigned num_rounds; 1937*0957b409SSimon J. Gerraty #endif 1938*0957b409SSimon J. Gerraty } br_aes_pwr8_ctrcbc_keys; 1939*0957b409SSimon J. Gerraty 1940*0957b409SSimon J. Gerraty /** 1941*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC encryption (`aes_pwr8` implementation). 1942*0957b409SSimon J. Gerraty * 1943*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1944*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1945*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_pwr8_cbcenc_get_vtable()`. 1946*0957b409SSimon J. Gerraty */ 1947*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_aes_pwr8_cbcenc_vtable; 1948*0957b409SSimon J. Gerraty 1949*0957b409SSimon J. Gerraty /** 1950*0957b409SSimon J. Gerraty * \brief Class instance for AES CBC decryption (`aes_pwr8` implementation). 1951*0957b409SSimon J. Gerraty * 1952*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1953*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1954*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_pwr8_cbcdec_get_vtable()`. 1955*0957b409SSimon J. Gerraty */ 1956*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_aes_pwr8_cbcdec_vtable; 1957*0957b409SSimon J. Gerraty 1958*0957b409SSimon J. Gerraty /** 1959*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption and decryption 1960*0957b409SSimon J. Gerraty * (`aes_pwr8` implementation). 1961*0957b409SSimon J. Gerraty * 1962*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1963*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1964*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_pwr8_ctr_get_vtable()`. 1965*0957b409SSimon J. Gerraty */ 1966*0957b409SSimon J. Gerraty extern const br_block_ctr_class br_aes_pwr8_ctr_vtable; 1967*0957b409SSimon J. Gerraty 1968*0957b409SSimon J. Gerraty /** 1969*0957b409SSimon J. Gerraty * \brief Class instance for AES CTR encryption/decryption + CBC-MAC 1970*0957b409SSimon J. Gerraty * (`aes_pwr8` implementation). 1971*0957b409SSimon J. Gerraty * 1972*0957b409SSimon J. Gerraty * Since this implementation might be omitted from the library, or the 1973*0957b409SSimon J. Gerraty * AES opcode unavailable on the current CPU, a pointer to this class 1974*0957b409SSimon J. Gerraty * instance should be obtained through `br_aes_pwr8_ctrcbc_get_vtable()`. 1975*0957b409SSimon J. Gerraty */ 1976*0957b409SSimon J. Gerraty extern const br_block_ctrcbc_class br_aes_pwr8_ctrcbc_vtable; 1977*0957b409SSimon J. Gerraty 1978*0957b409SSimon J. Gerraty /** 1979*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC encryption 1980*0957b409SSimon J. Gerraty * (`aes_pwr8` implementation). 1981*0957b409SSimon J. Gerraty * 1982*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1983*0957b409SSimon J. Gerraty * \param key secret key. 1984*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1985*0957b409SSimon J. Gerraty */ 1986*0957b409SSimon J. Gerraty void br_aes_pwr8_cbcenc_init(br_aes_pwr8_cbcenc_keys *ctx, 1987*0957b409SSimon J. Gerraty const void *key, size_t len); 1988*0957b409SSimon J. Gerraty 1989*0957b409SSimon J. Gerraty /** 1990*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CBC decryption 1991*0957b409SSimon J. Gerraty * (`aes_pwr8` implementation). 1992*0957b409SSimon J. Gerraty * 1993*0957b409SSimon J. Gerraty * \param ctx context to initialise. 1994*0957b409SSimon J. Gerraty * \param key secret key. 1995*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 1996*0957b409SSimon J. Gerraty */ 1997*0957b409SSimon J. Gerraty void br_aes_pwr8_cbcdec_init(br_aes_pwr8_cbcdec_keys *ctx, 1998*0957b409SSimon J. Gerraty const void *key, size_t len); 1999*0957b409SSimon J. Gerraty 2000*0957b409SSimon J. Gerraty /** 2001*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR encryption 2002*0957b409SSimon J. Gerraty * and decryption (`aes_pwr8` implementation). 2003*0957b409SSimon J. Gerraty * 2004*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2005*0957b409SSimon J. Gerraty * \param key secret key. 2006*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2007*0957b409SSimon J. Gerraty */ 2008*0957b409SSimon J. Gerraty void br_aes_pwr8_ctr_init(br_aes_pwr8_ctr_keys *ctx, 2009*0957b409SSimon J. Gerraty const void *key, size_t len); 2010*0957b409SSimon J. Gerraty 2011*0957b409SSimon J. Gerraty /** 2012*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for AES CTR + CBC-MAC 2013*0957b409SSimon J. Gerraty * (`aes_pwr8` implementation). 2014*0957b409SSimon J. Gerraty * 2015*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2016*0957b409SSimon J. Gerraty * \param key secret key. 2017*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2018*0957b409SSimon J. Gerraty */ 2019*0957b409SSimon J. Gerraty void br_aes_pwr8_ctrcbc_init(br_aes_pwr8_ctrcbc_keys *ctx, 2020*0957b409SSimon J. Gerraty const void *key, size_t len); 2021*0957b409SSimon J. Gerraty 2022*0957b409SSimon J. Gerraty /** 2023*0957b409SSimon J. Gerraty * \brief CBC encryption with AES (`aes_pwr8` implementation). 2024*0957b409SSimon J. Gerraty * 2025*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2026*0957b409SSimon J. Gerraty * \param iv IV (updated). 2027*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 2028*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 2029*0957b409SSimon J. Gerraty */ 2030*0957b409SSimon J. Gerraty void br_aes_pwr8_cbcenc_run(const br_aes_pwr8_cbcenc_keys *ctx, void *iv, 2031*0957b409SSimon J. Gerraty void *data, size_t len); 2032*0957b409SSimon J. Gerraty 2033*0957b409SSimon J. Gerraty /** 2034*0957b409SSimon J. Gerraty * \brief CBC decryption with AES (`aes_pwr8` implementation). 2035*0957b409SSimon J. Gerraty * 2036*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2037*0957b409SSimon J. Gerraty * \param iv IV (updated). 2038*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 2039*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 16). 2040*0957b409SSimon J. Gerraty */ 2041*0957b409SSimon J. Gerraty void br_aes_pwr8_cbcdec_run(const br_aes_pwr8_cbcdec_keys *ctx, void *iv, 2042*0957b409SSimon J. Gerraty void *data, size_t len); 2043*0957b409SSimon J. Gerraty 2044*0957b409SSimon J. Gerraty /** 2045*0957b409SSimon J. Gerraty * \brief CTR encryption and decryption with AES (`aes_pwr8` implementation). 2046*0957b409SSimon J. Gerraty * 2047*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2048*0957b409SSimon J. Gerraty * \param iv IV (constant, 12 bytes). 2049*0957b409SSimon J. Gerraty * \param cc initial block counter value. 2050*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 2051*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2052*0957b409SSimon J. Gerraty * \return new block counter value. 2053*0957b409SSimon J. Gerraty */ 2054*0957b409SSimon J. Gerraty uint32_t br_aes_pwr8_ctr_run(const br_aes_pwr8_ctr_keys *ctx, 2055*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 2056*0957b409SSimon J. Gerraty 2057*0957b409SSimon J. Gerraty /** 2058*0957b409SSimon J. Gerraty * \brief CTR encryption + CBC-MAC with AES (`aes_pwr8` implementation). 2059*0957b409SSimon J. Gerraty * 2060*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2061*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 2062*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 2063*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 2064*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 2065*0957b409SSimon J. Gerraty */ 2066*0957b409SSimon J. Gerraty void br_aes_pwr8_ctrcbc_encrypt(const br_aes_pwr8_ctrcbc_keys *ctx, 2067*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 2068*0957b409SSimon J. Gerraty 2069*0957b409SSimon J. Gerraty /** 2070*0957b409SSimon J. Gerraty * \brief CTR decryption + CBC-MAC with AES (`aes_pwr8` implementation). 2071*0957b409SSimon J. Gerraty * 2072*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2073*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 2074*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 2075*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 2076*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 2077*0957b409SSimon J. Gerraty */ 2078*0957b409SSimon J. Gerraty void br_aes_pwr8_ctrcbc_decrypt(const br_aes_pwr8_ctrcbc_keys *ctx, 2079*0957b409SSimon J. Gerraty void *ctr, void *cbcmac, void *data, size_t len); 2080*0957b409SSimon J. Gerraty 2081*0957b409SSimon J. Gerraty /** 2082*0957b409SSimon J. Gerraty * \brief CTR encryption/decryption with AES (`aes_pwr8` implementation). 2083*0957b409SSimon J. Gerraty * 2084*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2085*0957b409SSimon J. Gerraty * \param ctr counter for CTR (16 bytes, updated). 2086*0957b409SSimon J. Gerraty * \param data data to MAC (updated). 2087*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 2088*0957b409SSimon J. Gerraty */ 2089*0957b409SSimon J. Gerraty void br_aes_pwr8_ctrcbc_ctr(const br_aes_pwr8_ctrcbc_keys *ctx, 2090*0957b409SSimon J. Gerraty void *ctr, void *data, size_t len); 2091*0957b409SSimon J. Gerraty 2092*0957b409SSimon J. Gerraty /** 2093*0957b409SSimon J. Gerraty * \brief CBC-MAC with AES (`aes_pwr8` implementation). 2094*0957b409SSimon J. Gerraty * 2095*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2096*0957b409SSimon J. Gerraty * \param cbcmac IV for CBC-MAC (updated). 2097*0957b409SSimon J. Gerraty * \param data data to MAC (unmodified). 2098*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be a multiple of 16). 2099*0957b409SSimon J. Gerraty */ 2100*0957b409SSimon J. Gerraty void br_aes_pwr8_ctrcbc_mac(const br_aes_pwr8_ctrcbc_keys *ctx, 2101*0957b409SSimon J. Gerraty void *cbcmac, const void *data, size_t len); 2102*0957b409SSimon J. Gerraty 2103*0957b409SSimon J. Gerraty /** 2104*0957b409SSimon J. Gerraty * \brief Obtain the `aes_pwr8` AES-CBC (encryption) implementation, if 2105*0957b409SSimon J. Gerraty * available. 2106*0957b409SSimon J. Gerraty * 2107*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_pwr8_cbcenc_vtable`, if 2108*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the POWER8 2109*0957b409SSimon J. Gerraty * crypto opcodes are available on the currently running CPU. If either 2110*0957b409SSimon J. Gerraty * of these conditions is not met, then this function returns `NULL`. 2111*0957b409SSimon J. Gerraty * 2112*0957b409SSimon J. Gerraty * \return the `aes_pwr8` AES-CBC (encryption) implementation, or `NULL`. 2113*0957b409SSimon J. Gerraty */ 2114*0957b409SSimon J. Gerraty const br_block_cbcenc_class *br_aes_pwr8_cbcenc_get_vtable(void); 2115*0957b409SSimon J. Gerraty 2116*0957b409SSimon J. Gerraty /** 2117*0957b409SSimon J. Gerraty * \brief Obtain the `aes_pwr8` AES-CBC (decryption) implementation, if 2118*0957b409SSimon J. Gerraty * available. 2119*0957b409SSimon J. Gerraty * 2120*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_pwr8_cbcdec_vtable`, if 2121*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the POWER8 2122*0957b409SSimon J. Gerraty * crypto opcodes are available on the currently running CPU. If either 2123*0957b409SSimon J. Gerraty * of these conditions is not met, then this function returns `NULL`. 2124*0957b409SSimon J. Gerraty * 2125*0957b409SSimon J. Gerraty * \return the `aes_pwr8` AES-CBC (decryption) implementation, or `NULL`. 2126*0957b409SSimon J. Gerraty */ 2127*0957b409SSimon J. Gerraty const br_block_cbcdec_class *br_aes_pwr8_cbcdec_get_vtable(void); 2128*0957b409SSimon J. Gerraty 2129*0957b409SSimon J. Gerraty /** 2130*0957b409SSimon J. Gerraty * \brief Obtain the `aes_pwr8` AES-CTR implementation, if available. 2131*0957b409SSimon J. Gerraty * 2132*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_pwr8_ctr_vtable`, if that 2133*0957b409SSimon J. Gerraty * implementation was compiled in the library _and_ the POWER8 crypto 2134*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 2135*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 2136*0957b409SSimon J. Gerraty * 2137*0957b409SSimon J. Gerraty * \return the `aes_pwr8` AES-CTR implementation, or `NULL`. 2138*0957b409SSimon J. Gerraty */ 2139*0957b409SSimon J. Gerraty const br_block_ctr_class *br_aes_pwr8_ctr_get_vtable(void); 2140*0957b409SSimon J. Gerraty 2141*0957b409SSimon J. Gerraty /** 2142*0957b409SSimon J. Gerraty * \brief Obtain the `aes_pwr8` AES-CTR + CBC-MAC implementation, if 2143*0957b409SSimon J. Gerraty * available. 2144*0957b409SSimon J. Gerraty * 2145*0957b409SSimon J. Gerraty * This function returns a pointer to `br_aes_pwr8_ctrcbc_vtable`, if 2146*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the POWER8 AES 2147*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 2148*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `NULL`. 2149*0957b409SSimon J. Gerraty * 2150*0957b409SSimon J. Gerraty * \return the `aes_pwr8` AES-CTR implementation, or `NULL`. 2151*0957b409SSimon J. Gerraty */ 2152*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *br_aes_pwr8_ctrcbc_get_vtable(void); 2153*0957b409SSimon J. Gerraty 2154*0957b409SSimon J. Gerraty /** 2155*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2156*0957b409SSimon J. Gerraty * subkeys (CBC encryption) for all AES implementations. 2157*0957b409SSimon J. Gerraty */ 2158*0957b409SSimon J. Gerraty typedef union { 2159*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 2160*0957b409SSimon J. Gerraty br_aes_big_cbcenc_keys c_big; 2161*0957b409SSimon J. Gerraty br_aes_small_cbcenc_keys c_small; 2162*0957b409SSimon J. Gerraty br_aes_ct_cbcenc_keys c_ct; 2163*0957b409SSimon J. Gerraty br_aes_ct64_cbcenc_keys c_ct64; 2164*0957b409SSimon J. Gerraty br_aes_x86ni_cbcenc_keys c_x86ni; 2165*0957b409SSimon J. Gerraty br_aes_pwr8_cbcenc_keys c_pwr8; 2166*0957b409SSimon J. Gerraty } br_aes_gen_cbcenc_keys; 2167*0957b409SSimon J. Gerraty 2168*0957b409SSimon J. Gerraty /** 2169*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2170*0957b409SSimon J. Gerraty * subkeys (CBC decryption) for all AES implementations. 2171*0957b409SSimon J. Gerraty */ 2172*0957b409SSimon J. Gerraty typedef union { 2173*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 2174*0957b409SSimon J. Gerraty br_aes_big_cbcdec_keys c_big; 2175*0957b409SSimon J. Gerraty br_aes_small_cbcdec_keys c_small; 2176*0957b409SSimon J. Gerraty br_aes_ct_cbcdec_keys c_ct; 2177*0957b409SSimon J. Gerraty br_aes_ct64_cbcdec_keys c_ct64; 2178*0957b409SSimon J. Gerraty br_aes_x86ni_cbcdec_keys c_x86ni; 2179*0957b409SSimon J. Gerraty br_aes_pwr8_cbcdec_keys c_pwr8; 2180*0957b409SSimon J. Gerraty } br_aes_gen_cbcdec_keys; 2181*0957b409SSimon J. Gerraty 2182*0957b409SSimon J. Gerraty /** 2183*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2184*0957b409SSimon J. Gerraty * subkeys (CTR encryption and decryption) for all AES implementations. 2185*0957b409SSimon J. Gerraty */ 2186*0957b409SSimon J. Gerraty typedef union { 2187*0957b409SSimon J. Gerraty const br_block_ctr_class *vtable; 2188*0957b409SSimon J. Gerraty br_aes_big_ctr_keys c_big; 2189*0957b409SSimon J. Gerraty br_aes_small_ctr_keys c_small; 2190*0957b409SSimon J. Gerraty br_aes_ct_ctr_keys c_ct; 2191*0957b409SSimon J. Gerraty br_aes_ct64_ctr_keys c_ct64; 2192*0957b409SSimon J. Gerraty br_aes_x86ni_ctr_keys c_x86ni; 2193*0957b409SSimon J. Gerraty br_aes_pwr8_ctr_keys c_pwr8; 2194*0957b409SSimon J. Gerraty } br_aes_gen_ctr_keys; 2195*0957b409SSimon J. Gerraty 2196*0957b409SSimon J. Gerraty /** 2197*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2198*0957b409SSimon J. Gerraty * subkeys (CTR encryption/decryption + CBC-MAC) for all AES implementations. 2199*0957b409SSimon J. Gerraty */ 2200*0957b409SSimon J. Gerraty typedef union { 2201*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *vtable; 2202*0957b409SSimon J. Gerraty br_aes_big_ctrcbc_keys c_big; 2203*0957b409SSimon J. Gerraty br_aes_small_ctrcbc_keys c_small; 2204*0957b409SSimon J. Gerraty br_aes_ct_ctrcbc_keys c_ct; 2205*0957b409SSimon J. Gerraty br_aes_ct64_ctrcbc_keys c_ct64; 2206*0957b409SSimon J. Gerraty br_aes_x86ni_ctrcbc_keys c_x86ni; 2207*0957b409SSimon J. Gerraty br_aes_pwr8_ctrcbc_keys c_pwr8; 2208*0957b409SSimon J. Gerraty } br_aes_gen_ctrcbc_keys; 2209*0957b409SSimon J. Gerraty 2210*0957b409SSimon J. Gerraty /* 2211*0957b409SSimon J. Gerraty * Traditional, table-based implementation for DES/3DES. Since tables are 2212*0957b409SSimon J. Gerraty * used, cache-timing attacks are conceptually possible. 2213*0957b409SSimon J. Gerraty */ 2214*0957b409SSimon J. Gerraty 2215*0957b409SSimon J. Gerraty /** \brief DES/3DES block size (8 bytes). */ 2216*0957b409SSimon J. Gerraty #define br_des_tab_BLOCK_SIZE 8 2217*0957b409SSimon J. Gerraty 2218*0957b409SSimon J. Gerraty /** 2219*0957b409SSimon J. Gerraty * \brief Context for DES subkeys (`des_tab` implementation, CBC encryption). 2220*0957b409SSimon J. Gerraty * 2221*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 2222*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 2223*0957b409SSimon J. Gerraty */ 2224*0957b409SSimon J. Gerraty typedef struct { 2225*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 2226*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 2227*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 2228*0957b409SSimon J. Gerraty uint32_t skey[96]; 2229*0957b409SSimon J. Gerraty unsigned num_rounds; 2230*0957b409SSimon J. Gerraty #endif 2231*0957b409SSimon J. Gerraty } br_des_tab_cbcenc_keys; 2232*0957b409SSimon J. Gerraty 2233*0957b409SSimon J. Gerraty /** 2234*0957b409SSimon J. Gerraty * \brief Context for DES subkeys (`des_tab` implementation, CBC decryption). 2235*0957b409SSimon J. Gerraty * 2236*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 2237*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 2238*0957b409SSimon J. Gerraty */ 2239*0957b409SSimon J. Gerraty typedef struct { 2240*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 2241*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 2242*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 2243*0957b409SSimon J. Gerraty uint32_t skey[96]; 2244*0957b409SSimon J. Gerraty unsigned num_rounds; 2245*0957b409SSimon J. Gerraty #endif 2246*0957b409SSimon J. Gerraty } br_des_tab_cbcdec_keys; 2247*0957b409SSimon J. Gerraty 2248*0957b409SSimon J. Gerraty /** 2249*0957b409SSimon J. Gerraty * \brief Class instance for DES CBC encryption (`des_tab` implementation). 2250*0957b409SSimon J. Gerraty */ 2251*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_des_tab_cbcenc_vtable; 2252*0957b409SSimon J. Gerraty 2253*0957b409SSimon J. Gerraty /** 2254*0957b409SSimon J. Gerraty * \brief Class instance for DES CBC decryption (`des_tab` implementation). 2255*0957b409SSimon J. Gerraty */ 2256*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_des_tab_cbcdec_vtable; 2257*0957b409SSimon J. Gerraty 2258*0957b409SSimon J. Gerraty /** 2259*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for DES CBC encryption 2260*0957b409SSimon J. Gerraty * (`des_tab` implementation). 2261*0957b409SSimon J. Gerraty * 2262*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2263*0957b409SSimon J. Gerraty * \param key secret key. 2264*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2265*0957b409SSimon J. Gerraty */ 2266*0957b409SSimon J. Gerraty void br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx, 2267*0957b409SSimon J. Gerraty const void *key, size_t len); 2268*0957b409SSimon J. Gerraty 2269*0957b409SSimon J. Gerraty /** 2270*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for DES CBC decryption 2271*0957b409SSimon J. Gerraty * (`des_tab` implementation). 2272*0957b409SSimon J. Gerraty * 2273*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2274*0957b409SSimon J. Gerraty * \param key secret key. 2275*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2276*0957b409SSimon J. Gerraty */ 2277*0957b409SSimon J. Gerraty void br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx, 2278*0957b409SSimon J. Gerraty const void *key, size_t len); 2279*0957b409SSimon J. Gerraty 2280*0957b409SSimon J. Gerraty /** 2281*0957b409SSimon J. Gerraty * \brief CBC encryption with DES (`des_tab` implementation). 2282*0957b409SSimon J. Gerraty * 2283*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2284*0957b409SSimon J. Gerraty * \param iv IV (updated). 2285*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 2286*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 8). 2287*0957b409SSimon J. Gerraty */ 2288*0957b409SSimon J. Gerraty void br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx, void *iv, 2289*0957b409SSimon J. Gerraty void *data, size_t len); 2290*0957b409SSimon J. Gerraty 2291*0957b409SSimon J. Gerraty /** 2292*0957b409SSimon J. Gerraty * \brief CBC decryption with DES (`des_tab` implementation). 2293*0957b409SSimon J. Gerraty * 2294*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2295*0957b409SSimon J. Gerraty * \param iv IV (updated). 2296*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 2297*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 8). 2298*0957b409SSimon J. Gerraty */ 2299*0957b409SSimon J. Gerraty void br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx, void *iv, 2300*0957b409SSimon J. Gerraty void *data, size_t len); 2301*0957b409SSimon J. Gerraty 2302*0957b409SSimon J. Gerraty /* 2303*0957b409SSimon J. Gerraty * Constant-time implementation for DES/3DES. It is substantially slower 2304*0957b409SSimon J. Gerraty * (by a factor of about 4x), but also immune to cache-timing attacks. 2305*0957b409SSimon J. Gerraty */ 2306*0957b409SSimon J. Gerraty 2307*0957b409SSimon J. Gerraty /** \brief DES/3DES block size (8 bytes). */ 2308*0957b409SSimon J. Gerraty #define br_des_ct_BLOCK_SIZE 8 2309*0957b409SSimon J. Gerraty 2310*0957b409SSimon J. Gerraty /** 2311*0957b409SSimon J. Gerraty * \brief Context for DES subkeys (`des_ct` implementation, CBC encryption). 2312*0957b409SSimon J. Gerraty * 2313*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 2314*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 2315*0957b409SSimon J. Gerraty */ 2316*0957b409SSimon J. Gerraty typedef struct { 2317*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 2318*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 2319*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 2320*0957b409SSimon J. Gerraty uint32_t skey[96]; 2321*0957b409SSimon J. Gerraty unsigned num_rounds; 2322*0957b409SSimon J. Gerraty #endif 2323*0957b409SSimon J. Gerraty } br_des_ct_cbcenc_keys; 2324*0957b409SSimon J. Gerraty 2325*0957b409SSimon J. Gerraty /** 2326*0957b409SSimon J. Gerraty * \brief Context for DES subkeys (`des_ct` implementation, CBC decryption). 2327*0957b409SSimon J. Gerraty * 2328*0957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation 2329*0957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code. 2330*0957b409SSimon J. Gerraty */ 2331*0957b409SSimon J. Gerraty typedef struct { 2332*0957b409SSimon J. Gerraty /** \brief Pointer to vtable for this context. */ 2333*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 2334*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 2335*0957b409SSimon J. Gerraty uint32_t skey[96]; 2336*0957b409SSimon J. Gerraty unsigned num_rounds; 2337*0957b409SSimon J. Gerraty #endif 2338*0957b409SSimon J. Gerraty } br_des_ct_cbcdec_keys; 2339*0957b409SSimon J. Gerraty 2340*0957b409SSimon J. Gerraty /** 2341*0957b409SSimon J. Gerraty * \brief Class instance for DES CBC encryption (`des_ct` implementation). 2342*0957b409SSimon J. Gerraty */ 2343*0957b409SSimon J. Gerraty extern const br_block_cbcenc_class br_des_ct_cbcenc_vtable; 2344*0957b409SSimon J. Gerraty 2345*0957b409SSimon J. Gerraty /** 2346*0957b409SSimon J. Gerraty * \brief Class instance for DES CBC decryption (`des_ct` implementation). 2347*0957b409SSimon J. Gerraty */ 2348*0957b409SSimon J. Gerraty extern const br_block_cbcdec_class br_des_ct_cbcdec_vtable; 2349*0957b409SSimon J. Gerraty 2350*0957b409SSimon J. Gerraty /** 2351*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for DES CBC encryption 2352*0957b409SSimon J. Gerraty * (`des_ct` implementation). 2353*0957b409SSimon J. Gerraty * 2354*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2355*0957b409SSimon J. Gerraty * \param key secret key. 2356*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2357*0957b409SSimon J. Gerraty */ 2358*0957b409SSimon J. Gerraty void br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx, 2359*0957b409SSimon J. Gerraty const void *key, size_t len); 2360*0957b409SSimon J. Gerraty 2361*0957b409SSimon J. Gerraty /** 2362*0957b409SSimon J. Gerraty * \brief Context initialisation (key schedule) for DES CBC decryption 2363*0957b409SSimon J. Gerraty * (`des_ct` implementation). 2364*0957b409SSimon J. Gerraty * 2365*0957b409SSimon J. Gerraty * \param ctx context to initialise. 2366*0957b409SSimon J. Gerraty * \param key secret key. 2367*0957b409SSimon J. Gerraty * \param len secret key length (in bytes). 2368*0957b409SSimon J. Gerraty */ 2369*0957b409SSimon J. Gerraty void br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx, 2370*0957b409SSimon J. Gerraty const void *key, size_t len); 2371*0957b409SSimon J. Gerraty 2372*0957b409SSimon J. Gerraty /** 2373*0957b409SSimon J. Gerraty * \brief CBC encryption with DES (`des_ct` implementation). 2374*0957b409SSimon J. Gerraty * 2375*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2376*0957b409SSimon J. Gerraty * \param iv IV (updated). 2377*0957b409SSimon J. Gerraty * \param data data to encrypt (updated). 2378*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 8). 2379*0957b409SSimon J. Gerraty */ 2380*0957b409SSimon J. Gerraty void br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx, void *iv, 2381*0957b409SSimon J. Gerraty void *data, size_t len); 2382*0957b409SSimon J. Gerraty 2383*0957b409SSimon J. Gerraty /** 2384*0957b409SSimon J. Gerraty * \brief CBC decryption with DES (`des_ct` implementation). 2385*0957b409SSimon J. Gerraty * 2386*0957b409SSimon J. Gerraty * \param ctx context (already initialised). 2387*0957b409SSimon J. Gerraty * \param iv IV (updated). 2388*0957b409SSimon J. Gerraty * \param data data to decrypt (updated). 2389*0957b409SSimon J. Gerraty * \param len data length (in bytes, MUST be multiple of 8). 2390*0957b409SSimon J. Gerraty */ 2391*0957b409SSimon J. Gerraty void br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx, void *iv, 2392*0957b409SSimon J. Gerraty void *data, size_t len); 2393*0957b409SSimon J. Gerraty 2394*0957b409SSimon J. Gerraty /* 2395*0957b409SSimon J. Gerraty * These structures are large enough to accommodate subkeys for all 2396*0957b409SSimon J. Gerraty * DES/3DES implementations. 2397*0957b409SSimon J. Gerraty */ 2398*0957b409SSimon J. Gerraty 2399*0957b409SSimon J. Gerraty /** 2400*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2401*0957b409SSimon J. Gerraty * subkeys (CBC encryption) for all DES implementations. 2402*0957b409SSimon J. Gerraty */ 2403*0957b409SSimon J. Gerraty typedef union { 2404*0957b409SSimon J. Gerraty const br_block_cbcenc_class *vtable; 2405*0957b409SSimon J. Gerraty br_des_tab_cbcenc_keys tab; 2406*0957b409SSimon J. Gerraty br_des_ct_cbcenc_keys ct; 2407*0957b409SSimon J. Gerraty } br_des_gen_cbcenc_keys; 2408*0957b409SSimon J. Gerraty 2409*0957b409SSimon J. Gerraty /** 2410*0957b409SSimon J. Gerraty * \brief Aggregate structure large enough to be used as context for 2411*0957b409SSimon J. Gerraty * subkeys (CBC decryption) for all DES implementations. 2412*0957b409SSimon J. Gerraty */ 2413*0957b409SSimon J. Gerraty typedef union { 2414*0957b409SSimon J. Gerraty const br_block_cbcdec_class *vtable; 2415*0957b409SSimon J. Gerraty br_des_tab_cbcdec_keys c_tab; 2416*0957b409SSimon J. Gerraty br_des_ct_cbcdec_keys c_ct; 2417*0957b409SSimon J. Gerraty } br_des_gen_cbcdec_keys; 2418*0957b409SSimon J. Gerraty 2419*0957b409SSimon J. Gerraty /** 2420*0957b409SSimon J. Gerraty * \brief Type for a ChaCha20 implementation. 2421*0957b409SSimon J. Gerraty * 2422*0957b409SSimon J. Gerraty * An implementation follows the description in RFC 7539: 2423*0957b409SSimon J. Gerraty * 2424*0957b409SSimon J. Gerraty * - Key is 256 bits (`key` points to exactly 32 bytes). 2425*0957b409SSimon J. Gerraty * 2426*0957b409SSimon J. Gerraty * - IV is 96 bits (`iv` points to exactly 12 bytes). 2427*0957b409SSimon J. Gerraty * 2428*0957b409SSimon J. Gerraty * - Block counter is over 32 bits and starts at value `cc`; the 2429*0957b409SSimon J. Gerraty * resulting value is returned. 2430*0957b409SSimon J. Gerraty * 2431*0957b409SSimon J. Gerraty * Data (pointed to by `data`, of length `len`) is encrypted/decrypted 2432*0957b409SSimon J. Gerraty * in place. If `len` is not a multiple of 64, then the excess bytes from 2433*0957b409SSimon J. Gerraty * the last block processing are dropped (therefore, "chunked" processing 2434*0957b409SSimon J. Gerraty * works only as long as each non-final chunk has a length multiple of 64). 2435*0957b409SSimon J. Gerraty * 2436*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2437*0957b409SSimon J. Gerraty * \param iv IV (12 bytes). 2438*0957b409SSimon J. Gerraty * \param cc initial counter value. 2439*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2440*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2441*0957b409SSimon J. Gerraty */ 2442*0957b409SSimon J. Gerraty typedef uint32_t (*br_chacha20_run)(const void *key, 2443*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 2444*0957b409SSimon J. Gerraty 2445*0957b409SSimon J. Gerraty /** 2446*0957b409SSimon J. Gerraty * \brief ChaCha20 implementation (straightforward C code, constant-time). 2447*0957b409SSimon J. Gerraty * 2448*0957b409SSimon J. Gerraty * \see br_chacha20_run 2449*0957b409SSimon J. Gerraty * 2450*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2451*0957b409SSimon J. Gerraty * \param iv IV (12 bytes). 2452*0957b409SSimon J. Gerraty * \param cc initial counter value. 2453*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2454*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2455*0957b409SSimon J. Gerraty */ 2456*0957b409SSimon J. Gerraty uint32_t br_chacha20_ct_run(const void *key, 2457*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 2458*0957b409SSimon J. Gerraty 2459*0957b409SSimon J. Gerraty /** 2460*0957b409SSimon J. Gerraty * \brief ChaCha20 implementation (SSE2 code, constant-time). 2461*0957b409SSimon J. Gerraty * 2462*0957b409SSimon J. Gerraty * This implementation is available only on x86 platforms, depending on 2463*0957b409SSimon J. Gerraty * compiler support. Moreover, in 32-bit mode, it might not actually run, 2464*0957b409SSimon J. Gerraty * if the underlying hardware does not implement the SSE2 opcode (in 2465*0957b409SSimon J. Gerraty * 64-bit mode, SSE2 is part of the ABI, so if the code could be compiled 2466*0957b409SSimon J. Gerraty * at all, then it can run). Use `br_chacha20_sse2_get()` to safely obtain 2467*0957b409SSimon J. Gerraty * a pointer to that function. 2468*0957b409SSimon J. Gerraty * 2469*0957b409SSimon J. Gerraty * \see br_chacha20_run 2470*0957b409SSimon J. Gerraty * 2471*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2472*0957b409SSimon J. Gerraty * \param iv IV (12 bytes). 2473*0957b409SSimon J. Gerraty * \param cc initial counter value. 2474*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2475*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2476*0957b409SSimon J. Gerraty */ 2477*0957b409SSimon J. Gerraty uint32_t br_chacha20_sse2_run(const void *key, 2478*0957b409SSimon J. Gerraty const void *iv, uint32_t cc, void *data, size_t len); 2479*0957b409SSimon J. Gerraty 2480*0957b409SSimon J. Gerraty /** 2481*0957b409SSimon J. Gerraty * \brief Obtain the `sse2` ChaCha20 implementation, if available. 2482*0957b409SSimon J. Gerraty * 2483*0957b409SSimon J. Gerraty * This function returns a pointer to `br_chacha20_sse2_run`, if 2484*0957b409SSimon J. Gerraty * that implementation was compiled in the library _and_ the SSE2 2485*0957b409SSimon J. Gerraty * opcodes are available on the currently running CPU. If either of 2486*0957b409SSimon J. Gerraty * these conditions is not met, then this function returns `0`. 2487*0957b409SSimon J. Gerraty * 2488*0957b409SSimon J. Gerraty * \return the `sse2` ChaCha20 implementation, or `0`. 2489*0957b409SSimon J. Gerraty */ 2490*0957b409SSimon J. Gerraty br_chacha20_run br_chacha20_sse2_get(void); 2491*0957b409SSimon J. Gerraty 2492*0957b409SSimon J. Gerraty /** 2493*0957b409SSimon J. Gerraty * \brief Type for a ChaCha20+Poly1305 AEAD implementation. 2494*0957b409SSimon J. Gerraty * 2495*0957b409SSimon J. Gerraty * The provided data is encrypted or decrypted with ChaCha20. The 2496*0957b409SSimon J. Gerraty * authentication tag is computed on the concatenation of the 2497*0957b409SSimon J. Gerraty * additional data and the ciphertext, with the padding and lengths 2498*0957b409SSimon J. Gerraty * as described in RFC 7539 (section 2.8). 2499*0957b409SSimon J. Gerraty * 2500*0957b409SSimon J. Gerraty * After decryption, the caller is responsible for checking that the 2501*0957b409SSimon J. Gerraty * computed tag matches the expected value. 2502*0957b409SSimon J. Gerraty * 2503*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2504*0957b409SSimon J. Gerraty * \param iv nonce (12 bytes). 2505*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2506*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2507*0957b409SSimon J. Gerraty * \param aad additional authenticated data. 2508*0957b409SSimon J. Gerraty * \param aad_len length of additional authenticated data (in bytes). 2509*0957b409SSimon J. Gerraty * \param tag output buffer for the authentication tag. 2510*0957b409SSimon J. Gerraty * \param ichacha implementation of ChaCha20. 2511*0957b409SSimon J. Gerraty * \param encrypt non-zero for encryption, zero for decryption. 2512*0957b409SSimon J. Gerraty */ 2513*0957b409SSimon J. Gerraty typedef void (*br_poly1305_run)(const void *key, const void *iv, 2514*0957b409SSimon J. Gerraty void *data, size_t len, const void *aad, size_t aad_len, 2515*0957b409SSimon J. Gerraty void *tag, br_chacha20_run ichacha, int encrypt); 2516*0957b409SSimon J. Gerraty 2517*0957b409SSimon J. Gerraty /** 2518*0957b409SSimon J. Gerraty * \brief ChaCha20+Poly1305 AEAD implementation (mixed 32-bit multiplications). 2519*0957b409SSimon J. Gerraty * 2520*0957b409SSimon J. Gerraty * \see br_poly1305_run 2521*0957b409SSimon J. Gerraty * 2522*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2523*0957b409SSimon J. Gerraty * \param iv nonce (12 bytes). 2524*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2525*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2526*0957b409SSimon J. Gerraty * \param aad additional authenticated data. 2527*0957b409SSimon J. Gerraty * \param aad_len length of additional authenticated data (in bytes). 2528*0957b409SSimon J. Gerraty * \param tag output buffer for the authentication tag. 2529*0957b409SSimon J. Gerraty * \param ichacha implementation of ChaCha20. 2530*0957b409SSimon J. Gerraty * \param encrypt non-zero for encryption, zero for decryption. 2531*0957b409SSimon J. Gerraty */ 2532*0957b409SSimon J. Gerraty void br_poly1305_ctmul_run(const void *key, const void *iv, 2533*0957b409SSimon J. Gerraty void *data, size_t len, const void *aad, size_t aad_len, 2534*0957b409SSimon J. Gerraty void *tag, br_chacha20_run ichacha, int encrypt); 2535*0957b409SSimon J. Gerraty 2536*0957b409SSimon J. Gerraty /** 2537*0957b409SSimon J. Gerraty * \brief ChaCha20+Poly1305 AEAD implementation (pure 32-bit multiplications). 2538*0957b409SSimon J. Gerraty * 2539*0957b409SSimon J. Gerraty * \see br_poly1305_run 2540*0957b409SSimon J. Gerraty * 2541*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2542*0957b409SSimon J. Gerraty * \param iv nonce (12 bytes). 2543*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2544*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2545*0957b409SSimon J. Gerraty * \param aad additional authenticated data. 2546*0957b409SSimon J. Gerraty * \param aad_len length of additional authenticated data (in bytes). 2547*0957b409SSimon J. Gerraty * \param tag output buffer for the authentication tag. 2548*0957b409SSimon J. Gerraty * \param ichacha implementation of ChaCha20. 2549*0957b409SSimon J. Gerraty * \param encrypt non-zero for encryption, zero for decryption. 2550*0957b409SSimon J. Gerraty */ 2551*0957b409SSimon J. Gerraty void br_poly1305_ctmul32_run(const void *key, const void *iv, 2552*0957b409SSimon J. Gerraty void *data, size_t len, const void *aad, size_t aad_len, 2553*0957b409SSimon J. Gerraty void *tag, br_chacha20_run ichacha, int encrypt); 2554*0957b409SSimon J. Gerraty 2555*0957b409SSimon J. Gerraty /** 2556*0957b409SSimon J. Gerraty * \brief ChaCha20+Poly1305 AEAD implementation (i15). 2557*0957b409SSimon J. Gerraty * 2558*0957b409SSimon J. Gerraty * This implementation relies on the generic big integer code "i15" 2559*0957b409SSimon J. Gerraty * (which uses pure 32-bit multiplications). As such, it may save a 2560*0957b409SSimon J. Gerraty * little code footprint in a context where "i15" is already included 2561*0957b409SSimon J. Gerraty * (e.g. for elliptic curves or for RSA); however, it is also 2562*0957b409SSimon J. Gerraty * substantially slower than the ctmul and ctmul32 implementations. 2563*0957b409SSimon J. Gerraty * 2564*0957b409SSimon J. Gerraty * \see br_poly1305_run 2565*0957b409SSimon J. Gerraty * 2566*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2567*0957b409SSimon J. Gerraty * \param iv nonce (12 bytes). 2568*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2569*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2570*0957b409SSimon J. Gerraty * \param aad additional authenticated data. 2571*0957b409SSimon J. Gerraty * \param aad_len length of additional authenticated data (in bytes). 2572*0957b409SSimon J. Gerraty * \param tag output buffer for the authentication tag. 2573*0957b409SSimon J. Gerraty * \param ichacha implementation of ChaCha20. 2574*0957b409SSimon J. Gerraty * \param encrypt non-zero for encryption, zero for decryption. 2575*0957b409SSimon J. Gerraty */ 2576*0957b409SSimon J. Gerraty void br_poly1305_i15_run(const void *key, const void *iv, 2577*0957b409SSimon J. Gerraty void *data, size_t len, const void *aad, size_t aad_len, 2578*0957b409SSimon J. Gerraty void *tag, br_chacha20_run ichacha, int encrypt); 2579*0957b409SSimon J. Gerraty 2580*0957b409SSimon J. Gerraty /** 2581*0957b409SSimon J. Gerraty * \brief ChaCha20+Poly1305 AEAD implementation (ctmulq). 2582*0957b409SSimon J. Gerraty * 2583*0957b409SSimon J. Gerraty * This implementation uses 64-bit multiplications (result over 128 bits). 2584*0957b409SSimon J. Gerraty * It is available only on platforms that offer such a primitive (in 2585*0957b409SSimon J. Gerraty * practice, 64-bit architectures). Use `br_poly1305_ctmulq_get()` to 2586*0957b409SSimon J. Gerraty * dynamically obtain a pointer to that function, or 0 if not supported. 2587*0957b409SSimon J. Gerraty * 2588*0957b409SSimon J. Gerraty * \see br_poly1305_run 2589*0957b409SSimon J. Gerraty * 2590*0957b409SSimon J. Gerraty * \param key secret key (32 bytes). 2591*0957b409SSimon J. Gerraty * \param iv nonce (12 bytes). 2592*0957b409SSimon J. Gerraty * \param data data to encrypt or decrypt. 2593*0957b409SSimon J. Gerraty * \param len data length (in bytes). 2594*0957b409SSimon J. Gerraty * \param aad additional authenticated data. 2595*0957b409SSimon J. Gerraty * \param aad_len length of additional authenticated data (in bytes). 2596*0957b409SSimon J. Gerraty * \param tag output buffer for the authentication tag. 2597*0957b409SSimon J. Gerraty * \param ichacha implementation of ChaCha20. 2598*0957b409SSimon J. Gerraty * \param encrypt non-zero for encryption, zero for decryption. 2599*0957b409SSimon J. Gerraty */ 2600*0957b409SSimon J. Gerraty void br_poly1305_ctmulq_run(const void *key, const void *iv, 2601*0957b409SSimon J. Gerraty void *data, size_t len, const void *aad, size_t aad_len, 2602*0957b409SSimon J. Gerraty void *tag, br_chacha20_run ichacha, int encrypt); 2603*0957b409SSimon J. Gerraty 2604*0957b409SSimon J. Gerraty /** 2605*0957b409SSimon J. Gerraty * \brief Get the ChaCha20+Poly1305 "ctmulq" implementation, if available. 2606*0957b409SSimon J. Gerraty * 2607*0957b409SSimon J. Gerraty * This function returns a pointer to the `br_poly1305_ctmulq_run()` 2608*0957b409SSimon J. Gerraty * function if supported on the current platform; otherwise, it returns 0. 2609*0957b409SSimon J. Gerraty * 2610*0957b409SSimon J. Gerraty * \return the ctmulq ChaCha20+Poly1305 implementation, or 0. 2611*0957b409SSimon J. Gerraty */ 2612*0957b409SSimon J. Gerraty br_poly1305_run br_poly1305_ctmulq_get(void); 2613*0957b409SSimon J. Gerraty 2614*0957b409SSimon J. Gerraty #ifdef __cplusplus 2615*0957b409SSimon J. Gerraty } 2616*0957b409SSimon J. Gerraty #endif 2617*0957b409SSimon J. Gerraty 2618*0957b409SSimon J. Gerraty #endif 2619