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