2 * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
4 * Copyright (C) 2013 - 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.
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>
19 #include "aes-ce-setkey.h"
21 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
25 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
26 asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
32 static int num_rounds(struct crypto_aes_ctx *ctx)
35 * # of rounds specified by AES:
36 * 128 bit key 10 rounds
37 * 192 bit key 12 rounds
38 * 256 bit key 14 rounds
39 * => n byte key => 6 + (n/4) rounds
41 return 6 + ctx->key_length / 4;
44 static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
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;
52 if (!may_use_simd()) {
53 __aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
59 __asm__(" ld1 {v0.16b}, %[in] ;"
60 " ld1 {v1.4s}, [%[key]], #16 ;"
61 " cmp %w[rounds], #10 ;"
64 " mov v3.16b, v1.16b ;"
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 ;"
79 " aese v0.16b, v2.16b ;"
80 " eor v0.16b, v0.16b, v3.16b ;"
81 " st1 {v0.16b}, %[out] ;"
88 "2"(num_rounds(ctx) - 2)
94 static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
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;
102 if (!may_use_simd()) {
103 __aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
109 __asm__(" ld1 {v0.16b}, %[in] ;"
110 " ld1 {v1.4s}, [%[key]], #16 ;"
111 " cmp %w[rounds], #10 ;"
114 " mov v3.16b, v1.16b ;"
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 ;"
129 " aesd v0.16b, v2.16b ;"
130 " eor v0.16b, v0.16b, v3.16b ;"
131 " st1 {v0.16b}, %[out] ;"
135 [rounds] "=r"(dummy1)
138 "2"(num_rounds(ctx) - 2)
145 * aes_sub() - use the aese instruction to perform the AES sbox substitution
146 * on each byte in 'input'
148 static u32 aes_sub(u32 input)
152 __asm__("dup v1.4s, %w[in] ;"
154 "aese v0.16b, v1.16b ;"
155 "umov %w[out], v0.4s[0] ;"
164 int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
165 unsigned int key_len)
168 * The AES key schedule round constants
170 static u8 const rcon[] = {
171 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
174 u32 kwords = key_len / sizeof(u32);
175 struct aes_block *key_enc, *key_dec;
178 if (key_len != AES_KEYSIZE_128 &&
179 key_len != AES_KEYSIZE_192 &&
180 key_len != AES_KEYSIZE_256)
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));
188 for (i = 0; i < sizeof(rcon); i++) {
189 u32 *rki = ctx->key_enc + (i * kwords);
190 u32 *rko = rki + kwords;
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];
197 if (key_len == AES_KEYSIZE_192) {
200 rko[4] = rko[3] ^ rki[4];
201 rko[5] = rko[4] ^ rki[5];
202 } else if (key_len == AES_KEYSIZE_256) {
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];
213 * Generate the decryption keys for the Equivalent Inverse Cipher.
214 * This involves reversing the order of the round keys, and applying
215 * the Inverse Mix Columns transformation on all but the first and
218 key_enc = (struct aes_block *)ctx->key_enc;
219 key_dec = (struct aes_block *)ctx->key_dec;
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] ;"
228 : [out] "=Q"(key_dec[i])
229 : [in] "Q"(key_enc[j])
231 key_dec[i] = key_enc[0];
236 EXPORT_SYMBOL(ce_aes_expandkey);
238 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
239 unsigned int key_len)
241 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
244 ret = ce_aes_expandkey(ctx, in_key, key_len);
248 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
251 EXPORT_SYMBOL(ce_aes_setkey);
253 static struct crypto_alg aes_alg = {
255 .cra_driver_name = "aes-ce",
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,
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
270 static int __init aes_mod_init(void)
272 return crypto_register_alg(&aes_alg);
275 static void __exit aes_mod_exit(void)
277 crypto_unregister_alg(&aes_alg);
280 module_cpu_feature_match(AES, aes_mod_init);
281 module_exit(aes_mod_exit);