GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / arm64 / crypto / ghash-ce-glue.c
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                         const int blocks =
422                                 walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
423                         u8 *dst = walk.dst.virt.addr;
424                         u8 *src = walk.src.virt.addr;
425                         int remaining = blocks;
426
427                         do {
428                                 __aes_arm64_encrypt(ctx->aes_key.key_enc,
429                                                     ks, iv, nrounds);
430                                 crypto_xor_cpy(dst, src, ks, AES_BLOCK_SIZE);
431                                 crypto_inc(iv, AES_BLOCK_SIZE);
432
433                                 dst += AES_BLOCK_SIZE;
434                                 src += AES_BLOCK_SIZE;
435                         } while (--remaining > 0);
436
437                         ghash_do_update(blocks, dg,
438                                         walk.dst.virt.addr, &ctx->ghash_key,
439                                         NULL);
440
441                         err = skcipher_walk_done(&walk,
442                                                  walk.nbytes % (2 * AES_BLOCK_SIZE));
443                 }
444                 if (walk.nbytes) {
445                         __aes_arm64_encrypt(ctx->aes_key.key_enc, ks, iv,
446                                             nrounds);
447                         if (walk.nbytes > AES_BLOCK_SIZE) {
448                                 crypto_inc(iv, AES_BLOCK_SIZE);
449                                 __aes_arm64_encrypt(ctx->aes_key.key_enc,
450                                                     ks + AES_BLOCK_SIZE, iv,
451                                                     nrounds);
452                         }
453                 }
454         }
455
456         /* handle the tail */
457         if (walk.nbytes) {
458                 u8 buf[GHASH_BLOCK_SIZE];
459                 unsigned int nbytes = walk.nbytes;
460                 u8 *dst = walk.dst.virt.addr;
461                 u8 *head = NULL;
462
463                 crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, ks,
464                                walk.nbytes);
465
466                 if (walk.nbytes > GHASH_BLOCK_SIZE) {
467                         head = dst;
468                         dst += GHASH_BLOCK_SIZE;
469                         nbytes %= GHASH_BLOCK_SIZE;
470                 }
471
472                 memcpy(buf, dst, nbytes);
473                 memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
474                 ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
475
476                 err = skcipher_walk_done(&walk, 0);
477         }
478
479         if (err)
480                 return err;
481
482         gcm_final(req, ctx, dg, tag, req->cryptlen);
483
484         /* copy authtag to end of dst */
485         scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
486                                  crypto_aead_authsize(aead), 1);
487
488         return 0;
489 }
490
491 static int gcm_decrypt(struct aead_request *req)
492 {
493         struct crypto_aead *aead = crypto_aead_reqtfm(req);
494         struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
495         unsigned int authsize = crypto_aead_authsize(aead);
496         struct skcipher_walk walk;
497         u8 iv[2 * AES_BLOCK_SIZE];
498         u8 tag[AES_BLOCK_SIZE];
499         u8 buf[2 * GHASH_BLOCK_SIZE];
500         u64 dg[2] = {};
501         int nrounds = num_rounds(&ctx->aes_key);
502         int err;
503
504         if (req->assoclen)
505                 gcm_calculate_auth_mac(req, dg);
506
507         memcpy(iv, req->iv, GCM_IV_SIZE);
508         put_unaligned_be32(1, iv + GCM_IV_SIZE);
509
510         err = skcipher_walk_aead_decrypt(&walk, req, false);
511
512         if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
513                 u32 const *rk = NULL;
514
515                 kernel_neon_begin();
516                 pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
517                 put_unaligned_be32(2, iv + GCM_IV_SIZE);
518
519                 do {
520                         int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
521                         int rem = walk.total - blocks * AES_BLOCK_SIZE;
522
523                         if (rk)
524                                 kernel_neon_begin();
525
526                         pmull_gcm_decrypt(blocks, dg, walk.dst.virt.addr,
527                                           walk.src.virt.addr, &ctx->ghash_key,
528                                           iv, rk, nrounds);
529
530                         /* check if this is the final iteration of the loop */
531                         if (rem < (2 * AES_BLOCK_SIZE)) {
532                                 u8 *iv2 = iv + AES_BLOCK_SIZE;
533
534                                 if (rem > AES_BLOCK_SIZE) {
535                                         memcpy(iv2, iv, AES_BLOCK_SIZE);
536                                         crypto_inc(iv2, AES_BLOCK_SIZE);
537                                 }
538
539                                 pmull_gcm_encrypt_block(iv, iv, NULL, nrounds);
540
541                                 if (rem > AES_BLOCK_SIZE)
542                                         pmull_gcm_encrypt_block(iv2, iv2, NULL,
543                                                                 nrounds);
544                         }
545
546                         kernel_neon_end();
547
548                         err = skcipher_walk_done(&walk,
549                                         walk.nbytes % (2 * AES_BLOCK_SIZE));
550
551                         rk = ctx->aes_key.key_enc;
552                 } while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
553         } else {
554                 __aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
555                 put_unaligned_be32(2, iv + GCM_IV_SIZE);
556
557                 while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
558                         int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
559                         u8 *dst = walk.dst.virt.addr;
560                         u8 *src = walk.src.virt.addr;
561
562                         ghash_do_update(blocks, dg, walk.src.virt.addr,
563                                         &ctx->ghash_key, NULL);
564
565                         do {
566                                 __aes_arm64_encrypt(ctx->aes_key.key_enc,
567                                                     buf, iv, nrounds);
568                                 crypto_xor_cpy(dst, src, buf, AES_BLOCK_SIZE);
569                                 crypto_inc(iv, AES_BLOCK_SIZE);
570
571                                 dst += AES_BLOCK_SIZE;
572                                 src += AES_BLOCK_SIZE;
573                         } while (--blocks > 0);
574
575                         err = skcipher_walk_done(&walk,
576                                                  walk.nbytes % (2 * AES_BLOCK_SIZE));
577                 }
578                 if (walk.nbytes) {
579                         if (walk.nbytes > AES_BLOCK_SIZE) {
580                                 u8 *iv2 = iv + AES_BLOCK_SIZE;
581
582                                 memcpy(iv2, iv, AES_BLOCK_SIZE);
583                                 crypto_inc(iv2, AES_BLOCK_SIZE);
584
585                                 __aes_arm64_encrypt(ctx->aes_key.key_enc, iv2,
586                                                     iv2, nrounds);
587                         }
588                         __aes_arm64_encrypt(ctx->aes_key.key_enc, iv, iv,
589                                             nrounds);
590                 }
591         }
592
593         /* handle the tail */
594         if (walk.nbytes) {
595                 const u8 *src = walk.src.virt.addr;
596                 const u8 *head = NULL;
597                 unsigned int nbytes = walk.nbytes;
598
599                 if (walk.nbytes > GHASH_BLOCK_SIZE) {
600                         head = src;
601                         src += GHASH_BLOCK_SIZE;
602                         nbytes %= GHASH_BLOCK_SIZE;
603                 }
604
605                 memcpy(buf, src, nbytes);
606                 memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
607                 ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
608
609                 crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, iv,
610                                walk.nbytes);
611
612                 err = skcipher_walk_done(&walk, 0);
613         }
614
615         if (err)
616                 return err;
617
618         gcm_final(req, ctx, dg, tag, req->cryptlen - authsize);
619
620         /* compare calculated auth tag with the stored one */
621         scatterwalk_map_and_copy(buf, req->src,
622                                  req->assoclen + req->cryptlen - authsize,
623                                  authsize, 0);
624
625         if (crypto_memneq(tag, buf, authsize))
626                 return -EBADMSG;
627         return 0;
628 }
629
630 static struct aead_alg gcm_aes_alg = {
631         .ivsize                 = GCM_IV_SIZE,
632         .chunksize              = 2 * AES_BLOCK_SIZE,
633         .maxauthsize            = AES_BLOCK_SIZE,
634         .setkey                 = gcm_setkey,
635         .setauthsize            = gcm_setauthsize,
636         .encrypt                = gcm_encrypt,
637         .decrypt                = gcm_decrypt,
638
639         .base.cra_name          = "gcm(aes)",
640         .base.cra_driver_name   = "gcm-aes-ce",
641         .base.cra_priority      = 300,
642         .base.cra_blocksize     = 1,
643         .base.cra_ctxsize       = sizeof(struct gcm_aes_ctx),
644         .base.cra_module        = THIS_MODULE,
645 };
646
647 static int __init ghash_ce_mod_init(void)
648 {
649         int ret;
650
651         if (!(elf_hwcap & HWCAP_ASIMD))
652                 return -ENODEV;
653
654         if (elf_hwcap & HWCAP_PMULL)
655                 pmull_ghash_update = pmull_ghash_update_p64;
656
657         else
658                 pmull_ghash_update = pmull_ghash_update_p8;
659
660         ret = crypto_register_shash(&ghash_alg);
661         if (ret)
662                 return ret;
663
664         if (elf_hwcap & HWCAP_PMULL) {
665                 ret = crypto_register_aead(&gcm_aes_alg);
666                 if (ret)
667                         crypto_unregister_shash(&ghash_alg);
668         }
669         return ret;
670 }
671
672 static void __exit ghash_ce_mod_exit(void)
673 {
674         crypto_unregister_shash(&ghash_alg);
675         crypto_unregister_aead(&gcm_aes_alg);
676 }
677
678 static const struct cpu_feature ghash_cpu_feature[] = {
679         { cpu_feature(PMULL) }, { }
680 };
681 MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
682
683 module_init(ghash_ce_mod_init);
684 module_exit(ghash_ce_mod_exit);