1 /*
2  * Accelerated GHASH implementation with ARMv8 PMULL instructions.
3  *
4  * Copyright (C) 2014 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * 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 <crypto/algapi.h>
16 #include <crypto/b128ops.h>
17 #include <crypto/gf128mul.h>
18 #include <crypto/internal/aead.h>
19 #include <crypto/internal/hash.h>
20 #include <crypto/internal/skcipher.h>
21 #include <crypto/scatterwalk.h>
22 #include <linux/cpufeature.h>
23 #include <linux/crypto.h>
24 #include <linux/module.h>
25 
26 MODULE_DESCRIPTION("GHASH and AES-GCM using ARMv8 Crypto Extensions");
27 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
28 MODULE_LICENSE("GPL v2");
29 MODULE_ALIAS_CRYPTO("ghash");
30 
31 #define GHASH_BLOCK_SIZE	16
32 #define GHASH_DIGEST_SIZE	16
33 #define GCM_IV_SIZE		12
34 
35 struct ghash_key {
36 	u64			h[2];
37 	u64			h2[2];
38 	u64			h3[2];
39 	u64			h4[2];
40 
41 	be128			k;
42 };
43 
44 struct ghash_desc_ctx {
45 	u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
46 	u8 buf[GHASH_BLOCK_SIZE];
47 	u32 count;
48 };
49 
50 struct gcm_aes_ctx {
51 	struct crypto_aes_ctx	aes_key;
52 	struct ghash_key	ghash_key;
53 };
54 
55 asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
56 				       struct ghash_key const *k,
57 				       const char *head);
58 
59 asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
60 				      struct ghash_key const *k,
61 				      const char *head);
62 
63 asmlinkage void pmull_gcm_encrypt(int blocks, u64 dg[], u8 dst[],
64 				  const u8 src[], struct ghash_key const *k,
65 				  u8 ctr[], u32 const rk[], int rounds,
66 				  u8 ks[]);
67 
68 asmlinkage void pmull_gcm_decrypt(int blocks, u64 dg[], u8 dst[],
69 				  const u8 src[], struct ghash_key const *k,
70 				  u8 ctr[], u32 const rk[], int rounds);
71 
72 asmlinkage void pmull_gcm_encrypt_block(u8 dst[], u8 const src[],
73 					u32 const rk[], int rounds);
74 
75 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
76 
77 static int ghash_init(struct shash_desc *desc)
78 {
79 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
80 
81 	*ctx = (struct ghash_desc_ctx){};
82 	return 0;
83 }
84 
85 static void ghash_do_update(int blocks, u64 dg[], const char *src,
86 			    struct ghash_key *key, const char *head,
87 			    void (*simd_update)(int blocks, u64 dg[],
88 						const char *src,
89 						struct ghash_key const *k,
90 						const char *head))
91 {
92 	if (likely(may_use_simd())) {
93 		kernel_neon_begin();
94 		simd_update(blocks, dg, src, key, head);
95 		kernel_neon_end();
96 	} else {
97 		be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
98 
99 		do {
100 			const u8 *in = src;
101 
102 			if (head) {
103 				in = head;
104 				blocks++;
105 				head = NULL;
106 			} else {
107 				src += GHASH_BLOCK_SIZE;
108 			}
109 
110 			crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
111 			gf128mul_lle(&dst, &key->k);
112 		} while (--blocks);
113 
114 		dg[0] = be64_to_cpu(dst.b);
115 		dg[1] = be64_to_cpu(dst.a);
116 	}
117 }
118 
119 /* avoid hogging the CPU for too long */
120 #define MAX_BLOCKS	(SZ_64K / GHASH_BLOCK_SIZE)
121 
122 static int __ghash_update(struct shash_desc *desc, const u8 *src,
123 			  unsigned int len,
124 			  void (*simd_update)(int blocks, u64 dg[],
125 					      const char *src,
126 					      struct ghash_key const *k,
127 					      const char *head))
128 {
129 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
130 	unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
131 
132 	ctx->count += len;
133 
134 	if ((partial + len) >= GHASH_BLOCK_SIZE) {
135 		struct ghash_key *key = crypto_shash_ctx(desc->tfm);
136 		int blocks;
137 
138 		if (partial) {
139 			int p = GHASH_BLOCK_SIZE - partial;
140 
141 			memcpy(ctx->buf + partial, src, p);
142 			src += p;
143 			len -= p;
144 		}
145 
146 		blocks = len / GHASH_BLOCK_SIZE;
147 		len %= GHASH_BLOCK_SIZE;
148 
149 		do {
150 			int chunk = min(blocks, MAX_BLOCKS);
151 
152 			ghash_do_update(chunk, ctx->digest, src, key,
153 					partial ? ctx->buf : NULL,
154 					simd_update);
155 
156 			blocks -= chunk;
157 			src += chunk * GHASH_BLOCK_SIZE;
158 			partial = 0;
159 		} while (unlikely(blocks > 0));
160 	}
161 	if (len)
162 		memcpy(ctx->buf + partial, src, len);
163 	return 0;
164 }
165 
166 static int ghash_update_p8(struct shash_desc *desc, const u8 *src,
167 			   unsigned int len)
168 {
169 	return __ghash_update(desc, src, len, pmull_ghash_update_p8);
170 }
171 
172 static int ghash_update_p64(struct shash_desc *desc, const u8 *src,
173 			    unsigned int len)
174 {
175 	return __ghash_update(desc, src, len, pmull_ghash_update_p64);
176 }
177 
178 static int ghash_final_p8(struct shash_desc *desc, u8 *dst)
179 {
180 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
181 	unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
182 
183 	if (partial) {
184 		struct ghash_key *key = crypto_shash_ctx(desc->tfm);
185 
186 		memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
187 
188 		ghash_do_update(1, ctx->digest, ctx->buf, key, NULL,
189 				pmull_ghash_update_p8);
190 	}
191 	put_unaligned_be64(ctx->digest[1], dst);
192 	put_unaligned_be64(ctx->digest[0], dst + 8);
193 
194 	*ctx = (struct ghash_desc_ctx){};
195 	return 0;
196 }
197 
198 static int ghash_final_p64(struct shash_desc *desc, u8 *dst)
199 {
200 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
201 	unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
202 
203 	if (partial) {
204 		struct ghash_key *key = crypto_shash_ctx(desc->tfm);
205 
206 		memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
207 
208 		ghash_do_update(1, ctx->digest, ctx->buf, key, NULL,
209 				pmull_ghash_update_p64);
210 	}
211 	put_unaligned_be64(ctx->digest[1], dst);
212 	put_unaligned_be64(ctx->digest[0], dst + 8);
213 
214 	*ctx = (struct ghash_desc_ctx){};
215 	return 0;
216 }
217 
218 static void ghash_reflect(u64 h[], const be128 *k)
219 {
220 	u64 carry = be64_to_cpu(k->a) & BIT(63) ? 1 : 0;
221 
222 	h[0] = (be64_to_cpu(k->b) << 1) | carry;
223 	h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
224 
225 	if (carry)
226 		h[1] ^= 0xc200000000000000UL;
227 }
228 
229 static int __ghash_setkey(struct ghash_key *key,
230 			  const u8 *inkey, unsigned int keylen)
231 {
232 	be128 h;
233 
234 	/* needed for the fallback */
235 	memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
236 
237 	ghash_reflect(key->h, &key->k);
238 
239 	h = key->k;
240 	gf128mul_lle(&h, &key->k);
241 	ghash_reflect(key->h2, &h);
242 
243 	gf128mul_lle(&h, &key->k);
244 	ghash_reflect(key->h3, &h);
245 
246 	gf128mul_lle(&h, &key->k);
247 	ghash_reflect(key->h4, &h);
248 
249 	return 0;
250 }
251 
252 static int ghash_setkey(struct crypto_shash *tfm,
253 			const u8 *inkey, unsigned int keylen)
254 {
255 	struct ghash_key *key = crypto_shash_ctx(tfm);
256 
257 	if (keylen != GHASH_BLOCK_SIZE) {
258 		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
259 		return -EINVAL;
260 	}
261 
262 	return __ghash_setkey(key, inkey, keylen);
263 }
264 
265 static struct shash_alg ghash_alg[] = {{
266 	.base.cra_name		= "ghash",
267 	.base.cra_driver_name	= "ghash-neon",
268 	.base.cra_priority	= 100,
269 	.base.cra_blocksize	= GHASH_BLOCK_SIZE,
270 	.base.cra_ctxsize	= sizeof(struct ghash_key),
271 	.base.cra_module	= THIS_MODULE,
272 
273 	.digestsize		= GHASH_DIGEST_SIZE,
274 	.init			= ghash_init,
275 	.update			= ghash_update_p8,
276 	.final			= ghash_final_p8,
277 	.setkey			= ghash_setkey,
278 	.descsize		= sizeof(struct ghash_desc_ctx),
279 }, {
280 	.base.cra_name		= "ghash",
281 	.base.cra_driver_name	= "ghash-ce",
282 	.base.cra_priority	= 200,
283 	.base.cra_blocksize	= GHASH_BLOCK_SIZE,
284 	.base.cra_ctxsize	= sizeof(struct ghash_key),
285 	.base.cra_module	= THIS_MODULE,
286 
287 	.digestsize		= GHASH_DIGEST_SIZE,
288 	.init			= ghash_init,
289 	.update			= ghash_update_p64,
290 	.final			= ghash_final_p64,
291 	.setkey			= ghash_setkey,
292 	.descsize		= sizeof(struct ghash_desc_ctx),
293 }};
294 
295 static int num_rounds(struct crypto_aes_ctx *ctx)
296 {
297 	/*
298 	 * # of rounds specified by AES:
299 	 * 128 bit key		10 rounds
300 	 * 192 bit key		12 rounds
301 	 * 256 bit key		14 rounds
302 	 * => n byte key	=> 6 + (n/4) rounds
303 	 */
304 	return 6 + ctx->key_length / 4;
305 }
306 
307 static int gcm_setkey(struct crypto_aead *tfm, const u8 *inkey,
308 		      unsigned int keylen)
309 {
310 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(tfm);
311 	u8 key[GHASH_BLOCK_SIZE];
312 	int ret;
313 
314 	ret = crypto_aes_expand_key(&ctx->aes_key, inkey, keylen);
315 	if (ret) {
316 		tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
317 		return -EINVAL;
318 	}
319 
320 	__aes_arm64_encrypt(ctx->aes_key.key_enc, key, (u8[AES_BLOCK_SIZE]){},
321 			    num_rounds(&ctx->aes_key));
322 
323 	return __ghash_setkey(&ctx->ghash_key, key, sizeof(be128));
324 }
325 
326 static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
327 {
328 	switch (authsize) {
329 	case 4:
330 	case 8:
331 	case 12 ... 16:
332 		break;
333 	default:
334 		return -EINVAL;
335 	}
336 	return 0;
337 }
338 
339 static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
340 			   int *buf_count, struct gcm_aes_ctx *ctx)
341 {
342 	if (*buf_count > 0) {
343 		int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
344 
345 		memcpy(&buf[*buf_count], src, buf_added);
346 
347 		*buf_count += buf_added;
348 		src += buf_added;
349 		count -= buf_added;
350 	}
351 
352 	if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
353 		int blocks = count / GHASH_BLOCK_SIZE;
354 
355 		ghash_do_update(blocks, dg, src, &ctx->ghash_key,
356 				*buf_count ? buf : NULL,
357 				pmull_ghash_update_p64);
358 
359 		src += blocks * GHASH_BLOCK_SIZE;
360 		count %= GHASH_BLOCK_SIZE;
361 		*buf_count = 0;
362 	}
363 
364 	if (count > 0) {
365 		memcpy(buf, src, count);
366 		*buf_count = count;
367 	}
368 }
369 
370 static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
371 {
372 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
373 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
374 	u8 buf[GHASH_BLOCK_SIZE];
375 	struct scatter_walk walk;
376 	u32 len = req->assoclen;
377 	int buf_count = 0;
378 
379 	scatterwalk_start(&walk, req->src);
380 
381 	do {
382 		u32 n = scatterwalk_clamp(&walk, len);
383 		u8 *p;
384 
385 		if (!n) {
386 			scatterwalk_start(&walk, sg_next(walk.sg));
387 			n = scatterwalk_clamp(&walk, len);
388 		}
389 		p = scatterwalk_map(&walk);
390 
391 		gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
392 		len -= n;
393 
394 		scatterwalk_unmap(p);
395 		scatterwalk_advance(&walk, n);
396 		scatterwalk_done(&walk, 0, len);
397 	} while (len);
398 
399 	if (buf_count) {
400 		memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
401 		ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL,
402 				pmull_ghash_update_p64);
403 	}
404 }
405 
406 static void gcm_final(struct aead_request *req, struct gcm_aes_ctx *ctx,
407 		      u64 dg[], u8 tag[], int cryptlen)
408 {
409 	u8 mac[AES_BLOCK_SIZE];
410 	u128 lengths;
411 
412 	lengths.a = cpu_to_be64(req->assoclen * 8);
413 	lengths.b = cpu_to_be64(cryptlen * 8);
414 
415 	ghash_do_update(1, dg, (void *)&lengths, &ctx->ghash_key, NULL,
416 			pmull_ghash_update_p64);
417 
418 	put_unaligned_be64(dg[1], mac);
419 	put_unaligned_be64(dg[0], mac + 8);
420 
421 	crypto_xor(tag, mac, AES_BLOCK_SIZE);
422 }
423 
424 static int gcm_encrypt(struct aead_request *req)
425 {
426 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
427 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
428 	struct skcipher_walk walk;
429 	u8 iv[AES_BLOCK_SIZE];
430 	u8 ks[2 * AES_BLOCK_SIZE];
431 	u8 tag[AES_BLOCK_SIZE];
432 	u64 dg[2] = {};
433 	int nrounds = num_rounds(&ctx->aes_key);
434 	int err;
435 
436 	if (req->assoclen)
437 		gcm_calculate_auth_mac(req, dg);
438 
439 	memcpy(iv, req->iv, GCM_IV_SIZE);
440 	put_unaligned_be32(1, iv + GCM_IV_SIZE);
441 
442 	err = skcipher_walk_aead_encrypt(&walk, req, false);
443 
444 	if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
445 		u32 const *rk = NULL;
446 
447 		kernel_neon_begin();
448 		pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
449 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
450 		pmull_gcm_encrypt_block(ks, iv, NULL, nrounds);
451 		put_unaligned_be32(3, iv + GCM_IV_SIZE);
452 		pmull_gcm_encrypt_block(ks + AES_BLOCK_SIZE, iv, NULL, nrounds);
453 		put_unaligned_be32(4, iv + GCM_IV_SIZE);
454 
455 		do {
456 			int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
457 
458 			if (rk)
459 				kernel_neon_begin();
460 
461 			pmull_gcm_encrypt(blocks, dg, walk.dst.virt.addr,
462 					  walk.src.virt.addr, &ctx->ghash_key,
463 					  iv, rk, nrounds, ks);
464 			kernel_neon_end();
465 
466 			err = skcipher_walk_done(&walk,
467 					walk.nbytes % (2 * AES_BLOCK_SIZE));
468 
469 			rk = ctx->aes_key.key_enc;
470 		} while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
471 	} else {
472 		__aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
473 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
474 
475 		while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
476 			int blocks = walk.nbytes / AES_BLOCK_SIZE;
477 			u8 *dst = walk.dst.virt.addr;
478 			u8 *src = walk.src.virt.addr;
479 
480 			do {
481 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
482 						    ks, iv, nrounds);
483 				crypto_xor_cpy(dst, src, ks, AES_BLOCK_SIZE);
484 				crypto_inc(iv, AES_BLOCK_SIZE);
485 
486 				dst += AES_BLOCK_SIZE;
487 				src += AES_BLOCK_SIZE;
488 			} while (--blocks > 0);
489 
490 			ghash_do_update(walk.nbytes / AES_BLOCK_SIZE, dg,
491 					walk.dst.virt.addr, &ctx->ghash_key,
492 					NULL, pmull_ghash_update_p64);
493 
494 			err = skcipher_walk_done(&walk,
495 						 walk.nbytes % (2 * AES_BLOCK_SIZE));
496 		}
497 		if (walk.nbytes) {
498 			__aes_arm64_encrypt(ctx->aes_key.key_enc, ks, iv,
499 					    nrounds);
500 			if (walk.nbytes > AES_BLOCK_SIZE) {
501 				crypto_inc(iv, AES_BLOCK_SIZE);
502 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
503 					            ks + AES_BLOCK_SIZE, iv,
504 						    nrounds);
505 			}
506 		}
507 	}
508 
509 	/* handle the tail */
510 	if (walk.nbytes) {
511 		u8 buf[GHASH_BLOCK_SIZE];
512 		unsigned int nbytes = walk.nbytes;
513 		u8 *dst = walk.dst.virt.addr;
514 		u8 *head = NULL;
515 
516 		crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, ks,
517 			       walk.nbytes);
518 
519 		if (walk.nbytes > GHASH_BLOCK_SIZE) {
520 			head = dst;
521 			dst += GHASH_BLOCK_SIZE;
522 			nbytes %= GHASH_BLOCK_SIZE;
523 		}
524 
525 		memcpy(buf, dst, nbytes);
526 		memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
527 		ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head,
528 				pmull_ghash_update_p64);
529 
530 		err = skcipher_walk_done(&walk, 0);
531 	}
532 
533 	if (err)
534 		return err;
535 
536 	gcm_final(req, ctx, dg, tag, req->cryptlen);
537 
538 	/* copy authtag to end of dst */
539 	scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
540 				 crypto_aead_authsize(aead), 1);
541 
542 	return 0;
543 }
544 
545 static int gcm_decrypt(struct aead_request *req)
546 {
547 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
548 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
549 	unsigned int authsize = crypto_aead_authsize(aead);
550 	struct skcipher_walk walk;
551 	u8 iv[2 * AES_BLOCK_SIZE];
552 	u8 tag[AES_BLOCK_SIZE];
553 	u8 buf[2 * GHASH_BLOCK_SIZE];
554 	u64 dg[2] = {};
555 	int nrounds = num_rounds(&ctx->aes_key);
556 	int err;
557 
558 	if (req->assoclen)
559 		gcm_calculate_auth_mac(req, dg);
560 
561 	memcpy(iv, req->iv, GCM_IV_SIZE);
562 	put_unaligned_be32(1, iv + GCM_IV_SIZE);
563 
564 	err = skcipher_walk_aead_decrypt(&walk, req, false);
565 
566 	if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
567 		u32 const *rk = NULL;
568 
569 		kernel_neon_begin();
570 		pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
571 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
572 
573 		do {
574 			int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
575 			int rem = walk.total - blocks * AES_BLOCK_SIZE;
576 
577 			if (rk)
578 				kernel_neon_begin();
579 
580 			pmull_gcm_decrypt(blocks, dg, walk.dst.virt.addr,
581 					  walk.src.virt.addr, &ctx->ghash_key,
582 					  iv, rk, nrounds);
583 
584 			/* check if this is the final iteration of the loop */
585 			if (rem < (2 * AES_BLOCK_SIZE)) {
586 				u8 *iv2 = iv + AES_BLOCK_SIZE;
587 
588 				if (rem > AES_BLOCK_SIZE) {
589 					memcpy(iv2, iv, AES_BLOCK_SIZE);
590 					crypto_inc(iv2, AES_BLOCK_SIZE);
591 				}
592 
593 				pmull_gcm_encrypt_block(iv, iv, NULL, nrounds);
594 
595 				if (rem > AES_BLOCK_SIZE)
596 					pmull_gcm_encrypt_block(iv2, iv2, NULL,
597 								nrounds);
598 			}
599 
600 			kernel_neon_end();
601 
602 			err = skcipher_walk_done(&walk,
603 					walk.nbytes % (2 * AES_BLOCK_SIZE));
604 
605 			rk = ctx->aes_key.key_enc;
606 		} while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
607 	} else {
608 		__aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
609 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
610 
611 		while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
612 			int blocks = walk.nbytes / AES_BLOCK_SIZE;
613 			u8 *dst = walk.dst.virt.addr;
614 			u8 *src = walk.src.virt.addr;
615 
616 			ghash_do_update(blocks, dg, walk.src.virt.addr,
617 					&ctx->ghash_key, NULL,
618 					pmull_ghash_update_p64);
619 
620 			do {
621 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
622 						    buf, iv, nrounds);
623 				crypto_xor_cpy(dst, src, buf, AES_BLOCK_SIZE);
624 				crypto_inc(iv, AES_BLOCK_SIZE);
625 
626 				dst += AES_BLOCK_SIZE;
627 				src += AES_BLOCK_SIZE;
628 			} while (--blocks > 0);
629 
630 			err = skcipher_walk_done(&walk,
631 						 walk.nbytes % (2 * AES_BLOCK_SIZE));
632 		}
633 		if (walk.nbytes) {
634 			if (walk.nbytes > AES_BLOCK_SIZE) {
635 				u8 *iv2 = iv + AES_BLOCK_SIZE;
636 
637 				memcpy(iv2, iv, AES_BLOCK_SIZE);
638 				crypto_inc(iv2, AES_BLOCK_SIZE);
639 
640 				__aes_arm64_encrypt(ctx->aes_key.key_enc, iv2,
641 						    iv2, nrounds);
642 			}
643 			__aes_arm64_encrypt(ctx->aes_key.key_enc, iv, iv,
644 					    nrounds);
645 		}
646 	}
647 
648 	/* handle the tail */
649 	if (walk.nbytes) {
650 		const u8 *src = walk.src.virt.addr;
651 		const u8 *head = NULL;
652 		unsigned int nbytes = walk.nbytes;
653 
654 		if (walk.nbytes > GHASH_BLOCK_SIZE) {
655 			head = src;
656 			src += GHASH_BLOCK_SIZE;
657 			nbytes %= GHASH_BLOCK_SIZE;
658 		}
659 
660 		memcpy(buf, src, nbytes);
661 		memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
662 		ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head,
663 				pmull_ghash_update_p64);
664 
665 		crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, iv,
666 			       walk.nbytes);
667 
668 		err = skcipher_walk_done(&walk, 0);
669 	}
670 
671 	if (err)
672 		return err;
673 
674 	gcm_final(req, ctx, dg, tag, req->cryptlen - authsize);
675 
676 	/* compare calculated auth tag with the stored one */
677 	scatterwalk_map_and_copy(buf, req->src,
678 				 req->assoclen + req->cryptlen - authsize,
679 				 authsize, 0);
680 
681 	if (crypto_memneq(tag, buf, authsize))
682 		return -EBADMSG;
683 	return 0;
684 }
685 
686 static struct aead_alg gcm_aes_alg = {
687 	.ivsize			= GCM_IV_SIZE,
688 	.chunksize		= 2 * AES_BLOCK_SIZE,
689 	.maxauthsize		= AES_BLOCK_SIZE,
690 	.setkey			= gcm_setkey,
691 	.setauthsize		= gcm_setauthsize,
692 	.encrypt		= gcm_encrypt,
693 	.decrypt		= gcm_decrypt,
694 
695 	.base.cra_name		= "gcm(aes)",
696 	.base.cra_driver_name	= "gcm-aes-ce",
697 	.base.cra_priority	= 300,
698 	.base.cra_blocksize	= 1,
699 	.base.cra_ctxsize	= sizeof(struct gcm_aes_ctx),
700 	.base.cra_module	= THIS_MODULE,
701 };
702 
703 static int __init ghash_ce_mod_init(void)
704 {
705 	int ret;
706 
707 	if (!(elf_hwcap & HWCAP_ASIMD))
708 		return -ENODEV;
709 
710 	if (elf_hwcap & HWCAP_PMULL)
711 		ret = crypto_register_shashes(ghash_alg,
712 					      ARRAY_SIZE(ghash_alg));
713 	else
714 		/* only register the first array element */
715 		ret = crypto_register_shash(ghash_alg);
716 
717 	if (ret)
718 		return ret;
719 
720 	if (elf_hwcap & HWCAP_PMULL) {
721 		ret = crypto_register_aead(&gcm_aes_alg);
722 		if (ret)
723 			crypto_unregister_shashes(ghash_alg,
724 						  ARRAY_SIZE(ghash_alg));
725 	}
726 	return ret;
727 }
728 
729 static void __exit ghash_ce_mod_exit(void)
730 {
731 	if (elf_hwcap & HWCAP_PMULL)
732 		crypto_unregister_shashes(ghash_alg, ARRAY_SIZE(ghash_alg));
733 	else
734 		crypto_unregister_shash(ghash_alg);
735 	crypto_unregister_aead(&gcm_aes_alg);
736 }
737 
738 static const struct cpu_feature ghash_cpu_feature[] = {
739 	{ cpu_feature(PMULL) }, { }
740 };
741 MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
742 
743 module_init(ghash_ce_mod_init);
744 module_exit(ghash_ce_mod_exit);
745