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 static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src,
64 				  struct ghash_key const *k,
65 				  const char *head);
66 
67 asmlinkage void pmull_gcm_encrypt(int blocks, u64 dg[], u8 dst[],
68 				  const u8 src[], struct ghash_key const *k,
69 				  u8 ctr[], u32 const rk[], int rounds,
70 				  u8 ks[]);
71 
72 asmlinkage void pmull_gcm_decrypt(int blocks, u64 dg[], u8 dst[],
73 				  const u8 src[], struct ghash_key const *k,
74 				  u8 ctr[], u32 const rk[], int rounds);
75 
76 asmlinkage void pmull_gcm_encrypt_block(u8 dst[], u8 const src[],
77 					u32 const rk[], int rounds);
78 
79 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
80 
81 static int ghash_init(struct shash_desc *desc)
82 {
83 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
84 
85 	*ctx = (struct ghash_desc_ctx){};
86 	return 0;
87 }
88 
89 static void ghash_do_update(int blocks, u64 dg[], const char *src,
90 			    struct ghash_key *key, const char *head)
91 {
92 	if (likely(may_use_simd())) {
93 		kernel_neon_begin();
94 		pmull_ghash_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 {
125 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
126 	unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
127 
128 	ctx->count += len;
129 
130 	if ((partial + len) >= GHASH_BLOCK_SIZE) {
131 		struct ghash_key *key = crypto_shash_ctx(desc->tfm);
132 		int blocks;
133 
134 		if (partial) {
135 			int p = GHASH_BLOCK_SIZE - partial;
136 
137 			memcpy(ctx->buf + partial, src, p);
138 			src += p;
139 			len -= p;
140 		}
141 
142 		blocks = len / GHASH_BLOCK_SIZE;
143 		len %= GHASH_BLOCK_SIZE;
144 
145 		do {
146 			int chunk = min(blocks, MAX_BLOCKS);
147 
148 			ghash_do_update(chunk, ctx->digest, src, key,
149 					partial ? ctx->buf : NULL);
150 
151 			blocks -= chunk;
152 			src += chunk * GHASH_BLOCK_SIZE;
153 			partial = 0;
154 		} while (unlikely(blocks > 0));
155 	}
156 	if (len)
157 		memcpy(ctx->buf + partial, src, len);
158 	return 0;
159 }
160 
161 static int ghash_final(struct shash_desc *desc, u8 *dst)
162 {
163 	struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
164 	unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
165 
166 	if (partial) {
167 		struct ghash_key *key = crypto_shash_ctx(desc->tfm);
168 
169 		memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
170 
171 		ghash_do_update(1, ctx->digest, ctx->buf, key, NULL);
172 	}
173 	put_unaligned_be64(ctx->digest[1], dst);
174 	put_unaligned_be64(ctx->digest[0], dst + 8);
175 
176 	*ctx = (struct ghash_desc_ctx){};
177 	return 0;
178 }
179 
180 static void ghash_reflect(u64 h[], const be128 *k)
181 {
182 	u64 carry = be64_to_cpu(k->a) & BIT(63) ? 1 : 0;
183 
184 	h[0] = (be64_to_cpu(k->b) << 1) | carry;
185 	h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
186 
187 	if (carry)
188 		h[1] ^= 0xc200000000000000UL;
189 }
190 
191 static int __ghash_setkey(struct ghash_key *key,
192 			  const u8 *inkey, unsigned int keylen)
193 {
194 	be128 h;
195 
196 	/* needed for the fallback */
197 	memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
198 
199 	ghash_reflect(key->h, &key->k);
200 
201 	h = key->k;
202 	gf128mul_lle(&h, &key->k);
203 	ghash_reflect(key->h2, &h);
204 
205 	gf128mul_lle(&h, &key->k);
206 	ghash_reflect(key->h3, &h);
207 
208 	gf128mul_lle(&h, &key->k);
209 	ghash_reflect(key->h4, &h);
210 
211 	return 0;
212 }
213 
214 static int ghash_setkey(struct crypto_shash *tfm,
215 			const u8 *inkey, unsigned int keylen)
216 {
217 	struct ghash_key *key = crypto_shash_ctx(tfm);
218 
219 	if (keylen != GHASH_BLOCK_SIZE) {
220 		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
221 		return -EINVAL;
222 	}
223 
224 	return __ghash_setkey(key, inkey, keylen);
225 }
226 
227 static struct shash_alg ghash_alg = {
228 	.base.cra_name		= "ghash",
229 	.base.cra_driver_name	= "ghash-ce",
230 	.base.cra_priority	= 200,
231 	.base.cra_blocksize	= GHASH_BLOCK_SIZE,
232 	.base.cra_ctxsize	= sizeof(struct ghash_key),
233 	.base.cra_module	= THIS_MODULE,
234 
235 	.digestsize		= GHASH_DIGEST_SIZE,
236 	.init			= ghash_init,
237 	.update			= ghash_update,
238 	.final			= ghash_final,
239 	.setkey			= ghash_setkey,
240 	.descsize		= sizeof(struct ghash_desc_ctx),
241 };
242 
243 static int num_rounds(struct crypto_aes_ctx *ctx)
244 {
245 	/*
246 	 * # of rounds specified by AES:
247 	 * 128 bit key		10 rounds
248 	 * 192 bit key		12 rounds
249 	 * 256 bit key		14 rounds
250 	 * => n byte key	=> 6 + (n/4) rounds
251 	 */
252 	return 6 + ctx->key_length / 4;
253 }
254 
255 static int gcm_setkey(struct crypto_aead *tfm, const u8 *inkey,
256 		      unsigned int keylen)
257 {
258 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(tfm);
259 	u8 key[GHASH_BLOCK_SIZE];
260 	int ret;
261 
262 	ret = crypto_aes_expand_key(&ctx->aes_key, inkey, keylen);
263 	if (ret) {
264 		tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
265 		return -EINVAL;
266 	}
267 
268 	__aes_arm64_encrypt(ctx->aes_key.key_enc, key, (u8[AES_BLOCK_SIZE]){},
269 			    num_rounds(&ctx->aes_key));
270 
271 	return __ghash_setkey(&ctx->ghash_key, key, sizeof(be128));
272 }
273 
274 static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
275 {
276 	switch (authsize) {
277 	case 4:
278 	case 8:
279 	case 12 ... 16:
280 		break;
281 	default:
282 		return -EINVAL;
283 	}
284 	return 0;
285 }
286 
287 static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
288 			   int *buf_count, struct gcm_aes_ctx *ctx)
289 {
290 	if (*buf_count > 0) {
291 		int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
292 
293 		memcpy(&buf[*buf_count], src, buf_added);
294 
295 		*buf_count += buf_added;
296 		src += buf_added;
297 		count -= buf_added;
298 	}
299 
300 	if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
301 		int blocks = count / GHASH_BLOCK_SIZE;
302 
303 		ghash_do_update(blocks, dg, src, &ctx->ghash_key,
304 				*buf_count ? buf : NULL);
305 
306 		src += blocks * GHASH_BLOCK_SIZE;
307 		count %= GHASH_BLOCK_SIZE;
308 		*buf_count = 0;
309 	}
310 
311 	if (count > 0) {
312 		memcpy(buf, src, count);
313 		*buf_count = count;
314 	}
315 }
316 
317 static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
318 {
319 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
320 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
321 	u8 buf[GHASH_BLOCK_SIZE];
322 	struct scatter_walk walk;
323 	u32 len = req->assoclen;
324 	int buf_count = 0;
325 
326 	scatterwalk_start(&walk, req->src);
327 
328 	do {
329 		u32 n = scatterwalk_clamp(&walk, len);
330 		u8 *p;
331 
332 		if (!n) {
333 			scatterwalk_start(&walk, sg_next(walk.sg));
334 			n = scatterwalk_clamp(&walk, len);
335 		}
336 		p = scatterwalk_map(&walk);
337 
338 		gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
339 		len -= n;
340 
341 		scatterwalk_unmap(p);
342 		scatterwalk_advance(&walk, n);
343 		scatterwalk_done(&walk, 0, len);
344 	} while (len);
345 
346 	if (buf_count) {
347 		memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
348 		ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL);
349 	}
350 }
351 
352 static void gcm_final(struct aead_request *req, struct gcm_aes_ctx *ctx,
353 		      u64 dg[], u8 tag[], int cryptlen)
354 {
355 	u8 mac[AES_BLOCK_SIZE];
356 	u128 lengths;
357 
358 	lengths.a = cpu_to_be64(req->assoclen * 8);
359 	lengths.b = cpu_to_be64(cryptlen * 8);
360 
361 	ghash_do_update(1, dg, (void *)&lengths, &ctx->ghash_key, NULL);
362 
363 	put_unaligned_be64(dg[1], mac);
364 	put_unaligned_be64(dg[0], mac + 8);
365 
366 	crypto_xor(tag, mac, AES_BLOCK_SIZE);
367 }
368 
369 static int gcm_encrypt(struct aead_request *req)
370 {
371 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
372 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
373 	struct skcipher_walk walk;
374 	u8 iv[AES_BLOCK_SIZE];
375 	u8 ks[2 * AES_BLOCK_SIZE];
376 	u8 tag[AES_BLOCK_SIZE];
377 	u64 dg[2] = {};
378 	int nrounds = num_rounds(&ctx->aes_key);
379 	int err;
380 
381 	if (req->assoclen)
382 		gcm_calculate_auth_mac(req, dg);
383 
384 	memcpy(iv, req->iv, GCM_IV_SIZE);
385 	put_unaligned_be32(1, iv + GCM_IV_SIZE);
386 
387 	err = skcipher_walk_aead_encrypt(&walk, req, false);
388 
389 	if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
390 		u32 const *rk = NULL;
391 
392 		kernel_neon_begin();
393 		pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
394 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
395 		pmull_gcm_encrypt_block(ks, iv, NULL, nrounds);
396 		put_unaligned_be32(3, iv + GCM_IV_SIZE);
397 		pmull_gcm_encrypt_block(ks + AES_BLOCK_SIZE, iv, NULL, nrounds);
398 		put_unaligned_be32(4, iv + GCM_IV_SIZE);
399 
400 		do {
401 			int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
402 
403 			if (rk)
404 				kernel_neon_begin();
405 
406 			pmull_gcm_encrypt(blocks, dg, walk.dst.virt.addr,
407 					  walk.src.virt.addr, &ctx->ghash_key,
408 					  iv, rk, nrounds, ks);
409 			kernel_neon_end();
410 
411 			err = skcipher_walk_done(&walk,
412 					walk.nbytes % (2 * AES_BLOCK_SIZE));
413 
414 			rk = ctx->aes_key.key_enc;
415 		} while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
416 	} else {
417 		__aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
418 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
419 
420 		while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
421 			int blocks = walk.nbytes / AES_BLOCK_SIZE;
422 			u8 *dst = walk.dst.virt.addr;
423 			u8 *src = walk.src.virt.addr;
424 
425 			do {
426 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
427 						    ks, iv, nrounds);
428 				crypto_xor_cpy(dst, src, ks, AES_BLOCK_SIZE);
429 				crypto_inc(iv, AES_BLOCK_SIZE);
430 
431 				dst += AES_BLOCK_SIZE;
432 				src += AES_BLOCK_SIZE;
433 			} while (--blocks > 0);
434 
435 			ghash_do_update(walk.nbytes / AES_BLOCK_SIZE, dg,
436 					walk.dst.virt.addr, &ctx->ghash_key,
437 					NULL);
438 
439 			err = skcipher_walk_done(&walk,
440 						 walk.nbytes % (2 * AES_BLOCK_SIZE));
441 		}
442 		if (walk.nbytes) {
443 			__aes_arm64_encrypt(ctx->aes_key.key_enc, ks, iv,
444 					    nrounds);
445 			if (walk.nbytes > AES_BLOCK_SIZE) {
446 				crypto_inc(iv, AES_BLOCK_SIZE);
447 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
448 					            ks + AES_BLOCK_SIZE, iv,
449 						    nrounds);
450 			}
451 		}
452 	}
453 
454 	/* handle the tail */
455 	if (walk.nbytes) {
456 		u8 buf[GHASH_BLOCK_SIZE];
457 		unsigned int nbytes = walk.nbytes;
458 		u8 *dst = walk.dst.virt.addr;
459 		u8 *head = NULL;
460 
461 		crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, ks,
462 			       walk.nbytes);
463 
464 		if (walk.nbytes > GHASH_BLOCK_SIZE) {
465 			head = dst;
466 			dst += GHASH_BLOCK_SIZE;
467 			nbytes %= GHASH_BLOCK_SIZE;
468 		}
469 
470 		memcpy(buf, dst, nbytes);
471 		memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
472 		ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
473 
474 		err = skcipher_walk_done(&walk, 0);
475 	}
476 
477 	if (err)
478 		return err;
479 
480 	gcm_final(req, ctx, dg, tag, req->cryptlen);
481 
482 	/* copy authtag to end of dst */
483 	scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
484 				 crypto_aead_authsize(aead), 1);
485 
486 	return 0;
487 }
488 
489 static int gcm_decrypt(struct aead_request *req)
490 {
491 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
492 	struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
493 	unsigned int authsize = crypto_aead_authsize(aead);
494 	struct skcipher_walk walk;
495 	u8 iv[2 * AES_BLOCK_SIZE];
496 	u8 tag[AES_BLOCK_SIZE];
497 	u8 buf[2 * GHASH_BLOCK_SIZE];
498 	u64 dg[2] = {};
499 	int nrounds = num_rounds(&ctx->aes_key);
500 	int err;
501 
502 	if (req->assoclen)
503 		gcm_calculate_auth_mac(req, dg);
504 
505 	memcpy(iv, req->iv, GCM_IV_SIZE);
506 	put_unaligned_be32(1, iv + GCM_IV_SIZE);
507 
508 	err = skcipher_walk_aead_decrypt(&walk, req, false);
509 
510 	if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
511 		u32 const *rk = NULL;
512 
513 		kernel_neon_begin();
514 		pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
515 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
516 
517 		do {
518 			int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
519 			int rem = walk.total - blocks * AES_BLOCK_SIZE;
520 
521 			if (rk)
522 				kernel_neon_begin();
523 
524 			pmull_gcm_decrypt(blocks, dg, walk.dst.virt.addr,
525 					  walk.src.virt.addr, &ctx->ghash_key,
526 					  iv, rk, nrounds);
527 
528 			/* check if this is the final iteration of the loop */
529 			if (rem < (2 * AES_BLOCK_SIZE)) {
530 				u8 *iv2 = iv + AES_BLOCK_SIZE;
531 
532 				if (rem > AES_BLOCK_SIZE) {
533 					memcpy(iv2, iv, AES_BLOCK_SIZE);
534 					crypto_inc(iv2, AES_BLOCK_SIZE);
535 				}
536 
537 				pmull_gcm_encrypt_block(iv, iv, NULL, nrounds);
538 
539 				if (rem > AES_BLOCK_SIZE)
540 					pmull_gcm_encrypt_block(iv2, iv2, NULL,
541 								nrounds);
542 			}
543 
544 			kernel_neon_end();
545 
546 			err = skcipher_walk_done(&walk,
547 					walk.nbytes % (2 * AES_BLOCK_SIZE));
548 
549 			rk = ctx->aes_key.key_enc;
550 		} while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
551 	} else {
552 		__aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
553 		put_unaligned_be32(2, iv + GCM_IV_SIZE);
554 
555 		while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
556 			int blocks = walk.nbytes / AES_BLOCK_SIZE;
557 			u8 *dst = walk.dst.virt.addr;
558 			u8 *src = walk.src.virt.addr;
559 
560 			ghash_do_update(blocks, dg, walk.src.virt.addr,
561 					&ctx->ghash_key, NULL);
562 
563 			do {
564 				__aes_arm64_encrypt(ctx->aes_key.key_enc,
565 						    buf, iv, nrounds);
566 				crypto_xor_cpy(dst, src, buf, AES_BLOCK_SIZE);
567 				crypto_inc(iv, AES_BLOCK_SIZE);
568 
569 				dst += AES_BLOCK_SIZE;
570 				src += AES_BLOCK_SIZE;
571 			} while (--blocks > 0);
572 
573 			err = skcipher_walk_done(&walk,
574 						 walk.nbytes % (2 * AES_BLOCK_SIZE));
575 		}
576 		if (walk.nbytes) {
577 			if (walk.nbytes > AES_BLOCK_SIZE) {
578 				u8 *iv2 = iv + AES_BLOCK_SIZE;
579 
580 				memcpy(iv2, iv, AES_BLOCK_SIZE);
581 				crypto_inc(iv2, AES_BLOCK_SIZE);
582 
583 				__aes_arm64_encrypt(ctx->aes_key.key_enc, iv2,
584 						    iv2, nrounds);
585 			}
586 			__aes_arm64_encrypt(ctx->aes_key.key_enc, iv, iv,
587 					    nrounds);
588 		}
589 	}
590 
591 	/* handle the tail */
592 	if (walk.nbytes) {
593 		const u8 *src = walk.src.virt.addr;
594 		const u8 *head = NULL;
595 		unsigned int nbytes = walk.nbytes;
596 
597 		if (walk.nbytes > GHASH_BLOCK_SIZE) {
598 			head = src;
599 			src += GHASH_BLOCK_SIZE;
600 			nbytes %= GHASH_BLOCK_SIZE;
601 		}
602 
603 		memcpy(buf, src, nbytes);
604 		memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
605 		ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
606 
607 		crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, iv,
608 			       walk.nbytes);
609 
610 		err = skcipher_walk_done(&walk, 0);
611 	}
612 
613 	if (err)
614 		return err;
615 
616 	gcm_final(req, ctx, dg, tag, req->cryptlen - authsize);
617 
618 	/* compare calculated auth tag with the stored one */
619 	scatterwalk_map_and_copy(buf, req->src,
620 				 req->assoclen + req->cryptlen - authsize,
621 				 authsize, 0);
622 
623 	if (crypto_memneq(tag, buf, authsize))
624 		return -EBADMSG;
625 	return 0;
626 }
627 
628 static struct aead_alg gcm_aes_alg = {
629 	.ivsize			= GCM_IV_SIZE,
630 	.chunksize		= 2 * AES_BLOCK_SIZE,
631 	.maxauthsize		= AES_BLOCK_SIZE,
632 	.setkey			= gcm_setkey,
633 	.setauthsize		= gcm_setauthsize,
634 	.encrypt		= gcm_encrypt,
635 	.decrypt		= gcm_decrypt,
636 
637 	.base.cra_name		= "gcm(aes)",
638 	.base.cra_driver_name	= "gcm-aes-ce",
639 	.base.cra_priority	= 300,
640 	.base.cra_blocksize	= 1,
641 	.base.cra_ctxsize	= sizeof(struct gcm_aes_ctx),
642 	.base.cra_module	= THIS_MODULE,
643 };
644 
645 static int __init ghash_ce_mod_init(void)
646 {
647 	int ret;
648 
649 	if (!(elf_hwcap & HWCAP_ASIMD))
650 		return -ENODEV;
651 
652 	if (elf_hwcap & HWCAP_PMULL)
653 		pmull_ghash_update = pmull_ghash_update_p64;
654 
655 	else
656 		pmull_ghash_update = pmull_ghash_update_p8;
657 
658 	ret = crypto_register_shash(&ghash_alg);
659 	if (ret)
660 		return ret;
661 
662 	if (elf_hwcap & HWCAP_PMULL) {
663 		ret = crypto_register_aead(&gcm_aes_alg);
664 		if (ret)
665 			crypto_unregister_shash(&ghash_alg);
666 	}
667 	return ret;
668 }
669 
670 static void __exit ghash_ce_mod_exit(void)
671 {
672 	crypto_unregister_shash(&ghash_alg);
673 	crypto_unregister_aead(&gcm_aes_alg);
674 }
675 
676 static const struct cpu_feature ghash_cpu_feature[] = {
677 	{ cpu_feature(PMULL) }, { }
678 };
679 MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
680 
681 module_init(ghash_ce_mod_init);
682 module_exit(ghash_ce_mod_exit);
683