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