blob: b0d3cb12334db336e0801a0ea16145684554e9a3 [file] [log] [blame]
Mikko Herranen28db8e32007-11-26 22:24:11 +08001/*
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
Mikko Herranen28db8e32007-11-26 22:24:11 +080011#include <crypto/gf128mul.h>
Herbert Xudadbc532007-12-17 15:33:17 +080012#include <crypto/internal/aead.h>
Herbert Xu1472e5e2007-12-07 19:26:11 +080013#include <crypto/internal/skcipher.h>
Huang Ying9382d972009-08-06 15:34:26 +100014#include <crypto/internal/hash.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080015#include <crypto/scatterwalk.h>
Huang Ying9382d972009-08-06 15:34:26 +100016#include <crypto/hash.h>
17#include "internal.h"
Herbert Xu84c91152007-12-17 21:42:08 +080018#include <linux/completion.h>
Mikko Herranen28db8e32007-11-26 22:24:11 +080019#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
Mikko Herranen28db8e32007-11-26 22:24:11 +080025struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080026 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100027 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080028};
29
30struct crypto_gcm_ctx {
31 struct crypto_ablkcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100032 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080033};
34
Herbert Xudadbc532007-12-17 15:33:17 +080035struct crypto_rfc4106_ctx {
36 struct crypto_aead *child;
37 u8 nonce[4];
38};
39
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030040struct crypto_rfc4543_instance_ctx {
41 struct crypto_aead_spawn aead;
42 struct crypto_skcipher_spawn null;
43};
44
Tobias Brunner73c89c12010-01-17 21:52:11 +110045struct crypto_rfc4543_ctx {
46 struct crypto_aead *child;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030047 struct crypto_blkcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110048 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
Mikko Herranen28db8e32007-11-26 22:24:11 +080059struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100060 unsigned int cryptlen;
61 struct scatterlist *src;
Huang Ying62c55932009-11-16 21:52:22 +080062 void (*complete)(struct aead_request *req, int err);
Mikko Herranen28db8e32007-11-26 22:24:11 +080063};
64
65struct crypto_gcm_req_priv_ctx {
66 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110067 u8 iauth_tag[16];
Herbert Xu84c91152007-12-17 21:42:08 +080068 struct scatterlist src[2];
69 struct scatterlist dst[2];
Huang Ying9382d972009-08-06 15:34:26 +100070 struct crypto_gcm_ghash_ctx ghash_ctx;
71 union {
72 struct ahash_request ahreq;
73 struct ablkcipher_request abreq;
74 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080075};
76
Herbert Xu84c91152007-12-17 21:42:08 +080077struct crypto_gcm_setkey_result {
78 int err;
79 struct completion completion;
80};
81
Huang Ying9382d972009-08-06 15:34:26 +100082static void *gcm_zeroes;
83
Herbert Xu25894692007-12-12 19:16:38 +080084static 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
Herbert Xu84c91152007-12-17 21:42:08 +080092static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +080093{
Herbert Xu84c91152007-12-17 21:42:08 +080094 struct crypto_gcm_setkey_result *result = req->data;
Mikko Herranen28db8e32007-11-26 22:24:11 +080095
Herbert Xu84c91152007-12-17 21:42:08 +080096 if (err == -EINPROGRESS)
97 return;
Mikko Herranen28db8e32007-11-26 22:24:11 +080098
Herbert Xu84c91152007-12-17 21:42:08 +080099 result->err = err;
100 complete(&result->completion);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800101}
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);
Huang Ying9382d972009-08-06 15:34:26 +1000107 struct crypto_ahash *ghash = ctx->ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800108 struct crypto_ablkcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +0800109 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;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800119
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)
Herbert Xu84c91152007-12-17 21:42:08 +0800126 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800127
128 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
129 CRYPTO_TFM_RES_MASK);
130
Herbert Xu84c91152007-12-17 21:42:08 +0800131 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
Mikko Herranen28db8e32007-11-26 22:24:11 +0800154 if (err)
155 goto out;
156
Huang Ying9382d972009-08-06 15:34:26 +1000157 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);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800163
Herbert Xu84c91152007-12-17 21:42:08 +0800164out:
165 kfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800166 return err;
167}
168
Herbert Xudadbc532007-12-17 15:33:17 +0800169static 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
Herbert Xu84c91152007-12-17 21:42:08 +0800188static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
189 struct aead_request *req,
190 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800191{
192 struct crypto_aead *aead = crypto_aead_reqtfm(req);
193 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800194 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800195 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 }
Mikko Herranen28db8e32007-11-26 22:24:11 +0800212
213 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
Herbert Xu84c91152007-12-17 21:42:08 +0800214 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
215 cryptlen + sizeof(pctx->auth_tag),
216 req->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800217}
218
Huang Ying9382d972009-08-06 15:34:26 +1000219static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800220{
Huang Ying9382d972009-08-06 15:34:26 +1000221 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);
227
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;
235
236 ahash_request_set_callback(ahreq, aead_request_flags(req),
237 complete, req);
238 ahash_request_set_crypt(ahreq, src, NULL, len);
239
240 return crypto_ahash_update(ahreq);
241}
242
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)
247{
248 struct ahash_request *ahreq = &pctx->u.ahreq;
249
250 ahash_request_set_callback(ahreq, aead_request_flags(req),
251 complete, req);
252 sg_init_one(pctx->src, gcm_zeroes, remain);
253 ahash_request_set_crypt(ahreq, pctx->src, NULL, remain);
254
255 return crypto_ahash_update(ahreq);
256}
257
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
Huang Ying62c55932009-11-16 21:52:22 +0800289static void __gcm_hash_final_done(struct aead_request *req, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000290{
Herbert Xu25894692007-12-12 19:16:38 +0800291 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000292 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800293
Huang Ying9382d972009-08-06 15:34:26 +1000294 if (!err)
295 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800296
Huang Ying62c55932009-11-16 21:52:22 +0800297 gctx->complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000298}
299
Huang Ying62c55932009-11-16 21:52:22 +0800300static void gcm_hash_final_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000301{
302 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800303
304 __gcm_hash_final_done(req, err);
305}
306
307static void __gcm_hash_len_done(struct aead_request *req, int err)
308{
Huang Ying9382d972009-08-06 15:34:26 +1000309 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
Huang Ying62c55932009-11-16 21:52:22 +0800317 __gcm_hash_final_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000318}
319
Huang Ying62c55932009-11-16 21:52:22 +0800320static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000321{
322 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800323
324 __gcm_hash_len_done(req, err);
325}
326
327static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err)
328{
Huang Ying9382d972009-08-06 15:34:26 +1000329 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
Huang Ying62c55932009-11-16 21:52:22 +0800337 __gcm_hash_len_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000338}
339
Huang Ying62c55932009-11-16 21:52:22 +0800340static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
341 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000342{
343 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800344
345 __gcm_hash_crypt_remain_done(req, err);
346}
347
348static void __gcm_hash_crypt_done(struct aead_request *req, int err)
349{
Huang Ying9382d972009-08-06 15:34:26 +1000350 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
Huang Ying62c55932009-11-16 21:52:22 +0800363 __gcm_hash_crypt_remain_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000364}
365
Huang Ying62c55932009-11-16 21:52:22 +0800366static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000367{
368 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800369
370 __gcm_hash_crypt_done(req, err);
371}
372
373static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err)
374{
Huang Ying9382d972009-08-06 15:34:26 +1000375 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)
Huang Ying62c55932009-11-16 21:52:22 +0800391 __gcm_hash_crypt_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000392 else
Huang Ying62c55932009-11-16 21:52:22 +0800393 __gcm_hash_crypt_remain_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000394}
395
Huang Ying62c55932009-11-16 21:52:22 +0800396static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
397 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000398{
399 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800400
401 __gcm_hash_assoc_remain_done(req, err);
402}
403
404static void __gcm_hash_assoc_done(struct aead_request *req, int err)
405{
Huang Ying9382d972009-08-06 15:34:26 +1000406 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
Huang Ying62c55932009-11-16 21:52:22 +0800418 __gcm_hash_assoc_remain_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000419}
420
Huang Ying62c55932009-11-16 21:52:22 +0800421static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000422{
423 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800424
425 __gcm_hash_assoc_done(req, err);
426}
427
428static void __gcm_hash_init_done(struct aead_request *req, int err)
429{
Huang Ying9382d972009-08-06 15:34:26 +1000430 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)
Huang Ying62c55932009-11-16 21:52:22 +0800445 __gcm_hash_assoc_done(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000446 else
Huang Ying62c55932009-11-16 21:52:22 +0800447 __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);
Huang Ying9382d972009-08-06 15:34:26 +1000455}
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
Herbert Xu6160b282007-12-04 19:17:50 +1100503 return 0;
504}
505
Huang Ying9382d972009-08-06 15:34:26 +1000506static void gcm_enc_copy_hash(struct aead_request *req,
507 struct crypto_gcm_req_priv_ctx *pctx)
508{
509 struct crypto_aead *aead = crypto_aead_reqtfm(req);
510 u8 *auth_tag = pctx->auth_tag;
511
512 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
513 crypto_aead_authsize(aead), 1);
514}
515
Huang Ying62c55932009-11-16 21:52:22 +0800516static void gcm_enc_hash_done(struct aead_request *req, int err)
Herbert Xu6160b282007-12-04 19:17:50 +1100517{
Huang Ying9382d972009-08-06 15:34:26 +1000518 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu6160b282007-12-04 19:17:50 +1100519
520 if (!err)
Huang Ying9382d972009-08-06 15:34:26 +1000521 gcm_enc_copy_hash(req, pctx);
Herbert Xu6160b282007-12-04 19:17:50 +1100522
Mikko Herranen28db8e32007-11-26 22:24:11 +0800523 aead_request_complete(req, err);
524}
525
Huang Ying62c55932009-11-16 21:52:22 +0800526static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000527{
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;
Huang Ying62c55932009-11-16 21:52:22 +0800535 else if (!err) {
536 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
537 gcm_enc_copy_hash(req, pctx);
538 }
Huang Ying9382d972009-08-06 15:34:26 +1000539 }
540
Huang Ying62c55932009-11-16 21:52:22 +0800541 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000542}
543
Mikko Herranen28db8e32007-11-26 22:24:11 +0800544static int crypto_gcm_encrypt(struct aead_request *req)
545{
Herbert Xu25894692007-12-12 19:16:38 +0800546 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000547 struct ablkcipher_request *abreq = &pctx->u.abreq;
548 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu84c91152007-12-17 21:42:08 +0800549 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800550
Herbert Xu84c91152007-12-17 21:42:08 +0800551 crypto_gcm_init_crypt(abreq, req, req->cryptlen);
552 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
Huang Ying9382d972009-08-06 15:34:26 +1000553 gcm_encrypt_done, req);
554
555 gctx->src = req->dst;
556 gctx->cryptlen = req->cryptlen;
557 gctx->complete = gcm_enc_hash_done;
Herbert Xu84c91152007-12-17 21:42:08 +0800558
559 err = crypto_ablkcipher_encrypt(abreq);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800560 if (err)
561 return err;
562
Huang Ying9382d972009-08-06 15:34:26 +1000563 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;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800571}
572
Huang Ying9382d972009-08-06 15:34:26 +1000573static int crypto_gcm_verify(struct aead_request *req,
574 struct crypto_gcm_req_priv_ctx *pctx)
Herbert Xu84c91152007-12-17 21:42:08 +0800575{
576 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800577 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
Huang Ying9382d972009-08-06 15:34:26 +1000582 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xu84c91152007-12-17 21:42:08 +0800583 scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
584 return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
585}
586
Huang Ying9382d972009-08-06 15:34:26 +1000587static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800588{
Herbert Xu84c91152007-12-17 21:42:08 +0800589 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000590 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800591
592 if (!err)
Huang Ying9382d972009-08-06 15:34:26 +1000593 err = crypto_gcm_verify(req, pctx);
Herbert Xu84c91152007-12-17 21:42:08 +0800594
595 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800596}
597
Huang Ying62c55932009-11-16 21:52:22 +0800598static void gcm_dec_hash_done(struct aead_request *req, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000599{
Huang Ying9382d972009-08-06 15:34:26 +1000600 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;
Huang Ying62c55932009-11-16 21:52:22 +0800611 else if (!err)
612 err = crypto_gcm_verify(req, pctx);
Huang Ying9382d972009-08-06 15:34:26 +1000613 }
614
Huang Ying62c55932009-11-16 21:52:22 +0800615 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000616}
617
Mikko Herranen28db8e32007-11-26 22:24:11 +0800618static int crypto_gcm_decrypt(struct aead_request *req)
619{
Herbert Xu6160b282007-12-04 19:17:50 +1100620 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800621 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000622 struct ablkcipher_request *abreq = &pctx->u.abreq;
623 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100624 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000625 unsigned int cryptlen = req->cryptlen;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800626 int err;
627
Herbert Xu6160b282007-12-04 19:17:50 +1100628 if (cryptlen < authsize)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800629 return -EINVAL;
Herbert Xu6160b282007-12-04 19:17:50 +1100630 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800631
Huang Ying9382d972009-08-06 15:34:26 +1000632 gctx->src = req->src;
633 gctx->cryptlen = cryptlen;
634 gctx->complete = gcm_dec_hash_done;
635
636 err = gcm_hash(req, pctx);
637 if (err)
638 return err;
639
Herbert Xu84c91152007-12-17 21:42:08 +0800640 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
Huang Ying9382d972009-08-06 15:34:26 +1000641 gcm_decrypt_done, req);
642 crypto_gcm_init_crypt(abreq, req, cryptlen);
Herbert Xu84c91152007-12-17 21:42:08 +0800643 err = crypto_ablkcipher_decrypt(abreq);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800644 if (err)
645 return err;
646
Huang Ying9382d972009-08-06 15:34:26 +1000647 return crypto_gcm_verify(req, pctx);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800648}
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;
Huang Ying9382d972009-08-06 15:34:26 +1000656 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800657 unsigned long align;
658 int err;
659
Huang Ying9382d972009-08-06 15:34:26 +1000660 ghash = crypto_spawn_ahash(&ictx->ghash);
661 if (IS_ERR(ghash))
662 return PTR_ERR(ghash);
663
Herbert Xu1472e5e2007-12-07 19:26:11 +0800664 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800665 err = PTR_ERR(ctr);
666 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000667 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800668
669 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000670 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800671
Herbert Xu25894692007-12-12 19:16:38 +0800672 align = crypto_tfm_alg_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800673 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu7f681372007-12-06 14:59:53 +0800674 tfm->crt_aead.reqsize = align +
Huang Ying9382d972009-08-06 15:34:26 +1000675 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));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800680
681 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000682
683err_free_hash:
684 crypto_free_ahash(ghash);
685 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800686}
687
688static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
689{
690 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
691
Huang Ying9382d972009-08-06 15:34:26 +1000692 crypto_free_ahash(ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800693 crypto_free_ablkcipher(ctx->ctr);
694}
695
Herbert Xud00aa192007-12-07 20:31:10 +0800696static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
697 const char *full_name,
Huang Ying9382d972009-08-06 15:34:26 +1000698 const char *ctr_name,
699 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800700{
Herbert Xud00aa192007-12-07 20:31:10 +0800701 struct crypto_attr_type *algt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800702 struct crypto_instance *inst;
703 struct crypto_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000704 struct crypto_alg *ghash_alg;
705 struct ahash_alg *ghash_ahash_alg;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800706 struct gcm_instance_ctx *ctx;
707 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800708
Herbert Xud00aa192007-12-07 20:31:10 +0800709 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800710 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100711 return ERR_CAST(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800712
Herbert Xud00aa192007-12-07 20:31:10 +0800713 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
714 return ERR_PTR(-EINVAL);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800715
Huang Ying9382d972009-08-06 15:34:26 +1000716 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
717 CRYPTO_ALG_TYPE_HASH,
718 CRYPTO_ALG_TYPE_AHASH_MASK);
Huang Ying9382d972009-08-06 15:34:26 +1000719 if (IS_ERR(ghash_alg))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100720 return ERR_CAST(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000721
722 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800723 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
724 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000725 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800726
Herbert Xu1472e5e2007-12-07 19:26:11 +0800727 ctx = crypto_instance_ctx(inst);
Huang Ying9382d972009-08-06 15:34:26 +1000728 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
Herbert Xu1472e5e2007-12-07 19:26:11 +0800734 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)
Huang Ying9382d972009-08-06 15:34:26 +1000739 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800740
741 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800742
Herbert Xud00aa192007-12-07 20:31:10 +0800743 /* We only support 16-byte blocks. */
Herbert Xu1472e5e2007-12-07 19:26:11 +0800744 if (ctr->cra_ablkcipher.ivsize != 16)
Herbert Xud00aa192007-12-07 20:31:10 +0800745 goto out_put_ctr;
746
747 /* Not a stream cipher? */
748 err = -EINVAL;
749 if (ctr->cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800750 goto out_put_ctr;
751
Mikko Herranen28db8e32007-11-26 22:24:11 +0800752 err = -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800753 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Huang Ying9382d972009-08-06 15:34:26 +1000754 "gcm_base(%s,%s)", ctr->cra_driver_name,
755 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800756 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800757 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800758
Herbert Xud00aa192007-12-07 20:31:10 +0800759 memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
760
Herbert Xu1472e5e2007-12-07 19:26:11 +0800761 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
762 inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800763 inst->alg.cra_priority = ctr->cra_priority;
Herbert Xud00aa192007-12-07 20:31:10 +0800764 inst->alg.cra_blocksize = 1;
Herbert Xu25894692007-12-12 19:16:38 +0800765 inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800766 inst->alg.cra_type = &crypto_aead_type;
Herbert Xu84c91152007-12-17 21:42:08 +0800767 inst->alg.cra_aead.ivsize = 16;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100768 inst->alg.cra_aead.maxauthsize = 16;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800769 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;
Herbert Xudadbc532007-12-17 15:33:17 +0800773 inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800774 inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
775 inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
776
777out:
Huang Ying9382d972009-08-06 15:34:26 +1000778 crypto_mod_put(ghash_alg);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800779 return inst;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800780
781out_put_ctr:
782 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000783err_drop_ghash:
784 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800785err_free_inst:
786 kfree(inst);
Huang Ying9382d972009-08-06 15:34:26 +1000787out_put_ghash:
Mikko Herranen28db8e32007-11-26 22:24:11 +0800788 inst = ERR_PTR(err);
789 goto out;
790}
791
Herbert Xud00aa192007-12-07 20:31:10 +0800792static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
793{
Herbert Xud00aa192007-12-07 20:31:10 +0800794 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]);
Herbert Xud00aa192007-12-07 20:31:10 +0800799 if (IS_ERR(cipher_name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100800 return ERR_CAST(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800801
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
Huang Ying9382d972009-08-06 15:34:26 +1000810 return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800811}
812
Mikko Herranen28db8e32007-11-26 22:24:11 +0800813static void crypto_gcm_free(struct crypto_instance *inst)
814{
815 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
816
Herbert Xu1472e5e2007-12-07 19:26:11 +0800817 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000818 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800819 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
Herbert Xud00aa192007-12-07 20:31:10 +0800829static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
830{
Herbert Xud00aa192007-12-07 20:31:10 +0800831 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000832 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800833 char full_name[CRYPTO_MAX_ALG_NAME];
834
835 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800836 if (IS_ERR(ctr_name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100837 return ERR_CAST(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800838
Huang Ying9382d972009-08-06 15:34:26 +1000839 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000840 if (IS_ERR(ghash_name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100841 return ERR_CAST(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000842
843 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
844 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xud00aa192007-12-07 20:31:10 +0800845 return ERR_PTR(-ENAMETOOLONG);
846
Huang Ying9382d972009-08-06 15:34:26 +1000847 return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800848}
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
Herbert Xudadbc532007-12-17 15:33:17 +0800857static 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);
Herbert Xudadbc532007-12-17 15:33:17 +0800973 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100974 return ERR_CAST(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800975
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]);
Herbert Xudadbc532007-12-17 15:33:17 +0800980 if (IS_ERR(ccm_name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100981 return ERR_CAST(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800982
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
Tobias Brunner73c89c12010-01-17 21:52:11 +11001060static 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
Jussi Kivilinnad733ac92013-04-07 16:43:46 +03001102static void crypto_rfc4543_done(struct crypto_async_request *areq, int err)
1103{
1104 struct aead_request *req = areq->data;
1105 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1106 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1107
1108 if (!err) {
1109 scatterwalk_map_and_copy(rctx->auth_tag, req->dst,
1110 req->cryptlen,
1111 crypto_aead_authsize(aead), 1);
1112 }
1113
1114 aead_request_complete(req, err);
1115}
1116
Tobias Brunner73c89c12010-01-17 21:52:11 +11001117static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req,
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001118 bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001119{
1120 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1121 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1122 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1123 struct aead_request *subreq = &rctx->subreq;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001124 struct scatterlist *src = req->src;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125 struct scatterlist *cipher = rctx->cipher;
1126 struct scatterlist *payload = rctx->payload;
1127 struct scatterlist *assoc = rctx->assoc;
1128 unsigned int authsize = crypto_aead_authsize(aead);
1129 unsigned int assoclen = req->assoclen;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001130 struct page *srcp;
1131 u8 *vsrc;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001132 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1133 crypto_aead_alignmask(ctx->child) + 1);
1134
1135 memcpy(iv, ctx->nonce, 4);
1136 memcpy(iv + 4, req->iv, 8);
1137
1138 /* construct cipher/plaintext */
1139 if (enc)
1140 memset(rctx->auth_tag, 0, authsize);
1141 else
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001142 scatterwalk_map_and_copy(rctx->auth_tag, src,
Tobias Brunner73c89c12010-01-17 21:52:11 +11001143 req->cryptlen - authsize,
1144 authsize, 0);
1145
1146 sg_init_one(cipher, rctx->auth_tag, authsize);
1147
1148 /* construct the aad */
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001149 srcp = sg_page(src);
1150 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001151
1152 sg_init_table(payload, 2);
1153 sg_set_buf(payload, req->iv, 8);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001154 scatterwalk_crypto_chain(payload, src, vsrc == req->iv + 8, 2);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001155 assoclen += 8 + req->cryptlen - (enc ? 0 : authsize);
1156
1157 sg_init_table(assoc, 2);
1158 sg_set_page(assoc, sg_page(req->assoc), req->assoc->length,
1159 req->assoc->offset);
Steffen Klassertc920fa62010-11-22 11:26:54 +01001160 scatterwalk_crypto_chain(assoc, payload, 0, 2);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001161
1162 aead_request_set_tfm(subreq, ctx->child);
Jussi Kivilinnad733ac92013-04-07 16:43:46 +03001163 aead_request_set_callback(subreq, req->base.flags, crypto_rfc4543_done,
1164 req);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001165 aead_request_set_crypt(subreq, cipher, cipher, enc ? 0 : authsize, iv);
1166 aead_request_set_assoc(subreq, assoc, assoclen);
1167
1168 return subreq;
1169}
1170
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001171static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1172{
1173 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1174 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1175 unsigned int authsize = crypto_aead_authsize(aead);
1176 unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize);
1177 struct blkcipher_desc desc = {
1178 .tfm = ctx->null,
1179 };
1180
1181 return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
1182}
1183
Tobias Brunner73c89c12010-01-17 21:52:11 +11001184static int crypto_rfc4543_encrypt(struct aead_request *req)
1185{
1186 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1187 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1188 struct aead_request *subreq;
1189 int err;
1190
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001191 if (req->src != req->dst) {
1192 err = crypto_rfc4543_copy_src_to_dst(req, true);
1193 if (err)
1194 return err;
1195 }
1196
1197 subreq = crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001198 err = crypto_aead_encrypt(subreq);
1199 if (err)
1200 return err;
1201
1202 scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
1203 crypto_aead_authsize(aead), 1);
1204
1205 return 0;
1206}
1207
1208static int crypto_rfc4543_decrypt(struct aead_request *req)
1209{
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001210 int err;
1211
1212 if (req->src != req->dst) {
1213 err = crypto_rfc4543_copy_src_to_dst(req, false);
1214 if (err)
1215 return err;
1216 }
1217
1218 req = crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001219
1220 return crypto_aead_decrypt(req);
1221}
1222
1223static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm)
1224{
1225 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001226 struct crypto_rfc4543_instance_ctx *ictx = crypto_instance_ctx(inst);
1227 struct crypto_aead_spawn *spawn = &ictx->aead;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001228 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1229 struct crypto_aead *aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001230 struct crypto_blkcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001231 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001232 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001233
1234 aead = crypto_spawn_aead(spawn);
1235 if (IS_ERR(aead))
1236 return PTR_ERR(aead);
1237
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001238 null = crypto_spawn_blkcipher(&ictx->null.base);
1239 err = PTR_ERR(null);
1240 if (IS_ERR(null))
1241 goto err_free_aead;
1242
Tobias Brunner73c89c12010-01-17 21:52:11 +11001243 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001244 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001245
1246 align = crypto_aead_alignmask(aead);
1247 align &= ~(crypto_tfm_ctx_alignment() - 1);
1248 tfm->crt_aead.reqsize = sizeof(struct crypto_rfc4543_req_ctx) +
1249 ALIGN(crypto_aead_reqsize(aead),
1250 crypto_tfm_ctx_alignment()) +
1251 align + 16;
1252
1253 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001254
1255err_free_aead:
1256 crypto_free_aead(aead);
1257 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001258}
1259
1260static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm)
1261{
1262 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1263
1264 crypto_free_aead(ctx->child);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001265 crypto_free_blkcipher(ctx->null);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001266}
1267
1268static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb)
1269{
1270 struct crypto_attr_type *algt;
1271 struct crypto_instance *inst;
1272 struct crypto_aead_spawn *spawn;
1273 struct crypto_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001274 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001275 const char *ccm_name;
1276 int err;
1277
1278 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001279 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +01001280 return ERR_CAST(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001281
1282 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1283 return ERR_PTR(-EINVAL);
1284
1285 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001286 if (IS_ERR(ccm_name))
Julia Lawall3e8afe32013-01-22 12:29:26 +01001287 return ERR_CAST(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001288
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001289 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001290 if (!inst)
1291 return ERR_PTR(-ENOMEM);
1292
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001293 ctx = crypto_instance_ctx(inst);
1294 spawn = &ctx->aead;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001295 crypto_set_aead_spawn(spawn, inst);
1296 err = crypto_grab_aead(spawn, ccm_name, 0,
1297 crypto_requires_sync(algt->type, algt->mask));
1298 if (err)
1299 goto out_free_inst;
1300
1301 alg = crypto_aead_spawn_alg(spawn);
1302
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001303 crypto_set_skcipher_spawn(&ctx->null, inst);
1304 err = crypto_grab_skcipher(&ctx->null, "ecb(cipher_null)", 0,
1305 CRYPTO_ALG_ASYNC);
1306 if (err)
1307 goto out_drop_alg;
1308
1309 crypto_skcipher_spawn_alg(&ctx->null);
1310
Tobias Brunner73c89c12010-01-17 21:52:11 +11001311 err = -EINVAL;
1312
1313 /* We only support 16-byte blocks. */
1314 if (alg->cra_aead.ivsize != 16)
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001315 goto out_drop_ecbnull;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001316
1317 /* Not a stream cipher? */
1318 if (alg->cra_blocksize != 1)
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001319 goto out_drop_ecbnull;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001320
1321 err = -ENAMETOOLONG;
1322 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1323 "rfc4543(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1324 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1325 "rfc4543(%s)", alg->cra_driver_name) >=
1326 CRYPTO_MAX_ALG_NAME)
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001327 goto out_drop_ecbnull;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001328
1329 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1330 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1331 inst->alg.cra_priority = alg->cra_priority;
1332 inst->alg.cra_blocksize = 1;
1333 inst->alg.cra_alignmask = alg->cra_alignmask;
1334 inst->alg.cra_type = &crypto_nivaead_type;
1335
1336 inst->alg.cra_aead.ivsize = 8;
1337 inst->alg.cra_aead.maxauthsize = 16;
1338
1339 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1340
1341 inst->alg.cra_init = crypto_rfc4543_init_tfm;
1342 inst->alg.cra_exit = crypto_rfc4543_exit_tfm;
1343
1344 inst->alg.cra_aead.setkey = crypto_rfc4543_setkey;
1345 inst->alg.cra_aead.setauthsize = crypto_rfc4543_setauthsize;
1346 inst->alg.cra_aead.encrypt = crypto_rfc4543_encrypt;
1347 inst->alg.cra_aead.decrypt = crypto_rfc4543_decrypt;
1348
1349 inst->alg.cra_aead.geniv = "seqiv";
1350
1351out:
1352 return inst;
1353
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001354out_drop_ecbnull:
1355 crypto_drop_skcipher(&ctx->null);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001356out_drop_alg:
1357 crypto_drop_aead(spawn);
1358out_free_inst:
1359 kfree(inst);
1360 inst = ERR_PTR(err);
1361 goto out;
1362}
1363
1364static void crypto_rfc4543_free(struct crypto_instance *inst)
1365{
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001366 struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst);
1367
1368 crypto_drop_aead(&ctx->aead);
1369 crypto_drop_skcipher(&ctx->null);
1370
Tobias Brunner73c89c12010-01-17 21:52:11 +11001371 kfree(inst);
1372}
1373
1374static struct crypto_template crypto_rfc4543_tmpl = {
1375 .name = "rfc4543",
1376 .alloc = crypto_rfc4543_alloc,
1377 .free = crypto_rfc4543_free,
1378 .module = THIS_MODULE,
1379};
1380
Mikko Herranen28db8e32007-11-26 22:24:11 +08001381static int __init crypto_gcm_module_init(void)
1382{
Herbert Xud00aa192007-12-07 20:31:10 +08001383 int err;
1384
Huang Ying9382d972009-08-06 15:34:26 +10001385 gcm_zeroes = kzalloc(16, GFP_KERNEL);
1386 if (!gcm_zeroes)
1387 return -ENOMEM;
1388
Herbert Xud00aa192007-12-07 20:31:10 +08001389 err = crypto_register_template(&crypto_gcm_base_tmpl);
1390 if (err)
1391 goto out;
1392
1393 err = crypto_register_template(&crypto_gcm_tmpl);
1394 if (err)
1395 goto out_undo_base;
1396
Herbert Xudadbc532007-12-17 15:33:17 +08001397 err = crypto_register_template(&crypto_rfc4106_tmpl);
1398 if (err)
1399 goto out_undo_gcm;
1400
Tobias Brunner73c89c12010-01-17 21:52:11 +11001401 err = crypto_register_template(&crypto_rfc4543_tmpl);
1402 if (err)
1403 goto out_undo_rfc4106;
1404
Huang Ying9382d972009-08-06 15:34:26 +10001405 return 0;
Herbert Xud00aa192007-12-07 20:31:10 +08001406
Tobias Brunner73c89c12010-01-17 21:52:11 +11001407out_undo_rfc4106:
1408 crypto_unregister_template(&crypto_rfc4106_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001409out_undo_gcm:
1410 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001411out_undo_base:
1412 crypto_unregister_template(&crypto_gcm_base_tmpl);
Huang Ying9382d972009-08-06 15:34:26 +10001413out:
1414 kfree(gcm_zeroes);
1415 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001416}
1417
1418static void __exit crypto_gcm_module_exit(void)
1419{
Huang Ying9382d972009-08-06 15:34:26 +10001420 kfree(gcm_zeroes);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001421 crypto_unregister_template(&crypto_rfc4543_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001422 crypto_unregister_template(&crypto_rfc4106_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001423 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001424 crypto_unregister_template(&crypto_gcm_base_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001425}
1426
1427module_init(crypto_gcm_module_init);
1428module_exit(crypto_gcm_module_exit);
1429
1430MODULE_LICENSE("GPL");
1431MODULE_DESCRIPTION("Galois/Counter Mode");
1432MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Herbert Xud00aa192007-12-07 20:31:10 +08001433MODULE_ALIAS("gcm_base");
Herbert Xudadbc532007-12-17 15:33:17 +08001434MODULE_ALIAS("rfc4106");
Tobias Brunner73c89c12010-01-17 21:52:11 +11001435MODULE_ALIAS("rfc4543");