18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <asm/neon.h> 98c2ecf20Sopenharmony_ci#include <asm/simd.h> 108c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 118c2ecf20Sopenharmony_ci#include <crypto/aes.h> 128c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h> 138c2ecf20Sopenharmony_ci#include <linux/cpufeature.h> 148c2ecf20Sopenharmony_ci#include <linux/crypto.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "aes-ce-setkey.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions"); 208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct aes_block { 248c2ecf20Sopenharmony_ci u8 b[AES_BLOCK_SIZE]; 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ciasmlinkage void __aes_ce_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 288c2ecf20Sopenharmony_ciasmlinkage void __aes_ce_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciasmlinkage u32 __aes_ce_sub(u32 l); 318c2ecf20Sopenharmony_ciasmlinkage void __aes_ce_invert(struct aes_block *out, 328c2ecf20Sopenharmony_ci const struct aes_block *in); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic int num_rounds(struct crypto_aes_ctx *ctx) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci /* 378c2ecf20Sopenharmony_ci * # of rounds specified by AES: 388c2ecf20Sopenharmony_ci * 128 bit key 10 rounds 398c2ecf20Sopenharmony_ci * 192 bit key 12 rounds 408c2ecf20Sopenharmony_ci * 256 bit key 14 rounds 418c2ecf20Sopenharmony_ci * => n byte key => 6 + (n/4) rounds 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci return 6 + ctx->key_length / 4; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (!crypto_simd_usable()) { 518c2ecf20Sopenharmony_ci aes_encrypt(ctx, dst, src); 528c2ecf20Sopenharmony_ci return; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci kernel_neon_begin(); 568c2ecf20Sopenharmony_ci __aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx)); 578c2ecf20Sopenharmony_ci kernel_neon_end(); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci if (!crypto_simd_usable()) { 658c2ecf20Sopenharmony_ci aes_decrypt(ctx, dst, src); 668c2ecf20Sopenharmony_ci return; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci kernel_neon_begin(); 708c2ecf20Sopenharmony_ci __aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx)); 718c2ecf20Sopenharmony_ci kernel_neon_end(); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ciint ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, 758c2ecf20Sopenharmony_ci unsigned int key_len) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci /* 788c2ecf20Sopenharmony_ci * The AES key schedule round constants 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci static u8 const rcon[] = { 818c2ecf20Sopenharmony_ci 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 828c2ecf20Sopenharmony_ci }; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci u32 kwords = key_len / sizeof(u32); 858c2ecf20Sopenharmony_ci struct aes_block *key_enc, *key_dec; 868c2ecf20Sopenharmony_ci int i, j; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (key_len != AES_KEYSIZE_128 && 898c2ecf20Sopenharmony_ci key_len != AES_KEYSIZE_192 && 908c2ecf20Sopenharmony_ci key_len != AES_KEYSIZE_256) 918c2ecf20Sopenharmony_ci return -EINVAL; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci ctx->key_length = key_len; 948c2ecf20Sopenharmony_ci for (i = 0; i < kwords; i++) 958c2ecf20Sopenharmony_ci ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32)); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci kernel_neon_begin(); 988c2ecf20Sopenharmony_ci for (i = 0; i < sizeof(rcon); i++) { 998c2ecf20Sopenharmony_ci u32 *rki = ctx->key_enc + (i * kwords); 1008c2ecf20Sopenharmony_ci u32 *rko = rki + kwords; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0]; 1038c2ecf20Sopenharmony_ci rko[1] = rko[0] ^ rki[1]; 1048c2ecf20Sopenharmony_ci rko[2] = rko[1] ^ rki[2]; 1058c2ecf20Sopenharmony_ci rko[3] = rko[2] ^ rki[3]; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci if (key_len == AES_KEYSIZE_192) { 1088c2ecf20Sopenharmony_ci if (i >= 7) 1098c2ecf20Sopenharmony_ci break; 1108c2ecf20Sopenharmony_ci rko[4] = rko[3] ^ rki[4]; 1118c2ecf20Sopenharmony_ci rko[5] = rko[4] ^ rki[5]; 1128c2ecf20Sopenharmony_ci } else if (key_len == AES_KEYSIZE_256) { 1138c2ecf20Sopenharmony_ci if (i >= 6) 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci rko[4] = __aes_ce_sub(rko[3]) ^ rki[4]; 1168c2ecf20Sopenharmony_ci rko[5] = rko[4] ^ rki[5]; 1178c2ecf20Sopenharmony_ci rko[6] = rko[5] ^ rki[6]; 1188c2ecf20Sopenharmony_ci rko[7] = rko[6] ^ rki[7]; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* 1238c2ecf20Sopenharmony_ci * Generate the decryption keys for the Equivalent Inverse Cipher. 1248c2ecf20Sopenharmony_ci * This involves reversing the order of the round keys, and applying 1258c2ecf20Sopenharmony_ci * the Inverse Mix Columns transformation on all but the first and 1268c2ecf20Sopenharmony_ci * the last one. 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_ci key_enc = (struct aes_block *)ctx->key_enc; 1298c2ecf20Sopenharmony_ci key_dec = (struct aes_block *)ctx->key_dec; 1308c2ecf20Sopenharmony_ci j = num_rounds(ctx); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci key_dec[0] = key_enc[j]; 1338c2ecf20Sopenharmony_ci for (i = 1, j--; j > 0; i++, j--) 1348c2ecf20Sopenharmony_ci __aes_ce_invert(key_dec + i, key_enc + j); 1358c2ecf20Sopenharmony_ci key_dec[i] = key_enc[0]; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci kernel_neon_end(); 1388c2ecf20Sopenharmony_ci return 0; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ce_aes_expandkey); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciint ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key, 1438c2ecf20Sopenharmony_ci unsigned int key_len) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci return ce_aes_expandkey(ctx, in_key, key_len); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ce_aes_setkey); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic struct crypto_alg aes_alg = { 1528c2ecf20Sopenharmony_ci .cra_name = "aes", 1538c2ecf20Sopenharmony_ci .cra_driver_name = "aes-ce", 1548c2ecf20Sopenharmony_ci .cra_priority = 250, 1558c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 1568c2ecf20Sopenharmony_ci .cra_blocksize = AES_BLOCK_SIZE, 1578c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct crypto_aes_ctx), 1588c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 1598c2ecf20Sopenharmony_ci .cra_cipher = { 1608c2ecf20Sopenharmony_ci .cia_min_keysize = AES_MIN_KEY_SIZE, 1618c2ecf20Sopenharmony_ci .cia_max_keysize = AES_MAX_KEY_SIZE, 1628c2ecf20Sopenharmony_ci .cia_setkey = ce_aes_setkey, 1638c2ecf20Sopenharmony_ci .cia_encrypt = aes_cipher_encrypt, 1648c2ecf20Sopenharmony_ci .cia_decrypt = aes_cipher_decrypt 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci}; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic int __init aes_mod_init(void) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci return crypto_register_alg(&aes_alg); 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic void __exit aes_mod_exit(void) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci crypto_unregister_alg(&aes_alg); 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cimodule_cpu_feature_match(AES, aes_mod_init); 1798c2ecf20Sopenharmony_cimodule_exit(aes_mod_exit); 180