2 * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
4 * Copyright (C) 2013 - 2014 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 <linux/cpufeature.h>
14 #include <linux/crypto.h>
15 #include <linux/module.h>
17 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
18 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
19 MODULE_LICENSE("GPL v2");
25 static int num_rounds(struct crypto_aes_ctx
*ctx
)
28 * # of rounds specified by AES:
29 * 128 bit key 10 rounds
30 * 192 bit key 12 rounds
31 * 256 bit key 14 rounds
32 * => n byte key => 6 + (n/4) rounds
34 return 6 + ctx
->key_length
/ 4;
37 static void aes_cipher_encrypt(struct crypto_tfm
*tfm
, u8 dst
[], u8
const src
[])
39 struct crypto_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
40 struct aes_block
*out
= (struct aes_block
*)dst
;
41 struct aes_block
const *in
= (struct aes_block
*)src
;
45 kernel_neon_begin_partial(4);
47 __asm__(" ld1 {v0.16b}, %[in] ;"
48 " ld1 {v1.2d}, [%[key]], #16 ;"
49 " cmp %w[rounds], #10 ;"
52 " mov v3.16b, v1.16b ;"
54 "0: mov v2.16b, v1.16b ;"
55 " ld1 {v3.2d}, [%[key]], #16 ;"
56 "1: aese v0.16b, v2.16b ;"
57 " aesmc v0.16b, v0.16b ;"
58 "2: ld1 {v1.2d}, [%[key]], #16 ;"
59 " aese v0.16b, v3.16b ;"
60 " aesmc v0.16b, v0.16b ;"
61 "3: ld1 {v2.2d}, [%[key]], #16 ;"
62 " subs %w[rounds], %w[rounds], #3 ;"
63 " aese v0.16b, v1.16b ;"
64 " aesmc v0.16b, v0.16b ;"
65 " ld1 {v3.2d}, [%[key]], #16 ;"
67 " aese v0.16b, v2.16b ;"
68 " eor v0.16b, v0.16b, v3.16b ;"
69 " st1 {v0.16b}, %[out] ;"
76 "2"(num_rounds(ctx
) - 2)
82 static void aes_cipher_decrypt(struct crypto_tfm
*tfm
, u8 dst
[], u8
const src
[])
84 struct crypto_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
85 struct aes_block
*out
= (struct aes_block
*)dst
;
86 struct aes_block
const *in
= (struct aes_block
*)src
;
90 kernel_neon_begin_partial(4);
92 __asm__(" ld1 {v0.16b}, %[in] ;"
93 " ld1 {v1.2d}, [%[key]], #16 ;"
94 " cmp %w[rounds], #10 ;"
97 " mov v3.16b, v1.16b ;"
99 "0: mov v2.16b, v1.16b ;"
100 " ld1 {v3.2d}, [%[key]], #16 ;"
101 "1: aesd v0.16b, v2.16b ;"
102 " aesimc v0.16b, v0.16b ;"
103 "2: ld1 {v1.2d}, [%[key]], #16 ;"
104 " aesd v0.16b, v3.16b ;"
105 " aesimc v0.16b, v0.16b ;"
106 "3: ld1 {v2.2d}, [%[key]], #16 ;"
107 " subs %w[rounds], %w[rounds], #3 ;"
108 " aesd v0.16b, v1.16b ;"
109 " aesimc v0.16b, v0.16b ;"
110 " ld1 {v3.2d}, [%[key]], #16 ;"
112 " aesd v0.16b, v2.16b ;"
113 " eor v0.16b, v0.16b, v3.16b ;"
114 " st1 {v0.16b}, %[out] ;"
118 [rounds
] "=r"(dummy1
)
121 "2"(num_rounds(ctx
) - 2)
127 static struct crypto_alg aes_alg
= {
129 .cra_driver_name
= "aes-ce",
131 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
132 .cra_blocksize
= AES_BLOCK_SIZE
,
133 .cra_ctxsize
= sizeof(struct crypto_aes_ctx
),
134 .cra_module
= THIS_MODULE
,
136 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
137 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
138 .cia_setkey
= crypto_aes_set_key
,
139 .cia_encrypt
= aes_cipher_encrypt
,
140 .cia_decrypt
= aes_cipher_decrypt
144 static int __init
aes_mod_init(void)
146 return crypto_register_alg(&aes_alg
);
149 static void __exit
aes_mod_exit(void)
151 crypto_unregister_alg(&aes_alg
);
154 module_cpu_feature_match(AES
, aes_mod_init
);
155 module_exit(aes_mod_exit
);