blob: ab0b2f9e8fad707c8010c8f0b6c9817fe0f9e80c [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 Xu17db8542015-05-21 15:11:11 +080015#include <crypto/null.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080016#include <crypto/scatterwalk.h>
Huang Ying9382d972009-08-06 15:34:26 +100017#include <crypto/hash.h>
18#include "internal.h"
Herbert Xu84c91152007-12-17 21:42:08 +080019#include <linux/completion.h>
Mikko Herranen28db8e32007-11-26 22:24:11 +080020#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25
Mikko Herranen28db8e32007-11-26 22:24:11 +080026struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080027 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100028 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080029};
30
31struct crypto_gcm_ctx {
32 struct crypto_ablkcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100033 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080034};
35
Herbert Xudadbc532007-12-17 15:33:17 +080036struct crypto_rfc4106_ctx {
37 struct crypto_aead *child;
38 u8 nonce[4];
39};
40
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030041struct crypto_rfc4543_instance_ctx {
42 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030043};
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 {
Tobias Brunner73c89c12010-01-17 21:52:11 +110052 struct aead_request subreq;
53};
54
Mikko Herranen28db8e32007-11-26 22:24:11 +080055struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100056 unsigned int cryptlen;
57 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080058 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080059};
60
61struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080062 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080063 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110064 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080065 struct scatterlist src[3];
66 struct scatterlist dst[3];
67 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100068 struct crypto_gcm_ghash_ctx ghash_ctx;
69 union {
70 struct ahash_request ahreq;
71 struct ablkcipher_request abreq;
72 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080073};
74
Herbert Xu84c91152007-12-17 21:42:08 +080075struct crypto_gcm_setkey_result {
76 int err;
77 struct completion completion;
78};
79
Herbert Xuadcbc682015-06-16 13:54:18 +080080static struct {
81 u8 buf[16];
82 struct scatterlist sg;
83} *gcm_zeroes;
84
85static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100086
Herbert Xu25894692007-12-12 19:16:38 +080087static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
88 struct aead_request *req)
89{
90 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
91
92 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
93}
94
Herbert Xu84c91152007-12-17 21:42:08 +080095static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +080096{
Herbert Xu84c91152007-12-17 21:42:08 +080097 struct crypto_gcm_setkey_result *result = req->data;
Mikko Herranen28db8e32007-11-26 22:24:11 +080098
Herbert Xu84c91152007-12-17 21:42:08 +080099 if (err == -EINPROGRESS)
100 return;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800101
Herbert Xu84c91152007-12-17 21:42:08 +0800102 result->err = err;
103 complete(&result->completion);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800104}
105
106static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
107 unsigned int keylen)
108{
109 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000110 struct crypto_ahash *ghash = ctx->ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800111 struct crypto_ablkcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +0800112 struct {
113 be128 hash;
114 u8 iv[8];
115
116 struct crypto_gcm_setkey_result result;
117
118 struct scatterlist sg[1];
119 struct ablkcipher_request req;
120 } *data;
121 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800122
123 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
124 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
Herbert Xuadcbc682015-06-16 13:54:18 +0800125 CRYPTO_TFM_REQ_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800126 err = crypto_ablkcipher_setkey(ctr, key, keylen);
Herbert Xuadcbc682015-06-16 13:54:18 +0800127 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
128 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800129 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800130 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800131
Herbert Xu84c91152007-12-17 21:42:08 +0800132 data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr),
133 GFP_KERNEL);
134 if (!data)
135 return -ENOMEM;
136
137 init_completion(&data->result.completion);
138 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
139 ablkcipher_request_set_tfm(&data->req, ctr);
140 ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
141 CRYPTO_TFM_REQ_MAY_BACKLOG,
142 crypto_gcm_setkey_done,
143 &data->result);
144 ablkcipher_request_set_crypt(&data->req, data->sg, data->sg,
145 sizeof(data->hash), data->iv);
146
147 err = crypto_ablkcipher_encrypt(&data->req);
148 if (err == -EINPROGRESS || err == -EBUSY) {
149 err = wait_for_completion_interruptible(
150 &data->result.completion);
151 if (!err)
152 err = data->result.err;
153 }
154
Mikko Herranen28db8e32007-11-26 22:24:11 +0800155 if (err)
156 goto out;
157
Huang Ying9382d972009-08-06 15:34:26 +1000158 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
159 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
160 CRYPTO_TFM_REQ_MASK);
161 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
162 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
163 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800164
Herbert Xu84c91152007-12-17 21:42:08 +0800165out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800166 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800167 return err;
168}
169
Herbert Xudadbc532007-12-17 15:33:17 +0800170static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
171 unsigned int authsize)
172{
173 switch (authsize) {
174 case 4:
175 case 8:
176 case 12:
177 case 13:
178 case 14:
179 case 15:
180 case 16:
181 break;
182 default:
183 return -EINVAL;
184 }
185
186 return 0;
187}
188
Herbert Xuadcbc682015-06-16 13:54:18 +0800189static void crypto_gcm_init_common(struct aead_request *req)
190{
191 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
192 __be32 counter = cpu_to_be32(1);
193 struct scatterlist *sg;
194
195 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
196 memcpy(pctx->iv, req->iv, 12);
197 memcpy(pctx->iv + 12, &counter, 4);
198
199 sg_init_table(pctx->src, 3);
200 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
201 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
202 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200203 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800204
205 if (req->src != req->dst) {
206 sg_init_table(pctx->dst, 3);
207 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
208 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
209 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200210 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800211 }
212}
213
214static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800215 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800216{
217 struct crypto_aead *aead = crypto_aead_reqtfm(req);
218 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800219 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800220 struct ablkcipher_request *ablk_req = &pctx->u.abreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800221 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800222
Herbert Xuadcbc682015-06-16 13:54:18 +0800223 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800224
225 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
Herbert Xu84c91152007-12-17 21:42:08 +0800226 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
227 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800228 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800229}
230
Huang Ying9382d972009-08-06 15:34:26 +1000231static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800232{
Huang Ying9382d972009-08-06 15:34:26 +1000233 len &= 0xfU;
234 return len ? 16 - len : 0;
235}
236
237static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000238
239static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700240 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000241 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800242 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000243{
Herbert Xuadcbc682015-06-16 13:54:18 +0800244 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000245 struct ahash_request *ahreq = &pctx->u.ahreq;
246
Herbert Xuadcbc682015-06-16 13:54:18 +0800247 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000248 ahash_request_set_crypt(ahreq, src, NULL, len);
249
250 return crypto_ahash_update(ahreq);
251}
252
253static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000254 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800255 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000256{
Herbert Xuadcbc682015-06-16 13:54:18 +0800257 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000258}
259
Herbert Xuadcbc682015-06-16 13:54:18 +0800260static int gcm_hash_len(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000261{
Herbert Xuadcbc682015-06-16 13:54:18 +0800262 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000263 struct ahash_request *ahreq = &pctx->u.ahreq;
264 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
265 u128 lengths;
266
267 lengths.a = cpu_to_be64(req->assoclen * 8);
268 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
269 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800270 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
271 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
272 ahash_request_set_crypt(ahreq, &pctx->sg,
273 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000274
Herbert Xuadcbc682015-06-16 13:54:18 +0800275 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000276}
277
Herbert Xuadcbc682015-06-16 13:54:18 +0800278static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000279{
Herbert Xu25894692007-12-12 19:16:38 +0800280 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000281 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800282
Herbert Xuadcbc682015-06-16 13:54:18 +0800283 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000284}
285
Huang Ying62c55932009-11-16 21:52:22 +0800286static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000287{
288 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800289
Herbert Xuadcbc682015-06-16 13:54:18 +0800290 if (err)
291 goto out;
292
293 err = gcm_hash_len_continue(req, 0);
294 if (err == -EINPROGRESS)
295 return;
296
297out:
298 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800299}
300
Herbert Xuadcbc682015-06-16 13:54:18 +0800301static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800302{
Herbert Xuadcbc682015-06-16 13:54:18 +0800303 return gcm_hash_len(req, flags) ?:
304 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000305}
306
Huang Ying62c55932009-11-16 21:52:22 +0800307static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
308 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000309{
310 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800311
Herbert Xuadcbc682015-06-16 13:54:18 +0800312 if (err)
313 goto out;
314
315 err = gcm_hash_crypt_remain_continue(req, 0);
316 if (err == -EINPROGRESS)
317 return;
318
319out:
320 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800321}
322
Herbert Xuadcbc682015-06-16 13:54:18 +0800323static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800324{
Huang Ying9382d972009-08-06 15:34:26 +1000325 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
326 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
327 unsigned int remain;
328
Herbert Xuadcbc682015-06-16 13:54:18 +0800329 remain = gcm_remain(gctx->cryptlen);
330 if (remain)
331 return gcm_hash_remain(req, remain,
332 gcm_hash_crypt_remain_done, flags) ?:
333 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000334
Herbert Xuadcbc682015-06-16 13:54:18 +0800335 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000336}
337
Huang Ying62c55932009-11-16 21:52:22 +0800338static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000339{
340 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800341
Herbert Xuadcbc682015-06-16 13:54:18 +0800342 if (err)
343 goto out;
344
345 err = gcm_hash_crypt_continue(req, 0);
346 if (err == -EINPROGRESS)
347 return;
348
349out:
350 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800351}
352
Herbert Xuadcbc682015-06-16 13:54:18 +0800353static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800354{
Huang Ying9382d972009-08-06 15:34:26 +1000355 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
356 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000357
Herbert Xuadcbc682015-06-16 13:54:18 +0800358 if (gctx->cryptlen)
359 return gcm_hash_update(req, gcm_hash_crypt_done,
360 gctx->src, gctx->cryptlen, flags) ?:
361 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000362
Herbert Xuadcbc682015-06-16 13:54:18 +0800363 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000364}
365
Huang Ying62c55932009-11-16 21:52:22 +0800366static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
367 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000368{
369 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800370
Herbert Xuadcbc682015-06-16 13:54:18 +0800371 if (err)
372 goto out;
373
374 err = gcm_hash_assoc_remain_continue(req, 0);
375 if (err == -EINPROGRESS)
376 return;
377
378out:
379 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800380}
381
Herbert Xuadcbc682015-06-16 13:54:18 +0800382static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800383{
Huang Ying9382d972009-08-06 15:34:26 +1000384 unsigned int remain;
385
Herbert Xuadcbc682015-06-16 13:54:18 +0800386 remain = gcm_remain(req->assoclen);
387 if (remain)
388 return gcm_hash_remain(req, remain,
389 gcm_hash_assoc_remain_done, flags) ?:
390 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000391
Herbert Xuadcbc682015-06-16 13:54:18 +0800392 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000393}
394
Huang Ying62c55932009-11-16 21:52:22 +0800395static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000396{
397 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800398
Herbert Xuadcbc682015-06-16 13:54:18 +0800399 if (err)
400 goto out;
401
402 err = gcm_hash_assoc_continue(req, 0);
403 if (err == -EINPROGRESS)
404 return;
405
406out:
407 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800408}
409
Herbert Xuadcbc682015-06-16 13:54:18 +0800410static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800411{
Herbert Xuadcbc682015-06-16 13:54:18 +0800412 if (req->assoclen)
413 return gcm_hash_update(req, gcm_hash_assoc_done,
414 req->src, req->assoclen, flags) ?:
415 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000416
Herbert Xuadcbc682015-06-16 13:54:18 +0800417 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800418}
419
420static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
421{
422 struct aead_request *req = areq->data;
423
Herbert Xuadcbc682015-06-16 13:54:18 +0800424 if (err)
425 goto out;
426
427 err = gcm_hash_init_continue(req, 0);
428 if (err == -EINPROGRESS)
429 return;
430
431out:
432 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000433}
434
Herbert Xuadcbc682015-06-16 13:54:18 +0800435static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000436{
Herbert Xuadcbc682015-06-16 13:54:18 +0800437 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000438 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800439 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000440
441 ahash_request_set_tfm(ahreq, ctx->ghash);
442
Herbert Xuadcbc682015-06-16 13:54:18 +0800443 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
444 return crypto_ahash_init(ahreq) ?:
445 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100446}
447
Herbert Xuadcbc682015-06-16 13:54:18 +0800448static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000449{
Herbert Xuadcbc682015-06-16 13:54:18 +0800450 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000451 struct crypto_aead *aead = crypto_aead_reqtfm(req);
452 u8 *auth_tag = pctx->auth_tag;
453
Herbert Xuadcbc682015-06-16 13:54:18 +0800454 crypto_xor(auth_tag, pctx->iauth_tag, 16);
455 scatterwalk_map_and_copy(auth_tag, req->dst,
456 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000457 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800458 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000459}
460
Herbert Xuadcbc682015-06-16 13:54:18 +0800461static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100462{
Huang Ying9382d972009-08-06 15:34:26 +1000463 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800464 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100465
Herbert Xuadcbc682015-06-16 13:54:18 +0800466 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
467 gctx->cryptlen = req->cryptlen;
468 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100469
Herbert Xuadcbc682015-06-16 13:54:18 +0800470 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800471}
472
Huang Ying62c55932009-11-16 21:52:22 +0800473static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000474{
475 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000476
Herbert Xuadcbc682015-06-16 13:54:18 +0800477 if (err)
478 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000479
Herbert Xuadcbc682015-06-16 13:54:18 +0800480 err = gcm_encrypt_continue(req, 0);
481 if (err == -EINPROGRESS)
482 return;
483
484out:
Huang Ying62c55932009-11-16 21:52:22 +0800485 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000486}
487
Mikko Herranen28db8e32007-11-26 22:24:11 +0800488static int crypto_gcm_encrypt(struct aead_request *req)
489{
Herbert Xu25894692007-12-12 19:16:38 +0800490 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000491 struct ablkcipher_request *abreq = &pctx->u.abreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800492 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800493
Herbert Xuadcbc682015-06-16 13:54:18 +0800494 crypto_gcm_init_common(req);
495 crypto_gcm_init_crypt(req, req->cryptlen);
496 ablkcipher_request_set_callback(abreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000497
Herbert Xuadcbc682015-06-16 13:54:18 +0800498 return crypto_ablkcipher_encrypt(abreq) ?:
499 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800500}
501
Herbert Xuadcbc682015-06-16 13:54:18 +0800502static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800503{
Herbert Xuadcbc682015-06-16 13:54:18 +0800504 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800505 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800506 u8 *auth_tag = pctx->auth_tag;
507 u8 *iauth_tag = pctx->iauth_tag;
508 unsigned int authsize = crypto_aead_authsize(aead);
509 unsigned int cryptlen = req->cryptlen - authsize;
510
Huang Ying9382d972009-08-06 15:34:26 +1000511 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800512 scatterwalk_map_and_copy(iauth_tag, req->src,
513 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600514 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800515}
516
Huang Ying9382d972009-08-06 15:34:26 +1000517static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800518{
Herbert Xu84c91152007-12-17 21:42:08 +0800519 struct aead_request *req = areq->data;
520
521 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800522 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800523
524 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800525}
526
Herbert Xuadcbc682015-06-16 13:54:18 +0800527static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000528{
Huang Ying9382d972009-08-06 15:34:26 +1000529 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
530 struct ablkcipher_request *abreq = &pctx->u.abreq;
531 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
532
Herbert Xuadcbc682015-06-16 13:54:18 +0800533 crypto_gcm_init_crypt(req, gctx->cryptlen);
534 ablkcipher_request_set_callback(abreq, flags, gcm_decrypt_done, req);
535 return crypto_ablkcipher_decrypt(abreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000536}
537
Mikko Herranen28db8e32007-11-26 22:24:11 +0800538static int crypto_gcm_decrypt(struct aead_request *req)
539{
Herbert Xu6160b282007-12-04 19:17:50 +1100540 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800541 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000542 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100543 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000544 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800545 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800546
Herbert Xu6160b282007-12-04 19:17:50 +1100547 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800548
Herbert Xuadcbc682015-06-16 13:54:18 +0800549 crypto_gcm_init_common(req);
550
551 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000552 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800553 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000554
Herbert Xuadcbc682015-06-16 13:54:18 +0800555 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800556}
557
Herbert Xuadcbc682015-06-16 13:54:18 +0800558static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800559{
Herbert Xuadcbc682015-06-16 13:54:18 +0800560 struct aead_instance *inst = aead_alg_instance(tfm);
561 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
562 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800563 struct crypto_ablkcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000564 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800565 unsigned long align;
566 int err;
567
Huang Ying9382d972009-08-06 15:34:26 +1000568 ghash = crypto_spawn_ahash(&ictx->ghash);
569 if (IS_ERR(ghash))
570 return PTR_ERR(ghash);
571
Herbert Xu1472e5e2007-12-07 19:26:11 +0800572 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800573 err = PTR_ERR(ctr);
574 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000575 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800576
577 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000578 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800579
Herbert Xuadcbc682015-06-16 13:54:18 +0800580 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800581 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800582 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800583 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Huang Ying9382d972009-08-06 15:34:26 +1000584 max(sizeof(struct ablkcipher_request) +
585 crypto_ablkcipher_reqsize(ctr),
586 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800587 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800588
589 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000590
591err_free_hash:
592 crypto_free_ahash(ghash);
593 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800594}
595
Herbert Xuadcbc682015-06-16 13:54:18 +0800596static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800597{
Herbert Xuadcbc682015-06-16 13:54:18 +0800598 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800599
Huang Ying9382d972009-08-06 15:34:26 +1000600 crypto_free_ahash(ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800601 crypto_free_ablkcipher(ctx->ctr);
602}
603
Herbert Xuadcbc682015-06-16 13:54:18 +0800604static int crypto_gcm_create_common(struct crypto_template *tmpl,
605 struct rtattr **tb,
606 const char *full_name,
607 const char *ctr_name,
608 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800609{
Herbert Xud00aa192007-12-07 20:31:10 +0800610 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800611 struct aead_instance *inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800612 struct crypto_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000613 struct crypto_alg *ghash_alg;
Herbert Xuadcbc682015-06-16 13:54:18 +0800614 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800615 struct gcm_instance_ctx *ctx;
616 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800617
Herbert Xud00aa192007-12-07 20:31:10 +0800618 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800619 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800620 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800621
Herbert Xud00aa192007-12-07 20:31:10 +0800622 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800623 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800624
Huang Ying9382d972009-08-06 15:34:26 +1000625 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
626 CRYPTO_ALG_TYPE_HASH,
627 CRYPTO_ALG_TYPE_AHASH_MASK);
Huang Ying9382d972009-08-06 15:34:26 +1000628 if (IS_ERR(ghash_alg))
Herbert Xuadcbc682015-06-16 13:54:18 +0800629 return PTR_ERR(ghash_alg);
630
631 ghash = __crypto_hash_alg_common(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000632
633 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800634 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
635 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000636 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800637
Herbert Xuadcbc682015-06-16 13:54:18 +0800638 ctx = aead_instance_ctx(inst);
639 err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
640 aead_crypto_instance(inst));
Huang Ying9382d972009-08-06 15:34:26 +1000641 if (err)
642 goto err_free_inst;
643
Herbert Xuadcbc682015-06-16 13:54:18 +0800644 err = -EINVAL;
645 if (ghash->digestsize != 16)
646 goto err_drop_ghash;
647
648 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
Herbert Xu1472e5e2007-12-07 19:26:11 +0800649 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
650 crypto_requires_sync(algt->type,
651 algt->mask));
652 if (err)
Huang Ying9382d972009-08-06 15:34:26 +1000653 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800654
655 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800656
Herbert Xud00aa192007-12-07 20:31:10 +0800657 /* We only support 16-byte blocks. */
Herbert Xu1472e5e2007-12-07 19:26:11 +0800658 if (ctr->cra_ablkcipher.ivsize != 16)
Herbert Xud00aa192007-12-07 20:31:10 +0800659 goto out_put_ctr;
660
661 /* Not a stream cipher? */
662 err = -EINVAL;
663 if (ctr->cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800664 goto out_put_ctr;
665
Mikko Herranen28db8e32007-11-26 22:24:11 +0800666 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800667 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Huang Ying9382d972009-08-06 15:34:26 +1000668 "gcm_base(%s,%s)", ctr->cra_driver_name,
669 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800670 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800671 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800672
Herbert Xuadcbc682015-06-16 13:54:18 +0800673 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
Herbert Xud00aa192007-12-07 20:31:10 +0800674
Herbert Xuadcbc682015-06-16 13:54:18 +0800675 inst->alg.base.cra_flags = (ghash->base.cra_flags | ctr->cra_flags) &
676 CRYPTO_ALG_ASYNC;
677 inst->alg.base.cra_priority = (ghash->base.cra_priority +
678 ctr->cra_priority) / 2;
679 inst->alg.base.cra_blocksize = 1;
680 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
681 ctr->cra_alignmask;
682 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
683 inst->alg.ivsize = 12;
684 inst->alg.maxauthsize = 16;
685 inst->alg.init = crypto_gcm_init_tfm;
686 inst->alg.exit = crypto_gcm_exit_tfm;
687 inst->alg.setkey = crypto_gcm_setkey;
688 inst->alg.setauthsize = crypto_gcm_setauthsize;
689 inst->alg.encrypt = crypto_gcm_encrypt;
690 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800691
Herbert Xuadcbc682015-06-16 13:54:18 +0800692 err = aead_register_instance(tmpl, inst);
693 if (err)
694 goto out_put_ctr;
695
696out_put_ghash:
Huang Ying9382d972009-08-06 15:34:26 +1000697 crypto_mod_put(ghash_alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800698 return err;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800699
700out_put_ctr:
701 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000702err_drop_ghash:
703 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800704err_free_inst:
705 kfree(inst);
Herbert Xuadcbc682015-06-16 13:54:18 +0800706 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800707}
708
Herbert Xuadcbc682015-06-16 13:54:18 +0800709static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800710{
Herbert Xud00aa192007-12-07 20:31:10 +0800711 const char *cipher_name;
712 char ctr_name[CRYPTO_MAX_ALG_NAME];
713 char full_name[CRYPTO_MAX_ALG_NAME];
714
715 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800716 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800717 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800718
719 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
720 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800721 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800722
723 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
724 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800725 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800726
Herbert Xuadcbc682015-06-16 13:54:18 +0800727 return crypto_gcm_create_common(tmpl, tb, full_name,
728 ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800729}
730
Mikko Herranen28db8e32007-11-26 22:24:11 +0800731static void crypto_gcm_free(struct crypto_instance *inst)
732{
733 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
734
Herbert Xu1472e5e2007-12-07 19:26:11 +0800735 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000736 crypto_drop_ahash(&ctx->ghash);
Herbert Xuadcbc682015-06-16 13:54:18 +0800737 kfree(aead_instance(inst));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800738}
739
740static struct crypto_template crypto_gcm_tmpl = {
741 .name = "gcm",
Herbert Xuadcbc682015-06-16 13:54:18 +0800742 .create = crypto_gcm_create,
Mikko Herranen28db8e32007-11-26 22:24:11 +0800743 .free = crypto_gcm_free,
744 .module = THIS_MODULE,
745};
746
Herbert Xuadcbc682015-06-16 13:54:18 +0800747static int crypto_gcm_base_create(struct crypto_template *tmpl,
748 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800749{
Herbert Xud00aa192007-12-07 20:31:10 +0800750 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000751 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800752 char full_name[CRYPTO_MAX_ALG_NAME];
753
754 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800755 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800756 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800757
Huang Ying9382d972009-08-06 15:34:26 +1000758 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000759 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800760 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000761
762 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
763 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800764 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800765
Herbert Xuadcbc682015-06-16 13:54:18 +0800766 return crypto_gcm_create_common(tmpl, tb, full_name,
767 ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800768}
769
770static struct crypto_template crypto_gcm_base_tmpl = {
771 .name = "gcm_base",
Herbert Xuadcbc682015-06-16 13:54:18 +0800772 .create = crypto_gcm_base_create,
Herbert Xud00aa192007-12-07 20:31:10 +0800773 .free = crypto_gcm_free,
774 .module = THIS_MODULE,
775};
776
Herbert Xudadbc532007-12-17 15:33:17 +0800777static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
778 unsigned int keylen)
779{
780 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
781 struct crypto_aead *child = ctx->child;
782 int err;
783
784 if (keylen < 4)
785 return -EINVAL;
786
787 keylen -= 4;
788 memcpy(ctx->nonce, key + keylen, 4);
789
790 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
791 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
792 CRYPTO_TFM_REQ_MASK);
793 err = crypto_aead_setkey(child, key, keylen);
794 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
795 CRYPTO_TFM_RES_MASK);
796
797 return err;
798}
799
800static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
801 unsigned int authsize)
802{
803 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
804
805 switch (authsize) {
806 case 8:
807 case 12:
808 case 16:
809 break;
810 default:
811 return -EINVAL;
812 }
813
814 return crypto_aead_setauthsize(ctx->child, authsize);
815}
816
817static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
818{
819 struct aead_request *subreq = aead_request_ctx(req);
820 struct crypto_aead *aead = crypto_aead_reqtfm(req);
821 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
822 struct crypto_aead *child = ctx->child;
823 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
824 crypto_aead_alignmask(child) + 1);
825
826 memcpy(iv, ctx->nonce, 4);
827 memcpy(iv + 4, req->iv, 8);
828
829 aead_request_set_tfm(subreq, child);
830 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
831 req->base.data);
832 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
Herbert Xuadcbc682015-06-16 13:54:18 +0800833 aead_request_set_ad(subreq, req->assoclen);
Herbert Xudadbc532007-12-17 15:33:17 +0800834
835 return subreq;
836}
837
838static int crypto_rfc4106_encrypt(struct aead_request *req)
839{
840 req = crypto_rfc4106_crypt(req);
841
842 return crypto_aead_encrypt(req);
843}
844
845static int crypto_rfc4106_decrypt(struct aead_request *req)
846{
847 req = crypto_rfc4106_crypt(req);
848
849 return crypto_aead_decrypt(req);
850}
851
Herbert Xuadcbc682015-06-16 13:54:18 +0800852static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800853{
Herbert Xuadcbc682015-06-16 13:54:18 +0800854 struct aead_instance *inst = aead_alg_instance(tfm);
855 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
856 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800857 struct crypto_aead *aead;
858 unsigned long align;
859
860 aead = crypto_spawn_aead(spawn);
861 if (IS_ERR(aead))
862 return PTR_ERR(aead);
863
864 ctx->child = aead;
865
866 align = crypto_aead_alignmask(aead);
867 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800868 crypto_aead_set_reqsize(
869 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800870 sizeof(struct aead_request) +
871 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xuadcbc682015-06-16 13:54:18 +0800872 align + 12);
Herbert Xudadbc532007-12-17 15:33:17 +0800873
874 return 0;
875}
876
Herbert Xuadcbc682015-06-16 13:54:18 +0800877static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800878{
Herbert Xuadcbc682015-06-16 13:54:18 +0800879 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800880
881 crypto_free_aead(ctx->child);
882}
883
Herbert Xuadcbc682015-06-16 13:54:18 +0800884static int crypto_rfc4106_create(struct crypto_template *tmpl,
885 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800886{
887 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800888 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800889 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800890 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800891 const char *ccm_name;
892 int err;
893
894 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800895 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800897
898 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800899 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800900
901 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800902 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800903 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800904
905 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
906 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800907 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800908
Herbert Xuadcbc682015-06-16 13:54:18 +0800909 spawn = aead_instance_ctx(inst);
910 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800911 err = crypto_grab_aead(spawn, ccm_name, 0,
912 crypto_requires_sync(algt->type, algt->mask));
913 if (err)
914 goto out_free_inst;
915
Herbert Xuadcbc682015-06-16 13:54:18 +0800916 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800917
918 err = -EINVAL;
919
Herbert Xuadcbc682015-06-16 13:54:18 +0800920 /* Underlying IV size must be 12. */
921 if (crypto_aead_alg_ivsize(alg) != 12)
Herbert Xudadbc532007-12-17 15:33:17 +0800922 goto out_drop_alg;
923
924 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800925 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800926 goto out_drop_alg;
927
928 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800929 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
930 "rfc4106(%s)", alg->base.cra_name) >=
931 CRYPTO_MAX_ALG_NAME ||
932 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
933 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800934 CRYPTO_MAX_ALG_NAME)
935 goto out_drop_alg;
936
Herbert Xuadcbc682015-06-16 13:54:18 +0800937 inst->alg.base.cra_flags |= alg->base.cra_flags & CRYPTO_ALG_ASYNC;
938 inst->alg.base.cra_priority = alg->base.cra_priority;
939 inst->alg.base.cra_blocksize = 1;
940 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800941
Herbert Xuadcbc682015-06-16 13:54:18 +0800942 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800943
Herbert Xuadcbc682015-06-16 13:54:18 +0800944 inst->alg.ivsize = 8;
945 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800946
Herbert Xuadcbc682015-06-16 13:54:18 +0800947 inst->alg.init = crypto_rfc4106_init_tfm;
948 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800949
Herbert Xuadcbc682015-06-16 13:54:18 +0800950 inst->alg.setkey = crypto_rfc4106_setkey;
951 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
952 inst->alg.encrypt = crypto_rfc4106_encrypt;
953 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800954
Herbert Xuadcbc682015-06-16 13:54:18 +0800955 err = aead_register_instance(tmpl, inst);
956 if (err)
957 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800958
959out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800960 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800961
962out_drop_alg:
963 crypto_drop_aead(spawn);
964out_free_inst:
965 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800966 goto out;
967}
968
969static void crypto_rfc4106_free(struct crypto_instance *inst)
970{
Herbert Xuadcbc682015-06-16 13:54:18 +0800971 crypto_drop_aead(crypto_instance_ctx(inst));
972 kfree(aead_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800973}
974
975static struct crypto_template crypto_rfc4106_tmpl = {
976 .name = "rfc4106",
Herbert Xuadcbc682015-06-16 13:54:18 +0800977 .create = crypto_rfc4106_create,
Herbert Xudadbc532007-12-17 15:33:17 +0800978 .free = crypto_rfc4106_free,
979 .module = THIS_MODULE,
980};
981
Tobias Brunner73c89c12010-01-17 21:52:11 +1100982static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
983 unsigned int keylen)
984{
985 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
986 struct crypto_aead *child = ctx->child;
987 int err;
988
989 if (keylen < 4)
990 return -EINVAL;
991
992 keylen -= 4;
993 memcpy(ctx->nonce, key + keylen, 4);
994
995 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
996 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
997 CRYPTO_TFM_REQ_MASK);
998 err = crypto_aead_setkey(child, key, keylen);
999 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1000 CRYPTO_TFM_RES_MASK);
1001
1002 return err;
1003}
1004
1005static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1006 unsigned int authsize)
1007{
1008 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1009
1010 if (authsize != 16)
1011 return -EINVAL;
1012
1013 return crypto_aead_setauthsize(ctx->child, authsize);
1014}
1015
Herbert Xuadcbc682015-06-16 13:54:18 +08001016static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001017{
1018 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1019 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001020 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001021 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001022 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001023 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1024 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001025 int err;
1026
1027 if (req->src != req->dst) {
1028 err = crypto_rfc4543_copy_src_to_dst(req, enc);
1029 if (err)
1030 return err;
1031 }
Tobias Brunner73c89c12010-01-17 21:52:11 +11001032
1033 memcpy(iv, ctx->nonce, 4);
1034 memcpy(iv + 4, req->iv, 8);
1035
Tobias Brunner73c89c12010-01-17 21:52:11 +11001036 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +08001037 aead_request_set_callback(subreq, req->base.flags,
1038 req->base.complete, req->base.data);
1039 aead_request_set_crypt(subreq, req->src, req->dst,
1040 enc ? 0 : authsize, iv);
1041 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1042 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001043
Herbert Xuadcbc682015-06-16 13:54:18 +08001044 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001045}
1046
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001047static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1048{
1049 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1050 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1051 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001052 unsigned int nbytes = req->assoclen + req->cryptlen -
1053 (enc ? 0 : authsize);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001054 struct blkcipher_desc desc = {
1055 .tfm = ctx->null,
1056 };
1057
1058 return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
1059}
1060
Tobias Brunner73c89c12010-01-17 21:52:11 +11001061static int crypto_rfc4543_encrypt(struct aead_request *req)
1062{
Herbert Xuadcbc682015-06-16 13:54:18 +08001063 return crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001064}
1065
1066static int crypto_rfc4543_decrypt(struct aead_request *req)
1067{
Herbert Xuadcbc682015-06-16 13:54:18 +08001068 return crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001069}
1070
Herbert Xuadcbc682015-06-16 13:54:18 +08001071static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001072{
Herbert Xuadcbc682015-06-16 13:54:18 +08001073 struct aead_instance *inst = aead_alg_instance(tfm);
1074 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001075 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001076 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001077 struct crypto_aead *aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001078 struct crypto_blkcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001079 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001080 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001081
1082 aead = crypto_spawn_aead(spawn);
1083 if (IS_ERR(aead))
1084 return PTR_ERR(aead);
1085
Herbert Xu17db8542015-05-21 15:11:11 +08001086 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001087 err = PTR_ERR(null);
1088 if (IS_ERR(null))
1089 goto err_free_aead;
1090
Tobias Brunner73c89c12010-01-17 21:52:11 +11001091 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001092 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001093
1094 align = crypto_aead_alignmask(aead);
1095 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001096 crypto_aead_set_reqsize(
1097 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001098 sizeof(struct crypto_rfc4543_req_ctx) +
1099 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xuadcbc682015-06-16 13:54:18 +08001100 align + 12);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001101
1102 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001103
1104err_free_aead:
1105 crypto_free_aead(aead);
1106 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001107}
1108
Herbert Xuadcbc682015-06-16 13:54:18 +08001109static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001110{
Herbert Xuadcbc682015-06-16 13:54:18 +08001111 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001112
1113 crypto_free_aead(ctx->child);
Herbert Xu17db8542015-05-21 15:11:11 +08001114 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001115}
1116
Herbert Xuadcbc682015-06-16 13:54:18 +08001117static int crypto_rfc4543_create(struct crypto_template *tmpl,
1118 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001119{
1120 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +08001121 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001122 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001123 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001124 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125 const char *ccm_name;
1126 int err;
1127
1128 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001129 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001130 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001131
1132 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001133 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001134
1135 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001136 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001137 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001138
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001139 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001140 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001141 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001142
Herbert Xuadcbc682015-06-16 13:54:18 +08001143 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001144 spawn = &ctx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001145 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001146 err = crypto_grab_aead(spawn, ccm_name, 0,
1147 crypto_requires_sync(algt->type, algt->mask));
1148 if (err)
1149 goto out_free_inst;
1150
Herbert Xuadcbc682015-06-16 13:54:18 +08001151 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001152
1153 err = -EINVAL;
1154
Herbert Xuadcbc682015-06-16 13:54:18 +08001155 /* Underlying IV size must be 12. */
1156 if (crypto_aead_alg_ivsize(alg) != 12)
Herbert Xu17db8542015-05-21 15:11:11 +08001157 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001158
1159 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001160 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001161 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001162
1163 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001164 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1165 "rfc4543(%s)", alg->base.cra_name) >=
1166 CRYPTO_MAX_ALG_NAME ||
1167 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1168 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001169 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001170 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001171
Herbert Xuadcbc682015-06-16 13:54:18 +08001172 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1173 inst->alg.base.cra_priority = alg->base.cra_priority;
1174 inst->alg.base.cra_blocksize = 1;
1175 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001176
Herbert Xuadcbc682015-06-16 13:54:18 +08001177 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001178
Herbert Xuadcbc682015-06-16 13:54:18 +08001179 inst->alg.ivsize = 8;
1180 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001181
Herbert Xuadcbc682015-06-16 13:54:18 +08001182 inst->alg.init = crypto_rfc4543_init_tfm;
1183 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001184
Herbert Xuadcbc682015-06-16 13:54:18 +08001185 inst->alg.setkey = crypto_rfc4543_setkey;
1186 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1187 inst->alg.encrypt = crypto_rfc4543_encrypt;
1188 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001189
Herbert Xuadcbc682015-06-16 13:54:18 +08001190 err = aead_register_instance(tmpl, inst);
1191 if (err)
1192 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001193
1194out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001195 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001196
1197out_drop_alg:
1198 crypto_drop_aead(spawn);
1199out_free_inst:
1200 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001201 goto out;
1202}
1203
1204static void crypto_rfc4543_free(struct crypto_instance *inst)
1205{
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001206 struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst);
1207
1208 crypto_drop_aead(&ctx->aead);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001209
Herbert Xuadcbc682015-06-16 13:54:18 +08001210 kfree(aead_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001211}
1212
1213static struct crypto_template crypto_rfc4543_tmpl = {
1214 .name = "rfc4543",
Herbert Xuadcbc682015-06-16 13:54:18 +08001215 .create = crypto_rfc4543_create,
Tobias Brunner73c89c12010-01-17 21:52:11 +11001216 .free = crypto_rfc4543_free,
1217 .module = THIS_MODULE,
1218};
1219
Mikko Herranen28db8e32007-11-26 22:24:11 +08001220static int __init crypto_gcm_module_init(void)
1221{
Herbert Xud00aa192007-12-07 20:31:10 +08001222 int err;
1223
Herbert Xuadcbc682015-06-16 13:54:18 +08001224 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001225 if (!gcm_zeroes)
1226 return -ENOMEM;
1227
Herbert Xuadcbc682015-06-16 13:54:18 +08001228 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1229
Herbert Xud00aa192007-12-07 20:31:10 +08001230 err = crypto_register_template(&crypto_gcm_base_tmpl);
1231 if (err)
1232 goto out;
1233
1234 err = crypto_register_template(&crypto_gcm_tmpl);
1235 if (err)
1236 goto out_undo_base;
1237
Herbert Xudadbc532007-12-17 15:33:17 +08001238 err = crypto_register_template(&crypto_rfc4106_tmpl);
1239 if (err)
1240 goto out_undo_gcm;
1241
Tobias Brunner73c89c12010-01-17 21:52:11 +11001242 err = crypto_register_template(&crypto_rfc4543_tmpl);
1243 if (err)
1244 goto out_undo_rfc4106;
1245
Huang Ying9382d972009-08-06 15:34:26 +10001246 return 0;
Herbert Xud00aa192007-12-07 20:31:10 +08001247
Tobias Brunner73c89c12010-01-17 21:52:11 +11001248out_undo_rfc4106:
1249 crypto_unregister_template(&crypto_rfc4106_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001250out_undo_gcm:
1251 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001252out_undo_base:
1253 crypto_unregister_template(&crypto_gcm_base_tmpl);
Huang Ying9382d972009-08-06 15:34:26 +10001254out:
1255 kfree(gcm_zeroes);
1256 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001257}
1258
1259static void __exit crypto_gcm_module_exit(void)
1260{
Huang Ying9382d972009-08-06 15:34:26 +10001261 kfree(gcm_zeroes);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001262 crypto_unregister_template(&crypto_rfc4543_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001263 crypto_unregister_template(&crypto_rfc4106_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001264 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001265 crypto_unregister_template(&crypto_gcm_base_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001266}
1267
1268module_init(crypto_gcm_module_init);
1269module_exit(crypto_gcm_module_exit);
1270
1271MODULE_LICENSE("GPL");
1272MODULE_DESCRIPTION("Galois/Counter Mode");
1273MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001274MODULE_ALIAS_CRYPTO("gcm_base");
1275MODULE_ALIAS_CRYPTO("rfc4106");
1276MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001277MODULE_ALIAS_CRYPTO("gcm");