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 <crypto/internal/simd.h>
16 #include <linux/cpufeature.h>
17 #include <linux/crypto.h>
18 #include <linux/module.h>
20 #include "aes-ce-setkey.h"
22 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
26 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
27 asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
33 asmlinkage void __aes_ce_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
34 asmlinkage void __aes_ce_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
36 asmlinkage u32 __aes_ce_sub(u32 l);
37 asmlinkage void __aes_ce_invert(struct aes_block *out,
38 const struct aes_block *in);
40 static int num_rounds(struct crypto_aes_ctx *ctx)
43 * # of rounds specified by AES:
44 * 128 bit key 10 rounds
45 * 192 bit key 12 rounds
46 * 256 bit key 14 rounds
47 * => n byte key => 6 + (n/4) rounds
49 return 6 + ctx->key_length / 4;
52 static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
54 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
56 if (!crypto_simd_usable()) {
57 __aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
62 __aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
66 static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
68 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
70 if (!crypto_simd_usable()) {
71 __aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
76 __aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
80 int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
84 * The AES key schedule round constants
86 static u8 const rcon[] = {
87 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
90 u32 kwords = key_len / sizeof(u32);
91 struct aes_block *key_enc, *key_dec;
94 if (key_len != AES_KEYSIZE_128 &&
95 key_len != AES_KEYSIZE_192 &&
96 key_len != AES_KEYSIZE_256)
99 ctx->key_length = key_len;
100 for (i = 0; i < kwords; i++)
101 ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));
104 for (i = 0; i < sizeof(rcon); i++) {
105 u32 *rki = ctx->key_enc + (i * kwords);
106 u32 *rko = rki + kwords;
108 rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0];
109 rko[1] = rko[0] ^ rki[1];
110 rko[2] = rko[1] ^ rki[2];
111 rko[3] = rko[2] ^ rki[3];
113 if (key_len == AES_KEYSIZE_192) {
116 rko[4] = rko[3] ^ rki[4];
117 rko[5] = rko[4] ^ rki[5];
118 } else if (key_len == AES_KEYSIZE_256) {
121 rko[4] = __aes_ce_sub(rko[3]) ^ rki[4];
122 rko[5] = rko[4] ^ rki[5];
123 rko[6] = rko[5] ^ rki[6];
124 rko[7] = rko[6] ^ rki[7];
129 * Generate the decryption keys for the Equivalent Inverse Cipher.
130 * This involves reversing the order of the round keys, and applying
131 * the Inverse Mix Columns transformation on all but the first and
134 key_enc = (struct aes_block *)ctx->key_enc;
135 key_dec = (struct aes_block *)ctx->key_dec;
138 key_dec[0] = key_enc[j];
139 for (i = 1, j--; j > 0; i++, j--)
140 __aes_ce_invert(key_dec + i, key_enc + j);
141 key_dec[i] = key_enc[0];
146 EXPORT_SYMBOL(ce_aes_expandkey);
148 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
149 unsigned int key_len)
151 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
154 ret = ce_aes_expandkey(ctx, in_key, key_len);
158 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
161 EXPORT_SYMBOL(ce_aes_setkey);
163 static struct crypto_alg aes_alg = {
165 .cra_driver_name = "aes-ce",
167 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
168 .cra_blocksize = AES_BLOCK_SIZE,
169 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
170 .cra_module = THIS_MODULE,
172 .cia_min_keysize = AES_MIN_KEY_SIZE,
173 .cia_max_keysize = AES_MAX_KEY_SIZE,
174 .cia_setkey = ce_aes_setkey,
175 .cia_encrypt = aes_cipher_encrypt,
176 .cia_decrypt = aes_cipher_decrypt
180 static int __init aes_mod_init(void)
182 return crypto_register_alg(&aes_alg);
185 static void __exit aes_mod_exit(void)
187 crypto_unregister_alg(&aes_alg);
190 module_cpu_feature_match(AES, aes_mod_init);
191 module_exit(aes_mod_exit);