crypto: gcm - Use GCM IV size constant
[sfrench/cifs-2.6.git] / crypto / gcm.c
1 /*
2  * GCM: Galois/Counter Mode.
3  *
4  * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
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 <crypto/gf128mul.h>
12 #include <crypto/internal/aead.h>
13 #include <crypto/internal/skcipher.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/null.h>
16 #include <crypto/scatterwalk.h>
17 #include <crypto/gcm.h>
18 #include <crypto/hash.h>
19 #include "internal.h"
20 #include <linux/completion.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26
27 struct gcm_instance_ctx {
28         struct crypto_skcipher_spawn ctr;
29         struct crypto_ahash_spawn ghash;
30 };
31
32 struct crypto_gcm_ctx {
33         struct crypto_skcipher *ctr;
34         struct crypto_ahash *ghash;
35 };
36
37 struct crypto_rfc4106_ctx {
38         struct crypto_aead *child;
39         u8 nonce[4];
40 };
41
42 struct crypto_rfc4106_req_ctx {
43         struct scatterlist src[3];
44         struct scatterlist dst[3];
45         struct aead_request subreq;
46 };
47
48 struct crypto_rfc4543_instance_ctx {
49         struct crypto_aead_spawn aead;
50 };
51
52 struct crypto_rfc4543_ctx {
53         struct crypto_aead *child;
54         struct crypto_skcipher *null;
55         u8 nonce[4];
56 };
57
58 struct crypto_rfc4543_req_ctx {
59         struct aead_request subreq;
60 };
61
62 struct crypto_gcm_ghash_ctx {
63         unsigned int cryptlen;
64         struct scatterlist *src;
65         int (*complete)(struct aead_request *req, u32 flags);
66 };
67
68 struct crypto_gcm_req_priv_ctx {
69         u8 iv[16];
70         u8 auth_tag[16];
71         u8 iauth_tag[16];
72         struct scatterlist src[3];
73         struct scatterlist dst[3];
74         struct scatterlist sg;
75         struct crypto_gcm_ghash_ctx ghash_ctx;
76         union {
77                 struct ahash_request ahreq;
78                 struct skcipher_request skreq;
79         } u;
80 };
81
82 struct crypto_gcm_setkey_result {
83         int err;
84         struct completion completion;
85 };
86
87 static struct {
88         u8 buf[16];
89         struct scatterlist sg;
90 } *gcm_zeroes;
91
92 static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
93
94 static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
95         struct aead_request *req)
96 {
97         unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
98
99         return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
100 }
101
102 static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
103 {
104         struct crypto_gcm_setkey_result *result = req->data;
105
106         if (err == -EINPROGRESS)
107                 return;
108
109         result->err = err;
110         complete(&result->completion);
111 }
112
113 static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
114                              unsigned int keylen)
115 {
116         struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
117         struct crypto_ahash *ghash = ctx->ghash;
118         struct crypto_skcipher *ctr = ctx->ctr;
119         struct {
120                 be128 hash;
121                 u8 iv[16];
122
123                 struct crypto_gcm_setkey_result result;
124
125                 struct scatterlist sg[1];
126                 struct skcipher_request req;
127         } *data;
128         int err;
129
130         crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
131         crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
132                                        CRYPTO_TFM_REQ_MASK);
133         err = crypto_skcipher_setkey(ctr, key, keylen);
134         crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
135                                     CRYPTO_TFM_RES_MASK);
136         if (err)
137                 return err;
138
139         data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
140                        GFP_KERNEL);
141         if (!data)
142                 return -ENOMEM;
143
144         init_completion(&data->result.completion);
145         sg_init_one(data->sg, &data->hash, sizeof(data->hash));
146         skcipher_request_set_tfm(&data->req, ctr);
147         skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
148                                                   CRYPTO_TFM_REQ_MAY_BACKLOG,
149                                       crypto_gcm_setkey_done,
150                                       &data->result);
151         skcipher_request_set_crypt(&data->req, data->sg, data->sg,
152                                    sizeof(data->hash), data->iv);
153
154         err = crypto_skcipher_encrypt(&data->req);
155         if (err == -EINPROGRESS || err == -EBUSY) {
156                 wait_for_completion(&data->result.completion);
157                 err = data->result.err;
158         }
159
160         if (err)
161                 goto out;
162
163         crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
164         crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
165                                CRYPTO_TFM_REQ_MASK);
166         err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
167         crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
168                               CRYPTO_TFM_RES_MASK);
169
170 out:
171         kzfree(data);
172         return err;
173 }
174
175 static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
176                                   unsigned int authsize)
177 {
178         switch (authsize) {
179         case 4:
180         case 8:
181         case 12:
182         case 13:
183         case 14:
184         case 15:
185         case 16:
186                 break;
187         default:
188                 return -EINVAL;
189         }
190
191         return 0;
192 }
193
194 static void crypto_gcm_init_common(struct aead_request *req)
195 {
196         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
197         __be32 counter = cpu_to_be32(1);
198         struct scatterlist *sg;
199
200         memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
201         memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
202         memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
203
204         sg_init_table(pctx->src, 3);
205         sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
206         sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
207         if (sg != pctx->src + 1)
208                 sg_chain(pctx->src, 2, sg);
209
210         if (req->src != req->dst) {
211                 sg_init_table(pctx->dst, 3);
212                 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
213                 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
214                 if (sg != pctx->dst + 1)
215                         sg_chain(pctx->dst, 2, sg);
216         }
217 }
218
219 static void crypto_gcm_init_crypt(struct aead_request *req,
220                                   unsigned int cryptlen)
221 {
222         struct crypto_aead *aead = crypto_aead_reqtfm(req);
223         struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
224         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
225         struct skcipher_request *skreq = &pctx->u.skreq;
226         struct scatterlist *dst;
227
228         dst = req->src == req->dst ? pctx->src : pctx->dst;
229
230         skcipher_request_set_tfm(skreq, ctx->ctr);
231         skcipher_request_set_crypt(skreq, pctx->src, dst,
232                                      cryptlen + sizeof(pctx->auth_tag),
233                                      pctx->iv);
234 }
235
236 static inline unsigned int gcm_remain(unsigned int len)
237 {
238         len &= 0xfU;
239         return len ? 16 - len : 0;
240 }
241
242 static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
243
244 static int gcm_hash_update(struct aead_request *req,
245                            crypto_completion_t compl,
246                            struct scatterlist *src,
247                            unsigned int len, u32 flags)
248 {
249         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
250         struct ahash_request *ahreq = &pctx->u.ahreq;
251
252         ahash_request_set_callback(ahreq, flags, compl, req);
253         ahash_request_set_crypt(ahreq, src, NULL, len);
254
255         return crypto_ahash_update(ahreq);
256 }
257
258 static int gcm_hash_remain(struct aead_request *req,
259                            unsigned int remain,
260                            crypto_completion_t compl, u32 flags)
261 {
262         return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
263 }
264
265 static int gcm_hash_len(struct aead_request *req, u32 flags)
266 {
267         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
268         struct ahash_request *ahreq = &pctx->u.ahreq;
269         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
270         u128 lengths;
271
272         lengths.a = cpu_to_be64(req->assoclen * 8);
273         lengths.b = cpu_to_be64(gctx->cryptlen * 8);
274         memcpy(pctx->iauth_tag, &lengths, 16);
275         sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
276         ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
277         ahash_request_set_crypt(ahreq, &pctx->sg,
278                                 pctx->iauth_tag, sizeof(lengths));
279
280         return crypto_ahash_finup(ahreq);
281 }
282
283 static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
284 {
285         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
286         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
287
288         return gctx->complete(req, flags);
289 }
290
291 static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
292 {
293         struct aead_request *req = areq->data;
294
295         if (err)
296                 goto out;
297
298         err = gcm_hash_len_continue(req, 0);
299         if (err == -EINPROGRESS)
300                 return;
301
302 out:
303         aead_request_complete(req, err);
304 }
305
306 static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
307 {
308         return gcm_hash_len(req, flags) ?:
309                gcm_hash_len_continue(req, flags);
310 }
311
312 static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
313                                        int err)
314 {
315         struct aead_request *req = areq->data;
316
317         if (err)
318                 goto out;
319
320         err = gcm_hash_crypt_remain_continue(req, 0);
321         if (err == -EINPROGRESS)
322                 return;
323
324 out:
325         aead_request_complete(req, err);
326 }
327
328 static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
329 {
330         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
331         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
332         unsigned int remain;
333
334         remain = gcm_remain(gctx->cryptlen);
335         if (remain)
336                 return gcm_hash_remain(req, remain,
337                                        gcm_hash_crypt_remain_done, flags) ?:
338                        gcm_hash_crypt_remain_continue(req, flags);
339
340         return gcm_hash_crypt_remain_continue(req, flags);
341 }
342
343 static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
344 {
345         struct aead_request *req = areq->data;
346
347         if (err)
348                 goto out;
349
350         err = gcm_hash_crypt_continue(req, 0);
351         if (err == -EINPROGRESS)
352                 return;
353
354 out:
355         aead_request_complete(req, err);
356 }
357
358 static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
359 {
360         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
361         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
362
363         if (gctx->cryptlen)
364                 return gcm_hash_update(req, gcm_hash_crypt_done,
365                                        gctx->src, gctx->cryptlen, flags) ?:
366                        gcm_hash_crypt_continue(req, flags);
367
368         return gcm_hash_crypt_remain_continue(req, flags);
369 }
370
371 static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
372                                        int err)
373 {
374         struct aead_request *req = areq->data;
375
376         if (err)
377                 goto out;
378
379         err = gcm_hash_assoc_remain_continue(req, 0);
380         if (err == -EINPROGRESS)
381                 return;
382
383 out:
384         aead_request_complete(req, err);
385 }
386
387 static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
388 {
389         unsigned int remain;
390
391         remain = gcm_remain(req->assoclen);
392         if (remain)
393                 return gcm_hash_remain(req, remain,
394                                        gcm_hash_assoc_remain_done, flags) ?:
395                        gcm_hash_assoc_remain_continue(req, flags);
396
397         return gcm_hash_assoc_remain_continue(req, flags);
398 }
399
400 static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
401 {
402         struct aead_request *req = areq->data;
403
404         if (err)
405                 goto out;
406
407         err = gcm_hash_assoc_continue(req, 0);
408         if (err == -EINPROGRESS)
409                 return;
410
411 out:
412         aead_request_complete(req, err);
413 }
414
415 static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
416 {
417         if (req->assoclen)
418                 return gcm_hash_update(req, gcm_hash_assoc_done,
419                                        req->src, req->assoclen, flags) ?:
420                        gcm_hash_assoc_continue(req, flags);
421
422         return gcm_hash_assoc_remain_continue(req, flags);
423 }
424
425 static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
426 {
427         struct aead_request *req = areq->data;
428
429         if (err)
430                 goto out;
431
432         err = gcm_hash_init_continue(req, 0);
433         if (err == -EINPROGRESS)
434                 return;
435
436 out:
437         aead_request_complete(req, err);
438 }
439
440 static int gcm_hash(struct aead_request *req, u32 flags)
441 {
442         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
443         struct ahash_request *ahreq = &pctx->u.ahreq;
444         struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
445
446         ahash_request_set_tfm(ahreq, ctx->ghash);
447
448         ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
449         return crypto_ahash_init(ahreq) ?:
450                gcm_hash_init_continue(req, flags);
451 }
452
453 static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
454 {
455         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
456         struct crypto_aead *aead = crypto_aead_reqtfm(req);
457         u8 *auth_tag = pctx->auth_tag;
458
459         crypto_xor(auth_tag, pctx->iauth_tag, 16);
460         scatterwalk_map_and_copy(auth_tag, req->dst,
461                                  req->assoclen + req->cryptlen,
462                                  crypto_aead_authsize(aead), 1);
463         return 0;
464 }
465
466 static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
467 {
468         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
469         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
470
471         gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
472         gctx->cryptlen = req->cryptlen;
473         gctx->complete = gcm_enc_copy_hash;
474
475         return gcm_hash(req, flags);
476 }
477
478 static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
479 {
480         struct aead_request *req = areq->data;
481
482         if (err)
483                 goto out;
484
485         err = gcm_encrypt_continue(req, 0);
486         if (err == -EINPROGRESS)
487                 return;
488
489 out:
490         aead_request_complete(req, err);
491 }
492
493 static int crypto_gcm_encrypt(struct aead_request *req)
494 {
495         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
496         struct skcipher_request *skreq = &pctx->u.skreq;
497         u32 flags = aead_request_flags(req);
498
499         crypto_gcm_init_common(req);
500         crypto_gcm_init_crypt(req, req->cryptlen);
501         skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
502
503         return crypto_skcipher_encrypt(skreq) ?:
504                gcm_encrypt_continue(req, flags);
505 }
506
507 static int crypto_gcm_verify(struct aead_request *req)
508 {
509         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
510         struct crypto_aead *aead = crypto_aead_reqtfm(req);
511         u8 *auth_tag = pctx->auth_tag;
512         u8 *iauth_tag = pctx->iauth_tag;
513         unsigned int authsize = crypto_aead_authsize(aead);
514         unsigned int cryptlen = req->cryptlen - authsize;
515
516         crypto_xor(auth_tag, iauth_tag, 16);
517         scatterwalk_map_and_copy(iauth_tag, req->src,
518                                  req->assoclen + cryptlen, authsize, 0);
519         return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
520 }
521
522 static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
523 {
524         struct aead_request *req = areq->data;
525
526         if (!err)
527                 err = crypto_gcm_verify(req);
528
529         aead_request_complete(req, err);
530 }
531
532 static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
533 {
534         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
535         struct skcipher_request *skreq = &pctx->u.skreq;
536         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
537
538         crypto_gcm_init_crypt(req, gctx->cryptlen);
539         skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
540         return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
541 }
542
543 static int crypto_gcm_decrypt(struct aead_request *req)
544 {
545         struct crypto_aead *aead = crypto_aead_reqtfm(req);
546         struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
547         struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
548         unsigned int authsize = crypto_aead_authsize(aead);
549         unsigned int cryptlen = req->cryptlen;
550         u32 flags = aead_request_flags(req);
551
552         cryptlen -= authsize;
553
554         crypto_gcm_init_common(req);
555
556         gctx->src = sg_next(pctx->src);
557         gctx->cryptlen = cryptlen;
558         gctx->complete = gcm_dec_hash_continue;
559
560         return gcm_hash(req, flags);
561 }
562
563 static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
564 {
565         struct aead_instance *inst = aead_alg_instance(tfm);
566         struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
567         struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
568         struct crypto_skcipher *ctr;
569         struct crypto_ahash *ghash;
570         unsigned long align;
571         int err;
572
573         ghash = crypto_spawn_ahash(&ictx->ghash);
574         if (IS_ERR(ghash))
575                 return PTR_ERR(ghash);
576
577         ctr = crypto_spawn_skcipher(&ictx->ctr);
578         err = PTR_ERR(ctr);
579         if (IS_ERR(ctr))
580                 goto err_free_hash;
581
582         ctx->ctr = ctr;
583         ctx->ghash = ghash;
584
585         align = crypto_aead_alignmask(tfm);
586         align &= ~(crypto_tfm_ctx_alignment() - 1);
587         crypto_aead_set_reqsize(tfm,
588                 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
589                 max(sizeof(struct skcipher_request) +
590                     crypto_skcipher_reqsize(ctr),
591                     sizeof(struct ahash_request) +
592                     crypto_ahash_reqsize(ghash)));
593
594         return 0;
595
596 err_free_hash:
597         crypto_free_ahash(ghash);
598         return err;
599 }
600
601 static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
602 {
603         struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
604
605         crypto_free_ahash(ctx->ghash);
606         crypto_free_skcipher(ctx->ctr);
607 }
608
609 static void crypto_gcm_free(struct aead_instance *inst)
610 {
611         struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
612
613         crypto_drop_skcipher(&ctx->ctr);
614         crypto_drop_ahash(&ctx->ghash);
615         kfree(inst);
616 }
617
618 static int crypto_gcm_create_common(struct crypto_template *tmpl,
619                                     struct rtattr **tb,
620                                     const char *full_name,
621                                     const char *ctr_name,
622                                     const char *ghash_name)
623 {
624         struct crypto_attr_type *algt;
625         struct aead_instance *inst;
626         struct skcipher_alg *ctr;
627         struct crypto_alg *ghash_alg;
628         struct hash_alg_common *ghash;
629         struct gcm_instance_ctx *ctx;
630         int err;
631
632         algt = crypto_get_attr_type(tb);
633         if (IS_ERR(algt))
634                 return PTR_ERR(algt);
635
636         if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
637                 return -EINVAL;
638
639         ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
640                                     CRYPTO_ALG_TYPE_HASH,
641                                     CRYPTO_ALG_TYPE_AHASH_MASK |
642                                     crypto_requires_sync(algt->type,
643                                                          algt->mask));
644         if (IS_ERR(ghash_alg))
645                 return PTR_ERR(ghash_alg);
646
647         ghash = __crypto_hash_alg_common(ghash_alg);
648
649         err = -ENOMEM;
650         inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
651         if (!inst)
652                 goto out_put_ghash;
653
654         ctx = aead_instance_ctx(inst);
655         err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
656                                       aead_crypto_instance(inst));
657         if (err)
658                 goto err_free_inst;
659
660         err = -EINVAL;
661         if (ghash->digestsize != 16)
662                 goto err_drop_ghash;
663
664         crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
665         err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
666                                    crypto_requires_sync(algt->type,
667                                                         algt->mask));
668         if (err)
669                 goto err_drop_ghash;
670
671         ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
672
673         /* We only support 16-byte blocks. */
674         err = -EINVAL;
675         if (crypto_skcipher_alg_ivsize(ctr) != 16)
676                 goto out_put_ctr;
677
678         /* Not a stream cipher? */
679         if (ctr->base.cra_blocksize != 1)
680                 goto out_put_ctr;
681
682         err = -ENAMETOOLONG;
683         if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
684                      "gcm_base(%s,%s)", ctr->base.cra_driver_name,
685                      ghash_alg->cra_driver_name) >=
686             CRYPTO_MAX_ALG_NAME)
687                 goto out_put_ctr;
688
689         memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
690
691         inst->alg.base.cra_flags = (ghash->base.cra_flags |
692                                     ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
693         inst->alg.base.cra_priority = (ghash->base.cra_priority +
694                                        ctr->base.cra_priority) / 2;
695         inst->alg.base.cra_blocksize = 1;
696         inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
697                                        ctr->base.cra_alignmask;
698         inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
699         inst->alg.ivsize = GCM_AES_IV_SIZE;
700         inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
701         inst->alg.maxauthsize = 16;
702         inst->alg.init = crypto_gcm_init_tfm;
703         inst->alg.exit = crypto_gcm_exit_tfm;
704         inst->alg.setkey = crypto_gcm_setkey;
705         inst->alg.setauthsize = crypto_gcm_setauthsize;
706         inst->alg.encrypt = crypto_gcm_encrypt;
707         inst->alg.decrypt = crypto_gcm_decrypt;
708
709         inst->free = crypto_gcm_free;
710
711         err = aead_register_instance(tmpl, inst);
712         if (err)
713                 goto out_put_ctr;
714
715 out_put_ghash:
716         crypto_mod_put(ghash_alg);
717         return err;
718
719 out_put_ctr:
720         crypto_drop_skcipher(&ctx->ctr);
721 err_drop_ghash:
722         crypto_drop_ahash(&ctx->ghash);
723 err_free_inst:
724         kfree(inst);
725         goto out_put_ghash;
726 }
727
728 static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
729 {
730         const char *cipher_name;
731         char ctr_name[CRYPTO_MAX_ALG_NAME];
732         char full_name[CRYPTO_MAX_ALG_NAME];
733
734         cipher_name = crypto_attr_alg_name(tb[1]);
735         if (IS_ERR(cipher_name))
736                 return PTR_ERR(cipher_name);
737
738         if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
739             CRYPTO_MAX_ALG_NAME)
740                 return -ENAMETOOLONG;
741
742         if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
743             CRYPTO_MAX_ALG_NAME)
744                 return -ENAMETOOLONG;
745
746         return crypto_gcm_create_common(tmpl, tb, full_name,
747                                         ctr_name, "ghash");
748 }
749
750 static struct crypto_template crypto_gcm_tmpl = {
751         .name = "gcm",
752         .create = crypto_gcm_create,
753         .module = THIS_MODULE,
754 };
755
756 static int crypto_gcm_base_create(struct crypto_template *tmpl,
757                                   struct rtattr **tb)
758 {
759         const char *ctr_name;
760         const char *ghash_name;
761         char full_name[CRYPTO_MAX_ALG_NAME];
762
763         ctr_name = crypto_attr_alg_name(tb[1]);
764         if (IS_ERR(ctr_name))
765                 return PTR_ERR(ctr_name);
766
767         ghash_name = crypto_attr_alg_name(tb[2]);
768         if (IS_ERR(ghash_name))
769                 return PTR_ERR(ghash_name);
770
771         if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
772                      ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
773                 return -ENAMETOOLONG;
774
775         return crypto_gcm_create_common(tmpl, tb, full_name,
776                                         ctr_name, ghash_name);
777 }
778
779 static struct crypto_template crypto_gcm_base_tmpl = {
780         .name = "gcm_base",
781         .create = crypto_gcm_base_create,
782         .module = THIS_MODULE,
783 };
784
785 static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
786                                  unsigned int keylen)
787 {
788         struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
789         struct crypto_aead *child = ctx->child;
790         int err;
791
792         if (keylen < 4)
793                 return -EINVAL;
794
795         keylen -= 4;
796         memcpy(ctx->nonce, key + keylen, 4);
797
798         crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
799         crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
800                                      CRYPTO_TFM_REQ_MASK);
801         err = crypto_aead_setkey(child, key, keylen);
802         crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
803                                       CRYPTO_TFM_RES_MASK);
804
805         return err;
806 }
807
808 static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
809                                       unsigned int authsize)
810 {
811         struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
812
813         switch (authsize) {
814         case 8:
815         case 12:
816         case 16:
817                 break;
818         default:
819                 return -EINVAL;
820         }
821
822         return crypto_aead_setauthsize(ctx->child, authsize);
823 }
824
825 static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
826 {
827         struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
828         struct crypto_aead *aead = crypto_aead_reqtfm(req);
829         struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
830         struct aead_request *subreq = &rctx->subreq;
831         struct crypto_aead *child = ctx->child;
832         struct scatterlist *sg;
833         u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
834                            crypto_aead_alignmask(child) + 1);
835
836         scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
837
838         memcpy(iv, ctx->nonce, 4);
839         memcpy(iv + 4, req->iv, 8);
840
841         sg_init_table(rctx->src, 3);
842         sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
843         sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
844         if (sg != rctx->src + 1)
845                 sg_chain(rctx->src, 2, sg);
846
847         if (req->src != req->dst) {
848                 sg_init_table(rctx->dst, 3);
849                 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
850                 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
851                 if (sg != rctx->dst + 1)
852                         sg_chain(rctx->dst, 2, sg);
853         }
854
855         aead_request_set_tfm(subreq, child);
856         aead_request_set_callback(subreq, req->base.flags, req->base.complete,
857                                   req->base.data);
858         aead_request_set_crypt(subreq, rctx->src,
859                                req->src == req->dst ? rctx->src : rctx->dst,
860                                req->cryptlen, iv);
861         aead_request_set_ad(subreq, req->assoclen - 8);
862
863         return subreq;
864 }
865
866 static int crypto_rfc4106_encrypt(struct aead_request *req)
867 {
868         if (req->assoclen != 16 && req->assoclen != 20)
869                 return -EINVAL;
870
871         req = crypto_rfc4106_crypt(req);
872
873         return crypto_aead_encrypt(req);
874 }
875
876 static int crypto_rfc4106_decrypt(struct aead_request *req)
877 {
878         if (req->assoclen != 16 && req->assoclen != 20)
879                 return -EINVAL;
880
881         req = crypto_rfc4106_crypt(req);
882
883         return crypto_aead_decrypt(req);
884 }
885
886 static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
887 {
888         struct aead_instance *inst = aead_alg_instance(tfm);
889         struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
890         struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
891         struct crypto_aead *aead;
892         unsigned long align;
893
894         aead = crypto_spawn_aead(spawn);
895         if (IS_ERR(aead))
896                 return PTR_ERR(aead);
897
898         ctx->child = aead;
899
900         align = crypto_aead_alignmask(aead);
901         align &= ~(crypto_tfm_ctx_alignment() - 1);
902         crypto_aead_set_reqsize(
903                 tfm,
904                 sizeof(struct crypto_rfc4106_req_ctx) +
905                 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
906                 align + 24);
907
908         return 0;
909 }
910
911 static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
912 {
913         struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
914
915         crypto_free_aead(ctx->child);
916 }
917
918 static void crypto_rfc4106_free(struct aead_instance *inst)
919 {
920         crypto_drop_aead(aead_instance_ctx(inst));
921         kfree(inst);
922 }
923
924 static int crypto_rfc4106_create(struct crypto_template *tmpl,
925                                  struct rtattr **tb)
926 {
927         struct crypto_attr_type *algt;
928         struct aead_instance *inst;
929         struct crypto_aead_spawn *spawn;
930         struct aead_alg *alg;
931         const char *ccm_name;
932         int err;
933
934         algt = crypto_get_attr_type(tb);
935         if (IS_ERR(algt))
936                 return PTR_ERR(algt);
937
938         if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
939                 return -EINVAL;
940
941         ccm_name = crypto_attr_alg_name(tb[1]);
942         if (IS_ERR(ccm_name))
943                 return PTR_ERR(ccm_name);
944
945         inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
946         if (!inst)
947                 return -ENOMEM;
948
949         spawn = aead_instance_ctx(inst);
950         crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
951         err = crypto_grab_aead(spawn, ccm_name, 0,
952                                crypto_requires_sync(algt->type, algt->mask));
953         if (err)
954                 goto out_free_inst;
955
956         alg = crypto_spawn_aead_alg(spawn);
957
958         err = -EINVAL;
959
960         /* Underlying IV size must be 12. */
961         if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
962                 goto out_drop_alg;
963
964         /* Not a stream cipher? */
965         if (alg->base.cra_blocksize != 1)
966                 goto out_drop_alg;
967
968         err = -ENAMETOOLONG;
969         if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
970                      "rfc4106(%s)", alg->base.cra_name) >=
971             CRYPTO_MAX_ALG_NAME ||
972             snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
973                      "rfc4106(%s)", alg->base.cra_driver_name) >=
974             CRYPTO_MAX_ALG_NAME)
975                 goto out_drop_alg;
976
977         inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
978         inst->alg.base.cra_priority = alg->base.cra_priority;
979         inst->alg.base.cra_blocksize = 1;
980         inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
981
982         inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
983
984         inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
985         inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
986         inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
987
988         inst->alg.init = crypto_rfc4106_init_tfm;
989         inst->alg.exit = crypto_rfc4106_exit_tfm;
990
991         inst->alg.setkey = crypto_rfc4106_setkey;
992         inst->alg.setauthsize = crypto_rfc4106_setauthsize;
993         inst->alg.encrypt = crypto_rfc4106_encrypt;
994         inst->alg.decrypt = crypto_rfc4106_decrypt;
995
996         inst->free = crypto_rfc4106_free;
997
998         err = aead_register_instance(tmpl, inst);
999         if (err)
1000                 goto out_drop_alg;
1001
1002 out:
1003         return err;
1004
1005 out_drop_alg:
1006         crypto_drop_aead(spawn);
1007 out_free_inst:
1008         kfree(inst);
1009         goto out;
1010 }
1011
1012 static struct crypto_template crypto_rfc4106_tmpl = {
1013         .name = "rfc4106",
1014         .create = crypto_rfc4106_create,
1015         .module = THIS_MODULE,
1016 };
1017
1018 static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
1019                                  unsigned int keylen)
1020 {
1021         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1022         struct crypto_aead *child = ctx->child;
1023         int err;
1024
1025         if (keylen < 4)
1026                 return -EINVAL;
1027
1028         keylen -= 4;
1029         memcpy(ctx->nonce, key + keylen, 4);
1030
1031         crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
1032         crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
1033                                      CRYPTO_TFM_REQ_MASK);
1034         err = crypto_aead_setkey(child, key, keylen);
1035         crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1036                                       CRYPTO_TFM_RES_MASK);
1037
1038         return err;
1039 }
1040
1041 static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1042                                       unsigned int authsize)
1043 {
1044         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1045
1046         if (authsize != 16)
1047                 return -EINVAL;
1048
1049         return crypto_aead_setauthsize(ctx->child, authsize);
1050 }
1051
1052 static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
1053 {
1054         struct crypto_aead *aead = crypto_aead_reqtfm(req);
1055         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1056         struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
1057         struct aead_request *subreq = &rctx->subreq;
1058         unsigned int authsize = crypto_aead_authsize(aead);
1059         u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1060                            crypto_aead_alignmask(ctx->child) + 1);
1061         int err;
1062
1063         if (req->src != req->dst) {
1064                 err = crypto_rfc4543_copy_src_to_dst(req, enc);
1065                 if (err)
1066                         return err;
1067         }
1068
1069         memcpy(iv, ctx->nonce, 4);
1070         memcpy(iv + 4, req->iv, 8);
1071
1072         aead_request_set_tfm(subreq, ctx->child);
1073         aead_request_set_callback(subreq, req->base.flags,
1074                                   req->base.complete, req->base.data);
1075         aead_request_set_crypt(subreq, req->src, req->dst,
1076                                enc ? 0 : authsize, iv);
1077         aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1078                                     subreq->cryptlen);
1079
1080         return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
1081 }
1082
1083 static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1084 {
1085         struct crypto_aead *aead = crypto_aead_reqtfm(req);
1086         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1087         unsigned int authsize = crypto_aead_authsize(aead);
1088         unsigned int nbytes = req->assoclen + req->cryptlen -
1089                               (enc ? 0 : authsize);
1090         SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
1091
1092         skcipher_request_set_tfm(nreq, ctx->null);
1093         skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1094         skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1095
1096         return crypto_skcipher_encrypt(nreq);
1097 }
1098
1099 static int crypto_rfc4543_encrypt(struct aead_request *req)
1100 {
1101         return crypto_rfc4543_crypt(req, true);
1102 }
1103
1104 static int crypto_rfc4543_decrypt(struct aead_request *req)
1105 {
1106         return crypto_rfc4543_crypt(req, false);
1107 }
1108
1109 static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
1110 {
1111         struct aead_instance *inst = aead_alg_instance(tfm);
1112         struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
1113         struct crypto_aead_spawn *spawn = &ictx->aead;
1114         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1115         struct crypto_aead *aead;
1116         struct crypto_skcipher *null;
1117         unsigned long align;
1118         int err = 0;
1119
1120         aead = crypto_spawn_aead(spawn);
1121         if (IS_ERR(aead))
1122                 return PTR_ERR(aead);
1123
1124         null = crypto_get_default_null_skcipher2();
1125         err = PTR_ERR(null);
1126         if (IS_ERR(null))
1127                 goto err_free_aead;
1128
1129         ctx->child = aead;
1130         ctx->null = null;
1131
1132         align = crypto_aead_alignmask(aead);
1133         align &= ~(crypto_tfm_ctx_alignment() - 1);
1134         crypto_aead_set_reqsize(
1135                 tfm,
1136                 sizeof(struct crypto_rfc4543_req_ctx) +
1137                 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
1138                 align + GCM_AES_IV_SIZE);
1139
1140         return 0;
1141
1142 err_free_aead:
1143         crypto_free_aead(aead);
1144         return err;
1145 }
1146
1147 static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
1148 {
1149         struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1150
1151         crypto_free_aead(ctx->child);
1152         crypto_put_default_null_skcipher2();
1153 }
1154
1155 static void crypto_rfc4543_free(struct aead_instance *inst)
1156 {
1157         struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1158
1159         crypto_drop_aead(&ctx->aead);
1160
1161         kfree(inst);
1162 }
1163
1164 static int crypto_rfc4543_create(struct crypto_template *tmpl,
1165                                 struct rtattr **tb)
1166 {
1167         struct crypto_attr_type *algt;
1168         struct aead_instance *inst;
1169         struct crypto_aead_spawn *spawn;
1170         struct aead_alg *alg;
1171         struct crypto_rfc4543_instance_ctx *ctx;
1172         const char *ccm_name;
1173         int err;
1174
1175         algt = crypto_get_attr_type(tb);
1176         if (IS_ERR(algt))
1177                 return PTR_ERR(algt);
1178
1179         if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1180                 return -EINVAL;
1181
1182         ccm_name = crypto_attr_alg_name(tb[1]);
1183         if (IS_ERR(ccm_name))
1184                 return PTR_ERR(ccm_name);
1185
1186         inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1187         if (!inst)
1188                 return -ENOMEM;
1189
1190         ctx = aead_instance_ctx(inst);
1191         spawn = &ctx->aead;
1192         crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
1193         err = crypto_grab_aead(spawn, ccm_name, 0,
1194                                crypto_requires_sync(algt->type, algt->mask));
1195         if (err)
1196                 goto out_free_inst;
1197
1198         alg = crypto_spawn_aead_alg(spawn);
1199
1200         err = -EINVAL;
1201
1202         /* Underlying IV size must be 12. */
1203         if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
1204                 goto out_drop_alg;
1205
1206         /* Not a stream cipher? */
1207         if (alg->base.cra_blocksize != 1)
1208                 goto out_drop_alg;
1209
1210         err = -ENAMETOOLONG;
1211         if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1212                      "rfc4543(%s)", alg->base.cra_name) >=
1213             CRYPTO_MAX_ALG_NAME ||
1214             snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1215                      "rfc4543(%s)", alg->base.cra_driver_name) >=
1216             CRYPTO_MAX_ALG_NAME)
1217                 goto out_drop_alg;
1218
1219         inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1220         inst->alg.base.cra_priority = alg->base.cra_priority;
1221         inst->alg.base.cra_blocksize = 1;
1222         inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
1223
1224         inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1225
1226         inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
1227         inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
1228         inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
1229
1230         inst->alg.init = crypto_rfc4543_init_tfm;
1231         inst->alg.exit = crypto_rfc4543_exit_tfm;
1232
1233         inst->alg.setkey = crypto_rfc4543_setkey;
1234         inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1235         inst->alg.encrypt = crypto_rfc4543_encrypt;
1236         inst->alg.decrypt = crypto_rfc4543_decrypt;
1237
1238         inst->free = crypto_rfc4543_free,
1239
1240         err = aead_register_instance(tmpl, inst);
1241         if (err)
1242                 goto out_drop_alg;
1243
1244 out:
1245         return err;
1246
1247 out_drop_alg:
1248         crypto_drop_aead(spawn);
1249 out_free_inst:
1250         kfree(inst);
1251         goto out;
1252 }
1253
1254 static struct crypto_template crypto_rfc4543_tmpl = {
1255         .name = "rfc4543",
1256         .create = crypto_rfc4543_create,
1257         .module = THIS_MODULE,
1258 };
1259
1260 static int __init crypto_gcm_module_init(void)
1261 {
1262         int err;
1263
1264         gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
1265         if (!gcm_zeroes)
1266                 return -ENOMEM;
1267
1268         sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1269
1270         err = crypto_register_template(&crypto_gcm_base_tmpl);
1271         if (err)
1272                 goto out;
1273
1274         err = crypto_register_template(&crypto_gcm_tmpl);
1275         if (err)
1276                 goto out_undo_base;
1277
1278         err = crypto_register_template(&crypto_rfc4106_tmpl);
1279         if (err)
1280                 goto out_undo_gcm;
1281
1282         err = crypto_register_template(&crypto_rfc4543_tmpl);
1283         if (err)
1284                 goto out_undo_rfc4106;
1285
1286         return 0;
1287
1288 out_undo_rfc4106:
1289         crypto_unregister_template(&crypto_rfc4106_tmpl);
1290 out_undo_gcm:
1291         crypto_unregister_template(&crypto_gcm_tmpl);
1292 out_undo_base:
1293         crypto_unregister_template(&crypto_gcm_base_tmpl);
1294 out:
1295         kfree(gcm_zeroes);
1296         return err;
1297 }
1298
1299 static void __exit crypto_gcm_module_exit(void)
1300 {
1301         kfree(gcm_zeroes);
1302         crypto_unregister_template(&crypto_rfc4543_tmpl);
1303         crypto_unregister_template(&crypto_rfc4106_tmpl);
1304         crypto_unregister_template(&crypto_gcm_tmpl);
1305         crypto_unregister_template(&crypto_gcm_base_tmpl);
1306 }
1307
1308 module_init(crypto_gcm_module_init);
1309 module_exit(crypto_gcm_module_exit);
1310
1311 MODULE_LICENSE("GPL");
1312 MODULE_DESCRIPTION("Galois/Counter Mode");
1313 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
1314 MODULE_ALIAS_CRYPTO("gcm_base");
1315 MODULE_ALIAS_CRYPTO("rfc4106");
1316 MODULE_ALIAS_CRYPTO("rfc4543");
1317 MODULE_ALIAS_CRYPTO("gcm");