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