1
2
3
4
5
6
7
8
9
10
11#include <asm/neon.h>
12#include <asm/simd.h>
13#include <asm/unaligned.h>
14#include <crypto/aes.h>
15#include <linux/cpufeature.h>
16#include <linux/crypto.h>
17#include <linux/module.h>
18
19#include "aes-ce-setkey.h"
20
21MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
22MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23MODULE_LICENSE("GPL v2");
24
25asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
26asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
27
28struct aes_block {
29 u8 b[AES_BLOCK_SIZE];
30};
31
32static int num_rounds(struct crypto_aes_ctx *ctx)
33{
34
35
36
37
38
39
40
41 return 6 + ctx->key_length / 4;
42}
43
44static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
45{
46 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
47 struct aes_block *out = (struct aes_block *)dst;
48 struct aes_block const *in = (struct aes_block *)src;
49 void *dummy0;
50 int dummy1;
51
52 if (!may_use_simd()) {
53 __aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
54 return;
55 }
56
57 kernel_neon_begin();
58
59 __asm__(" ld1 {v0.16b}, %[in] ;"
60 " ld1 {v1.4s}, [%[key]], #16 ;"
61 " cmp %w[rounds], #10 ;"
62 " bmi 0f ;"
63 " bne 3f ;"
64 " mov v3.16b, v1.16b ;"
65 " b 2f ;"
66 "0: mov v2.16b, v1.16b ;"
67 " ld1 {v3.4s}, [%[key]], #16 ;"
68 "1: aese v0.16b, v2.16b ;"
69 " aesmc v0.16b, v0.16b ;"
70 "2: ld1 {v1.4s}, [%[key]], #16 ;"
71 " aese v0.16b, v3.16b ;"
72 " aesmc v0.16b, v0.16b ;"
73 "3: ld1 {v2.4s}, [%[key]], #16 ;"
74 " subs %w[rounds], %w[rounds], #3 ;"
75 " aese v0.16b, v1.16b ;"
76 " aesmc v0.16b, v0.16b ;"
77 " ld1 {v3.4s}, [%[key]], #16 ;"
78 " bpl 1b ;"
79 " aese v0.16b, v2.16b ;"
80 " eor v0.16b, v0.16b, v3.16b ;"
81 " st1 {v0.16b}, %[out] ;"
82
83 : [out] "=Q"(*out),
84 [key] "=r"(dummy0),
85 [rounds] "=r"(dummy1)
86 : [in] "Q"(*in),
87 "1"(ctx->key_enc),
88 "2"(num_rounds(ctx) - 2)
89 : "cc");
90
91 kernel_neon_end();
92}
93
94static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
95{
96 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
97 struct aes_block *out = (struct aes_block *)dst;
98 struct aes_block const *in = (struct aes_block *)src;
99 void *dummy0;
100 int dummy1;
101
102 if (!may_use_simd()) {
103 __aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
104 return;
105 }
106
107 kernel_neon_begin();
108
109 __asm__(" ld1 {v0.16b}, %[in] ;"
110 " ld1 {v1.4s}, [%[key]], #16 ;"
111 " cmp %w[rounds], #10 ;"
112 " bmi 0f ;"
113 " bne 3f ;"
114 " mov v3.16b, v1.16b ;"
115 " b 2f ;"
116 "0: mov v2.16b, v1.16b ;"
117 " ld1 {v3.4s}, [%[key]], #16 ;"
118 "1: aesd v0.16b, v2.16b ;"
119 " aesimc v0.16b, v0.16b ;"
120 "2: ld1 {v1.4s}, [%[key]], #16 ;"
121 " aesd v0.16b, v3.16b ;"
122 " aesimc v0.16b, v0.16b ;"
123 "3: ld1 {v2.4s}, [%[key]], #16 ;"
124 " subs %w[rounds], %w[rounds], #3 ;"
125 " aesd v0.16b, v1.16b ;"
126 " aesimc v0.16b, v0.16b ;"
127 " ld1 {v3.4s}, [%[key]], #16 ;"
128 " bpl 1b ;"
129 " aesd v0.16b, v2.16b ;"
130 " eor v0.16b, v0.16b, v3.16b ;"
131 " st1 {v0.16b}, %[out] ;"
132
133 : [out] "=Q"(*out),
134 [key] "=r"(dummy0),
135 [rounds] "=r"(dummy1)
136 : [in] "Q"(*in),
137 "1"(ctx->key_dec),
138 "2"(num_rounds(ctx) - 2)
139 : "cc");
140
141 kernel_neon_end();
142}
143
144
145
146
147
148static u32 aes_sub(u32 input)
149{
150 u32 ret;
151
152 __asm__("dup v1.4s, %w[in] ;"
153 "movi v0.16b, #0 ;"
154 "aese v0.16b, v1.16b ;"
155 "umov %w[out], v0.4s[0] ;"
156
157 : [out] "=r"(ret)
158 : [in] "r"(input)
159 : "v0","v1");
160
161 return ret;
162}
163
164int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
165 unsigned int key_len)
166{
167
168
169
170 static u8 const rcon[] = {
171 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
172 };
173
174 u32 kwords = key_len / sizeof(u32);
175 struct aes_block *key_enc, *key_dec;
176 int i, j;
177
178 if (key_len != AES_KEYSIZE_128 &&
179 key_len != AES_KEYSIZE_192 &&
180 key_len != AES_KEYSIZE_256)
181 return -EINVAL;
182
183 ctx->key_length = key_len;
184 for (i = 0; i < kwords; i++)
185 ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
186
187 kernel_neon_begin();
188 for (i = 0; i < sizeof(rcon); i++) {
189 u32 *rki = ctx->key_enc + (i * kwords);
190 u32 *rko = rki + kwords;
191
192 rko[0] = ror32(aes_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
193 rko[1] = rko[0] ^ rki[1];
194 rko[2] = rko[1] ^ rki[2];
195 rko[3] = rko[2] ^ rki[3];
196
197 if (key_len == AES_KEYSIZE_192) {
198 if (i >= 7)
199 break;
200 rko[4] = rko[3] ^ rki[4];
201 rko[5] = rko[4] ^ rki[5];
202 } else if (key_len == AES_KEYSIZE_256) {
203 if (i >= 6)
204 break;
205 rko[4] = aes_sub(rko[3]) ^ rki[4];
206 rko[5] = rko[4] ^ rki[5];
207 rko[6] = rko[5] ^ rki[6];
208 rko[7] = rko[6] ^ rki[7];
209 }
210 }
211
212
213
214
215
216
217
218 key_enc = (struct aes_block *)ctx->key_enc;
219 key_dec = (struct aes_block *)ctx->key_dec;
220 j = num_rounds(ctx);
221
222 key_dec[0] = key_enc[j];
223 for (i = 1, j--; j > 0; i++, j--)
224 __asm__("ld1 {v0.4s}, %[in] ;"
225 "aesimc v1.16b, v0.16b ;"
226 "st1 {v1.4s}, %[out] ;"
227
228 : [out] "=Q"(key_dec[i])
229 : [in] "Q"(key_enc[j])
230 : "v0","v1");
231 key_dec[i] = key_enc[0];
232
233 kernel_neon_end();
234 return 0;
235}
236EXPORT_SYMBOL(ce_aes_expandkey);
237
238int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
239 unsigned int key_len)
240{
241 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
242 int ret;
243
244 ret = ce_aes_expandkey(ctx, in_key, key_len);
245 if (!ret)
246 return 0;
247
248 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
249 return -EINVAL;
250}
251EXPORT_SYMBOL(ce_aes_setkey);
252
253static struct crypto_alg aes_alg = {
254 .cra_name = "aes",
255 .cra_driver_name = "aes-ce",
256 .cra_priority = 250,
257 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
258 .cra_blocksize = AES_BLOCK_SIZE,
259 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
260 .cra_module = THIS_MODULE,
261 .cra_cipher = {
262 .cia_min_keysize = AES_MIN_KEY_SIZE,
263 .cia_max_keysize = AES_MAX_KEY_SIZE,
264 .cia_setkey = ce_aes_setkey,
265 .cia_encrypt = aes_cipher_encrypt,
266 .cia_decrypt = aes_cipher_decrypt
267 }
268};
269
270static int __init aes_mod_init(void)
271{
272 return crypto_register_alg(&aes_alg);
273}
274
275static void __exit aes_mod_exit(void)
276{
277 crypto_unregister_alg(&aes_alg);
278}
279
280module_cpu_feature_match(AES, aes_mod_init);
281module_exit(aes_mod_exit);
282