xref: /linux/arch/arm64/crypto/aes-ce-glue.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5  *
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.
9  */
10 
11 #include <asm/neon.h>
12 #include <asm/simd.h>
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>
18 
19 #include "aes-ce-setkey.h"
20 
21 MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
24 
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);
27 
28 struct aes_block {
29 	u8 b[AES_BLOCK_SIZE];
30 };
31 
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);
34 
35 asmlinkage u32 __aes_ce_sub(u32 l);
36 asmlinkage void __aes_ce_invert(struct aes_block *out,
37 				const struct aes_block *in);
38 
39 static int num_rounds(struct crypto_aes_ctx *ctx)
40 {
41 	/*
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
47 	 */
48 	return 6 + ctx->key_length / 4;
49 }
50 
51 static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
52 {
53 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
54 
55 	if (!may_use_simd()) {
56 		__aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
57 		return;
58 	}
59 
60 	kernel_neon_begin();
61 	__aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx));
62 	kernel_neon_end();
63 }
64 
65 static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])
66 {
67 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
68 
69 	if (!may_use_simd()) {
70 		__aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
71 		return;
72 	}
73 
74 	kernel_neon_begin();
75 	__aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx));
76 	kernel_neon_end();
77 }
78 
79 int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
80 		     unsigned int key_len)
81 {
82 	/*
83 	 * The AES key schedule round constants
84 	 */
85 	static u8 const rcon[] = {
86 		0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
87 	};
88 
89 	u32 kwords = key_len / sizeof(u32);
90 	struct aes_block *key_enc, *key_dec;
91 	int i, j;
92 
93 	if (key_len != AES_KEYSIZE_128 &&
94 	    key_len != AES_KEYSIZE_192 &&
95 	    key_len != AES_KEYSIZE_256)
96 		return -EINVAL;
97 
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));
101 
102 	kernel_neon_begin();
103 	for (i = 0; i < sizeof(rcon); i++) {
104 		u32 *rki = ctx->key_enc + (i * kwords);
105 		u32 *rko = rki + kwords;
106 
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];
111 
112 		if (key_len == AES_KEYSIZE_192) {
113 			if (i >= 7)
114 				break;
115 			rko[4] = rko[3] ^ rki[4];
116 			rko[5] = rko[4] ^ rki[5];
117 		} else if (key_len == AES_KEYSIZE_256) {
118 			if (i >= 6)
119 				break;
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];
124 		}
125 	}
126 
127 	/*
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
131 	 * the last one.
132 	 */
133 	key_enc = (struct aes_block *)ctx->key_enc;
134 	key_dec = (struct aes_block *)ctx->key_dec;
135 	j = num_rounds(ctx);
136 
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];
141 
142 	kernel_neon_end();
143 	return 0;
144 }
145 EXPORT_SYMBOL(ce_aes_expandkey);
146 
147 int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
148 		  unsigned int key_len)
149 {
150 	struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
151 	int ret;
152 
153 	ret = ce_aes_expandkey(ctx, in_key, key_len);
154 	if (!ret)
155 		return 0;
156 
157 	tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
158 	return -EINVAL;
159 }
160 EXPORT_SYMBOL(ce_aes_setkey);
161 
162 static struct crypto_alg aes_alg = {
163 	.cra_name		= "aes",
164 	.cra_driver_name	= "aes-ce",
165 	.cra_priority		= 250,
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,
170 	.cra_cipher = {
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
176 	}
177 };
178 
179 static int __init aes_mod_init(void)
180 {
181 	return crypto_register_alg(&aes_alg);
182 }
183 
184 static void __exit aes_mod_exit(void)
185 {
186 	crypto_unregister_alg(&aes_alg);
187 }
188 
189 module_cpu_feature_match(AES, aes_mod_init);
190 module_exit(aes_mod_exit);
191