aes.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Please ensure that the first two fields are 16-byte aligned
  19. * relative to the start of the structure, i.e., don't move them!
  20. */
  21. struct crypto_aes_ctx {
  22. u32 key_enc[AES_MAX_KEYLENGTH_U32];
  23. u32 key_dec[AES_MAX_KEYLENGTH_U32];
  24. u32 key_length;
  25. };
  26. extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned;
  27. extern const u32 crypto_it_tab[4][256] ____cacheline_aligned;
  28. /*
  29. * validate key length for AES algorithms
  30. */
  31. static inline int aes_check_keylen(unsigned int keylen)
  32. {
  33. switch (keylen) {
  34. case AES_KEYSIZE_128:
  35. case AES_KEYSIZE_192:
  36. case AES_KEYSIZE_256:
  37. break;
  38. default:
  39. return -EINVAL;
  40. }
  41. return 0;
  42. }
  43. int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  44. unsigned int key_len);
  45. /**
  46. * aes_expandkey - Expands the AES key as described in FIPS-197
  47. * @ctx: The location where the computed key will be stored.
  48. * @in_key: The supplied key.
  49. * @key_len: The length of the supplied key.
  50. *
  51. * Returns 0 on success. The function fails only if an invalid key size (or
  52. * pointer) is supplied.
  53. * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
  54. * key schedule plus a 16 bytes key which is used before the first round).
  55. * The decryption key is prepared for the "Equivalent Inverse Cipher" as
  56. * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
  57. * for the initial combination, the second slot for the first round and so on.
  58. */
  59. int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
  60. unsigned int key_len);
  61. /**
  62. * aes_encrypt - Encrypt a single AES block
  63. * @ctx: Context struct containing the key schedule
  64. * @out: Buffer to store the ciphertext
  65. * @in: Buffer containing the plaintext
  66. */
  67. void aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
  68. /**
  69. * aes_decrypt - Decrypt a single AES block
  70. * @ctx: Context struct containing the key schedule
  71. * @out: Buffer to store the plaintext
  72. * @in: Buffer containing the ciphertext
  73. */
  74. void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
  75. extern const u8 crypto_aes_sbox[];
  76. extern const u8 crypto_aes_inv_sbox[];
  77. #endif