1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Common values for AES algorithms
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef _CRYPTO_AES_H
7*4882a593Smuzhiyun #define _CRYPTO_AES_H
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/types.h>
10*4882a593Smuzhiyun #include <linux/crypto.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define AES_MIN_KEY_SIZE 16
13*4882a593Smuzhiyun #define AES_MAX_KEY_SIZE 32
14*4882a593Smuzhiyun #define AES_KEYSIZE_128 16
15*4882a593Smuzhiyun #define AES_KEYSIZE_192 24
16*4882a593Smuzhiyun #define AES_KEYSIZE_256 32
17*4882a593Smuzhiyun #define AES_BLOCK_SIZE 16
18*4882a593Smuzhiyun #define AES_MAX_KEYLENGTH (15 * 16)
19*4882a593Smuzhiyun #define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32))
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * Please ensure that the first two fields are 16-byte aligned
23*4882a593Smuzhiyun * relative to the start of the structure, i.e., don't move them!
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun struct crypto_aes_ctx {
26*4882a593Smuzhiyun u32 key_enc[AES_MAX_KEYLENGTH_U32];
27*4882a593Smuzhiyun u32 key_dec[AES_MAX_KEYLENGTH_U32];
28*4882a593Smuzhiyun u32 key_length;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned;
32*4882a593Smuzhiyun extern const u32 crypto_it_tab[4][256] ____cacheline_aligned;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * validate key length for AES algorithms
36*4882a593Smuzhiyun */
aes_check_keylen(unsigned int keylen)37*4882a593Smuzhiyun static inline int aes_check_keylen(unsigned int keylen)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun switch (keylen) {
40*4882a593Smuzhiyun case AES_KEYSIZE_128:
41*4882a593Smuzhiyun case AES_KEYSIZE_192:
42*4882a593Smuzhiyun case AES_KEYSIZE_256:
43*4882a593Smuzhiyun break;
44*4882a593Smuzhiyun default:
45*4882a593Smuzhiyun return -EINVAL;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
52*4882a593Smuzhiyun unsigned int key_len);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * aes_expandkey - Expands the AES key as described in FIPS-197
56*4882a593Smuzhiyun * @ctx: The location where the computed key will be stored.
57*4882a593Smuzhiyun * @in_key: The supplied key.
58*4882a593Smuzhiyun * @key_len: The length of the supplied key.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Returns 0 on success. The function fails only if an invalid key size (or
61*4882a593Smuzhiyun * pointer) is supplied.
62*4882a593Smuzhiyun * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
63*4882a593Smuzhiyun * key schedule plus a 16 bytes key which is used before the first round).
64*4882a593Smuzhiyun * The decryption key is prepared for the "Equivalent Inverse Cipher" as
65*4882a593Smuzhiyun * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
66*4882a593Smuzhiyun * for the initial combination, the second slot for the first round and so on.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
69*4882a593Smuzhiyun unsigned int key_len);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * aes_encrypt - Encrypt a single AES block
73*4882a593Smuzhiyun * @ctx: Context struct containing the key schedule
74*4882a593Smuzhiyun * @out: Buffer to store the ciphertext
75*4882a593Smuzhiyun * @in: Buffer containing the plaintext
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun void aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun * aes_decrypt - Decrypt a single AES block
81*4882a593Smuzhiyun * @ctx: Context struct containing the key schedule
82*4882a593Smuzhiyun * @out: Buffer to store the plaintext
83*4882a593Smuzhiyun * @in: Buffer containing the ciphertext
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun extern const u8 crypto_aes_sbox[];
88*4882a593Smuzhiyun extern const u8 crypto_aes_inv_sbox[];
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #endif
91