18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Common values for AES algorithms 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#ifndef _CRYPTO_AES_H 78c2ecf20Sopenharmony_ci#define _CRYPTO_AES_H 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/types.h> 108c2ecf20Sopenharmony_ci#include <linux/crypto.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define AES_MIN_KEY_SIZE 16 138c2ecf20Sopenharmony_ci#define AES_MAX_KEY_SIZE 32 148c2ecf20Sopenharmony_ci#define AES_KEYSIZE_128 16 158c2ecf20Sopenharmony_ci#define AES_KEYSIZE_192 24 168c2ecf20Sopenharmony_ci#define AES_KEYSIZE_256 32 178c2ecf20Sopenharmony_ci#define AES_BLOCK_SIZE 16 188c2ecf20Sopenharmony_ci#define AES_MAX_KEYLENGTH (15 * 16) 198c2ecf20Sopenharmony_ci#define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32)) 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * Please ensure that the first two fields are 16-byte aligned 238c2ecf20Sopenharmony_ci * relative to the start of the structure, i.e., don't move them! 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_cistruct crypto_aes_ctx { 268c2ecf20Sopenharmony_ci u32 key_enc[AES_MAX_KEYLENGTH_U32]; 278c2ecf20Sopenharmony_ci u32 key_dec[AES_MAX_KEYLENGTH_U32]; 288c2ecf20Sopenharmony_ci u32 key_length; 298c2ecf20Sopenharmony_ci}; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ciextern const u32 crypto_ft_tab[4][256] ____cacheline_aligned; 328c2ecf20Sopenharmony_ciextern const u32 crypto_it_tab[4][256] ____cacheline_aligned; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* 358c2ecf20Sopenharmony_ci * validate key length for AES algorithms 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_cistatic inline int aes_check_keylen(unsigned int keylen) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci switch (keylen) { 408c2ecf20Sopenharmony_ci case AES_KEYSIZE_128: 418c2ecf20Sopenharmony_ci case AES_KEYSIZE_192: 428c2ecf20Sopenharmony_ci case AES_KEYSIZE_256: 438c2ecf20Sopenharmony_ci break; 448c2ecf20Sopenharmony_ci default: 458c2ecf20Sopenharmony_ci return -EINVAL; 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return 0; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciint crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 528c2ecf20Sopenharmony_ci unsigned int key_len); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * aes_expandkey - Expands the AES key as described in FIPS-197 568c2ecf20Sopenharmony_ci * @ctx: The location where the computed key will be stored. 578c2ecf20Sopenharmony_ci * @in_key: The supplied key. 588c2ecf20Sopenharmony_ci * @key_len: The length of the supplied key. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Returns 0 on success. The function fails only if an invalid key size (or 618c2ecf20Sopenharmony_ci * pointer) is supplied. 628c2ecf20Sopenharmony_ci * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes 638c2ecf20Sopenharmony_ci * key schedule plus a 16 bytes key which is used before the first round). 648c2ecf20Sopenharmony_ci * The decryption key is prepared for the "Equivalent Inverse Cipher" as 658c2ecf20Sopenharmony_ci * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is 668c2ecf20Sopenharmony_ci * for the initial combination, the second slot for the first round and so on. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ciint aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, 698c2ecf20Sopenharmony_ci unsigned int key_len); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/** 728c2ecf20Sopenharmony_ci * aes_encrypt - Encrypt a single AES block 738c2ecf20Sopenharmony_ci * @ctx: Context struct containing the key schedule 748c2ecf20Sopenharmony_ci * @out: Buffer to store the ciphertext 758c2ecf20Sopenharmony_ci * @in: Buffer containing the plaintext 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_civoid aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/** 808c2ecf20Sopenharmony_ci * aes_decrypt - Decrypt a single AES block 818c2ecf20Sopenharmony_ci * @ctx: Context struct containing the key schedule 828c2ecf20Sopenharmony_ci * @out: Buffer to store the plaintext 838c2ecf20Sopenharmony_ci * @in: Buffer containing the ciphertext 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_civoid aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciextern const u8 crypto_aes_sbox[]; 888c2ecf20Sopenharmony_ciextern const u8 crypto_aes_inv_sbox[]; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#endif 91