1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * aes-ce-cipher.c - core AES cipher using ARM 4 * 5 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.b 6 */ 7 8 #include <asm/neon.h> 9 #include <asm/simd.h> 10 #include <asm/unaligned.h> 11 #include <crypto/aes.h> 12 #include <crypto/algapi.h> 13 #include <crypto/internal/simd.h> 14 #include <linux/cpufeature.h> 15 #include <linux/module.h> 16 17 #include "aes-ce-setkey.h" 18 19 MODULE_DESCRIPTION("Synchronous AES cipher usi 20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@ 21 MODULE_LICENSE("GPL v2"); 22 23 struct aes_block { 24 u8 b[AES_BLOCK_SIZE]; 25 }; 26 27 asmlinkage void __aes_ce_encrypt(u32 *rk, u8 * 28 asmlinkage void __aes_ce_decrypt(u32 *rk, u8 * 29 30 asmlinkage u32 __aes_ce_sub(u32 l); 31 asmlinkage void __aes_ce_invert(struct aes_blo 32 const struct a 33 34 static int num_rounds(struct crypto_aes_ctx *c 35 { 36 /* 37 * # of rounds specified by AES: 38 * 128 bit key 10 rounds 39 * 192 bit key 12 rounds 40 * 256 bit key 14 rounds 41 * => n byte key => 6 + (n/4) r 42 */ 43 return 6 + ctx->key_length / 4; 44 } 45 46 static void aes_cipher_encrypt(struct crypto_t 47 { 48 struct crypto_aes_ctx *ctx = crypto_tf 49 50 if (!crypto_simd_usable()) { 51 aes_encrypt(ctx, dst, src); 52 return; 53 } 54 55 kernel_neon_begin(); 56 __aes_ce_encrypt(ctx->key_enc, dst, sr 57 kernel_neon_end(); 58 } 59 60 static void aes_cipher_decrypt(struct crypto_t 61 { 62 struct crypto_aes_ctx *ctx = crypto_tf 63 64 if (!crypto_simd_usable()) { 65 aes_decrypt(ctx, dst, src); 66 return; 67 } 68 69 kernel_neon_begin(); 70 __aes_ce_decrypt(ctx->key_dec, dst, sr 71 kernel_neon_end(); 72 } 73 74 int ce_aes_expandkey(struct crypto_aes_ctx *ct 75 unsigned int key_len) 76 { 77 /* 78 * The AES key schedule round constant 79 */ 80 static u8 const rcon[] = { 81 0x01, 0x02, 0x04, 0x08, 0x10, 82 }; 83 84 u32 kwords = key_len / sizeof(u32); 85 struct aes_block *key_enc, *key_dec; 86 int i, j; 87 88 if (key_len != AES_KEYSIZE_128 && 89 key_len != AES_KEYSIZE_192 && 90 key_len != AES_KEYSIZE_256) 91 return -EINVAL; 92 93 ctx->key_length = key_len; 94 for (i = 0; i < kwords; i++) 95 ctx->key_enc[i] = get_unaligne 96 97 kernel_neon_begin(); 98 for (i = 0; i < sizeof(rcon); i++) { 99 u32 *rki = ctx->key_enc + (i * 100 u32 *rko = rki + kwords; 101 102 rko[0] = ror32(__aes_ce_sub(rk 103 rko[1] = rko[0] ^ rki[1]; 104 rko[2] = rko[1] ^ rki[2]; 105 rko[3] = rko[2] ^ rki[3]; 106 107 if (key_len == AES_KEYSIZE_192 108 if (i >= 7) 109 break; 110 rko[4] = rko[3] ^ rki[ 111 rko[5] = rko[4] ^ rki[ 112 } else if (key_len == AES_KEYS 113 if (i >= 6) 114 break; 115 rko[4] = __aes_ce_sub( 116 rko[5] = rko[4] ^ rki[ 117 rko[6] = rko[5] ^ rki[ 118 rko[7] = rko[6] ^ rki[ 119 } 120 } 121 122 /* 123 * Generate the decryption keys for th 124 * This involves reversing the order o 125 * the Inverse Mix Columns transformat 126 * the last one. 127 */ 128 key_enc = (struct aes_block *)ctx->key 129 key_dec = (struct aes_block *)ctx->key 130 j = num_rounds(ctx); 131 132 key_dec[0] = key_enc[j]; 133 for (i = 1, j--; j > 0; i++, j--) 134 __aes_ce_invert(key_dec + i, k 135 key_dec[i] = key_enc[0]; 136 137 kernel_neon_end(); 138 return 0; 139 } 140 EXPORT_SYMBOL(ce_aes_expandkey); 141 142 int ce_aes_setkey(struct crypto_tfm *tfm, cons 143 unsigned int key_len) 144 { 145 struct crypto_aes_ctx *ctx = crypto_tf 146 147 return ce_aes_expandkey(ctx, in_key, k 148 } 149 EXPORT_SYMBOL(ce_aes_setkey); 150 151 static struct crypto_alg aes_alg = { 152 .cra_name = "aes", 153 .cra_driver_name = "aes-ce", 154 .cra_priority = 250, 155 .cra_flags = CRYPTO_ALG_T 156 .cra_blocksize = AES_BLOCK_SI 157 .cra_ctxsize = sizeof(struc 158 .cra_module = THIS_MODULE, 159 .cra_cipher = { 160 .cia_min_keysize = AES_ 161 .cia_max_keysize = AES_ 162 .cia_setkey = ce_a 163 .cia_encrypt = aes_ 164 .cia_decrypt = aes_ 165 } 166 }; 167 168 static int __init aes_mod_init(void) 169 { 170 return crypto_register_alg(&aes_alg); 171 } 172 173 static void __exit aes_mod_exit(void) 174 { 175 crypto_unregister_alg(&aes_alg); 176 } 177 178 module_cpu_feature_match(AES, aes_mod_init); 179 module_exit(aes_mod_exit); 180
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.