2 * Bit sliced AES using NEON instructions
4 * Copyright (C) 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 <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>
19 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
20 MODULE_LICENSE("GPL v2");
22 MODULE_ALIAS_CRYPTO("ecb(aes)");
23 MODULE_ALIAS_CRYPTO("cbc(aes)");
24 MODULE_ALIAS_CRYPTO("ctr(aes)");
25 MODULE_ALIAS_CRYPTO("xts(aes)");
27 asmlinkage
void aesbs_convert_key(u8 out
[], u32
const rk
[], int rounds
);
29 asmlinkage
void aesbs_ecb_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
30 int rounds
, int blocks
);
31 asmlinkage
void aesbs_ecb_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
32 int rounds
, int blocks
);
34 asmlinkage
void aesbs_cbc_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
35 int rounds
, int blocks
, u8 iv
[]);
37 asmlinkage
void aesbs_ctr_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
38 int rounds
, int blocks
, u8 ctr
[], u8 final
[]);
40 asmlinkage
void aesbs_xts_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
41 int rounds
, int blocks
, u8 iv
[]);
42 asmlinkage
void aesbs_xts_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
43 int rounds
, int blocks
, u8 iv
[]);
47 u8 rk
[13 * (8 * AES_BLOCK_SIZE
) + 32] __aligned(AES_BLOCK_SIZE
);
50 struct aesbs_cbc_ctx
{
52 struct crypto_cipher
*enc_tfm
;
55 struct aesbs_xts_ctx
{
57 struct crypto_cipher
*tweak_tfm
;
60 static int aesbs_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
63 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
64 struct crypto_aes_ctx rk
;
67 err
= crypto_aes_expand_key(&rk
, in_key
, key_len
);
71 ctx
->rounds
= 6 + key_len
/ 4;
74 aesbs_convert_key(ctx
->rk
, rk
.key_enc
, ctx
->rounds
);
80 static int __ecb_crypt(struct skcipher_request
*req
,
81 void (*fn
)(u8 out
[], u8
const in
[], u8
const rk
[],
82 int rounds
, int blocks
))
84 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
85 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
86 struct skcipher_walk walk
;
89 err
= skcipher_walk_virt(&walk
, req
, true);
92 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
93 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
95 if (walk
.nbytes
< walk
.total
)
96 blocks
= round_down(blocks
,
97 walk
.stride
/ AES_BLOCK_SIZE
);
99 fn(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
, ctx
->rk
,
100 ctx
->rounds
, blocks
);
101 err
= skcipher_walk_done(&walk
,
102 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
109 static int ecb_encrypt(struct skcipher_request
*req
)
111 return __ecb_crypt(req
, aesbs_ecb_encrypt
);
114 static int ecb_decrypt(struct skcipher_request
*req
)
116 return __ecb_crypt(req
, aesbs_ecb_decrypt
);
119 static int aesbs_cbc_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
120 unsigned int key_len
)
122 struct aesbs_cbc_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
123 struct crypto_aes_ctx rk
;
126 err
= crypto_aes_expand_key(&rk
, in_key
, key_len
);
130 ctx
->key
.rounds
= 6 + key_len
/ 4;
133 aesbs_convert_key(ctx
->key
.rk
, rk
.key_enc
, ctx
->key
.rounds
);
136 return crypto_cipher_setkey(ctx
->enc_tfm
, in_key
, key_len
);
139 static void cbc_encrypt_one(struct crypto_skcipher
*tfm
, const u8
*src
, u8
*dst
)
141 struct aesbs_cbc_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
143 crypto_cipher_encrypt_one(ctx
->enc_tfm
, dst
, src
);
146 static int cbc_encrypt(struct skcipher_request
*req
)
148 return crypto_cbc_encrypt_walk(req
, cbc_encrypt_one
);
151 static int cbc_decrypt(struct skcipher_request
*req
)
153 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
154 struct aesbs_cbc_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
155 struct skcipher_walk walk
;
158 err
= skcipher_walk_virt(&walk
, req
, true);
161 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
162 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
164 if (walk
.nbytes
< walk
.total
)
165 blocks
= round_down(blocks
,
166 walk
.stride
/ AES_BLOCK_SIZE
);
168 aesbs_cbc_decrypt(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
169 ctx
->key
.rk
, ctx
->key
.rounds
, blocks
,
171 err
= skcipher_walk_done(&walk
,
172 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
179 static int cbc_init(struct crypto_tfm
*tfm
)
181 struct aesbs_cbc_ctx
*ctx
= crypto_tfm_ctx(tfm
);
183 ctx
->enc_tfm
= crypto_alloc_cipher("aes", 0, 0);
185 return PTR_ERR_OR_ZERO(ctx
->enc_tfm
);
188 static void cbc_exit(struct crypto_tfm
*tfm
)
190 struct aesbs_cbc_ctx
*ctx
= crypto_tfm_ctx(tfm
);
192 crypto_free_cipher(ctx
->enc_tfm
);
195 static int ctr_encrypt(struct skcipher_request
*req
)
197 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
198 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
199 struct skcipher_walk walk
;
200 u8 buf
[AES_BLOCK_SIZE
];
203 err
= skcipher_walk_virt(&walk
, req
, true);
206 while (walk
.nbytes
> 0) {
207 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
208 u8
*final
= (walk
.total
% AES_BLOCK_SIZE
) ? buf
: NULL
;
210 if (walk
.nbytes
< walk
.total
) {
211 blocks
= round_down(blocks
,
212 walk
.stride
/ AES_BLOCK_SIZE
);
216 aesbs_ctr_encrypt(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
217 ctx
->rk
, ctx
->rounds
, blocks
, walk
.iv
, final
);
220 u8
*dst
= walk
.dst
.virt
.addr
+ blocks
* AES_BLOCK_SIZE
;
221 u8
*src
= walk
.src
.virt
.addr
+ blocks
* AES_BLOCK_SIZE
;
223 crypto_xor_cpy(dst
, src
, final
,
224 walk
.total
% AES_BLOCK_SIZE
);
226 err
= skcipher_walk_done(&walk
, 0);
229 err
= skcipher_walk_done(&walk
,
230 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
237 static int aesbs_xts_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
238 unsigned int key_len
)
240 struct aesbs_xts_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
243 err
= xts_verify_key(tfm
, in_key
, key_len
);
248 err
= crypto_cipher_setkey(ctx
->tweak_tfm
, in_key
+ key_len
, key_len
);
252 return aesbs_setkey(tfm
, in_key
, key_len
);
255 static int xts_init(struct crypto_tfm
*tfm
)
257 struct aesbs_xts_ctx
*ctx
= crypto_tfm_ctx(tfm
);
259 ctx
->tweak_tfm
= crypto_alloc_cipher("aes", 0, 0);
261 return PTR_ERR_OR_ZERO(ctx
->tweak_tfm
);
264 static void xts_exit(struct crypto_tfm
*tfm
)
266 struct aesbs_xts_ctx
*ctx
= crypto_tfm_ctx(tfm
);
268 crypto_free_cipher(ctx
->tweak_tfm
);
271 static int __xts_crypt(struct skcipher_request
*req
,
272 void (*fn
)(u8 out
[], u8
const in
[], u8
const rk
[],
273 int rounds
, int blocks
, u8 iv
[]))
275 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
276 struct aesbs_xts_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
277 struct skcipher_walk walk
;
280 err
= skcipher_walk_virt(&walk
, req
, true);
282 crypto_cipher_encrypt_one(ctx
->tweak_tfm
, walk
.iv
, walk
.iv
);
285 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
286 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
288 if (walk
.nbytes
< walk
.total
)
289 blocks
= round_down(blocks
,
290 walk
.stride
/ AES_BLOCK_SIZE
);
292 fn(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
, ctx
->key
.rk
,
293 ctx
->key
.rounds
, blocks
, walk
.iv
);
294 err
= skcipher_walk_done(&walk
,
295 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
302 static int xts_encrypt(struct skcipher_request
*req
)
304 return __xts_crypt(req
, aesbs_xts_encrypt
);
307 static int xts_decrypt(struct skcipher_request
*req
)
309 return __xts_crypt(req
, aesbs_xts_decrypt
);
312 static struct skcipher_alg aes_algs
[] = { {
313 .base
.cra_name
= "__ecb(aes)",
314 .base
.cra_driver_name
= "__ecb-aes-neonbs",
315 .base
.cra_priority
= 250,
316 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
317 .base
.cra_ctxsize
= sizeof(struct aesbs_ctx
),
318 .base
.cra_module
= THIS_MODULE
,
319 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
321 .min_keysize
= AES_MIN_KEY_SIZE
,
322 .max_keysize
= AES_MAX_KEY_SIZE
,
323 .walksize
= 8 * AES_BLOCK_SIZE
,
324 .setkey
= aesbs_setkey
,
325 .encrypt
= ecb_encrypt
,
326 .decrypt
= ecb_decrypt
,
328 .base
.cra_name
= "__cbc(aes)",
329 .base
.cra_driver_name
= "__cbc-aes-neonbs",
330 .base
.cra_priority
= 250,
331 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
332 .base
.cra_ctxsize
= sizeof(struct aesbs_cbc_ctx
),
333 .base
.cra_module
= THIS_MODULE
,
334 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
335 .base
.cra_init
= cbc_init
,
336 .base
.cra_exit
= cbc_exit
,
338 .min_keysize
= AES_MIN_KEY_SIZE
,
339 .max_keysize
= AES_MAX_KEY_SIZE
,
340 .walksize
= 8 * AES_BLOCK_SIZE
,
341 .ivsize
= AES_BLOCK_SIZE
,
342 .setkey
= aesbs_cbc_setkey
,
343 .encrypt
= cbc_encrypt
,
344 .decrypt
= cbc_decrypt
,
346 .base
.cra_name
= "__ctr(aes)",
347 .base
.cra_driver_name
= "__ctr-aes-neonbs",
348 .base
.cra_priority
= 250,
349 .base
.cra_blocksize
= 1,
350 .base
.cra_ctxsize
= sizeof(struct aesbs_ctx
),
351 .base
.cra_module
= THIS_MODULE
,
352 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
354 .min_keysize
= AES_MIN_KEY_SIZE
,
355 .max_keysize
= AES_MAX_KEY_SIZE
,
356 .chunksize
= AES_BLOCK_SIZE
,
357 .walksize
= 8 * AES_BLOCK_SIZE
,
358 .ivsize
= AES_BLOCK_SIZE
,
359 .setkey
= aesbs_setkey
,
360 .encrypt
= ctr_encrypt
,
361 .decrypt
= ctr_encrypt
,
363 .base
.cra_name
= "__xts(aes)",
364 .base
.cra_driver_name
= "__xts-aes-neonbs",
365 .base
.cra_priority
= 250,
366 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
367 .base
.cra_ctxsize
= sizeof(struct aesbs_xts_ctx
),
368 .base
.cra_module
= THIS_MODULE
,
369 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
370 .base
.cra_init
= xts_init
,
371 .base
.cra_exit
= xts_exit
,
373 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
374 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
375 .walksize
= 8 * AES_BLOCK_SIZE
,
376 .ivsize
= AES_BLOCK_SIZE
,
377 .setkey
= aesbs_xts_setkey
,
378 .encrypt
= xts_encrypt
,
379 .decrypt
= xts_decrypt
,
382 static struct simd_skcipher_alg
*aes_simd_algs
[ARRAY_SIZE(aes_algs
)];
384 static void aes_exit(void)
388 for (i
= 0; i
< ARRAY_SIZE(aes_simd_algs
); i
++)
389 if (aes_simd_algs
[i
])
390 simd_skcipher_free(aes_simd_algs
[i
]);
392 crypto_unregister_skciphers(aes_algs
, ARRAY_SIZE(aes_algs
));
395 static int __init
aes_init(void)
397 struct simd_skcipher_alg
*simd
;
398 const char *basename
;
404 if (!(elf_hwcap
& HWCAP_NEON
))
407 err
= crypto_register_skciphers(aes_algs
, ARRAY_SIZE(aes_algs
));
411 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
412 if (!(aes_algs
[i
].base
.cra_flags
& CRYPTO_ALG_INTERNAL
))
415 algname
= aes_algs
[i
].base
.cra_name
+ 2;
416 drvname
= aes_algs
[i
].base
.cra_driver_name
+ 2;
417 basename
= aes_algs
[i
].base
.cra_driver_name
;
418 simd
= simd_skcipher_create_compat(algname
, drvname
, basename
);
421 goto unregister_simds
;
423 aes_simd_algs
[i
] = simd
;
432 late_initcall(aes_init
);
433 module_exit(aes_exit
);