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