Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Common values for AES algorithms
0004  */
0005 
0006 #ifndef _CRYPTO_AES_H
0007 #define _CRYPTO_AES_H
0008 
0009 #include <linux/types.h>
0010 #include <linux/crypto.h>
0011 
0012 #define AES_MIN_KEY_SIZE    16
0013 #define AES_MAX_KEY_SIZE    32
0014 #define AES_KEYSIZE_128     16
0015 #define AES_KEYSIZE_192     24
0016 #define AES_KEYSIZE_256     32
0017 #define AES_BLOCK_SIZE      16
0018 #define AES_MAX_KEYLENGTH   (15 * 16)
0019 #define AES_MAX_KEYLENGTH_U32   (AES_MAX_KEYLENGTH / sizeof(u32))
0020 
0021 /*
0022  * Please ensure that the first two fields are 16-byte aligned
0023  * relative to the start of the structure, i.e., don't move them!
0024  */
0025 struct crypto_aes_ctx {
0026     u32 key_enc[AES_MAX_KEYLENGTH_U32];
0027     u32 key_dec[AES_MAX_KEYLENGTH_U32];
0028     u32 key_length;
0029 };
0030 
0031 extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned;
0032 extern const u32 crypto_it_tab[4][256] ____cacheline_aligned;
0033 
0034 /*
0035  * validate key length for AES algorithms
0036  */
0037 static inline int aes_check_keylen(unsigned int keylen)
0038 {
0039     switch (keylen) {
0040     case AES_KEYSIZE_128:
0041     case AES_KEYSIZE_192:
0042     case AES_KEYSIZE_256:
0043         break;
0044     default:
0045         return -EINVAL;
0046     }
0047 
0048     return 0;
0049 }
0050 
0051 int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
0052         unsigned int key_len);
0053 
0054 /**
0055  * aes_expandkey - Expands the AES key as described in FIPS-197
0056  * @ctx:    The location where the computed key will be stored.
0057  * @in_key: The supplied key.
0058  * @key_len:    The length of the supplied key.
0059  *
0060  * Returns 0 on success. The function fails only if an invalid key size (or
0061  * pointer) is supplied.
0062  * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
0063  * key schedule plus a 16 bytes key which is used before the first round).
0064  * The decryption key is prepared for the "Equivalent Inverse Cipher" as
0065  * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
0066  * for the initial combination, the second slot for the first round and so on.
0067  */
0068 int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
0069           unsigned int key_len);
0070 
0071 /**
0072  * aes_encrypt - Encrypt a single AES block
0073  * @ctx:    Context struct containing the key schedule
0074  * @out:    Buffer to store the ciphertext
0075  * @in:     Buffer containing the plaintext
0076  */
0077 void aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
0078 
0079 /**
0080  * aes_decrypt - Decrypt a single AES block
0081  * @ctx:    Context struct containing the key schedule
0082  * @out:    Buffer to store the plaintext
0083  * @in:     Buffer containing the ciphertext
0084  */
0085 void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
0086 
0087 extern const u8 crypto_aes_sbox[];
0088 extern const u8 crypto_aes_inv_sbox[];
0089 
0090 #endif