blob: e262f99a44d324dd96f7bc218be3e240481cd681 [file] [log] [blame]
Ard Biesheuvelcc477bf2017-01-11 16:41:54 +00001/*
2 * Bit sliced AES using NEON instructions
3 *
4 * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5 *
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.
9 */
10
11#include <asm/neon.h>
12#include <crypto/aes.h>
13#include <crypto/cbc.h>
14#include <crypto/internal/simd.h>
15#include <crypto/internal/skcipher.h>
16#include <crypto/xts.h>
17#include <linux/module.h>
18
19MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
20MODULE_LICENSE("GPL v2");
21
22MODULE_ALIAS_CRYPTO("ecb(aes)");
23MODULE_ALIAS_CRYPTO("cbc(aes)");
24MODULE_ALIAS_CRYPTO("ctr(aes)");
25MODULE_ALIAS_CRYPTO("xts(aes)");
26
27asmlinkage void aesbs_convert_key(u8 out[], u32 const rk[], int rounds);
28
29asmlinkage void aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],
30 int rounds, int blocks);
31asmlinkage void aesbs_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[],
32 int rounds, int blocks);
33
34asmlinkage void aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
35 int rounds, int blocks, u8 iv[]);
36
37asmlinkage void aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
38 int rounds, int blocks, u8 ctr[], bool final);
39
40asmlinkage void aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[],
41 int rounds, int blocks, u8 iv[]);
42asmlinkage void aesbs_xts_decrypt(u8 out[], u8 const in[], u8 const rk[],
43 int rounds, int blocks, u8 iv[]);
44
45asmlinkage void __aes_arm_encrypt(const u32 rk[], int rounds, const u8 in[],
46 u8 out[]);
47
48struct aesbs_ctx {
49 int rounds;
50 u8 rk[13 * (8 * AES_BLOCK_SIZE) + 32] __aligned(AES_BLOCK_SIZE);
51};
52
53struct aesbs_cbc_ctx {
54 struct aesbs_ctx key;
55 u32 enc[AES_MAX_KEYLENGTH_U32];
56};
57
58struct aesbs_xts_ctx {
59 struct aesbs_ctx key;
60 u32 twkey[AES_MAX_KEYLENGTH_U32];
61};
62
63static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
64 unsigned int key_len)
65{
66 struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
67 struct crypto_aes_ctx rk;
68 int err;
69
70 err = crypto_aes_expand_key(&rk, in_key, key_len);
71 if (err)
72 return err;
73
74 ctx->rounds = 6 + key_len / 4;
75
76 kernel_neon_begin();
77 aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds);
78 kernel_neon_end();
79
80 return 0;
81}
82
83static int __ecb_crypt(struct skcipher_request *req,
84 void (*fn)(u8 out[], u8 const in[], u8 const rk[],
85 int rounds, int blocks))
86{
87 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
88 struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
89 struct skcipher_walk walk;
90 int err;
91
92 err = skcipher_walk_virt(&walk, req, true);
93
94 kernel_neon_begin();
95 while (walk.nbytes >= AES_BLOCK_SIZE) {
96 unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
97
98 if (walk.nbytes < walk.total)
99 blocks = round_down(blocks,
100 walk.stride / AES_BLOCK_SIZE);
101
102 fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->rk,
103 ctx->rounds, blocks);
104 err = skcipher_walk_done(&walk,
105 walk.nbytes - blocks * AES_BLOCK_SIZE);
106 }
107 kernel_neon_end();
108
109 return err;
110}
111
112static int ecb_encrypt(struct skcipher_request *req)
113{
114 return __ecb_crypt(req, aesbs_ecb_encrypt);
115}
116
117static int ecb_decrypt(struct skcipher_request *req)
118{
119 return __ecb_crypt(req, aesbs_ecb_decrypt);
120}
121
122static int aesbs_cbc_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
123 unsigned int key_len)
124{
125 struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
126 struct crypto_aes_ctx rk;
127 int err;
128
129 err = crypto_aes_expand_key(&rk, in_key, key_len);
130 if (err)
131 return err;
132
133 ctx->key.rounds = 6 + key_len / 4;
134
135 memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc));
136
137 kernel_neon_begin();
138 aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds);
139 kernel_neon_end();
140
141 return 0;
142}
143
144static void cbc_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst)
145{
146 struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
147
148 __aes_arm_encrypt(ctx->enc, ctx->key.rounds, src, dst);
149}
150
151static int cbc_encrypt(struct skcipher_request *req)
152{
153 return crypto_cbc_encrypt_walk(req, cbc_encrypt_one);
154}
155
156static int cbc_decrypt(struct skcipher_request *req)
157{
158 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
159 struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
160 struct skcipher_walk walk;
161 int err;
162
163 err = skcipher_walk_virt(&walk, req, true);
164
165 kernel_neon_begin();
166 while (walk.nbytes >= AES_BLOCK_SIZE) {
167 unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
168
169 if (walk.nbytes < walk.total)
170 blocks = round_down(blocks,
171 walk.stride / AES_BLOCK_SIZE);
172
173 aesbs_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
174 ctx->key.rk, ctx->key.rounds, blocks,
175 walk.iv);
176 err = skcipher_walk_done(&walk,
177 walk.nbytes - blocks * AES_BLOCK_SIZE);
178 }
179 kernel_neon_end();
180
181 return err;
182}
183
184static int ctr_encrypt(struct skcipher_request *req)
185{
186 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
187 struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
188 struct skcipher_walk walk;
189 int err;
190
191 err = skcipher_walk_virt(&walk, req, true);
192
193 kernel_neon_begin();
194 while (walk.nbytes > 0) {
195 unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
196 bool final = (walk.total % AES_BLOCK_SIZE) != 0;
197
198 if (walk.nbytes < walk.total) {
199 blocks = round_down(blocks,
200 walk.stride / AES_BLOCK_SIZE);
201 final = false;
202 }
203
204 aesbs_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
205 ctx->rk, ctx->rounds, blocks, walk.iv, final);
206
207 if (final) {
208 u8 *dst = walk.dst.virt.addr + blocks * AES_BLOCK_SIZE;
209 u8 *src = walk.src.virt.addr + blocks * AES_BLOCK_SIZE;
210
211 if (dst != src)
212 memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
213 crypto_xor(dst, walk.iv, walk.total % AES_BLOCK_SIZE);
214
215 err = skcipher_walk_done(&walk, 0);
216 break;
217 }
218 err = skcipher_walk_done(&walk,
219 walk.nbytes - blocks * AES_BLOCK_SIZE);
220 }
221 kernel_neon_end();
222
223 return err;
224}
225
226static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
227 unsigned int key_len)
228{
229 struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
230 struct crypto_aes_ctx rk;
231 int err;
232
233 err = xts_verify_key(tfm, in_key, key_len);
234 if (err)
235 return err;
236
237 key_len /= 2;
238 err = crypto_aes_expand_key(&rk, in_key + key_len, key_len);
239 if (err)
240 return err;
241
242 memcpy(ctx->twkey, rk.key_enc, sizeof(ctx->twkey));
243
244 return aesbs_setkey(tfm, in_key, key_len);
245}
246
247static int __xts_crypt(struct skcipher_request *req,
248 void (*fn)(u8 out[], u8 const in[], u8 const rk[],
249 int rounds, int blocks, u8 iv[]))
250{
251 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
252 struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
253 struct skcipher_walk walk;
254 int err;
255
256 err = skcipher_walk_virt(&walk, req, true);
257
258 __aes_arm_encrypt(ctx->twkey, ctx->key.rounds, walk.iv, walk.iv);
259
260 kernel_neon_begin();
261 while (walk.nbytes >= AES_BLOCK_SIZE) {
262 unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
263
264 if (walk.nbytes < walk.total)
265 blocks = round_down(blocks,
266 walk.stride / AES_BLOCK_SIZE);
267
268 fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->key.rk,
269 ctx->key.rounds, blocks, walk.iv);
270 err = skcipher_walk_done(&walk,
271 walk.nbytes - blocks * AES_BLOCK_SIZE);
272 }
273 kernel_neon_end();
274
275 return err;
276}
277
278static int xts_encrypt(struct skcipher_request *req)
279{
280 return __xts_crypt(req, aesbs_xts_encrypt);
281}
282
283static int xts_decrypt(struct skcipher_request *req)
284{
285 return __xts_crypt(req, aesbs_xts_decrypt);
286}
287
288static struct skcipher_alg aes_algs[] = { {
289 .base.cra_name = "__ecb(aes)",
290 .base.cra_driver_name = "__ecb-aes-neonbs",
291 .base.cra_priority = 250,
292 .base.cra_blocksize = AES_BLOCK_SIZE,
293 .base.cra_ctxsize = sizeof(struct aesbs_ctx),
294 .base.cra_module = THIS_MODULE,
295 .base.cra_flags = CRYPTO_ALG_INTERNAL,
296
297 .min_keysize = AES_MIN_KEY_SIZE,
298 .max_keysize = AES_MAX_KEY_SIZE,
299 .walksize = 8 * AES_BLOCK_SIZE,
300 .setkey = aesbs_setkey,
301 .encrypt = ecb_encrypt,
302 .decrypt = ecb_decrypt,
303}, {
304 .base.cra_name = "__cbc(aes)",
305 .base.cra_driver_name = "__cbc-aes-neonbs",
306 .base.cra_priority = 250,
307 .base.cra_blocksize = AES_BLOCK_SIZE,
308 .base.cra_ctxsize = sizeof(struct aesbs_cbc_ctx),
309 .base.cra_module = THIS_MODULE,
310 .base.cra_flags = CRYPTO_ALG_INTERNAL,
311
312 .min_keysize = AES_MIN_KEY_SIZE,
313 .max_keysize = AES_MAX_KEY_SIZE,
314 .walksize = 8 * AES_BLOCK_SIZE,
315 .ivsize = AES_BLOCK_SIZE,
316 .setkey = aesbs_cbc_setkey,
317 .encrypt = cbc_encrypt,
318 .decrypt = cbc_decrypt,
319}, {
320 .base.cra_name = "__ctr(aes)",
321 .base.cra_driver_name = "__ctr-aes-neonbs",
322 .base.cra_priority = 250,
323 .base.cra_blocksize = 1,
324 .base.cra_ctxsize = sizeof(struct aesbs_ctx),
325 .base.cra_module = THIS_MODULE,
326 .base.cra_flags = CRYPTO_ALG_INTERNAL,
327
328 .min_keysize = AES_MIN_KEY_SIZE,
329 .max_keysize = AES_MAX_KEY_SIZE,
330 .chunksize = AES_BLOCK_SIZE,
331 .walksize = 8 * AES_BLOCK_SIZE,
332 .ivsize = AES_BLOCK_SIZE,
333 .setkey = aesbs_setkey,
334 .encrypt = ctr_encrypt,
335 .decrypt = ctr_encrypt,
336}, {
337 .base.cra_name = "__xts(aes)",
338 .base.cra_driver_name = "__xts-aes-neonbs",
339 .base.cra_priority = 250,
340 .base.cra_blocksize = AES_BLOCK_SIZE,
341 .base.cra_ctxsize = sizeof(struct aesbs_xts_ctx),
342 .base.cra_module = THIS_MODULE,
343 .base.cra_flags = CRYPTO_ALG_INTERNAL,
344
345 .min_keysize = 2 * AES_MIN_KEY_SIZE,
346 .max_keysize = 2 * AES_MAX_KEY_SIZE,
347 .walksize = 8 * AES_BLOCK_SIZE,
348 .ivsize = AES_BLOCK_SIZE,
349 .setkey = aesbs_xts_setkey,
350 .encrypt = xts_encrypt,
351 .decrypt = xts_decrypt,
352} };
353
354static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
355
356static void aes_exit(void)
357{
358 int i;
359
360 for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++)
361 if (aes_simd_algs[i])
362 simd_skcipher_free(aes_simd_algs[i]);
363
364 crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
365}
366
367static int __init aes_init(void)
368{
369 struct simd_skcipher_alg *simd;
370 const char *basename;
371 const char *algname;
372 const char *drvname;
373 int err;
374 int i;
375
376 if (!(elf_hwcap & HWCAP_NEON))
377 return -ENODEV;
378
379 err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
380 if (err)
381 return err;
382
383 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
384 if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL))
385 continue;
386
387 algname = aes_algs[i].base.cra_name + 2;
388 drvname = aes_algs[i].base.cra_driver_name + 2;
389 basename = aes_algs[i].base.cra_driver_name;
390 simd = simd_skcipher_create_compat(algname, drvname, basename);
391 err = PTR_ERR(simd);
392 if (IS_ERR(simd))
393 goto unregister_simds;
394
395 aes_simd_algs[i] = simd;
396 }
397 return 0;
398
399unregister_simds:
400 aes_exit();
401 return err;
402}
403
404module_init(aes_init);
405module_exit(aes_exit);