1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bit sliced AES using NEON instructions
5 * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
10 #include <crypto/aes.h>
11 #include <crypto/internal/simd.h>
12 #include <crypto/internal/skcipher.h>
13 #include <crypto/xts.h>
14 #include <linux/module.h>
16 #include "aes-ctr-fallback.h"
18 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
19 MODULE_LICENSE("GPL v2");
21 MODULE_ALIAS_CRYPTO("ecb(aes)");
22 MODULE_ALIAS_CRYPTO("cbc(aes)");
23 MODULE_ALIAS_CRYPTO("ctr(aes)");
24 MODULE_ALIAS_CRYPTO("xts(aes)");
26 asmlinkage
void aesbs_convert_key(u8 out
[], u32
const rk
[], int rounds
);
28 asmlinkage
void aesbs_ecb_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
29 int rounds
, int blocks
);
30 asmlinkage
void aesbs_ecb_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
31 int rounds
, int blocks
);
33 asmlinkage
void aesbs_cbc_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
34 int rounds
, int blocks
, u8 iv
[]);
36 asmlinkage
void aesbs_ctr_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
37 int rounds
, int blocks
, u8 iv
[], u8 final
[]);
39 asmlinkage
void aesbs_xts_encrypt(u8 out
[], u8
const in
[], u8
const rk
[],
40 int rounds
, int blocks
, u8 iv
[]);
41 asmlinkage
void aesbs_xts_decrypt(u8 out
[], u8
const in
[], u8
const rk
[],
42 int rounds
, int blocks
, u8 iv
[]);
44 /* borrowed from aes-neon-blk.ko */
45 asmlinkage
void neon_aes_ecb_encrypt(u8 out
[], u8
const in
[], u32
const rk
[],
46 int rounds
, int blocks
);
47 asmlinkage
void neon_aes_cbc_encrypt(u8 out
[], u8
const in
[], u32
const rk
[],
48 int rounds
, int blocks
, u8 iv
[]);
51 u8 rk
[13 * (8 * AES_BLOCK_SIZE
) + 32];
53 } __aligned(AES_BLOCK_SIZE
);
55 struct aesbs_cbc_ctx
{
57 u32 enc
[AES_MAX_KEYLENGTH_U32
];
60 struct aesbs_ctr_ctx
{
61 struct aesbs_ctx key
; /* must be first member */
62 struct crypto_aes_ctx fallback
;
65 struct aesbs_xts_ctx
{
67 u32 twkey
[AES_MAX_KEYLENGTH_U32
];
70 static int aesbs_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
73 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
74 struct crypto_aes_ctx rk
;
77 err
= crypto_aes_expand_key(&rk
, in_key
, key_len
);
81 ctx
->rounds
= 6 + key_len
/ 4;
84 aesbs_convert_key(ctx
->rk
, rk
.key_enc
, ctx
->rounds
);
90 static int __ecb_crypt(struct skcipher_request
*req
,
91 void (*fn
)(u8 out
[], u8
const in
[], u8
const rk
[],
92 int rounds
, int blocks
))
94 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
95 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
96 struct skcipher_walk walk
;
99 err
= skcipher_walk_virt(&walk
, req
, false);
101 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
102 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
104 if (walk
.nbytes
< walk
.total
)
105 blocks
= round_down(blocks
,
106 walk
.stride
/ AES_BLOCK_SIZE
);
109 fn(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
, ctx
->rk
,
110 ctx
->rounds
, blocks
);
112 err
= skcipher_walk_done(&walk
,
113 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
119 static int ecb_encrypt(struct skcipher_request
*req
)
121 return __ecb_crypt(req
, aesbs_ecb_encrypt
);
124 static int ecb_decrypt(struct skcipher_request
*req
)
126 return __ecb_crypt(req
, aesbs_ecb_decrypt
);
129 static int aesbs_cbc_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
130 unsigned int key_len
)
132 struct aesbs_cbc_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
133 struct crypto_aes_ctx rk
;
136 err
= crypto_aes_expand_key(&rk
, in_key
, key_len
);
140 ctx
->key
.rounds
= 6 + key_len
/ 4;
142 memcpy(ctx
->enc
, rk
.key_enc
, sizeof(ctx
->enc
));
145 aesbs_convert_key(ctx
->key
.rk
, rk
.key_enc
, ctx
->key
.rounds
);
151 static int cbc_encrypt(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
, false);
160 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
161 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
163 /* fall back to the non-bitsliced NEON implementation */
165 neon_aes_cbc_encrypt(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
166 ctx
->enc
, ctx
->key
.rounds
, blocks
,
169 err
= skcipher_walk_done(&walk
, walk
.nbytes
% AES_BLOCK_SIZE
);
174 static int cbc_decrypt(struct skcipher_request
*req
)
176 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
177 struct aesbs_cbc_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
178 struct skcipher_walk walk
;
181 err
= skcipher_walk_virt(&walk
, req
, false);
183 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
184 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
186 if (walk
.nbytes
< walk
.total
)
187 blocks
= round_down(blocks
,
188 walk
.stride
/ AES_BLOCK_SIZE
);
191 aesbs_cbc_decrypt(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
192 ctx
->key
.rk
, ctx
->key
.rounds
, blocks
,
195 err
= skcipher_walk_done(&walk
,
196 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
202 static int aesbs_ctr_setkey_sync(struct crypto_skcipher
*tfm
, const u8
*in_key
,
203 unsigned int key_len
)
205 struct aesbs_ctr_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
208 err
= crypto_aes_expand_key(&ctx
->fallback
, in_key
, key_len
);
212 ctx
->key
.rounds
= 6 + key_len
/ 4;
215 aesbs_convert_key(ctx
->key
.rk
, ctx
->fallback
.key_enc
, ctx
->key
.rounds
);
221 static int ctr_encrypt(struct skcipher_request
*req
)
223 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
224 struct aesbs_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
225 struct skcipher_walk walk
;
226 u8 buf
[AES_BLOCK_SIZE
];
229 err
= skcipher_walk_virt(&walk
, req
, false);
231 while (walk
.nbytes
> 0) {
232 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
233 u8
*final
= (walk
.total
% AES_BLOCK_SIZE
) ? buf
: NULL
;
235 if (walk
.nbytes
< walk
.total
) {
236 blocks
= round_down(blocks
,
237 walk
.stride
/ AES_BLOCK_SIZE
);
242 aesbs_ctr_encrypt(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
243 ctx
->rk
, ctx
->rounds
, blocks
, walk
.iv
, final
);
247 u8
*dst
= walk
.dst
.virt
.addr
+ blocks
* AES_BLOCK_SIZE
;
248 u8
*src
= walk
.src
.virt
.addr
+ blocks
* AES_BLOCK_SIZE
;
250 crypto_xor_cpy(dst
, src
, final
,
251 walk
.total
% AES_BLOCK_SIZE
);
253 err
= skcipher_walk_done(&walk
, 0);
256 err
= skcipher_walk_done(&walk
,
257 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
262 static int aesbs_xts_setkey(struct crypto_skcipher
*tfm
, const u8
*in_key
,
263 unsigned int key_len
)
265 struct aesbs_xts_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
266 struct crypto_aes_ctx rk
;
269 err
= xts_verify_key(tfm
, in_key
, key_len
);
274 err
= crypto_aes_expand_key(&rk
, in_key
+ key_len
, key_len
);
278 memcpy(ctx
->twkey
, rk
.key_enc
, sizeof(ctx
->twkey
));
280 return aesbs_setkey(tfm
, in_key
, key_len
);
283 static int ctr_encrypt_sync(struct skcipher_request
*req
)
285 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
286 struct aesbs_ctr_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
288 if (!crypto_simd_usable())
289 return aes_ctr_encrypt_fallback(&ctx
->fallback
, req
);
291 return ctr_encrypt(req
);
294 static int __xts_crypt(struct skcipher_request
*req
,
295 void (*fn
)(u8 out
[], u8
const in
[], u8
const rk
[],
296 int rounds
, int blocks
, u8 iv
[]))
298 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
299 struct aesbs_xts_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
300 struct skcipher_walk walk
;
303 err
= skcipher_walk_virt(&walk
, req
, false);
308 neon_aes_ecb_encrypt(walk
.iv
, walk
.iv
, ctx
->twkey
, ctx
->key
.rounds
, 1);
311 while (walk
.nbytes
>= AES_BLOCK_SIZE
) {
312 unsigned int blocks
= walk
.nbytes
/ AES_BLOCK_SIZE
;
314 if (walk
.nbytes
< walk
.total
)
315 blocks
= round_down(blocks
,
316 walk
.stride
/ AES_BLOCK_SIZE
);
319 fn(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
, ctx
->key
.rk
,
320 ctx
->key
.rounds
, blocks
, walk
.iv
);
322 err
= skcipher_walk_done(&walk
,
323 walk
.nbytes
- blocks
* AES_BLOCK_SIZE
);
328 static int xts_encrypt(struct skcipher_request
*req
)
330 return __xts_crypt(req
, aesbs_xts_encrypt
);
333 static int xts_decrypt(struct skcipher_request
*req
)
335 return __xts_crypt(req
, aesbs_xts_decrypt
);
338 static struct skcipher_alg aes_algs
[] = { {
339 .base
.cra_name
= "__ecb(aes)",
340 .base
.cra_driver_name
= "__ecb-aes-neonbs",
341 .base
.cra_priority
= 250,
342 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
343 .base
.cra_ctxsize
= sizeof(struct aesbs_ctx
),
344 .base
.cra_module
= THIS_MODULE
,
345 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
347 .min_keysize
= AES_MIN_KEY_SIZE
,
348 .max_keysize
= AES_MAX_KEY_SIZE
,
349 .walksize
= 8 * AES_BLOCK_SIZE
,
350 .setkey
= aesbs_setkey
,
351 .encrypt
= ecb_encrypt
,
352 .decrypt
= ecb_decrypt
,
354 .base
.cra_name
= "__cbc(aes)",
355 .base
.cra_driver_name
= "__cbc-aes-neonbs",
356 .base
.cra_priority
= 250,
357 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
358 .base
.cra_ctxsize
= sizeof(struct aesbs_cbc_ctx
),
359 .base
.cra_module
= THIS_MODULE
,
360 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
362 .min_keysize
= AES_MIN_KEY_SIZE
,
363 .max_keysize
= AES_MAX_KEY_SIZE
,
364 .walksize
= 8 * AES_BLOCK_SIZE
,
365 .ivsize
= AES_BLOCK_SIZE
,
366 .setkey
= aesbs_cbc_setkey
,
367 .encrypt
= cbc_encrypt
,
368 .decrypt
= cbc_decrypt
,
370 .base
.cra_name
= "__ctr(aes)",
371 .base
.cra_driver_name
= "__ctr-aes-neonbs",
372 .base
.cra_priority
= 250,
373 .base
.cra_blocksize
= 1,
374 .base
.cra_ctxsize
= sizeof(struct aesbs_ctx
),
375 .base
.cra_module
= THIS_MODULE
,
376 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
378 .min_keysize
= AES_MIN_KEY_SIZE
,
379 .max_keysize
= AES_MAX_KEY_SIZE
,
380 .chunksize
= AES_BLOCK_SIZE
,
381 .walksize
= 8 * AES_BLOCK_SIZE
,
382 .ivsize
= AES_BLOCK_SIZE
,
383 .setkey
= aesbs_setkey
,
384 .encrypt
= ctr_encrypt
,
385 .decrypt
= ctr_encrypt
,
387 .base
.cra_name
= "ctr(aes)",
388 .base
.cra_driver_name
= "ctr-aes-neonbs",
389 .base
.cra_priority
= 250 - 1,
390 .base
.cra_blocksize
= 1,
391 .base
.cra_ctxsize
= sizeof(struct aesbs_ctr_ctx
),
392 .base
.cra_module
= THIS_MODULE
,
394 .min_keysize
= AES_MIN_KEY_SIZE
,
395 .max_keysize
= AES_MAX_KEY_SIZE
,
396 .chunksize
= AES_BLOCK_SIZE
,
397 .walksize
= 8 * AES_BLOCK_SIZE
,
398 .ivsize
= AES_BLOCK_SIZE
,
399 .setkey
= aesbs_ctr_setkey_sync
,
400 .encrypt
= ctr_encrypt_sync
,
401 .decrypt
= ctr_encrypt_sync
,
403 .base
.cra_name
= "__xts(aes)",
404 .base
.cra_driver_name
= "__xts-aes-neonbs",
405 .base
.cra_priority
= 250,
406 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
407 .base
.cra_ctxsize
= sizeof(struct aesbs_xts_ctx
),
408 .base
.cra_module
= THIS_MODULE
,
409 .base
.cra_flags
= CRYPTO_ALG_INTERNAL
,
411 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
412 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
413 .walksize
= 8 * AES_BLOCK_SIZE
,
414 .ivsize
= AES_BLOCK_SIZE
,
415 .setkey
= aesbs_xts_setkey
,
416 .encrypt
= xts_encrypt
,
417 .decrypt
= xts_decrypt
,
420 static struct simd_skcipher_alg
*aes_simd_algs
[ARRAY_SIZE(aes_algs
)];
422 static void aes_exit(void)
426 for (i
= 0; i
< ARRAY_SIZE(aes_simd_algs
); i
++)
427 if (aes_simd_algs
[i
])
428 simd_skcipher_free(aes_simd_algs
[i
]);
430 crypto_unregister_skciphers(aes_algs
, ARRAY_SIZE(aes_algs
));
433 static int __init
aes_init(void)
435 struct simd_skcipher_alg
*simd
;
436 const char *basename
;
442 if (!cpu_have_named_feature(ASIMD
))
445 err
= crypto_register_skciphers(aes_algs
, ARRAY_SIZE(aes_algs
));
449 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
450 if (!(aes_algs
[i
].base
.cra_flags
& CRYPTO_ALG_INTERNAL
))
453 algname
= aes_algs
[i
].base
.cra_name
+ 2;
454 drvname
= aes_algs
[i
].base
.cra_driver_name
+ 2;
455 basename
= aes_algs
[i
].base
.cra_driver_name
;
456 simd
= simd_skcipher_create_compat(algname
, drvname
, basename
);
459 goto unregister_simds
;
461 aes_simd_algs
[i
] = simd
;
470 module_init(aes_init
);
471 module_exit(aes_exit
);