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