aes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for AES algorithms
  4. */
  5. #ifndef _CRYPTO_AES_H
  6. #define _CRYPTO_AES_H
  7. #include <linux/types.h>
  8. #include <linux/crypto.h>
  9. #define AES_MIN_KEY_SIZE 16
  10. #define AES_MAX_KEY_SIZE 32
  11. #define AES_KEYSIZE_128 16
  12. #define AES_KEYSIZE_192 24
  13. #define AES_KEYSIZE_256 32
  14. #define AES_BLOCK_SIZE 16
  15. #define AES_MAX_KEYLENGTH (15 * 16)
  16. #define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32))
  17. /*
  18. * The POWER8 VSX optimized AES assembly code is borrowed from OpenSSL and
  19. * inherits OpenSSL's AES_KEY format, which stores the number of rounds after
  20. * the round keys. That assembly code is difficult to change. So for
  21. * compatibility purposes we reserve space for the extra nrounds field on PPC64.
  22. *
  23. * Note: when prepared for decryption, the round keys are just the reversed
  24. * standard round keys, not the round keys for the Equivalent Inverse Cipher.
  25. */
  26. struct p8_aes_key {
  27. u32 rndkeys[AES_MAX_KEYLENGTH_U32];
  28. int nrounds;
  29. };
  30. union aes_enckey_arch {
  31. u32 rndkeys[AES_MAX_KEYLENGTH_U32];
  32. #ifdef CONFIG_CRYPTO_LIB_AES_ARCH
  33. #if defined(CONFIG_PPC) && defined(CONFIG_SPE)
  34. /* Used unconditionally (when SPE AES code is enabled in kconfig) */
  35. u32 spe_enc_key[AES_MAX_KEYLENGTH_U32] __aligned(8);
  36. #elif defined(CONFIG_PPC)
  37. /*
  38. * Kernels that include the POWER8 VSX optimized AES code use this field
  39. * when that code is usable at key preparation time. Otherwise they
  40. * fall back to rndkeys. In the latter case, p8.nrounds (which doesn't
  41. * overlap rndkeys) is set to 0 to differentiate the two formats.
  42. */
  43. struct p8_aes_key p8;
  44. #elif defined(CONFIG_S390)
  45. /* Used when the CPU supports CPACF AES for this key's length */
  46. u8 raw_key[AES_MAX_KEY_SIZE];
  47. #elif defined(CONFIG_SPARC64)
  48. /* Used when the CPU supports the SPARC64 AES opcodes */
  49. u64 sparc_rndkeys[AES_MAX_KEYLENGTH / sizeof(u64)];
  50. #endif
  51. #endif /* CONFIG_CRYPTO_LIB_AES_ARCH */
  52. };
  53. union aes_invkey_arch {
  54. u32 inv_rndkeys[AES_MAX_KEYLENGTH_U32];
  55. #ifdef CONFIG_CRYPTO_LIB_AES_ARCH
  56. #if defined(CONFIG_PPC) && defined(CONFIG_SPE)
  57. /* Used unconditionally (when SPE AES code is enabled in kconfig) */
  58. u32 spe_dec_key[AES_MAX_KEYLENGTH_U32] __aligned(8);
  59. #elif defined(CONFIG_PPC)
  60. /* Used conditionally, analogous to aes_enckey_arch::p8 */
  61. struct p8_aes_key p8;
  62. #endif
  63. #endif /* CONFIG_CRYPTO_LIB_AES_ARCH */
  64. };
  65. /**
  66. * struct aes_enckey - An AES key prepared for encryption
  67. * @len: Key length in bytes: 16 for AES-128, 24 for AES-192, 32 for AES-256.
  68. * @nrounds: Number of rounds: 10 for AES-128, 12 for AES-192, 14 for AES-256.
  69. * This is '6 + @len / 4' and is cached so that AES implementations
  70. * that need it don't have to recompute it for each en/decryption.
  71. * @padding: Padding to make offsetof(@k) be a multiple of 16, so that aligning
  72. * this struct to a 16-byte boundary results in @k also being 16-byte
  73. * aligned. Users aren't required to align this struct to 16 bytes,
  74. * but it may slightly improve performance.
  75. * @k: This typically contains the AES round keys as an array of '@nrounds + 1'
  76. * groups of four u32 words. However, architecture-specific implementations
  77. * of AES may store something else here, e.g. just the raw key if it's all
  78. * they need.
  79. *
  80. * Note that this struct is about half the size of struct aes_key. This is
  81. * separate from struct aes_key so that modes that need only AES encryption
  82. * (e.g. AES-GCM, AES-CTR, AES-CMAC, tweak key in AES-XTS) don't incur the time
  83. * and space overhead of computing and caching the decryption round keys.
  84. *
  85. * Note that there's no decryption-only equivalent (i.e. "struct aes_deckey"),
  86. * since (a) it's rare that modes need decryption-only, and (b) some AES
  87. * implementations use the same @k for both encryption and decryption, either
  88. * always or conditionally; in the latter case both @k and @inv_k are needed.
  89. */
  90. struct aes_enckey {
  91. u32 len;
  92. u32 nrounds;
  93. u32 padding[2];
  94. union aes_enckey_arch k;
  95. };
  96. /**
  97. * struct aes_key - An AES key prepared for encryption and decryption
  98. * @aes_enckey: Common fields and the key prepared for encryption
  99. * @inv_k: This generally contains the round keys for the AES Equivalent
  100. * Inverse Cipher, as an array of '@nrounds + 1' groups of four u32
  101. * words. However, architecture-specific implementations of AES may
  102. * store something else here. For example, they may leave this field
  103. * uninitialized if they use @k for both encryption and decryption.
  104. */
  105. struct aes_key {
  106. struct aes_enckey; /* Include all fields of aes_enckey. */
  107. union aes_invkey_arch inv_k;
  108. };
  109. /*
  110. * Please ensure that the first two fields are 16-byte aligned
  111. * relative to the start of the structure, i.e., don't move them!
  112. */
  113. struct crypto_aes_ctx {
  114. u32 key_enc[AES_MAX_KEYLENGTH_U32];
  115. u32 key_dec[AES_MAX_KEYLENGTH_U32];
  116. u32 key_length;
  117. };
  118. /*
  119. * validate key length for AES algorithms
  120. */
  121. static inline int aes_check_keylen(size_t keylen)
  122. {
  123. switch (keylen) {
  124. case AES_KEYSIZE_128:
  125. case AES_KEYSIZE_192:
  126. case AES_KEYSIZE_256:
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. /**
  134. * aes_expandkey - Expands the AES key as described in FIPS-197
  135. * @ctx: The location where the computed key will be stored.
  136. * @in_key: The supplied key.
  137. * @key_len: The length of the supplied key.
  138. *
  139. * Returns 0 on success. The function fails only if an invalid key size (or
  140. * pointer) is supplied.
  141. * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
  142. * key schedule plus a 16 bytes key which is used before the first round).
  143. * The decryption key is prepared for the "Equivalent Inverse Cipher" as
  144. * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
  145. * for the initial combination, the second slot for the first round and so on.
  146. */
  147. int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
  148. unsigned int key_len);
  149. /*
  150. * The following functions are temporarily exported for use by the AES mode
  151. * implementations in arch/$(SRCARCH)/crypto/. These exports will go away when
  152. * that code is migrated into lib/crypto/.
  153. */
  154. #ifdef CONFIG_ARM64
  155. int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
  156. unsigned int key_len);
  157. #elif defined(CONFIG_PPC)
  158. void ppc_expand_key_128(u32 *key_enc, const u8 *key);
  159. void ppc_expand_key_192(u32 *key_enc, const u8 *key);
  160. void ppc_expand_key_256(u32 *key_enc, const u8 *key);
  161. void ppc_generate_decrypt_key(u32 *key_dec, u32 *key_enc, unsigned int key_len);
  162. void ppc_encrypt_ecb(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
  163. u32 bytes);
  164. void ppc_decrypt_ecb(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
  165. u32 bytes);
  166. void ppc_encrypt_cbc(u8 *out, const u8 *in, u32 *key_enc, u32 rounds, u32 bytes,
  167. u8 *iv);
  168. void ppc_decrypt_cbc(u8 *out, const u8 *in, u32 *key_dec, u32 rounds, u32 bytes,
  169. u8 *iv);
  170. void ppc_crypt_ctr(u8 *out, const u8 *in, u32 *key_enc, u32 rounds, u32 bytes,
  171. u8 *iv);
  172. void ppc_encrypt_xts(u8 *out, const u8 *in, u32 *key_enc, u32 rounds, u32 bytes,
  173. u8 *iv, u32 *key_twk);
  174. void ppc_decrypt_xts(u8 *out, const u8 *in, u32 *key_dec, u32 rounds, u32 bytes,
  175. u8 *iv, u32 *key_twk);
  176. int aes_p8_set_encrypt_key(const u8 *userKey, const int bits,
  177. struct p8_aes_key *key);
  178. int aes_p8_set_decrypt_key(const u8 *userKey, const int bits,
  179. struct p8_aes_key *key);
  180. void aes_p8_encrypt(const u8 *in, u8 *out, const struct p8_aes_key *key);
  181. void aes_p8_decrypt(const u8 *in, u8 *out, const struct p8_aes_key *key);
  182. void aes_p8_cbc_encrypt(const u8 *in, u8 *out, size_t len,
  183. const struct p8_aes_key *key, u8 *iv, const int enc);
  184. void aes_p8_ctr32_encrypt_blocks(const u8 *in, u8 *out, size_t len,
  185. const struct p8_aes_key *key, const u8 *iv);
  186. void aes_p8_xts_encrypt(const u8 *in, u8 *out, size_t len,
  187. const struct p8_aes_key *key1,
  188. const struct p8_aes_key *key2, u8 *iv);
  189. void aes_p8_xts_decrypt(const u8 *in, u8 *out, size_t len,
  190. const struct p8_aes_key *key1,
  191. const struct p8_aes_key *key2, u8 *iv);
  192. #elif defined(CONFIG_SPARC64)
  193. void aes_sparc64_key_expand(const u32 *in_key, u64 *output_key,
  194. unsigned int key_len);
  195. void aes_sparc64_load_encrypt_keys_128(const u64 *key);
  196. void aes_sparc64_load_encrypt_keys_192(const u64 *key);
  197. void aes_sparc64_load_encrypt_keys_256(const u64 *key);
  198. void aes_sparc64_load_decrypt_keys_128(const u64 *key);
  199. void aes_sparc64_load_decrypt_keys_192(const u64 *key);
  200. void aes_sparc64_load_decrypt_keys_256(const u64 *key);
  201. void aes_sparc64_ecb_encrypt_128(const u64 *key, const u64 *input, u64 *output,
  202. unsigned int len);
  203. void aes_sparc64_ecb_encrypt_192(const u64 *key, const u64 *input, u64 *output,
  204. unsigned int len);
  205. void aes_sparc64_ecb_encrypt_256(const u64 *key, const u64 *input, u64 *output,
  206. unsigned int len);
  207. void aes_sparc64_ecb_decrypt_128(const u64 *key, const u64 *input, u64 *output,
  208. unsigned int len);
  209. void aes_sparc64_ecb_decrypt_192(const u64 *key, const u64 *input, u64 *output,
  210. unsigned int len);
  211. void aes_sparc64_ecb_decrypt_256(const u64 *key, const u64 *input, u64 *output,
  212. unsigned int len);
  213. void aes_sparc64_cbc_encrypt_128(const u64 *key, const u64 *input, u64 *output,
  214. unsigned int len, u64 *iv);
  215. void aes_sparc64_cbc_encrypt_192(const u64 *key, const u64 *input, u64 *output,
  216. unsigned int len, u64 *iv);
  217. void aes_sparc64_cbc_encrypt_256(const u64 *key, const u64 *input, u64 *output,
  218. unsigned int len, u64 *iv);
  219. void aes_sparc64_cbc_decrypt_128(const u64 *key, const u64 *input, u64 *output,
  220. unsigned int len, u64 *iv);
  221. void aes_sparc64_cbc_decrypt_192(const u64 *key, const u64 *input, u64 *output,
  222. unsigned int len, u64 *iv);
  223. void aes_sparc64_cbc_decrypt_256(const u64 *key, const u64 *input, u64 *output,
  224. unsigned int len, u64 *iv);
  225. void aes_sparc64_ctr_crypt_128(const u64 *key, const u64 *input, u64 *output,
  226. unsigned int len, u64 *iv);
  227. void aes_sparc64_ctr_crypt_192(const u64 *key, const u64 *input, u64 *output,
  228. unsigned int len, u64 *iv);
  229. void aes_sparc64_ctr_crypt_256(const u64 *key, const u64 *input, u64 *output,
  230. unsigned int len, u64 *iv);
  231. #endif
  232. /**
  233. * aes_preparekey() - Prepare an AES key for encryption and decryption
  234. * @key: (output) The key structure to initialize
  235. * @in_key: The raw AES key
  236. * @key_len: Length of the raw key in bytes. Should be either AES_KEYSIZE_128,
  237. * AES_KEYSIZE_192, or AES_KEYSIZE_256.
  238. *
  239. * This prepares an AES key for both the encryption and decryption directions of
  240. * the block cipher. Typically this involves expanding the raw key into both
  241. * the standard round keys and the Equivalent Inverse Cipher round keys, but
  242. * some architecture-specific implementations don't do the full expansion here.
  243. *
  244. * The caller is responsible for zeroizing both the struct aes_key and the raw
  245. * key once they are no longer needed.
  246. *
  247. * If you don't need decryption support, use aes_prepareenckey() instead.
  248. *
  249. * Return: 0 on success or -EINVAL if the given key length is invalid. No other
  250. * errors are possible, so callers that always pass a valid key length
  251. * don't need to check for errors.
  252. *
  253. * Context: Any context.
  254. */
  255. int aes_preparekey(struct aes_key *key, const u8 *in_key, size_t key_len);
  256. /**
  257. * aes_prepareenckey() - Prepare an AES key for encryption-only
  258. * @key: (output) The key structure to initialize
  259. * @in_key: The raw AES key
  260. * @key_len: Length of the raw key in bytes. Should be either AES_KEYSIZE_128,
  261. * AES_KEYSIZE_192, or AES_KEYSIZE_256.
  262. *
  263. * This prepares an AES key for only the encryption direction of the block
  264. * cipher. Typically this involves expanding the raw key into only the standard
  265. * round keys, resulting in a struct about half the size of struct aes_key.
  266. *
  267. * The caller is responsible for zeroizing both the struct aes_enckey and the
  268. * raw key once they are no longer needed.
  269. *
  270. * Note that while the resulting prepared key supports only AES encryption, it
  271. * can still be used for decrypting in a mode of operation that uses AES in only
  272. * the encryption (forward) direction, for example counter mode.
  273. *
  274. * Return: 0 on success or -EINVAL if the given key length is invalid. No other
  275. * errors are possible, so callers that always pass a valid key length
  276. * don't need to check for errors.
  277. *
  278. * Context: Any context.
  279. */
  280. int aes_prepareenckey(struct aes_enckey *key, const u8 *in_key, size_t key_len);
  281. typedef union {
  282. const struct aes_enckey *enc_key;
  283. const struct aes_key *full_key;
  284. } aes_encrypt_arg __attribute__ ((__transparent_union__));
  285. /**
  286. * aes_encrypt() - Encrypt a single AES block
  287. * @key: The AES key, as a pointer to either an encryption-only key
  288. * (struct aes_enckey) or a full, bidirectional key (struct aes_key).
  289. * @out: Buffer to store the ciphertext block
  290. * @in: Buffer containing the plaintext block
  291. *
  292. * Context: Any context.
  293. */
  294. void aes_encrypt(aes_encrypt_arg key, u8 out[at_least AES_BLOCK_SIZE],
  295. const u8 in[at_least AES_BLOCK_SIZE]);
  296. /**
  297. * aes_decrypt() - Decrypt a single AES block
  298. * @key: The AES key, previously initialized by aes_preparekey()
  299. * @out: Buffer to store the plaintext block
  300. * @in: Buffer containing the ciphertext block
  301. *
  302. * Context: Any context.
  303. */
  304. void aes_decrypt(const struct aes_key *key, u8 out[at_least AES_BLOCK_SIZE],
  305. const u8 in[at_least AES_BLOCK_SIZE]);
  306. extern const u8 crypto_aes_sbox[];
  307. extern const u8 crypto_aes_inv_sbox[];
  308. extern const u32 aes_enc_tab[256];
  309. extern const u32 aes_dec_tab[256];
  310. void aescfb_encrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
  311. int len, const u8 iv[AES_BLOCK_SIZE]);
  312. void aescfb_decrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
  313. int len, const u8 iv[AES_BLOCK_SIZE]);
  314. #endif