]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - crypto/gcm.c
crypto: gcm - make GMAC work when dst and src are different
[mirror_ubuntu-zesty-kernel.git] / crypto / gcm.c
CommitLineData
28db8e3e
MH
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
28db8e3e 11#include <crypto/gf128mul.h>
dadbc53d 12#include <crypto/internal/aead.h>
1472e5eb 13#include <crypto/internal/skcipher.h>
9382d97a 14#include <crypto/internal/hash.h>
42c271c6 15#include <crypto/scatterwalk.h>
9382d97a
HY
16#include <crypto/hash.h>
17#include "internal.h"
84c91152 18#include <linux/completion.h>
28db8e3e
MH
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
28db8e3e 25struct gcm_instance_ctx {
1472e5eb 26 struct crypto_skcipher_spawn ctr;
9382d97a 27 struct crypto_ahash_spawn ghash;
28db8e3e
MH
28};
29
30struct crypto_gcm_ctx {
31 struct crypto_ablkcipher *ctr;
9382d97a 32 struct crypto_ahash *ghash;
28db8e3e
MH
33};
34
dadbc53d
HX
35struct crypto_rfc4106_ctx {
36 struct crypto_aead *child;
37 u8 nonce[4];
38};
39
9489667d
JK
40struct crypto_rfc4543_instance_ctx {
41 struct crypto_aead_spawn aead;
42 struct crypto_skcipher_spawn null;
43};
44
73c89c15
TB
45struct crypto_rfc4543_ctx {
46 struct crypto_aead *child;
9489667d 47 struct crypto_blkcipher *null;
73c89c15
TB
48 u8 nonce[4];
49};
50
51struct 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
28db8e3e 59struct crypto_gcm_ghash_ctx {
9382d97a
HY
60 unsigned int cryptlen;
61 struct scatterlist *src;
62c5593a 62 void (*complete)(struct aead_request *req, int err);
28db8e3e
MH
63};
64
65struct crypto_gcm_req_priv_ctx {
66 u8 auth_tag[16];
6160b289 67 u8 iauth_tag[16];
84c91152
HX
68 struct scatterlist src[2];
69 struct scatterlist dst[2];
9382d97a
HY
70 struct crypto_gcm_ghash_ctx ghash_ctx;
71 union {
72 struct ahash_request ahreq;
73 struct ablkcipher_request abreq;
74 } u;
28db8e3e
MH
75};
76
84c91152
HX
77struct crypto_gcm_setkey_result {
78 int err;
79 struct completion completion;
80};
81
9382d97a
HY
82static void *gcm_zeroes;
83
2589469d
HX
84static 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
84c91152 92static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
28db8e3e 93{
84c91152 94 struct crypto_gcm_setkey_result *result = req->data;
28db8e3e 95
84c91152
HX
96 if (err == -EINPROGRESS)
97 return;
98
99 result->err = err;
100 complete(&result->completion);
28db8e3e
MH
101}
102
103static 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);
9382d97a 107 struct crypto_ahash *ghash = ctx->ghash;
28db8e3e 108 struct crypto_ablkcipher *ctr = ctx->ctr;
84c91152
HX
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;
28db8e3e
MH
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)
84c91152 126 return err;
28db8e3e
MH
127
128 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
129 CRYPTO_TFM_RES_MASK);
130
84c91152
HX
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
28db8e3e
MH
154 if (err)
155 goto out;
156
9382d97a
HY
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);
28db8e3e 163
84c91152
HX
164out:
165 kfree(data);
28db8e3e
MH
166 return err;
167}
168
dadbc53d
HX
169static 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
84c91152
HX
188static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
189 struct aead_request *req,
190 unsigned int cryptlen)
28db8e3e
MH
191{
192 struct crypto_aead *aead = crypto_aead_reqtfm(req);
193 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
2589469d 194 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
84c91152
HX
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 }
28db8e3e
MH
212
213 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
84c91152
HX
214 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
215 cryptlen + sizeof(pctx->auth_tag),
216 req->iv);
9382d97a
HY
217}
218
219static inline unsigned int gcm_remain(unsigned int len)
220{
221 len &= 0xfU;
222 return len ? 16 - len : 0;
223}
224
225static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
226static void gcm_hash_final_done(struct crypto_async_request *areq, int err);
28db8e3e 227
9382d97a
HY
228static 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;
28db8e3e 235
9382d97a
HY
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);
28db8e3e
MH
241}
242
9382d97a
HY
243static 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)
28db8e3e 247{
9382d97a
HY
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
258static 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
277static 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
62c5593a 289static void __gcm_hash_final_done(struct aead_request *req, int err)
9382d97a 290{
2589469d 291 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
9382d97a
HY
292 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
293
294 if (!err)
295 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
28db8e3e 296
62c5593a 297 gctx->complete(req, err);
9382d97a
HY
298}
299
62c5593a 300static void gcm_hash_final_done(struct crypto_async_request *areq, int err)
9382d97a
HY
301{
302 struct aead_request *req = areq->data;
62c5593a
HY
303
304 __gcm_hash_final_done(req, err);
305}
306
307static void __gcm_hash_len_done(struct aead_request *req, int err)
308{
9382d97a
HY
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
62c5593a 317 __gcm_hash_final_done(req, err);
9382d97a
HY
318}
319
62c5593a 320static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
9382d97a
HY
321{
322 struct aead_request *req = areq->data;
62c5593a
HY
323
324 __gcm_hash_len_done(req, err);
325}
326
327static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err)
328{
9382d97a
HY
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
62c5593a 337 __gcm_hash_len_done(req, err);
9382d97a
HY
338}
339
62c5593a
HY
340static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
341 int err)
9382d97a
HY
342{
343 struct aead_request *req = areq->data;
62c5593a
HY
344
345 __gcm_hash_crypt_remain_done(req, err);
346}
347
348static void __gcm_hash_crypt_done(struct aead_request *req, int err)
349{
9382d97a
HY
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
62c5593a 363 __gcm_hash_crypt_remain_done(req, err);
9382d97a
HY
364}
365
62c5593a 366static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
9382d97a
HY
367{
368 struct aead_request *req = areq->data;
62c5593a
HY
369
370 __gcm_hash_crypt_done(req, err);
371}
372
373static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err)
374{
9382d97a
HY
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)
62c5593a 391 __gcm_hash_crypt_done(req, err);
9382d97a 392 else
62c5593a 393 __gcm_hash_crypt_remain_done(req, err);
9382d97a
HY
394}
395
62c5593a
HY
396static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
397 int err)
9382d97a
HY
398{
399 struct aead_request *req = areq->data;
62c5593a
HY
400
401 __gcm_hash_assoc_remain_done(req, err);
402}
403
404static void __gcm_hash_assoc_done(struct aead_request *req, int err)
405{
9382d97a
HY
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
62c5593a 418 __gcm_hash_assoc_remain_done(req, err);
9382d97a
HY
419}
420
62c5593a 421static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
9382d97a
HY
422{
423 struct aead_request *req = areq->data;
62c5593a
HY
424
425 __gcm_hash_assoc_done(req, err);
426}
427
428static void __gcm_hash_init_done(struct aead_request *req, int err)
429{
9382d97a
HY
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)
62c5593a 445 __gcm_hash_assoc_done(req, err);
9382d97a 446 else
62c5593a
HY
447 __gcm_hash_assoc_remain_done(req, err);
448}
449
450static 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);
9382d97a
HY
455}
456
457static 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
506static 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;
28db8e3e 511
6160b289
HX
512 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
513 crypto_aead_authsize(aead), 1);
6160b289
HX
514}
515
62c5593a 516static void gcm_enc_hash_done(struct aead_request *req, int err)
6160b289 517{
9382d97a 518 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
6160b289
HX
519
520 if (!err)
9382d97a 521 gcm_enc_copy_hash(req, pctx);
6160b289 522
28db8e3e
MH
523 aead_request_complete(req, err);
524}
525
62c5593a 526static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
9382d97a
HY
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;
62c5593a
HY
535 else if (!err) {
536 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
537 gcm_enc_copy_hash(req, pctx);
538 }
9382d97a
HY
539 }
540
62c5593a 541 aead_request_complete(req, err);
9382d97a
HY
542}
543
28db8e3e
MH
544static int crypto_gcm_encrypt(struct aead_request *req)
545{
2589469d 546 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
9382d97a
HY
547 struct ablkcipher_request *abreq = &pctx->u.abreq;
548 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
84c91152
HX
549 int err;
550
551 crypto_gcm_init_crypt(abreq, req, req->cryptlen);
552 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
9382d97a
HY
553 gcm_encrypt_done, req);
554
555 gctx->src = req->dst;
556 gctx->cryptlen = req->cryptlen;
557 gctx->complete = gcm_enc_hash_done;
28db8e3e 558
84c91152 559 err = crypto_ablkcipher_encrypt(abreq);
28db8e3e
MH
560 if (err)
561 return err;
562
9382d97a
HY
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;
28db8e3e
MH
571}
572
9382d97a
HY
573static int crypto_gcm_verify(struct aead_request *req,
574 struct crypto_gcm_req_priv_ctx *pctx)
84c91152
HX
575{
576 struct crypto_aead *aead = crypto_aead_reqtfm(req);
84c91152
HX
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
9382d97a 582 crypto_xor(auth_tag, iauth_tag, 16);
84c91152
HX
583 scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
584 return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
585}
586
9382d97a 587static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
28db8e3e 588{
84c91152 589 struct aead_request *req = areq->data;
9382d97a 590 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
84c91152
HX
591
592 if (!err)
9382d97a 593 err = crypto_gcm_verify(req, pctx);
84c91152
HX
594
595 aead_request_complete(req, err);
28db8e3e
MH
596}
597
62c5593a 598static void gcm_dec_hash_done(struct aead_request *req, int err)
9382d97a 599{
9382d97a
HY
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;
62c5593a
HY
611 else if (!err)
612 err = crypto_gcm_verify(req, pctx);
9382d97a
HY
613 }
614
62c5593a 615 aead_request_complete(req, err);
9382d97a
HY
616}
617
28db8e3e
MH
618static int crypto_gcm_decrypt(struct aead_request *req)
619{
6160b289 620 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2589469d 621 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
9382d97a
HY
622 struct ablkcipher_request *abreq = &pctx->u.abreq;
623 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
6160b289 624 unsigned int authsize = crypto_aead_authsize(aead);
9382d97a 625 unsigned int cryptlen = req->cryptlen;
28db8e3e
MH
626 int err;
627
6160b289 628 if (cryptlen < authsize)
28db8e3e 629 return -EINVAL;
6160b289 630 cryptlen -= authsize;
28db8e3e 631
9382d97a
HY
632 gctx->src = req->src;
633 gctx->cryptlen = cryptlen;
634 gctx->complete = gcm_dec_hash_done;
28db8e3e 635
9382d97a
HY
636 err = gcm_hash(req, pctx);
637 if (err)
638 return err;
28db8e3e 639
9382d97a
HY
640 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
641 gcm_decrypt_done, req);
642 crypto_gcm_init_crypt(abreq, req, cryptlen);
84c91152
HX
643 err = crypto_ablkcipher_decrypt(abreq);
644 if (err)
645 return err;
28db8e3e 646
9382d97a 647 return crypto_gcm_verify(req, pctx);
28db8e3e
MH
648}
649
650static 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;
9382d97a 656 struct crypto_ahash *ghash;
28db8e3e
MH
657 unsigned long align;
658 int err;
659
9382d97a
HY
660 ghash = crypto_spawn_ahash(&ictx->ghash);
661 if (IS_ERR(ghash))
662 return PTR_ERR(ghash);
663
1472e5eb 664 ctr = crypto_spawn_skcipher(&ictx->ctr);
28db8e3e
MH
665 err = PTR_ERR(ctr);
666 if (IS_ERR(ctr))
9382d97a 667 goto err_free_hash;
28db8e3e
MH
668
669 ctx->ctr = ctr;
9382d97a 670 ctx->ghash = ghash;
28db8e3e 671
2589469d 672 align = crypto_tfm_alg_alignmask(tfm);
28db8e3e 673 align &= ~(crypto_tfm_ctx_alignment() - 1);
7f681378 674 tfm->crt_aead.reqsize = align +
9382d97a
HY
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));
28db8e3e
MH
680
681 return 0;
9382d97a
HY
682
683err_free_hash:
684 crypto_free_ahash(ghash);
685 return err;
28db8e3e
MH
686}
687
688static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
689{
690 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
691
9382d97a 692 crypto_free_ahash(ctx->ghash);
28db8e3e
MH
693 crypto_free_ablkcipher(ctx->ctr);
694}
695
d00aa19b
HX
696static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
697 const char *full_name,
9382d97a
HY
698 const char *ctr_name,
699 const char *ghash_name)
28db8e3e 700{
d00aa19b 701 struct crypto_attr_type *algt;
28db8e3e
MH
702 struct crypto_instance *inst;
703 struct crypto_alg *ctr;
9382d97a
HY
704 struct crypto_alg *ghash_alg;
705 struct ahash_alg *ghash_ahash_alg;
28db8e3e
MH
706 struct gcm_instance_ctx *ctx;
707 int err;
28db8e3e 708
d00aa19b 709 algt = crypto_get_attr_type(tb);
d00aa19b 710 if (IS_ERR(algt))
3e8afe35 711 return ERR_CAST(algt);
28db8e3e 712
d00aa19b
HX
713 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
714 return ERR_PTR(-EINVAL);
28db8e3e 715
9382d97a
HY
716 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
717 CRYPTO_ALG_TYPE_HASH,
718 CRYPTO_ALG_TYPE_AHASH_MASK);
9382d97a 719 if (IS_ERR(ghash_alg))
3e8afe35 720 return ERR_CAST(ghash_alg);
9382d97a
HY
721
722 err = -ENOMEM;
1472e5eb
HX
723 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
724 if (!inst)
9382d97a 725 goto out_put_ghash;
28db8e3e 726
1472e5eb 727 ctx = crypto_instance_ctx(inst);
9382d97a
HY
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
1472e5eb
HX
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)
9382d97a 739 goto err_drop_ghash;
1472e5eb
HX
740
741 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
28db8e3e 742
d00aa19b 743 /* We only support 16-byte blocks. */
1472e5eb 744 if (ctr->cra_ablkcipher.ivsize != 16)
d00aa19b
HX
745 goto out_put_ctr;
746
747 /* Not a stream cipher? */
748 err = -EINVAL;
749 if (ctr->cra_blocksize != 1)
28db8e3e
MH
750 goto out_put_ctr;
751
28db8e3e 752 err = -ENAMETOOLONG;
d00aa19b 753 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
9382d97a
HY
754 "gcm_base(%s,%s)", ctr->cra_driver_name,
755 ghash_alg->cra_driver_name) >=
d00aa19b 756 CRYPTO_MAX_ALG_NAME)
1472e5eb 757 goto out_put_ctr;
28db8e3e 758
d00aa19b
HX
759 memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
760
1472e5eb
HX
761 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
762 inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
28db8e3e 763 inst->alg.cra_priority = ctr->cra_priority;
d00aa19b 764 inst->alg.cra_blocksize = 1;
2589469d 765 inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
28db8e3e 766 inst->alg.cra_type = &crypto_aead_type;
84c91152 767 inst->alg.cra_aead.ivsize = 16;
7ba683a6 768 inst->alg.cra_aead.maxauthsize = 16;
28db8e3e
MH
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;
dadbc53d 773 inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
28db8e3e
MH
774 inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
775 inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
776
777out:
9382d97a 778 crypto_mod_put(ghash_alg);
28db8e3e 779 return inst;
1472e5eb
HX
780
781out_put_ctr:
782 crypto_drop_skcipher(&ctx->ctr);
9382d97a
HY
783err_drop_ghash:
784 crypto_drop_ahash(&ctx->ghash);
28db8e3e
MH
785err_free_inst:
786 kfree(inst);
9382d97a 787out_put_ghash:
28db8e3e
MH
788 inst = ERR_PTR(err);
789 goto out;
790}
791
d00aa19b
HX
792static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
793{
d00aa19b
HX
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]);
d00aa19b 799 if (IS_ERR(cipher_name))
3e8afe35 800 return ERR_CAST(cipher_name);
d00aa19b
HX
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
9382d97a 810 return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
d00aa19b
HX
811}
812
28db8e3e
MH
813static void crypto_gcm_free(struct crypto_instance *inst)
814{
815 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
816
1472e5eb 817 crypto_drop_skcipher(&ctx->ctr);
9382d97a 818 crypto_drop_ahash(&ctx->ghash);
28db8e3e
MH
819 kfree(inst);
820}
821
822static 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
d00aa19b
HX
829static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
830{
d00aa19b 831 const char *ctr_name;
9382d97a 832 const char *ghash_name;
d00aa19b
HX
833 char full_name[CRYPTO_MAX_ALG_NAME];
834
835 ctr_name = crypto_attr_alg_name(tb[1]);
d00aa19b 836 if (IS_ERR(ctr_name))
3e8afe35 837 return ERR_CAST(ctr_name);
d00aa19b 838
9382d97a 839 ghash_name = crypto_attr_alg_name(tb[2]);
9382d97a 840 if (IS_ERR(ghash_name))
3e8afe35 841 return ERR_CAST(ghash_name);
9382d97a
HY
842
843 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
844 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
d00aa19b
HX
845 return ERR_PTR(-ENAMETOOLONG);
846
9382d97a 847 return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
d00aa19b
HX
848}
849
850static 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
dadbc53d
HX
857static 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
880static 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
897static 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
918static int crypto_rfc4106_encrypt(struct aead_request *req)
919{
920 req = crypto_rfc4106_crypt(req);
921
922 return crypto_aead_encrypt(req);
923}
924
925static int crypto_rfc4106_decrypt(struct aead_request *req)
926{
927 req = crypto_rfc4106_crypt(req);
928
929 return crypto_aead_decrypt(req);
930}
931
932static 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
956static 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
963static 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);
dadbc53d 973 if (IS_ERR(algt))
3e8afe35 974 return ERR_CAST(algt);
dadbc53d
HX
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]);
dadbc53d 980 if (IS_ERR(ccm_name))
3e8afe35 981 return ERR_CAST(ccm_name);
dadbc53d
HX
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
1036out:
1037 return inst;
1038
1039out_drop_alg:
1040 crypto_drop_aead(spawn);
1041out_free_inst:
1042 kfree(inst);
1043 inst = ERR_PTR(err);
1044 goto out;
1045}
1046
1047static void crypto_rfc4106_free(struct crypto_instance *inst)
1048{
1049 crypto_drop_spawn(crypto_instance_ctx(inst));
1050 kfree(inst);
1051}
1052
1053static 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
73c89c15
TB
1060static 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
1068static 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
1091static 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
73c89c15 1102static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req,
9489667d 1103 bool enc)
73c89c15
TB
1104{
1105 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1106 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1107 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1108 struct aead_request *subreq = &rctx->subreq;
9489667d 1109 struct scatterlist *src = req->src;
73c89c15
TB
1110 struct scatterlist *cipher = rctx->cipher;
1111 struct scatterlist *payload = rctx->payload;
1112 struct scatterlist *assoc = rctx->assoc;
1113 unsigned int authsize = crypto_aead_authsize(aead);
1114 unsigned int assoclen = req->assoclen;
9489667d
JK
1115 struct page *srcp;
1116 u8 *vsrc;
73c89c15
TB
1117 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1118 crypto_aead_alignmask(ctx->child) + 1);
1119
1120 memcpy(iv, ctx->nonce, 4);
1121 memcpy(iv + 4, req->iv, 8);
1122
1123 /* construct cipher/plaintext */
1124 if (enc)
1125 memset(rctx->auth_tag, 0, authsize);
1126 else
9489667d 1127 scatterwalk_map_and_copy(rctx->auth_tag, src,
73c89c15
TB
1128 req->cryptlen - authsize,
1129 authsize, 0);
1130
1131 sg_init_one(cipher, rctx->auth_tag, authsize);
1132
1133 /* construct the aad */
9489667d
JK
1134 srcp = sg_page(src);
1135 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
73c89c15
TB
1136
1137 sg_init_table(payload, 2);
1138 sg_set_buf(payload, req->iv, 8);
9489667d 1139 scatterwalk_crypto_chain(payload, src, vsrc == req->iv + 8, 2);
73c89c15
TB
1140 assoclen += 8 + req->cryptlen - (enc ? 0 : authsize);
1141
1142 sg_init_table(assoc, 2);
1143 sg_set_page(assoc, sg_page(req->assoc), req->assoc->length,
1144 req->assoc->offset);
c920fa60 1145 scatterwalk_crypto_chain(assoc, payload, 0, 2);
73c89c15
TB
1146
1147 aead_request_set_tfm(subreq, ctx->child);
1148 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
1149 req->base.data);
1150 aead_request_set_crypt(subreq, cipher, cipher, enc ? 0 : authsize, iv);
1151 aead_request_set_assoc(subreq, assoc, assoclen);
1152
1153 return subreq;
1154}
1155
9489667d
JK
1156static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1157{
1158 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1159 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1160 unsigned int authsize = crypto_aead_authsize(aead);
1161 unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize);
1162 struct blkcipher_desc desc = {
1163 .tfm = ctx->null,
1164 };
1165
1166 return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
1167}
1168
73c89c15
TB
1169static int crypto_rfc4543_encrypt(struct aead_request *req)
1170{
1171 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1172 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1173 struct aead_request *subreq;
1174 int err;
1175
9489667d
JK
1176 if (req->src != req->dst) {
1177 err = crypto_rfc4543_copy_src_to_dst(req, true);
1178 if (err)
1179 return err;
1180 }
1181
1182 subreq = crypto_rfc4543_crypt(req, true);
73c89c15
TB
1183 err = crypto_aead_encrypt(subreq);
1184 if (err)
1185 return err;
1186
1187 scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
1188 crypto_aead_authsize(aead), 1);
1189
1190 return 0;
1191}
1192
1193static int crypto_rfc4543_decrypt(struct aead_request *req)
1194{
9489667d
JK
1195 int err;
1196
1197 if (req->src != req->dst) {
1198 err = crypto_rfc4543_copy_src_to_dst(req, false);
1199 if (err)
1200 return err;
1201 }
1202
1203 req = crypto_rfc4543_crypt(req, false);
73c89c15
TB
1204
1205 return crypto_aead_decrypt(req);
1206}
1207
1208static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm)
1209{
1210 struct crypto_instance *inst = (void *)tfm->__crt_alg;
9489667d
JK
1211 struct crypto_rfc4543_instance_ctx *ictx = crypto_instance_ctx(inst);
1212 struct crypto_aead_spawn *spawn = &ictx->aead;
73c89c15
TB
1213 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1214 struct crypto_aead *aead;
9489667d 1215 struct crypto_blkcipher *null;
73c89c15 1216 unsigned long align;
9489667d 1217 int err = 0;
73c89c15
TB
1218
1219 aead = crypto_spawn_aead(spawn);
1220 if (IS_ERR(aead))
1221 return PTR_ERR(aead);
1222
9489667d
JK
1223 null = crypto_spawn_blkcipher(&ictx->null.base);
1224 err = PTR_ERR(null);
1225 if (IS_ERR(null))
1226 goto err_free_aead;
1227
73c89c15 1228 ctx->child = aead;
9489667d 1229 ctx->null = null;
73c89c15
TB
1230
1231 align = crypto_aead_alignmask(aead);
1232 align &= ~(crypto_tfm_ctx_alignment() - 1);
1233 tfm->crt_aead.reqsize = sizeof(struct crypto_rfc4543_req_ctx) +
1234 ALIGN(crypto_aead_reqsize(aead),
1235 crypto_tfm_ctx_alignment()) +
1236 align + 16;
1237
1238 return 0;
9489667d
JK
1239
1240err_free_aead:
1241 crypto_free_aead(aead);
1242 return err;
73c89c15
TB
1243}
1244
1245static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm)
1246{
1247 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1248
1249 crypto_free_aead(ctx->child);
9489667d 1250 crypto_free_blkcipher(ctx->null);
73c89c15
TB
1251}
1252
1253static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb)
1254{
1255 struct crypto_attr_type *algt;
1256 struct crypto_instance *inst;
1257 struct crypto_aead_spawn *spawn;
1258 struct crypto_alg *alg;
9489667d 1259 struct crypto_rfc4543_instance_ctx *ctx;
73c89c15
TB
1260 const char *ccm_name;
1261 int err;
1262
1263 algt = crypto_get_attr_type(tb);
73c89c15 1264 if (IS_ERR(algt))
3e8afe35 1265 return ERR_CAST(algt);
73c89c15
TB
1266
1267 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1268 return ERR_PTR(-EINVAL);
1269
1270 ccm_name = crypto_attr_alg_name(tb[1]);
73c89c15 1271 if (IS_ERR(ccm_name))
3e8afe35 1272 return ERR_CAST(ccm_name);
73c89c15 1273
9489667d 1274 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
73c89c15
TB
1275 if (!inst)
1276 return ERR_PTR(-ENOMEM);
1277
9489667d
JK
1278 ctx = crypto_instance_ctx(inst);
1279 spawn = &ctx->aead;
73c89c15
TB
1280 crypto_set_aead_spawn(spawn, inst);
1281 err = crypto_grab_aead(spawn, ccm_name, 0,
1282 crypto_requires_sync(algt->type, algt->mask));
1283 if (err)
1284 goto out_free_inst;
1285
1286 alg = crypto_aead_spawn_alg(spawn);
1287
9489667d
JK
1288 crypto_set_skcipher_spawn(&ctx->null, inst);
1289 err = crypto_grab_skcipher(&ctx->null, "ecb(cipher_null)", 0,
1290 CRYPTO_ALG_ASYNC);
1291 if (err)
1292 goto out_drop_alg;
1293
1294 crypto_skcipher_spawn_alg(&ctx->null);
1295
73c89c15
TB
1296 err = -EINVAL;
1297
1298 /* We only support 16-byte blocks. */
1299 if (alg->cra_aead.ivsize != 16)
9489667d 1300 goto out_drop_ecbnull;
73c89c15
TB
1301
1302 /* Not a stream cipher? */
1303 if (alg->cra_blocksize != 1)
9489667d 1304 goto out_drop_ecbnull;
73c89c15
TB
1305
1306 err = -ENAMETOOLONG;
1307 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1308 "rfc4543(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1309 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1310 "rfc4543(%s)", alg->cra_driver_name) >=
1311 CRYPTO_MAX_ALG_NAME)
9489667d 1312 goto out_drop_ecbnull;
73c89c15
TB
1313
1314 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1315 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1316 inst->alg.cra_priority = alg->cra_priority;
1317 inst->alg.cra_blocksize = 1;
1318 inst->alg.cra_alignmask = alg->cra_alignmask;
1319 inst->alg.cra_type = &crypto_nivaead_type;
1320
1321 inst->alg.cra_aead.ivsize = 8;
1322 inst->alg.cra_aead.maxauthsize = 16;
1323
1324 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1325
1326 inst->alg.cra_init = crypto_rfc4543_init_tfm;
1327 inst->alg.cra_exit = crypto_rfc4543_exit_tfm;
1328
1329 inst->alg.cra_aead.setkey = crypto_rfc4543_setkey;
1330 inst->alg.cra_aead.setauthsize = crypto_rfc4543_setauthsize;
1331 inst->alg.cra_aead.encrypt = crypto_rfc4543_encrypt;
1332 inst->alg.cra_aead.decrypt = crypto_rfc4543_decrypt;
1333
1334 inst->alg.cra_aead.geniv = "seqiv";
1335
1336out:
1337 return inst;
1338
9489667d
JK
1339out_drop_ecbnull:
1340 crypto_drop_skcipher(&ctx->null);
73c89c15
TB
1341out_drop_alg:
1342 crypto_drop_aead(spawn);
1343out_free_inst:
1344 kfree(inst);
1345 inst = ERR_PTR(err);
1346 goto out;
1347}
1348
1349static void crypto_rfc4543_free(struct crypto_instance *inst)
1350{
9489667d
JK
1351 struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst);
1352
1353 crypto_drop_aead(&ctx->aead);
1354 crypto_drop_skcipher(&ctx->null);
1355
73c89c15
TB
1356 kfree(inst);
1357}
1358
1359static struct crypto_template crypto_rfc4543_tmpl = {
1360 .name = "rfc4543",
1361 .alloc = crypto_rfc4543_alloc,
1362 .free = crypto_rfc4543_free,
1363 .module = THIS_MODULE,
1364};
1365
28db8e3e
MH
1366static int __init crypto_gcm_module_init(void)
1367{
d00aa19b
HX
1368 int err;
1369
9382d97a
HY
1370 gcm_zeroes = kzalloc(16, GFP_KERNEL);
1371 if (!gcm_zeroes)
1372 return -ENOMEM;
1373
d00aa19b
HX
1374 err = crypto_register_template(&crypto_gcm_base_tmpl);
1375 if (err)
1376 goto out;
1377
1378 err = crypto_register_template(&crypto_gcm_tmpl);
1379 if (err)
1380 goto out_undo_base;
1381
dadbc53d
HX
1382 err = crypto_register_template(&crypto_rfc4106_tmpl);
1383 if (err)
1384 goto out_undo_gcm;
1385
73c89c15
TB
1386 err = crypto_register_template(&crypto_rfc4543_tmpl);
1387 if (err)
1388 goto out_undo_rfc4106;
1389
9382d97a 1390 return 0;
d00aa19b 1391
73c89c15
TB
1392out_undo_rfc4106:
1393 crypto_unregister_template(&crypto_rfc4106_tmpl);
dadbc53d
HX
1394out_undo_gcm:
1395 crypto_unregister_template(&crypto_gcm_tmpl);
d00aa19b
HX
1396out_undo_base:
1397 crypto_unregister_template(&crypto_gcm_base_tmpl);
9382d97a
HY
1398out:
1399 kfree(gcm_zeroes);
1400 return err;
28db8e3e
MH
1401}
1402
1403static void __exit crypto_gcm_module_exit(void)
1404{
9382d97a 1405 kfree(gcm_zeroes);
73c89c15 1406 crypto_unregister_template(&crypto_rfc4543_tmpl);
dadbc53d 1407 crypto_unregister_template(&crypto_rfc4106_tmpl);
28db8e3e 1408 crypto_unregister_template(&crypto_gcm_tmpl);
d00aa19b 1409 crypto_unregister_template(&crypto_gcm_base_tmpl);
28db8e3e
MH
1410}
1411
1412module_init(crypto_gcm_module_init);
1413module_exit(crypto_gcm_module_exit);
1414
1415MODULE_LICENSE("GPL");
1416MODULE_DESCRIPTION("Galois/Counter Mode");
1417MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
d00aa19b 1418MODULE_ALIAS("gcm_base");
dadbc53d 1419MODULE_ALIAS("rfc4106");
73c89c15 1420MODULE_ALIAS("rfc4543");