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