2 * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
4 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <asm/unaligned.h>
13 #include <crypto/aes.h>
14 #include <linux/cpufeature.h>
15 #include <linux/crypto.h>
16 #include <linux/module.h>
18 #include "aes-ce-setkey.h"
20 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22 MODULE_LICENSE("GPL v2");
28 static int num_rounds(struct crypto_aes_ctx *ctx)
31 * # of rounds specified by AES:
32 * 128 bit key 10 rounds
33 * 192 bit key 12 rounds
34 * 256 bit key 14 rounds
35 * => n byte key => 6 + (n/4) rounds
37 return 6 + ctx->key_length / 4;
40 static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
42 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
43 struct aes_block *out = (struct aes_block *)dst;
44 struct aes_block const *in = (struct aes_block *)src;
48 kernel_neon_begin_partial(4);
50 __asm__(" ld1 {v0.16b}, %[in] ;"
51 " ld1 {v1.4s}, [%[key]], #16 ;"
52 " cmp %w[rounds], #10 ;"
55 " mov v3.16b, v1.16b ;"
57 "0: mov v2.16b, v1.16b ;"
58 " ld1 {v3.4s}, [%[key]], #16 ;"
59 "1: aese v0.16b, v2.16b ;"
60 " aesmc v0.16b, v0.16b ;"
61 "2: ld1 {v1.4s}, [%[key]], #16 ;"
62 " aese v0.16b, v3.16b ;"
63 " aesmc v0.16b, v0.16b ;"
64 "3: ld1 {v2.4s}, [%[key]], #16 ;"
65 " subs %w[rounds], %w[rounds], #3 ;"
66 " aese v0.16b, v1.16b ;"
67 " aesmc v0.16b, v0.16b ;"
68 " ld1 {v3.4s}, [%[key]], #16 ;"
70 " aese v0.16b, v2.16b ;"
71 " eor v0.16b, v0.16b, v3.16b ;"
72 " st1 {v0.16b}, %[out] ;"
79 "2"(num_rounds(ctx) - 2)
85 static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
87 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
88 struct aes_block *out = (struct aes_block *)dst;
89 struct aes_block const *in = (struct aes_block *)src;
93 kernel_neon_begin_partial(4);
95 __asm__(" ld1 {v0.16b}, %[in] ;"
96 " ld1 {v1.4s}, [%[key]], #16 ;"
97 " cmp %w[rounds], #10 ;"
100 " mov v3.16b, v1.16b ;"
102 "0: mov v2.16b, v1.16b ;"
103 " ld1 {v3.4s}, [%[key]], #16 ;"
104 "1: aesd v0.16b, v2.16b ;"
105 " aesimc v0.16b, v0.16b ;"
106 "2: ld1 {v1.4s}, [%[key]], #16 ;"
107 " aesd v0.16b, v3.16b ;"
108 " aesimc v0.16b, v0.16b ;"
109 "3: ld1 {v2.4s}, [%[key]], #16 ;"
110 " subs %w[rounds], %w[rounds], #3 ;"
111 " aesd v0.16b, v1.16b ;"
112 " aesimc v0.16b, v0.16b ;"
113 " ld1 {v3.4s}, [%[key]], #16 ;"
115 " aesd v0.16b, v2.16b ;"
116 " eor v0.16b, v0.16b, v3.16b ;"
117 " st1 {v0.16b}, %[out] ;"
121 [rounds] "=r"(dummy1)
124 "2"(num_rounds(ctx) - 2)
131 * aes_sub() - use the aese instruction to perform the AES sbox substitution
132 * on each byte in 'input'
134 static u32 aes_sub(u32 input)
138 __asm__("dup v1.4s, %w[in] ;"
140 "aese v0.16b, v1.16b ;"
141 "umov %w[out], v0.4s[0] ;"
150 int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
151 unsigned int key_len)
154 * The AES key schedule round constants
156 static u8 const rcon[] = {
157 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
160 u32 kwords = key_len / sizeof(u32);
161 struct aes_block *key_enc, *key_dec;
164 if (key_len != AES_KEYSIZE_128 &&
165 key_len != AES_KEYSIZE_192 &&
166 key_len != AES_KEYSIZE_256)
169 ctx->key_length = key_len;
170 for (i = 0; i < kwords; i++)
171 ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
173 kernel_neon_begin_partial(2);
174 for (i = 0; i < sizeof(rcon); i++) {
175 u32 *rki = ctx->key_enc + (i * kwords);
176 u32 *rko = rki + kwords;
178 rko[0] = ror32(aes_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
179 rko[1] = rko[0] ^ rki[1];
180 rko[2] = rko[1] ^ rki[2];
181 rko[3] = rko[2] ^ rki[3];
183 if (key_len == AES_KEYSIZE_192) {
186 rko[4] = rko[3] ^ rki[4];
187 rko[5] = rko[4] ^ rki[5];
188 } else if (key_len == AES_KEYSIZE_256) {
191 rko[4] = aes_sub(rko[3]) ^ rki[4];
192 rko[5] = rko[4] ^ rki[5];
193 rko[6] = rko[5] ^ rki[6];
194 rko[7] = rko[6] ^ rki[7];
199 * Generate the decryption keys for the Equivalent Inverse Cipher.
200 * This involves reversing the order of the round keys, and applying
201 * the Inverse Mix Columns transformation on all but the first and
204 key_enc = (struct aes_block *)ctx->key_enc;
205 key_dec = (struct aes_block *)ctx->key_dec;
208 key_dec[0] = key_enc[j];
209 for (i = 1, j--; j > 0; i++, j--)
210 __asm__("ld1 {v0.4s}, %[in] ;"
211 "aesimc v1.16b, v0.16b ;"
212 "st1 {v1.4s}, %[out] ;"
214 : [out] "=Q"(key_dec[i])
215 : [in] "Q"(key_enc[j])
217 key_dec[i] = key_enc[0];
222 EXPORT_SYMBOL(ce_aes_expandkey);
224 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
225 unsigned int key_len)
227 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
230 ret = ce_aes_expandkey(ctx, in_key, key_len);
234 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
237 EXPORT_SYMBOL(ce_aes_setkey);
239 static struct crypto_alg aes_alg = {
241 .cra_driver_name = "aes-ce",
243 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
244 .cra_blocksize = AES_BLOCK_SIZE,
245 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
246 .cra_module = THIS_MODULE,
248 .cia_min_keysize = AES_MIN_KEY_SIZE,
249 .cia_max_keysize = AES_MAX_KEY_SIZE,
250 .cia_setkey = ce_aes_setkey,
251 .cia_encrypt = aes_cipher_encrypt,
252 .cia_decrypt = aes_cipher_decrypt
256 static int __init aes_mod_init(void)
258 return crypto_register_alg(&aes_alg);
261 static void __exit aes_mod_exit(void)
263 crypto_unregister_alg(&aes_alg);
266 module_cpu_feature_match(AES, aes_mod_init);
267 module_exit(aes_mod_exit);