xref: /linux/arch/arm/crypto/aes-ce-glue.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * aes-ce-glue.c - wrapper code for ARMv8 AES
3  *
4  * Copyright (C) 2015 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/hwcap.h>
12 #include <asm/neon.h>
13 #include <crypto/aes.h>
14 #include <crypto/internal/simd.h>
15 #include <crypto/internal/skcipher.h>
16 #include <linux/cpufeature.h>
17 #include <linux/module.h>
18 #include <crypto/xts.h>
19 
20 MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 Crypto Extensions");
21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22 MODULE_LICENSE("GPL v2");
23 
24 /* defined in aes-ce-core.S */
25 asmlinkage u32 ce_aes_sub(u32 input);
26 asmlinkage void ce_aes_invert(void *dst, void *src);
27 
28 asmlinkage void ce_aes_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],
29 				   int rounds, int blocks);
30 asmlinkage void ce_aes_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[],
31 				   int rounds, int blocks);
32 
33 asmlinkage void ce_aes_cbc_encrypt(u8 out[], u8 const in[], u8 const rk[],
34 				   int rounds, int blocks, u8 iv[]);
35 asmlinkage void ce_aes_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
36 				   int rounds, int blocks, u8 iv[]);
37 
38 asmlinkage void ce_aes_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
39 				   int rounds, int blocks, u8 ctr[]);
40 
41 asmlinkage void ce_aes_xts_encrypt(u8 out[], u8 const in[], u8 const rk1[],
42 				   int rounds, int blocks, u8 iv[],
43 				   u8 const rk2[], int first);
44 asmlinkage void ce_aes_xts_decrypt(u8 out[], u8 const in[], u8 const rk1[],
45 				   int rounds, int blocks, u8 iv[],
46 				   u8 const rk2[], int first);
47 
48 struct aes_block {
49 	u8 b[AES_BLOCK_SIZE];
50 };
51 
52 static int num_rounds(struct crypto_aes_ctx *ctx)
53 {
54 	/*
55 	 * # of rounds specified by AES:
56 	 * 128 bit key		10 rounds
57 	 * 192 bit key		12 rounds
58 	 * 256 bit key		14 rounds
59 	 * => n byte key	=> 6 + (n/4) rounds
60 	 */
61 	return 6 + ctx->key_length / 4;
62 }
63 
64 static int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
65 			    unsigned int key_len)
66 {
67 	/*
68 	 * The AES key schedule round constants
69 	 */
70 	static u8 const rcon[] = {
71 		0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
72 	};
73 
74 	u32 kwords = key_len / sizeof(u32);
75 	struct aes_block *key_enc, *key_dec;
76 	int i, j;
77 
78 	if (key_len != AES_KEYSIZE_128 &&
79 	    key_len != AES_KEYSIZE_192 &&
80 	    key_len != AES_KEYSIZE_256)
81 		return -EINVAL;
82 
83 	memcpy(ctx->key_enc, in_key, key_len);
84 	ctx->key_length = key_len;
85 
86 	kernel_neon_begin();
87 	for (i = 0; i < sizeof(rcon); i++) {
88 		u32 *rki = ctx->key_enc + (i * kwords);
89 		u32 *rko = rki + kwords;
90 
91 #ifndef CONFIG_CPU_BIG_ENDIAN
92 		rko[0] = ror32(ce_aes_sub(rki[kwords - 1]), 8);
93 		rko[0] = rko[0] ^ rki[0] ^ rcon[i];
94 #else
95 		rko[0] = rol32(ce_aes_sub(rki[kwords - 1]), 8);
96 		rko[0] = rko[0] ^ rki[0] ^ (rcon[i] << 24);
97 #endif
98 		rko[1] = rko[0] ^ rki[1];
99 		rko[2] = rko[1] ^ rki[2];
100 		rko[3] = rko[2] ^ rki[3];
101 
102 		if (key_len == AES_KEYSIZE_192) {
103 			if (i >= 7)
104 				break;
105 			rko[4] = rko[3] ^ rki[4];
106 			rko[5] = rko[4] ^ rki[5];
107 		} else if (key_len == AES_KEYSIZE_256) {
108 			if (i >= 6)
109 				break;
110 			rko[4] = ce_aes_sub(rko[3]) ^ rki[4];
111 			rko[5] = rko[4] ^ rki[5];
112 			rko[6] = rko[5] ^ rki[6];
113 			rko[7] = rko[6] ^ rki[7];
114 		}
115 	}
116 
117 	/*
118 	 * Generate the decryption keys for the Equivalent Inverse Cipher.
119 	 * This involves reversing the order of the round keys, and applying
120 	 * the Inverse Mix Columns transformation on all but the first and
121 	 * the last one.
122 	 */
123 	key_enc = (struct aes_block *)ctx->key_enc;
124 	key_dec = (struct aes_block *)ctx->key_dec;
125 	j = num_rounds(ctx);
126 
127 	key_dec[0] = key_enc[j];
128 	for (i = 1, j--; j > 0; i++, j--)
129 		ce_aes_invert(key_dec + i, key_enc + j);
130 	key_dec[i] = key_enc[0];
131 
132 	kernel_neon_end();
133 	return 0;
134 }
135 
136 static int ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
137 			 unsigned int key_len)
138 {
139 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
140 	int ret;
141 
142 	ret = ce_aes_expandkey(ctx, in_key, key_len);
143 	if (!ret)
144 		return 0;
145 
146 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
147 	return -EINVAL;
148 }
149 
150 struct crypto_aes_xts_ctx {
151 	struct crypto_aes_ctx key1;
152 	struct crypto_aes_ctx __aligned(8) key2;
153 };
154 
155 static int xts_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
156 		       unsigned int key_len)
157 {
158 	struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
159 	int ret;
160 
161 	ret = xts_verify_key(tfm, in_key, key_len);
162 	if (ret)
163 		return ret;
164 
165 	ret = ce_aes_expandkey(&ctx->key1, in_key, key_len / 2);
166 	if (!ret)
167 		ret = ce_aes_expandkey(&ctx->key2, &in_key[key_len / 2],
168 				       key_len / 2);
169 	if (!ret)
170 		return 0;
171 
172 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
173 	return -EINVAL;
174 }
175 
176 static int ecb_encrypt(struct skcipher_request *req)
177 {
178 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
179 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
180 	struct skcipher_walk walk;
181 	unsigned int blocks;
182 	int err;
183 
184 	err = skcipher_walk_virt(&walk, req, true);
185 
186 	kernel_neon_begin();
187 	while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
188 		ce_aes_ecb_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
189 				   (u8 *)ctx->key_enc, num_rounds(ctx), blocks);
190 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
191 	}
192 	kernel_neon_end();
193 	return err;
194 }
195 
196 static int ecb_decrypt(struct skcipher_request *req)
197 {
198 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
199 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
200 	struct skcipher_walk walk;
201 	unsigned int blocks;
202 	int err;
203 
204 	err = skcipher_walk_virt(&walk, req, true);
205 
206 	kernel_neon_begin();
207 	while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
208 		ce_aes_ecb_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
209 				   (u8 *)ctx->key_dec, num_rounds(ctx), blocks);
210 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
211 	}
212 	kernel_neon_end();
213 	return err;
214 }
215 
216 static int cbc_encrypt(struct skcipher_request *req)
217 {
218 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
219 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
220 	struct skcipher_walk walk;
221 	unsigned int blocks;
222 	int err;
223 
224 	err = skcipher_walk_virt(&walk, req, true);
225 
226 	kernel_neon_begin();
227 	while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
228 		ce_aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
229 				   (u8 *)ctx->key_enc, num_rounds(ctx), blocks,
230 				   walk.iv);
231 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
232 	}
233 	kernel_neon_end();
234 	return err;
235 }
236 
237 static int cbc_decrypt(struct skcipher_request *req)
238 {
239 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
240 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
241 	struct skcipher_walk walk;
242 	unsigned int blocks;
243 	int err;
244 
245 	err = skcipher_walk_virt(&walk, req, true);
246 
247 	kernel_neon_begin();
248 	while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
249 		ce_aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
250 				   (u8 *)ctx->key_dec, num_rounds(ctx), blocks,
251 				   walk.iv);
252 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
253 	}
254 	kernel_neon_end();
255 	return err;
256 }
257 
258 static int ctr_encrypt(struct skcipher_request *req)
259 {
260 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
261 	struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
262 	struct skcipher_walk walk;
263 	int err, blocks;
264 
265 	err = skcipher_walk_virt(&walk, req, true);
266 
267 	kernel_neon_begin();
268 	while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
269 		ce_aes_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
270 				   (u8 *)ctx->key_enc, num_rounds(ctx), blocks,
271 				   walk.iv);
272 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
273 	}
274 	if (walk.nbytes) {
275 		u8 __aligned(8) tail[AES_BLOCK_SIZE];
276 		unsigned int nbytes = walk.nbytes;
277 		u8 *tdst = walk.dst.virt.addr;
278 		u8 *tsrc = walk.src.virt.addr;
279 
280 		/*
281 		 * Tell aes_ctr_encrypt() to process a tail block.
282 		 */
283 		blocks = -1;
284 
285 		ce_aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc,
286 				   num_rounds(ctx), blocks, walk.iv);
287 		crypto_xor_cpy(tdst, tsrc, tail, nbytes);
288 		err = skcipher_walk_done(&walk, 0);
289 	}
290 	kernel_neon_end();
291 
292 	return err;
293 }
294 
295 static int xts_encrypt(struct skcipher_request *req)
296 {
297 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
298 	struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
299 	int err, first, rounds = num_rounds(&ctx->key1);
300 	struct skcipher_walk walk;
301 	unsigned int blocks;
302 
303 	err = skcipher_walk_virt(&walk, req, true);
304 
305 	kernel_neon_begin();
306 	for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
307 		ce_aes_xts_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
308 				   (u8 *)ctx->key1.key_enc, rounds, blocks,
309 				   walk.iv, (u8 *)ctx->key2.key_enc, first);
310 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
311 	}
312 	kernel_neon_end();
313 
314 	return err;
315 }
316 
317 static int xts_decrypt(struct skcipher_request *req)
318 {
319 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
320 	struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
321 	int err, first, rounds = num_rounds(&ctx->key1);
322 	struct skcipher_walk walk;
323 	unsigned int blocks;
324 
325 	err = skcipher_walk_virt(&walk, req, true);
326 
327 	kernel_neon_begin();
328 	for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
329 		ce_aes_xts_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
330 				   (u8 *)ctx->key1.key_dec, rounds, blocks,
331 				   walk.iv, (u8 *)ctx->key2.key_enc, first);
332 		err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
333 	}
334 	kernel_neon_end();
335 
336 	return err;
337 }
338 
339 static struct skcipher_alg aes_algs[] = { {
340 	.base = {
341 		.cra_name		= "__ecb(aes)",
342 		.cra_driver_name	= "__ecb-aes-ce",
343 		.cra_priority		= 300,
344 		.cra_flags		= CRYPTO_ALG_INTERNAL,
345 		.cra_blocksize		= AES_BLOCK_SIZE,
346 		.cra_ctxsize		= sizeof(struct crypto_aes_ctx),
347 		.cra_module		= THIS_MODULE,
348 	},
349 	.min_keysize	= AES_MIN_KEY_SIZE,
350 	.max_keysize	= AES_MAX_KEY_SIZE,
351 	.setkey		= ce_aes_setkey,
352 	.encrypt	= ecb_encrypt,
353 	.decrypt	= ecb_decrypt,
354 }, {
355 	.base = {
356 		.cra_name		= "__cbc(aes)",
357 		.cra_driver_name	= "__cbc-aes-ce",
358 		.cra_priority		= 300,
359 		.cra_flags		= CRYPTO_ALG_INTERNAL,
360 		.cra_blocksize		= AES_BLOCK_SIZE,
361 		.cra_ctxsize		= sizeof(struct crypto_aes_ctx),
362 		.cra_module		= THIS_MODULE,
363 	},
364 	.min_keysize	= AES_MIN_KEY_SIZE,
365 	.max_keysize	= AES_MAX_KEY_SIZE,
366 	.ivsize		= AES_BLOCK_SIZE,
367 	.setkey		= ce_aes_setkey,
368 	.encrypt	= cbc_encrypt,
369 	.decrypt	= cbc_decrypt,
370 }, {
371 	.base = {
372 		.cra_name		= "__ctr(aes)",
373 		.cra_driver_name	= "__ctr-aes-ce",
374 		.cra_priority		= 300,
375 		.cra_flags		= CRYPTO_ALG_INTERNAL,
376 		.cra_blocksize		= 1,
377 		.cra_ctxsize		= sizeof(struct crypto_aes_ctx),
378 		.cra_module		= THIS_MODULE,
379 	},
380 	.min_keysize	= AES_MIN_KEY_SIZE,
381 	.max_keysize	= AES_MAX_KEY_SIZE,
382 	.ivsize		= AES_BLOCK_SIZE,
383 	.chunksize	= AES_BLOCK_SIZE,
384 	.setkey		= ce_aes_setkey,
385 	.encrypt	= ctr_encrypt,
386 	.decrypt	= ctr_encrypt,
387 }, {
388 	.base = {
389 		.cra_name		= "__xts(aes)",
390 		.cra_driver_name	= "__xts-aes-ce",
391 		.cra_priority		= 300,
392 		.cra_flags		= CRYPTO_ALG_INTERNAL,
393 		.cra_blocksize		= AES_BLOCK_SIZE,
394 		.cra_ctxsize		= sizeof(struct crypto_aes_xts_ctx),
395 		.cra_module		= THIS_MODULE,
396 	},
397 	.min_keysize	= 2 * AES_MIN_KEY_SIZE,
398 	.max_keysize	= 2 * AES_MAX_KEY_SIZE,
399 	.ivsize		= AES_BLOCK_SIZE,
400 	.setkey		= xts_set_key,
401 	.encrypt	= xts_encrypt,
402 	.decrypt	= xts_decrypt,
403 } };
404 
405 static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
406 
407 static void aes_exit(void)
408 {
409 	int i;
410 
411 	for (i = 0; i < ARRAY_SIZE(aes_simd_algs) && aes_simd_algs[i]; i++)
412 		simd_skcipher_free(aes_simd_algs[i]);
413 
414 	crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
415 }
416 
417 static int __init aes_init(void)
418 {
419 	struct simd_skcipher_alg *simd;
420 	const char *basename;
421 	const char *algname;
422 	const char *drvname;
423 	int err;
424 	int i;
425 
426 	err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
427 	if (err)
428 		return err;
429 
430 	for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
431 		algname = aes_algs[i].base.cra_name + 2;
432 		drvname = aes_algs[i].base.cra_driver_name + 2;
433 		basename = aes_algs[i].base.cra_driver_name;
434 		simd = simd_skcipher_create_compat(algname, drvname, basename);
435 		err = PTR_ERR(simd);
436 		if (IS_ERR(simd))
437 			goto unregister_simds;
438 
439 		aes_simd_algs[i] = simd;
440 	}
441 
442 	return 0;
443 
444 unregister_simds:
445 	aes_exit();
446 	return err;
447 }
448 
449 module_cpu_feature_match(AES, aes_init);
450 module_exit(aes_exit);
451