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 asmlinkage
void __aes_ce_encrypt(u32
*rk
, u8
*out
, const u8
*in
, int rounds
);
33 asmlinkage
void __aes_ce_decrypt(u32
*rk
, u8
*out
, const u8
*in
, int rounds
);
35 asmlinkage u32
__aes_ce_sub(u32 l
);
36 asmlinkage
void __aes_ce_invert(struct aes_block
*out
,
37 const struct aes_block
*in
);
39 static int num_rounds(struct crypto_aes_ctx
*ctx
)
42 * # of rounds specified by AES:
43 * 128 bit key 10 rounds
44 * 192 bit key 12 rounds
45 * 256 bit key 14 rounds
46 * => n byte key => 6 + (n/4) rounds
48 return 6 + ctx
->key_length
/ 4;
51 static void aes_cipher_encrypt(struct crypto_tfm
*tfm
, u8 dst
[], u8
const src
[])
53 struct crypto_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
55 if (!may_use_simd()) {
56 __aes_arm64_encrypt(ctx
->key_enc
, dst
, src
, num_rounds(ctx
));
61 __aes_ce_encrypt(ctx
->key_enc
, dst
, src
, num_rounds(ctx
));
65 static void aes_cipher_decrypt(struct crypto_tfm
*tfm
, u8 dst
[], u8
const src
[])
67 struct crypto_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
69 if (!may_use_simd()) {
70 __aes_arm64_decrypt(ctx
->key_dec
, dst
, src
, num_rounds(ctx
));
75 __aes_ce_decrypt(ctx
->key_dec
, dst
, src
, num_rounds(ctx
));
79 int ce_aes_expandkey(struct crypto_aes_ctx
*ctx
, const u8
*in_key
,
83 * The AES key schedule round constants
85 static u8
const rcon
[] = {
86 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
89 u32 kwords
= key_len
/ sizeof(u32
);
90 struct aes_block
*key_enc
, *key_dec
;
93 if (key_len
!= AES_KEYSIZE_128
&&
94 key_len
!= AES_KEYSIZE_192
&&
95 key_len
!= AES_KEYSIZE_256
)
98 ctx
->key_length
= key_len
;
99 for (i
= 0; i
< kwords
; i
++)
100 ctx
->key_enc
[i
] = get_unaligned_le32(in_key
+ i
* sizeof(u32
));
103 for (i
= 0; i
< sizeof(rcon
); i
++) {
104 u32
*rki
= ctx
->key_enc
+ (i
* kwords
);
105 u32
*rko
= rki
+ kwords
;
107 rko
[0] = ror32(__aes_ce_sub(rki
[kwords
- 1]), 8) ^ rcon
[i
] ^ rki
[0];
108 rko
[1] = rko
[0] ^ rki
[1];
109 rko
[2] = rko
[1] ^ rki
[2];
110 rko
[3] = rko
[2] ^ rki
[3];
112 if (key_len
== AES_KEYSIZE_192
) {
115 rko
[4] = rko
[3] ^ rki
[4];
116 rko
[5] = rko
[4] ^ rki
[5];
117 } else if (key_len
== AES_KEYSIZE_256
) {
120 rko
[4] = __aes_ce_sub(rko
[3]) ^ rki
[4];
121 rko
[5] = rko
[4] ^ rki
[5];
122 rko
[6] = rko
[5] ^ rki
[6];
123 rko
[7] = rko
[6] ^ rki
[7];
128 * Generate the decryption keys for the Equivalent Inverse Cipher.
129 * This involves reversing the order of the round keys, and applying
130 * the Inverse Mix Columns transformation on all but the first and
133 key_enc
= (struct aes_block
*)ctx
->key_enc
;
134 key_dec
= (struct aes_block
*)ctx
->key_dec
;
137 key_dec
[0] = key_enc
[j
];
138 for (i
= 1, j
--; j
> 0; i
++, j
--)
139 __aes_ce_invert(key_dec
+ i
, key_enc
+ j
);
140 key_dec
[i
] = key_enc
[0];
145 EXPORT_SYMBOL(ce_aes_expandkey
);
147 int ce_aes_setkey(struct crypto_tfm
*tfm
, const u8
*in_key
,
148 unsigned int key_len
)
150 struct crypto_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
153 ret
= ce_aes_expandkey(ctx
, in_key
, key_len
);
157 tfm
->crt_flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
160 EXPORT_SYMBOL(ce_aes_setkey
);
162 static struct crypto_alg aes_alg
= {
164 .cra_driver_name
= "aes-ce",
166 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
167 .cra_blocksize
= AES_BLOCK_SIZE
,
168 .cra_ctxsize
= sizeof(struct crypto_aes_ctx
),
169 .cra_module
= THIS_MODULE
,
171 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
172 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
173 .cia_setkey
= ce_aes_setkey
,
174 .cia_encrypt
= aes_cipher_encrypt
,
175 .cia_decrypt
= aes_cipher_decrypt
179 static int __init
aes_mod_init(void)
181 return crypto_register_alg(&aes_alg
);
184 static void __exit
aes_mod_exit(void)
186 crypto_unregister_alg(&aes_alg
);
189 module_cpu_feature_match(AES
, aes_mod_init
);
190 module_exit(aes_mod_exit
);