1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Common values for AES algorithms 4 */ 5 6 #ifndef _CRYPTO_AES_H 7 #define _CRYPTO_AES_H 8 9 #include <linux/types.h> 10 #include <linux/crypto.h> 11 12 #define AES_MIN_KEY_SIZE 16 13 #define AES_MAX_KEY_SIZE 32 14 #define AES_KEYSIZE_128 16 15 #define AES_KEYSIZE_192 24 16 #define AES_KEYSIZE_256 32 17 #define AES_BLOCK_SIZE 16 18 #define AES_MAX_KEYLENGTH (15 * 16) 19 #define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32)) 20 21 union aes_enckey_arch { 22 u32 rndkeys[AES_MAX_KEYLENGTH_U32]; 23 }; 24 25 union aes_invkey_arch { 26 u32 inv_rndkeys[AES_MAX_KEYLENGTH_U32]; 27 }; 28 29 /** 30 * struct aes_enckey - An AES key prepared for encryption 31 * @len: Key length in bytes: 16 for AES-128, 24 for AES-192, 32 for AES-256. 32 * @nrounds: Number of rounds: 10 for AES-128, 12 for AES-192, 14 for AES-256. 33 * This is '6 + @len / 4' and is cached so that AES implementations 34 * that need it don't have to recompute it for each en/decryption. 35 * @padding: Padding to make offsetof(@k) be a multiple of 16, so that aligning 36 * this struct to a 16-byte boundary results in @k also being 16-byte 37 * aligned. Users aren't required to align this struct to 16 bytes, 38 * but it may slightly improve performance. 39 * @k: This typically contains the AES round keys as an array of '@nrounds + 1' 40 * groups of four u32 words. However, architecture-specific implementations 41 * of AES may store something else here, e.g. just the raw key if it's all 42 * they need. 43 * 44 * Note that this struct is about half the size of struct aes_key. This is 45 * separate from struct aes_key so that modes that need only AES encryption 46 * (e.g. AES-GCM, AES-CTR, AES-CMAC, tweak key in AES-XTS) don't incur the time 47 * and space overhead of computing and caching the decryption round keys. 48 * 49 * Note that there's no decryption-only equivalent (i.e. "struct aes_deckey"), 50 * since (a) it's rare that modes need decryption-only, and (b) some AES 51 * implementations use the same @k for both encryption and decryption, either 52 * always or conditionally; in the latter case both @k and @inv_k are needed. 53 */ 54 struct aes_enckey { 55 u32 len; 56 u32 nrounds; 57 u32 padding[2]; 58 union aes_enckey_arch k; 59 }; 60 61 /** 62 * struct aes_key - An AES key prepared for encryption and decryption 63 * @aes_enckey: Common fields and the key prepared for encryption 64 * @inv_k: This generally contains the round keys for the AES Equivalent 65 * Inverse Cipher, as an array of '@nrounds + 1' groups of four u32 66 * words. However, architecture-specific implementations of AES may 67 * store something else here. For example, they may leave this field 68 * uninitialized if they use @k for both encryption and decryption. 69 */ 70 struct aes_key { 71 struct aes_enckey; /* Include all fields of aes_enckey. */ 72 union aes_invkey_arch inv_k; 73 }; 74 75 /* 76 * Please ensure that the first two fields are 16-byte aligned 77 * relative to the start of the structure, i.e., don't move them! 78 */ 79 struct crypto_aes_ctx { 80 u32 key_enc[AES_MAX_KEYLENGTH_U32]; 81 u32 key_dec[AES_MAX_KEYLENGTH_U32]; 82 u32 key_length; 83 }; 84 85 /* 86 * validate key length for AES algorithms 87 */ 88 static inline int aes_check_keylen(size_t keylen) 89 { 90 switch (keylen) { 91 case AES_KEYSIZE_128: 92 case AES_KEYSIZE_192: 93 case AES_KEYSIZE_256: 94 break; 95 default: 96 return -EINVAL; 97 } 98 99 return 0; 100 } 101 102 /** 103 * aes_expandkey - Expands the AES key as described in FIPS-197 104 * @ctx: The location where the computed key will be stored. 105 * @in_key: The supplied key. 106 * @key_len: The length of the supplied key. 107 * 108 * Returns 0 on success. The function fails only if an invalid key size (or 109 * pointer) is supplied. 110 * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes 111 * key schedule plus a 16 bytes key which is used before the first round). 112 * The decryption key is prepared for the "Equivalent Inverse Cipher" as 113 * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is 114 * for the initial combination, the second slot for the first round and so on. 115 */ 116 int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, 117 unsigned int key_len); 118 119 /** 120 * aes_preparekey() - Prepare an AES key for encryption and decryption 121 * @key: (output) The key structure to initialize 122 * @in_key: The raw AES key 123 * @key_len: Length of the raw key in bytes. Should be either AES_KEYSIZE_128, 124 * AES_KEYSIZE_192, or AES_KEYSIZE_256. 125 * 126 * This prepares an AES key for both the encryption and decryption directions of 127 * the block cipher. Typically this involves expanding the raw key into both 128 * the standard round keys and the Equivalent Inverse Cipher round keys, but 129 * some architecture-specific implementations don't do the full expansion here. 130 * 131 * The caller is responsible for zeroizing both the struct aes_key and the raw 132 * key once they are no longer needed. 133 * 134 * If you don't need decryption support, use aes_prepareenckey() instead. 135 * 136 * Return: 0 on success or -EINVAL if the given key length is invalid. No other 137 * errors are possible, so callers that always pass a valid key length 138 * don't need to check for errors. 139 * 140 * Context: Any context. 141 */ 142 int aes_preparekey(struct aes_key *key, const u8 *in_key, size_t key_len); 143 144 /** 145 * aes_prepareenckey() - Prepare an AES key for encryption-only 146 * @key: (output) The key structure to initialize 147 * @in_key: The raw AES key 148 * @key_len: Length of the raw key in bytes. Should be either AES_KEYSIZE_128, 149 * AES_KEYSIZE_192, or AES_KEYSIZE_256. 150 * 151 * This prepares an AES key for only the encryption direction of the block 152 * cipher. Typically this involves expanding the raw key into only the standard 153 * round keys, resulting in a struct about half the size of struct aes_key. 154 * 155 * The caller is responsible for zeroizing both the struct aes_enckey and the 156 * raw key once they are no longer needed. 157 * 158 * Note that while the resulting prepared key supports only AES encryption, it 159 * can still be used for decrypting in a mode of operation that uses AES in only 160 * the encryption (forward) direction, for example counter mode. 161 * 162 * Return: 0 on success or -EINVAL if the given key length is invalid. No other 163 * errors are possible, so callers that always pass a valid key length 164 * don't need to check for errors. 165 * 166 * Context: Any context. 167 */ 168 int aes_prepareenckey(struct aes_enckey *key, const u8 *in_key, size_t key_len); 169 170 typedef union { 171 const struct aes_enckey *enc_key; 172 const struct aes_key *full_key; 173 } aes_encrypt_arg __attribute__ ((__transparent_union__)); 174 175 /** 176 * aes_encrypt() - Encrypt a single AES block 177 * @key: The AES key, as a pointer to either an encryption-only key 178 * (struct aes_enckey) or a full, bidirectional key (struct aes_key). 179 * @out: Buffer to store the ciphertext block 180 * @in: Buffer containing the plaintext block 181 * 182 * Context: Any context. 183 */ 184 #define aes_encrypt(key, out, in) \ 185 _Generic((key), \ 186 struct crypto_aes_ctx *: aes_encrypt_old((const struct crypto_aes_ctx *)(key), (out), (in)), \ 187 const struct crypto_aes_ctx *: aes_encrypt_old((const struct crypto_aes_ctx *)(key), (out), (in)), \ 188 struct aes_enckey *: aes_encrypt_new((const struct aes_enckey *)(key), (out), (in)), \ 189 const struct aes_enckey *: aes_encrypt_new((const struct aes_enckey *)(key), (out), (in)), \ 190 struct aes_key *: aes_encrypt_new((const struct aes_key *)(key), (out), (in)), \ 191 const struct aes_key *: aes_encrypt_new((const struct aes_key *)(key), (out), (in))) 192 void aes_encrypt_old(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in); 193 void aes_encrypt_new(aes_encrypt_arg key, u8 out[at_least AES_BLOCK_SIZE], 194 const u8 in[at_least AES_BLOCK_SIZE]); 195 196 /** 197 * aes_decrypt() - Decrypt a single AES block 198 * @key: The AES key, previously initialized by aes_preparekey() 199 * @out: Buffer to store the plaintext block 200 * @in: Buffer containing the ciphertext block 201 * 202 * Context: Any context. 203 */ 204 #define aes_decrypt(key, out, in) \ 205 _Generic((key), \ 206 struct crypto_aes_ctx *: aes_decrypt_old((const struct crypto_aes_ctx *)(key), (out), (in)), \ 207 const struct crypto_aes_ctx *: aes_decrypt_old((const struct crypto_aes_ctx *)(key), (out), (in)), \ 208 struct aes_key *: aes_decrypt_new((const struct aes_key *)(key), (out), (in)), \ 209 const struct aes_key *: aes_decrypt_new((const struct aes_key *)(key), (out), (in))) 210 void aes_decrypt_old(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in); 211 void aes_decrypt_new(const struct aes_key *key, u8 out[at_least AES_BLOCK_SIZE], 212 const u8 in[at_least AES_BLOCK_SIZE]); 213 214 extern const u8 crypto_aes_sbox[]; 215 extern const u8 crypto_aes_inv_sbox[]; 216 extern const u32 aes_enc_tab[256]; 217 extern const u32 aes_dec_tab[256]; 218 219 void aescfb_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src, 220 int len, const u8 iv[AES_BLOCK_SIZE]); 221 void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src, 222 int len, const u8 iv[AES_BLOCK_SIZE]); 223 224 #endif 225