blob: e1a11f529d2576a213a9906c46a84478f6397856 [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>
Corentin LABBEe0ab7e92017-08-22 10:08:17 +020017#include <crypto/gcm.h>
Huang Ying9382d972009-08-06 15:34:26 +100018#include <crypto/hash.h>
19#include "internal.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 {
Herbert Xu16f37ecd2016-07-12 13:17:39 +080032 struct crypto_skcipher *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
Herbert Xu7b05a372015-07-09 07:17:30 +080041struct crypto_rfc4106_req_ctx {
42 struct scatterlist src[3];
43 struct scatterlist dst[3];
44 struct aead_request subreq;
45};
46
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030047struct crypto_rfc4543_instance_ctx {
48 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030049};
50
Tobias Brunner73c89c12010-01-17 21:52:11 +110051struct crypto_rfc4543_ctx {
52 struct crypto_aead *child;
Kees Cook8d605392018-09-18 19:10:51 -070053 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110054 u8 nonce[4];
55};
56
57struct crypto_rfc4543_req_ctx {
Tobias Brunner73c89c12010-01-17 21:52:11 +110058 struct aead_request subreq;
59};
60
Mikko Herranen28db8e32007-11-26 22:24:11 +080061struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100062 unsigned int cryptlen;
63 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080064 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080065};
66
67struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080068 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080069 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110070 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080071 struct scatterlist src[3];
72 struct scatterlist dst[3];
73 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100074 struct crypto_gcm_ghash_ctx ghash_ctx;
75 union {
76 struct ahash_request ahreq;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080077 struct skcipher_request skreq;
Huang Ying9382d972009-08-06 15:34:26 +100078 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080079};
80
Herbert Xuadcbc682015-06-16 13:54:18 +080081static struct {
82 u8 buf[16];
83 struct scatterlist sg;
84} *gcm_zeroes;
85
86static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100087
Herbert Xu25894692007-12-12 19:16:38 +080088static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
89 struct aead_request *req)
90{
91 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
92
93 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
94}
95
Mikko Herranen28db8e32007-11-26 22:24:11 +080096static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
97 unsigned int keylen)
98{
99 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000100 struct crypto_ahash *ghash = ctx->ghash;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800101 struct crypto_skcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +0800102 struct {
103 be128 hash;
Ondrej Mosnáček50d2e6d2016-09-23 10:47:32 +0200104 u8 iv[16];
Herbert Xu84c91152007-12-17 21:42:08 +0800105
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100106 struct crypto_wait wait;
Herbert Xu84c91152007-12-17 21:42:08 +0800107
108 struct scatterlist sg[1];
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800109 struct skcipher_request req;
Herbert Xu84c91152007-12-17 21:42:08 +0800110 } *data;
111 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800112
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800113 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
114 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
115 CRYPTO_TFM_REQ_MASK);
116 err = crypto_skcipher_setkey(ctr, key, keylen);
117 crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
Herbert Xuadcbc682015-06-16 13:54:18 +0800118 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800119 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800120 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800121
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800122 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
Herbert Xu84c91152007-12-17 21:42:08 +0800123 GFP_KERNEL);
124 if (!data)
125 return -ENOMEM;
126
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100127 crypto_init_wait(&data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800128 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800129 skcipher_request_set_tfm(&data->req, ctr);
130 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
131 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100132 crypto_req_done,
133 &data->wait);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800134 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
135 sizeof(data->hash), data->iv);
Herbert Xu84c91152007-12-17 21:42:08 +0800136
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100137 err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
138 &data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800139
Mikko Herranen28db8e32007-11-26 22:24:11 +0800140 if (err)
141 goto out;
142
Huang Ying9382d972009-08-06 15:34:26 +1000143 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
144 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
145 CRYPTO_TFM_REQ_MASK);
146 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
147 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
148 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800149
Herbert Xu84c91152007-12-17 21:42:08 +0800150out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800151 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800152 return err;
153}
154
Herbert Xudadbc532007-12-17 15:33:17 +0800155static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
156 unsigned int authsize)
157{
158 switch (authsize) {
159 case 4:
160 case 8:
161 case 12:
162 case 13:
163 case 14:
164 case 15:
165 case 16:
166 break;
167 default:
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
Herbert Xuadcbc682015-06-16 13:54:18 +0800174static void crypto_gcm_init_common(struct aead_request *req)
175{
176 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
177 __be32 counter = cpu_to_be32(1);
178 struct scatterlist *sg;
179
180 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200181 memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
182 memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
Herbert Xuadcbc682015-06-16 13:54:18 +0800183
184 sg_init_table(pctx->src, 3);
185 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
186 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
187 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200188 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800189
190 if (req->src != req->dst) {
191 sg_init_table(pctx->dst, 3);
192 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
193 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
194 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200195 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800196 }
197}
198
199static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800200 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800201{
202 struct crypto_aead *aead = crypto_aead_reqtfm(req);
203 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800204 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800205 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800206 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800207
Herbert Xuadcbc682015-06-16 13:54:18 +0800208 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800209
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800210 skcipher_request_set_tfm(skreq, ctx->ctr);
211 skcipher_request_set_crypt(skreq, pctx->src, dst,
Herbert Xu84c91152007-12-17 21:42:08 +0800212 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800213 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800214}
215
Huang Ying9382d972009-08-06 15:34:26 +1000216static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800217{
Huang Ying9382d972009-08-06 15:34:26 +1000218 len &= 0xfU;
219 return len ? 16 - len : 0;
220}
221
222static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000223
224static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700225 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000226 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800227 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000228{
Herbert Xuadcbc682015-06-16 13:54:18 +0800229 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000230 struct ahash_request *ahreq = &pctx->u.ahreq;
231
Herbert Xuadcbc682015-06-16 13:54:18 +0800232 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000233 ahash_request_set_crypt(ahreq, src, NULL, len);
234
235 return crypto_ahash_update(ahreq);
236}
237
238static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000239 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800240 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000241{
Herbert Xuadcbc682015-06-16 13:54:18 +0800242 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000243}
244
Herbert Xuadcbc682015-06-16 13:54:18 +0800245static int gcm_hash_len(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000246{
Herbert Xuadcbc682015-06-16 13:54:18 +0800247 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000248 struct ahash_request *ahreq = &pctx->u.ahreq;
249 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Eric Biggers18666552019-01-10 12:17:52 -0800250 be128 lengths;
Huang Ying9382d972009-08-06 15:34:26 +1000251
252 lengths.a = cpu_to_be64(req->assoclen * 8);
253 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
254 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800255 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
256 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
257 ahash_request_set_crypt(ahreq, &pctx->sg,
258 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000259
Herbert Xuadcbc682015-06-16 13:54:18 +0800260 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000261}
262
Herbert Xuadcbc682015-06-16 13:54:18 +0800263static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000264{
Herbert Xu25894692007-12-12 19:16:38 +0800265 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000266 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800267
Herbert Xuadcbc682015-06-16 13:54:18 +0800268 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000269}
270
Huang Ying62c55932009-11-16 21:52:22 +0800271static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000272{
273 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800274
Herbert Xuadcbc682015-06-16 13:54:18 +0800275 if (err)
276 goto out;
277
278 err = gcm_hash_len_continue(req, 0);
279 if (err == -EINPROGRESS)
280 return;
281
282out:
283 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800284}
285
Herbert Xuadcbc682015-06-16 13:54:18 +0800286static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800287{
Herbert Xuadcbc682015-06-16 13:54:18 +0800288 return gcm_hash_len(req, flags) ?:
289 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000290}
291
Huang Ying62c55932009-11-16 21:52:22 +0800292static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
293 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000294{
295 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800296
Herbert Xuadcbc682015-06-16 13:54:18 +0800297 if (err)
298 goto out;
299
300 err = gcm_hash_crypt_remain_continue(req, 0);
301 if (err == -EINPROGRESS)
302 return;
303
304out:
305 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800306}
307
Herbert Xuadcbc682015-06-16 13:54:18 +0800308static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800309{
Huang Ying9382d972009-08-06 15:34:26 +1000310 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
311 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
312 unsigned int remain;
313
Herbert Xuadcbc682015-06-16 13:54:18 +0800314 remain = gcm_remain(gctx->cryptlen);
315 if (remain)
316 return gcm_hash_remain(req, remain,
317 gcm_hash_crypt_remain_done, flags) ?:
318 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000319
Herbert Xuadcbc682015-06-16 13:54:18 +0800320 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000321}
322
Huang Ying62c55932009-11-16 21:52:22 +0800323static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000324{
325 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800326
Herbert Xuadcbc682015-06-16 13:54:18 +0800327 if (err)
328 goto out;
329
330 err = gcm_hash_crypt_continue(req, 0);
331 if (err == -EINPROGRESS)
332 return;
333
334out:
335 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800336}
337
Herbert Xuadcbc682015-06-16 13:54:18 +0800338static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800339{
Huang Ying9382d972009-08-06 15:34:26 +1000340 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
341 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000342
Herbert Xuadcbc682015-06-16 13:54:18 +0800343 if (gctx->cryptlen)
344 return gcm_hash_update(req, gcm_hash_crypt_done,
345 gctx->src, gctx->cryptlen, flags) ?:
346 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000347
Herbert Xuadcbc682015-06-16 13:54:18 +0800348 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000349}
350
Huang Ying62c55932009-11-16 21:52:22 +0800351static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
352 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000353{
354 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800355
Herbert Xuadcbc682015-06-16 13:54:18 +0800356 if (err)
357 goto out;
358
359 err = gcm_hash_assoc_remain_continue(req, 0);
360 if (err == -EINPROGRESS)
361 return;
362
363out:
364 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800365}
366
Herbert Xuadcbc682015-06-16 13:54:18 +0800367static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800368{
Huang Ying9382d972009-08-06 15:34:26 +1000369 unsigned int remain;
370
Herbert Xuadcbc682015-06-16 13:54:18 +0800371 remain = gcm_remain(req->assoclen);
372 if (remain)
373 return gcm_hash_remain(req, remain,
374 gcm_hash_assoc_remain_done, flags) ?:
375 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000376
Herbert Xuadcbc682015-06-16 13:54:18 +0800377 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000378}
379
Huang Ying62c55932009-11-16 21:52:22 +0800380static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000381{
382 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800383
Herbert Xuadcbc682015-06-16 13:54:18 +0800384 if (err)
385 goto out;
386
387 err = gcm_hash_assoc_continue(req, 0);
388 if (err == -EINPROGRESS)
389 return;
390
391out:
392 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800393}
394
Herbert Xuadcbc682015-06-16 13:54:18 +0800395static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800396{
Herbert Xuadcbc682015-06-16 13:54:18 +0800397 if (req->assoclen)
398 return gcm_hash_update(req, gcm_hash_assoc_done,
399 req->src, req->assoclen, flags) ?:
400 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000401
Herbert Xuadcbc682015-06-16 13:54:18 +0800402 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800403}
404
405static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
406{
407 struct aead_request *req = areq->data;
408
Herbert Xuadcbc682015-06-16 13:54:18 +0800409 if (err)
410 goto out;
411
412 err = gcm_hash_init_continue(req, 0);
413 if (err == -EINPROGRESS)
414 return;
415
416out:
417 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000418}
419
Herbert Xuadcbc682015-06-16 13:54:18 +0800420static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000421{
Herbert Xuadcbc682015-06-16 13:54:18 +0800422 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000423 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800424 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000425
426 ahash_request_set_tfm(ahreq, ctx->ghash);
427
Herbert Xuadcbc682015-06-16 13:54:18 +0800428 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
429 return crypto_ahash_init(ahreq) ?:
430 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100431}
432
Herbert Xuadcbc682015-06-16 13:54:18 +0800433static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000434{
Herbert Xuadcbc682015-06-16 13:54:18 +0800435 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000436 struct crypto_aead *aead = crypto_aead_reqtfm(req);
437 u8 *auth_tag = pctx->auth_tag;
438
Herbert Xuadcbc682015-06-16 13:54:18 +0800439 crypto_xor(auth_tag, pctx->iauth_tag, 16);
440 scatterwalk_map_and_copy(auth_tag, req->dst,
441 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000442 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800443 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000444}
445
Herbert Xuadcbc682015-06-16 13:54:18 +0800446static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100447{
Huang Ying9382d972009-08-06 15:34:26 +1000448 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800449 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100450
Herbert Xuadcbc682015-06-16 13:54:18 +0800451 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
452 gctx->cryptlen = req->cryptlen;
453 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100454
Herbert Xuadcbc682015-06-16 13:54:18 +0800455 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800456}
457
Huang Ying62c55932009-11-16 21:52:22 +0800458static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000459{
460 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000461
Herbert Xuadcbc682015-06-16 13:54:18 +0800462 if (err)
463 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000464
Herbert Xuadcbc682015-06-16 13:54:18 +0800465 err = gcm_encrypt_continue(req, 0);
466 if (err == -EINPROGRESS)
467 return;
468
469out:
Huang Ying62c55932009-11-16 21:52:22 +0800470 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000471}
472
Mikko Herranen28db8e32007-11-26 22:24:11 +0800473static int crypto_gcm_encrypt(struct aead_request *req)
474{
Herbert Xu25894692007-12-12 19:16:38 +0800475 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800476 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800477 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800478
Herbert Xuadcbc682015-06-16 13:54:18 +0800479 crypto_gcm_init_common(req);
480 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800481 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000482
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800483 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800484 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800485}
486
Herbert Xuadcbc682015-06-16 13:54:18 +0800487static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800488{
Herbert Xuadcbc682015-06-16 13:54:18 +0800489 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800490 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800491 u8 *auth_tag = pctx->auth_tag;
492 u8 *iauth_tag = pctx->iauth_tag;
493 unsigned int authsize = crypto_aead_authsize(aead);
494 unsigned int cryptlen = req->cryptlen - authsize;
495
Huang Ying9382d972009-08-06 15:34:26 +1000496 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800497 scatterwalk_map_and_copy(iauth_tag, req->src,
498 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600499 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800500}
501
Huang Ying9382d972009-08-06 15:34:26 +1000502static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800503{
Herbert Xu84c91152007-12-17 21:42:08 +0800504 struct aead_request *req = areq->data;
505
506 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800507 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800508
509 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800510}
511
Herbert Xuadcbc682015-06-16 13:54:18 +0800512static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000513{
Huang Ying9382d972009-08-06 15:34:26 +1000514 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800515 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000516 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
517
Herbert Xuadcbc682015-06-16 13:54:18 +0800518 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800519 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
520 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000521}
522
Mikko Herranen28db8e32007-11-26 22:24:11 +0800523static int crypto_gcm_decrypt(struct aead_request *req)
524{
Herbert Xu6160b282007-12-04 19:17:50 +1100525 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800526 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000527 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100528 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000529 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800530 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800531
Herbert Xu6160b282007-12-04 19:17:50 +1100532 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800533
Herbert Xuadcbc682015-06-16 13:54:18 +0800534 crypto_gcm_init_common(req);
535
536 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000537 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800538 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000539
Herbert Xuadcbc682015-06-16 13:54:18 +0800540 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800541}
542
Herbert Xuadcbc682015-06-16 13:54:18 +0800543static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800544{
Herbert Xuadcbc682015-06-16 13:54:18 +0800545 struct aead_instance *inst = aead_alg_instance(tfm);
546 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
547 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800548 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000549 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800550 unsigned long align;
551 int err;
552
Huang Ying9382d972009-08-06 15:34:26 +1000553 ghash = crypto_spawn_ahash(&ictx->ghash);
554 if (IS_ERR(ghash))
555 return PTR_ERR(ghash);
556
Eric Biggers60425a82016-10-28 09:52:19 -0700557 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800558 err = PTR_ERR(ctr);
559 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000560 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800561
562 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000563 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800564
Herbert Xuadcbc682015-06-16 13:54:18 +0800565 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800566 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800567 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800568 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800569 max(sizeof(struct skcipher_request) +
570 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000571 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800572 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800573
574 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000575
576err_free_hash:
577 crypto_free_ahash(ghash);
578 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800579}
580
Herbert Xuadcbc682015-06-16 13:54:18 +0800581static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800582{
Herbert Xuadcbc682015-06-16 13:54:18 +0800583 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800584
Huang Ying9382d972009-08-06 15:34:26 +1000585 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800586 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800587}
588
Herbert Xu7b05a372015-07-09 07:17:30 +0800589static void crypto_gcm_free(struct aead_instance *inst)
590{
591 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
592
593 crypto_drop_skcipher(&ctx->ctr);
594 crypto_drop_ahash(&ctx->ghash);
595 kfree(inst);
596}
597
Herbert Xuadcbc682015-06-16 13:54:18 +0800598static int crypto_gcm_create_common(struct crypto_template *tmpl,
599 struct rtattr **tb,
600 const char *full_name,
601 const char *ctr_name,
602 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800603{
Herbert Xud00aa192007-12-07 20:31:10 +0800604 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800605 struct aead_instance *inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800606 struct skcipher_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000607 struct crypto_alg *ghash_alg;
Herbert Xuadcbc682015-06-16 13:54:18 +0800608 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800609 struct gcm_instance_ctx *ctx;
610 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800611
Herbert Xud00aa192007-12-07 20:31:10 +0800612 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800613 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800614 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800615
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800616 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800617 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800618
Huang Ying9382d972009-08-06 15:34:26 +1000619 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
620 CRYPTO_ALG_TYPE_HASH,
Herbert Xub30bdfa2016-06-15 22:27:05 +0800621 CRYPTO_ALG_TYPE_AHASH_MASK |
622 crypto_requires_sync(algt->type,
623 algt->mask));
Huang Ying9382d972009-08-06 15:34:26 +1000624 if (IS_ERR(ghash_alg))
Herbert Xuadcbc682015-06-16 13:54:18 +0800625 return PTR_ERR(ghash_alg);
626
627 ghash = __crypto_hash_alg_common(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000628
629 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800630 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
631 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000632 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800633
Herbert Xuadcbc682015-06-16 13:54:18 +0800634 ctx = aead_instance_ctx(inst);
635 err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
636 aead_crypto_instance(inst));
Huang Ying9382d972009-08-06 15:34:26 +1000637 if (err)
638 goto err_free_inst;
639
Herbert Xuadcbc682015-06-16 13:54:18 +0800640 err = -EINVAL;
641 if (ghash->digestsize != 16)
642 goto err_drop_ghash;
643
644 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700645 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
646 crypto_requires_sync(algt->type,
647 algt->mask));
Herbert Xu1472e5e2007-12-07 19:26:11 +0800648 if (err)
Huang Ying9382d972009-08-06 15:34:26 +1000649 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800650
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800651 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800652
Herbert Xud00aa192007-12-07 20:31:10 +0800653 /* We only support 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000654 err = -EINVAL;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800655 if (crypto_skcipher_alg_ivsize(ctr) != 16)
Herbert Xud00aa192007-12-07 20:31:10 +0800656 goto out_put_ctr;
657
658 /* Not a stream cipher? */
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800659 if (ctr->base.cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800660 goto out_put_ctr;
661
Mikko Herranen28db8e32007-11-26 22:24:11 +0800662 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800663 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800664 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Huang Ying9382d972009-08-06 15:34:26 +1000665 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800666 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800667 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800668
Herbert Xuadcbc682015-06-16 13:54:18 +0800669 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
Herbert Xud00aa192007-12-07 20:31:10 +0800670
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800671 inst->alg.base.cra_flags = (ghash->base.cra_flags |
672 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800673 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800674 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800675 inst->alg.base.cra_blocksize = 1;
676 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800677 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800678 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200679 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800680 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800681 inst->alg.maxauthsize = 16;
682 inst->alg.init = crypto_gcm_init_tfm;
683 inst->alg.exit = crypto_gcm_exit_tfm;
684 inst->alg.setkey = crypto_gcm_setkey;
685 inst->alg.setauthsize = crypto_gcm_setauthsize;
686 inst->alg.encrypt = crypto_gcm_encrypt;
687 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800688
Herbert Xu7b05a372015-07-09 07:17:30 +0800689 inst->free = crypto_gcm_free;
690
Herbert Xuadcbc682015-06-16 13:54:18 +0800691 err = aead_register_instance(tmpl, inst);
692 if (err)
693 goto out_put_ctr;
694
695out_put_ghash:
Huang Ying9382d972009-08-06 15:34:26 +1000696 crypto_mod_put(ghash_alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800697 return err;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800698
699out_put_ctr:
700 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000701err_drop_ghash:
702 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800703err_free_inst:
704 kfree(inst);
Herbert Xuadcbc682015-06-16 13:54:18 +0800705 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800706}
707
Herbert Xuadcbc682015-06-16 13:54:18 +0800708static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800709{
Herbert Xud00aa192007-12-07 20:31:10 +0800710 const char *cipher_name;
711 char ctr_name[CRYPTO_MAX_ALG_NAME];
712 char full_name[CRYPTO_MAX_ALG_NAME];
713
714 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800715 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800716 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800717
718 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
719 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800720 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800721
722 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
723 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800724 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800725
Herbert Xuadcbc682015-06-16 13:54:18 +0800726 return crypto_gcm_create_common(tmpl, tb, full_name,
727 ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800728}
729
Herbert Xuadcbc682015-06-16 13:54:18 +0800730static int crypto_gcm_base_create(struct crypto_template *tmpl,
731 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800732{
Herbert Xud00aa192007-12-07 20:31:10 +0800733 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000734 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800735 char full_name[CRYPTO_MAX_ALG_NAME];
736
737 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800738 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800739 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800740
Huang Ying9382d972009-08-06 15:34:26 +1000741 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000742 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800743 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000744
745 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
746 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800747 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800748
Herbert Xuadcbc682015-06-16 13:54:18 +0800749 return crypto_gcm_create_common(tmpl, tb, full_name,
750 ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800751}
752
Herbert Xudadbc532007-12-17 15:33:17 +0800753static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
754 unsigned int keylen)
755{
756 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
757 struct crypto_aead *child = ctx->child;
758 int err;
759
760 if (keylen < 4)
761 return -EINVAL;
762
763 keylen -= 4;
764 memcpy(ctx->nonce, key + keylen, 4);
765
766 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
767 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
768 CRYPTO_TFM_REQ_MASK);
769 err = crypto_aead_setkey(child, key, keylen);
770 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
771 CRYPTO_TFM_RES_MASK);
772
773 return err;
774}
775
776static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
777 unsigned int authsize)
778{
779 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
780
781 switch (authsize) {
782 case 8:
783 case 12:
784 case 16:
785 break;
786 default:
787 return -EINVAL;
788 }
789
790 return crypto_aead_setauthsize(ctx->child, authsize);
791}
792
793static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
794{
Herbert Xu7b05a372015-07-09 07:17:30 +0800795 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800796 struct crypto_aead *aead = crypto_aead_reqtfm(req);
797 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800798 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800799 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800800 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800801 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
802 crypto_aead_alignmask(child) + 1);
803
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200804 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800805
Herbert Xudadbc532007-12-17 15:33:17 +0800806 memcpy(iv, ctx->nonce, 4);
807 memcpy(iv + 4, req->iv, 8);
808
Herbert Xu7b05a372015-07-09 07:17:30 +0800809 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200810 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800811 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
812 if (sg != rctx->src + 1)
813 sg_chain(rctx->src, 2, sg);
814
815 if (req->src != req->dst) {
816 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200817 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800818 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
819 if (sg != rctx->dst + 1)
820 sg_chain(rctx->dst, 2, sg);
821 }
822
Herbert Xudadbc532007-12-17 15:33:17 +0800823 aead_request_set_tfm(subreq, child);
824 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
825 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800826 aead_request_set_crypt(subreq, rctx->src,
827 req->src == req->dst ? rctx->src : rctx->dst,
828 req->cryptlen, iv);
829 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800830
831 return subreq;
832}
833
834static int crypto_rfc4106_encrypt(struct aead_request *req)
835{
Herbert Xu7b05a372015-07-09 07:17:30 +0800836 if (req->assoclen != 16 && req->assoclen != 20)
837 return -EINVAL;
838
Herbert Xudadbc532007-12-17 15:33:17 +0800839 req = crypto_rfc4106_crypt(req);
840
841 return crypto_aead_encrypt(req);
842}
843
844static int crypto_rfc4106_decrypt(struct aead_request *req)
845{
Herbert Xu7b05a372015-07-09 07:17:30 +0800846 if (req->assoclen != 16 && req->assoclen != 20)
847 return -EINVAL;
848
Herbert Xudadbc532007-12-17 15:33:17 +0800849 req = crypto_rfc4106_crypt(req);
850
851 return crypto_aead_decrypt(req);
852}
853
Herbert Xuadcbc682015-06-16 13:54:18 +0800854static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800855{
Herbert Xuadcbc682015-06-16 13:54:18 +0800856 struct aead_instance *inst = aead_alg_instance(tfm);
857 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
858 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800859 struct crypto_aead *aead;
860 unsigned long align;
861
862 aead = crypto_spawn_aead(spawn);
863 if (IS_ERR(aead))
864 return PTR_ERR(aead);
865
866 ctx->child = aead;
867
868 align = crypto_aead_alignmask(aead);
869 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800870 crypto_aead_set_reqsize(
871 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800872 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800873 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800874 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800875
876 return 0;
877}
878
Herbert Xuadcbc682015-06-16 13:54:18 +0800879static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800880{
Herbert Xuadcbc682015-06-16 13:54:18 +0800881 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800882
883 crypto_free_aead(ctx->child);
884}
885
Herbert Xu7b05a372015-07-09 07:17:30 +0800886static void crypto_rfc4106_free(struct aead_instance *inst)
887{
888 crypto_drop_aead(aead_instance_ctx(inst));
889 kfree(inst);
890}
891
Herbert Xuadcbc682015-06-16 13:54:18 +0800892static int crypto_rfc4106_create(struct crypto_template *tmpl,
893 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800894{
895 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800897 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800898 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800899 const char *ccm_name;
900 int err;
901
902 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800903 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800904 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800905
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800906 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800907 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800908
909 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800910 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800911 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800912
913 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
914 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800915 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800916
Herbert Xuadcbc682015-06-16 13:54:18 +0800917 spawn = aead_instance_ctx(inst);
918 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800919 err = crypto_grab_aead(spawn, ccm_name, 0,
920 crypto_requires_sync(algt->type, algt->mask));
921 if (err)
922 goto out_free_inst;
923
Herbert Xuadcbc682015-06-16 13:54:18 +0800924 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800925
926 err = -EINVAL;
927
Herbert Xuadcbc682015-06-16 13:54:18 +0800928 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200929 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xudadbc532007-12-17 15:33:17 +0800930 goto out_drop_alg;
931
932 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800933 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800934 goto out_drop_alg;
935
936 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800937 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
938 "rfc4106(%s)", alg->base.cra_name) >=
939 CRYPTO_MAX_ALG_NAME ||
940 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
941 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800942 CRYPTO_MAX_ALG_NAME)
943 goto out_drop_alg;
944
Herbert Xu7b05a372015-07-09 07:17:30 +0800945 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800946 inst->alg.base.cra_priority = alg->base.cra_priority;
947 inst->alg.base.cra_blocksize = 1;
948 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800949
Herbert Xuadcbc682015-06-16 13:54:18 +0800950 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800951
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200952 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800953 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800954 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800955
Herbert Xuadcbc682015-06-16 13:54:18 +0800956 inst->alg.init = crypto_rfc4106_init_tfm;
957 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800958
Herbert Xuadcbc682015-06-16 13:54:18 +0800959 inst->alg.setkey = crypto_rfc4106_setkey;
960 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
961 inst->alg.encrypt = crypto_rfc4106_encrypt;
962 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800963
Herbert Xu7b05a372015-07-09 07:17:30 +0800964 inst->free = crypto_rfc4106_free;
965
Herbert Xuadcbc682015-06-16 13:54:18 +0800966 err = aead_register_instance(tmpl, inst);
967 if (err)
968 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800969
970out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800971 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800972
973out_drop_alg:
974 crypto_drop_aead(spawn);
975out_free_inst:
976 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800977 goto out;
978}
979
Tobias Brunner73c89c12010-01-17 21:52:11 +1100980static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
981 unsigned int keylen)
982{
983 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
984 struct crypto_aead *child = ctx->child;
985 int err;
986
987 if (keylen < 4)
988 return -EINVAL;
989
990 keylen -= 4;
991 memcpy(ctx->nonce, key + keylen, 4);
992
993 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
994 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
995 CRYPTO_TFM_REQ_MASK);
996 err = crypto_aead_setkey(child, key, keylen);
997 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
998 CRYPTO_TFM_RES_MASK);
999
1000 return err;
1001}
1002
1003static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1004 unsigned int authsize)
1005{
1006 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1007
1008 if (authsize != 16)
1009 return -EINVAL;
1010
1011 return crypto_aead_setauthsize(ctx->child, authsize);
1012}
1013
Herbert Xuadcbc682015-06-16 13:54:18 +08001014static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001015{
1016 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1017 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001018 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001019 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001020 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001021 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1022 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001023 int err;
1024
1025 if (req->src != req->dst) {
1026 err = crypto_rfc4543_copy_src_to_dst(req, enc);
1027 if (err)
1028 return err;
1029 }
Tobias Brunner73c89c12010-01-17 21:52:11 +11001030
1031 memcpy(iv, ctx->nonce, 4);
1032 memcpy(iv + 4, req->iv, 8);
1033
Tobias Brunner73c89c12010-01-17 21:52:11 +11001034 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +08001035 aead_request_set_callback(subreq, req->base.flags,
1036 req->base.complete, req->base.data);
1037 aead_request_set_crypt(subreq, req->src, req->dst,
1038 enc ? 0 : authsize, iv);
1039 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1040 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001041
Herbert Xuadcbc682015-06-16 13:54:18 +08001042 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001043}
1044
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001045static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1046{
1047 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1048 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1049 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001050 unsigned int nbytes = req->assoclen + req->cryptlen -
1051 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -07001052 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001053
Kees Cook8d605392018-09-18 19:10:51 -07001054 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001055 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1056 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1057
1058 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001059}
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;
Kees Cook8d605392018-09-18 19:10:51 -07001078 struct crypto_sync_skcipher *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
Eric Biggers3a2d4fb2017-12-07 10:56:34 -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()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001100 align + GCM_AES_IV_SIZE);
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);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001114 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001115}
1116
Herbert Xu7b05a372015-07-09 07:17:30 +08001117static void crypto_rfc4543_free(struct aead_instance *inst)
1118{
1119 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1120
1121 crypto_drop_aead(&ctx->aead);
1122
1123 kfree(inst);
1124}
1125
Herbert Xuadcbc682015-06-16 13:54:18 +08001126static int crypto_rfc4543_create(struct crypto_template *tmpl,
1127 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001128{
1129 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +08001130 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001131 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001132 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001133 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001134 const char *ccm_name;
1135 int err;
1136
1137 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001138 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001139 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001140
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001141 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001142 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001143
1144 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001145 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001146 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001147
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001148 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001149 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001150 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001151
Herbert Xuadcbc682015-06-16 13:54:18 +08001152 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001153 spawn = &ctx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001154 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001155 err = crypto_grab_aead(spawn, ccm_name, 0,
1156 crypto_requires_sync(algt->type, algt->mask));
1157 if (err)
1158 goto out_free_inst;
1159
Herbert Xuadcbc682015-06-16 13:54:18 +08001160 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001161
1162 err = -EINVAL;
1163
Herbert Xuadcbc682015-06-16 13:54:18 +08001164 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001165 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xu17db8542015-05-21 15:11:11 +08001166 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001167
1168 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001169 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001170 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001171
1172 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001173 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1174 "rfc4543(%s)", alg->base.cra_name) >=
1175 CRYPTO_MAX_ALG_NAME ||
1176 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1177 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001178 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001179 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001180
Herbert Xuadcbc682015-06-16 13:54:18 +08001181 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1182 inst->alg.base.cra_priority = alg->base.cra_priority;
1183 inst->alg.base.cra_blocksize = 1;
1184 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001185
Herbert Xuadcbc682015-06-16 13:54:18 +08001186 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001187
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001188 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001189 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001190 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001191
Herbert Xuadcbc682015-06-16 13:54:18 +08001192 inst->alg.init = crypto_rfc4543_init_tfm;
1193 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001194
Herbert Xuadcbc682015-06-16 13:54:18 +08001195 inst->alg.setkey = crypto_rfc4543_setkey;
1196 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1197 inst->alg.encrypt = crypto_rfc4543_encrypt;
1198 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001199
Herbert Xu7b05a372015-07-09 07:17:30 +08001200 inst->free = crypto_rfc4543_free,
1201
Herbert Xuadcbc682015-06-16 13:54:18 +08001202 err = aead_register_instance(tmpl, inst);
1203 if (err)
1204 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001205
1206out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001207 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001208
1209out_drop_alg:
1210 crypto_drop_aead(spawn);
1211out_free_inst:
1212 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001213 goto out;
1214}
1215
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001216static struct crypto_template crypto_gcm_tmpls[] = {
1217 {
1218 .name = "gcm_base",
1219 .create = crypto_gcm_base_create,
1220 .module = THIS_MODULE,
1221 }, {
1222 .name = "gcm",
1223 .create = crypto_gcm_create,
1224 .module = THIS_MODULE,
1225 }, {
1226 .name = "rfc4106",
1227 .create = crypto_rfc4106_create,
1228 .module = THIS_MODULE,
1229 }, {
1230 .name = "rfc4543",
1231 .create = crypto_rfc4543_create,
1232 .module = THIS_MODULE,
1233 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001234};
1235
Mikko Herranen28db8e32007-11-26 22:24:11 +08001236static int __init crypto_gcm_module_init(void)
1237{
Herbert Xud00aa192007-12-07 20:31:10 +08001238 int err;
1239
Herbert Xuadcbc682015-06-16 13:54:18 +08001240 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001241 if (!gcm_zeroes)
1242 return -ENOMEM;
1243
Herbert Xuadcbc682015-06-16 13:54:18 +08001244 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1245
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001246 err = crypto_register_templates(crypto_gcm_tmpls,
1247 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001248 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001249 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001250
Huang Ying9382d972009-08-06 15:34:26 +10001251 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001252}
1253
1254static void __exit crypto_gcm_module_exit(void)
1255{
Huang Ying9382d972009-08-06 15:34:26 +10001256 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001257 crypto_unregister_templates(crypto_gcm_tmpls,
1258 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001259}
1260
1261module_init(crypto_gcm_module_init);
1262module_exit(crypto_gcm_module_exit);
1263
1264MODULE_LICENSE("GPL");
1265MODULE_DESCRIPTION("Galois/Counter Mode");
1266MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001267MODULE_ALIAS_CRYPTO("gcm_base");
1268MODULE_ALIAS_CRYPTO("rfc4106");
1269MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001270MODULE_ALIAS_CRYPTO("gcm");