blob: f254e2d4c206aacc341548dd40744f02459956ab [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mikko Herranen28db8e32007-11-26 22:24:11 +08002/*
3 * GCM: Galois/Counter Mode.
4 *
5 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
Mikko Herranen28db8e32007-11-26 22:24:11 +08006 */
7
Mikko Herranen28db8e32007-11-26 22:24:11 +08008#include <crypto/gf128mul.h>
Herbert Xudadbc532007-12-17 15:33:17 +08009#include <crypto/internal/aead.h>
Herbert Xu1472e5e2007-12-07 19:26:11 +080010#include <crypto/internal/skcipher.h>
Huang Ying9382d972009-08-06 15:34:26 +100011#include <crypto/internal/hash.h>
Herbert Xu17db8542015-05-21 15:11:11 +080012#include <crypto/null.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080013#include <crypto/scatterwalk.h>
Corentin LABBEe0ab7e92017-08-22 10:08:17 +020014#include <crypto/gcm.h>
Huang Ying9382d972009-08-06 15:34:26 +100015#include <crypto/hash.h>
16#include "internal.h"
Mikko Herranen28db8e32007-11-26 22:24:11 +080017#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22
Mikko Herranen28db8e32007-11-26 22:24:11 +080023struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080024 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100025 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080026};
27
28struct crypto_gcm_ctx {
Herbert Xu16f37ecd2016-07-12 13:17:39 +080029 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100030 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080031};
32
Herbert Xudadbc532007-12-17 15:33:17 +080033struct crypto_rfc4106_ctx {
34 struct crypto_aead *child;
35 u8 nonce[4];
36};
37
Herbert Xu7b05a372015-07-09 07:17:30 +080038struct crypto_rfc4106_req_ctx {
39 struct scatterlist src[3];
40 struct scatterlist dst[3];
41 struct aead_request subreq;
42};
43
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030044struct crypto_rfc4543_instance_ctx {
45 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030046};
47
Tobias Brunner73c89c12010-01-17 21:52:11 +110048struct crypto_rfc4543_ctx {
49 struct crypto_aead *child;
Kees Cook8d605392018-09-18 19:10:51 -070050 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110051 u8 nonce[4];
52};
53
54struct crypto_rfc4543_req_ctx {
Tobias Brunner73c89c12010-01-17 21:52:11 +110055 struct aead_request subreq;
56};
57
Mikko Herranen28db8e32007-11-26 22:24:11 +080058struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100059 unsigned int cryptlen;
60 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080061 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080062};
63
64struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080065 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080066 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110067 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080068 struct scatterlist src[3];
69 struct scatterlist dst[3];
70 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100071 struct crypto_gcm_ghash_ctx ghash_ctx;
72 union {
73 struct ahash_request ahreq;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080074 struct skcipher_request skreq;
Huang Ying9382d972009-08-06 15:34:26 +100075 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080076};
77
Herbert Xuadcbc682015-06-16 13:54:18 +080078static struct {
79 u8 buf[16];
80 struct scatterlist sg;
81} *gcm_zeroes;
82
83static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100084
Herbert Xu25894692007-12-12 19:16:38 +080085static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
86 struct aead_request *req)
87{
88 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
89
90 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
91}
92
Mikko Herranen28db8e32007-11-26 22:24:11 +080093static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
94 unsigned int keylen)
95{
96 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +100097 struct crypto_ahash *ghash = ctx->ghash;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080098 struct crypto_skcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +080099 struct {
100 be128 hash;
Ondrej Mosnáček50d2e6d2016-09-23 10:47:32 +0200101 u8 iv[16];
Herbert Xu84c91152007-12-17 21:42:08 +0800102
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100103 struct crypto_wait wait;
Herbert Xu84c91152007-12-17 21:42:08 +0800104
105 struct scatterlist sg[1];
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800106 struct skcipher_request req;
Herbert Xu84c91152007-12-17 21:42:08 +0800107 } *data;
108 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800109
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800110 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
111 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
112 CRYPTO_TFM_REQ_MASK);
113 err = crypto_skcipher_setkey(ctr, key, keylen);
114 crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
Herbert Xuadcbc682015-06-16 13:54:18 +0800115 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800116 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800117 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800118
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800119 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
Herbert Xu84c91152007-12-17 21:42:08 +0800120 GFP_KERNEL);
121 if (!data)
122 return -ENOMEM;
123
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100124 crypto_init_wait(&data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800125 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800126 skcipher_request_set_tfm(&data->req, ctr);
127 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
128 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100129 crypto_req_done,
130 &data->wait);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800131 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
132 sizeof(data->hash), data->iv);
Herbert Xu84c91152007-12-17 21:42:08 +0800133
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100134 err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
135 &data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800136
Mikko Herranen28db8e32007-11-26 22:24:11 +0800137 if (err)
138 goto out;
139
Huang Ying9382d972009-08-06 15:34:26 +1000140 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
141 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
142 CRYPTO_TFM_REQ_MASK);
143 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
144 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
145 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800146
Herbert Xu84c91152007-12-17 21:42:08 +0800147out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800148 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800149 return err;
150}
151
Herbert Xudadbc532007-12-17 15:33:17 +0800152static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
153 unsigned int authsize)
154{
155 switch (authsize) {
156 case 4:
157 case 8:
158 case 12:
159 case 13:
160 case 14:
161 case 15:
162 case 16:
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 return 0;
169}
170
Herbert Xuadcbc682015-06-16 13:54:18 +0800171static void crypto_gcm_init_common(struct aead_request *req)
172{
173 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
174 __be32 counter = cpu_to_be32(1);
175 struct scatterlist *sg;
176
177 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200178 memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
179 memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
Herbert Xuadcbc682015-06-16 13:54:18 +0800180
181 sg_init_table(pctx->src, 3);
182 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
183 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
184 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200185 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800186
187 if (req->src != req->dst) {
188 sg_init_table(pctx->dst, 3);
189 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
190 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
191 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200192 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800193 }
194}
195
196static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800197 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800198{
199 struct crypto_aead *aead = crypto_aead_reqtfm(req);
200 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800201 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800202 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800203 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800204
Herbert Xuadcbc682015-06-16 13:54:18 +0800205 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800206
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800207 skcipher_request_set_tfm(skreq, ctx->ctr);
208 skcipher_request_set_crypt(skreq, pctx->src, dst,
Herbert Xu84c91152007-12-17 21:42:08 +0800209 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800210 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800211}
212
Huang Ying9382d972009-08-06 15:34:26 +1000213static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800214{
Huang Ying9382d972009-08-06 15:34:26 +1000215 len &= 0xfU;
216 return len ? 16 - len : 0;
217}
218
219static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000220
221static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700222 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000223 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800224 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000225{
Herbert Xuadcbc682015-06-16 13:54:18 +0800226 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000227 struct ahash_request *ahreq = &pctx->u.ahreq;
228
Herbert Xuadcbc682015-06-16 13:54:18 +0800229 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000230 ahash_request_set_crypt(ahreq, src, NULL, len);
231
232 return crypto_ahash_update(ahreq);
233}
234
235static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000236 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800237 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000238{
Herbert Xuadcbc682015-06-16 13:54:18 +0800239 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000240}
241
Herbert Xuadcbc682015-06-16 13:54:18 +0800242static int gcm_hash_len(struct aead_request *req, 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 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Eric Biggers18666552019-01-10 12:17:52 -0800247 be128 lengths;
Huang Ying9382d972009-08-06 15:34:26 +1000248
249 lengths.a = cpu_to_be64(req->assoclen * 8);
250 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
251 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800252 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
253 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
254 ahash_request_set_crypt(ahreq, &pctx->sg,
255 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000256
Herbert Xuadcbc682015-06-16 13:54:18 +0800257 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000258}
259
Herbert Xuadcbc682015-06-16 13:54:18 +0800260static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000261{
Herbert Xu25894692007-12-12 19:16:38 +0800262 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000263 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800264
Herbert Xuadcbc682015-06-16 13:54:18 +0800265 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000266}
267
Huang Ying62c55932009-11-16 21:52:22 +0800268static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000269{
270 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800271
Herbert Xuadcbc682015-06-16 13:54:18 +0800272 if (err)
273 goto out;
274
275 err = gcm_hash_len_continue(req, 0);
276 if (err == -EINPROGRESS)
277 return;
278
279out:
280 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800281}
282
Herbert Xuadcbc682015-06-16 13:54:18 +0800283static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800284{
Herbert Xuadcbc682015-06-16 13:54:18 +0800285 return gcm_hash_len(req, flags) ?:
286 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000287}
288
Huang Ying62c55932009-11-16 21:52:22 +0800289static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
290 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000291{
292 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800293
Herbert Xuadcbc682015-06-16 13:54:18 +0800294 if (err)
295 goto out;
296
297 err = gcm_hash_crypt_remain_continue(req, 0);
298 if (err == -EINPROGRESS)
299 return;
300
301out:
302 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800303}
304
Herbert Xuadcbc682015-06-16 13:54:18 +0800305static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800306{
Huang Ying9382d972009-08-06 15:34:26 +1000307 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
308 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
309 unsigned int remain;
310
Herbert Xuadcbc682015-06-16 13:54:18 +0800311 remain = gcm_remain(gctx->cryptlen);
312 if (remain)
313 return gcm_hash_remain(req, remain,
314 gcm_hash_crypt_remain_done, flags) ?:
315 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000316
Herbert Xuadcbc682015-06-16 13:54:18 +0800317 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000318}
319
Huang Ying62c55932009-11-16 21:52:22 +0800320static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000321{
322 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800323
Herbert Xuadcbc682015-06-16 13:54:18 +0800324 if (err)
325 goto out;
326
327 err = gcm_hash_crypt_continue(req, 0);
328 if (err == -EINPROGRESS)
329 return;
330
331out:
332 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800333}
334
Herbert Xuadcbc682015-06-16 13:54:18 +0800335static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800336{
Huang Ying9382d972009-08-06 15:34:26 +1000337 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
338 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000339
Herbert Xuadcbc682015-06-16 13:54:18 +0800340 if (gctx->cryptlen)
341 return gcm_hash_update(req, gcm_hash_crypt_done,
342 gctx->src, gctx->cryptlen, flags) ?:
343 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000344
Herbert Xuadcbc682015-06-16 13:54:18 +0800345 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000346}
347
Huang Ying62c55932009-11-16 21:52:22 +0800348static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
349 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000350{
351 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800352
Herbert Xuadcbc682015-06-16 13:54:18 +0800353 if (err)
354 goto out;
355
356 err = gcm_hash_assoc_remain_continue(req, 0);
357 if (err == -EINPROGRESS)
358 return;
359
360out:
361 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800362}
363
Herbert Xuadcbc682015-06-16 13:54:18 +0800364static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800365{
Huang Ying9382d972009-08-06 15:34:26 +1000366 unsigned int remain;
367
Herbert Xuadcbc682015-06-16 13:54:18 +0800368 remain = gcm_remain(req->assoclen);
369 if (remain)
370 return gcm_hash_remain(req, remain,
371 gcm_hash_assoc_remain_done, flags) ?:
372 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000373
Herbert Xuadcbc682015-06-16 13:54:18 +0800374 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000375}
376
Huang Ying62c55932009-11-16 21:52:22 +0800377static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000378{
379 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800380
Herbert Xuadcbc682015-06-16 13:54:18 +0800381 if (err)
382 goto out;
383
384 err = gcm_hash_assoc_continue(req, 0);
385 if (err == -EINPROGRESS)
386 return;
387
388out:
389 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800390}
391
Herbert Xuadcbc682015-06-16 13:54:18 +0800392static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800393{
Herbert Xuadcbc682015-06-16 13:54:18 +0800394 if (req->assoclen)
395 return gcm_hash_update(req, gcm_hash_assoc_done,
396 req->src, req->assoclen, flags) ?:
397 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000398
Herbert Xuadcbc682015-06-16 13:54:18 +0800399 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800400}
401
402static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
403{
404 struct aead_request *req = areq->data;
405
Herbert Xuadcbc682015-06-16 13:54:18 +0800406 if (err)
407 goto out;
408
409 err = gcm_hash_init_continue(req, 0);
410 if (err == -EINPROGRESS)
411 return;
412
413out:
414 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000415}
416
Herbert Xuadcbc682015-06-16 13:54:18 +0800417static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000418{
Herbert Xuadcbc682015-06-16 13:54:18 +0800419 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000420 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800421 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000422
423 ahash_request_set_tfm(ahreq, ctx->ghash);
424
Herbert Xuadcbc682015-06-16 13:54:18 +0800425 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
426 return crypto_ahash_init(ahreq) ?:
427 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100428}
429
Herbert Xuadcbc682015-06-16 13:54:18 +0800430static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000431{
Herbert Xuadcbc682015-06-16 13:54:18 +0800432 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000433 struct crypto_aead *aead = crypto_aead_reqtfm(req);
434 u8 *auth_tag = pctx->auth_tag;
435
Herbert Xuadcbc682015-06-16 13:54:18 +0800436 crypto_xor(auth_tag, pctx->iauth_tag, 16);
437 scatterwalk_map_and_copy(auth_tag, req->dst,
438 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000439 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800440 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000441}
442
Herbert Xuadcbc682015-06-16 13:54:18 +0800443static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100444{
Huang Ying9382d972009-08-06 15:34:26 +1000445 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800446 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100447
Herbert Xuadcbc682015-06-16 13:54:18 +0800448 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
449 gctx->cryptlen = req->cryptlen;
450 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100451
Herbert Xuadcbc682015-06-16 13:54:18 +0800452 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800453}
454
Huang Ying62c55932009-11-16 21:52:22 +0800455static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000456{
457 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000458
Herbert Xuadcbc682015-06-16 13:54:18 +0800459 if (err)
460 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000461
Herbert Xuadcbc682015-06-16 13:54:18 +0800462 err = gcm_encrypt_continue(req, 0);
463 if (err == -EINPROGRESS)
464 return;
465
466out:
Huang Ying62c55932009-11-16 21:52:22 +0800467 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000468}
469
Mikko Herranen28db8e32007-11-26 22:24:11 +0800470static int crypto_gcm_encrypt(struct aead_request *req)
471{
Herbert Xu25894692007-12-12 19:16:38 +0800472 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800473 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800474 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800475
Herbert Xuadcbc682015-06-16 13:54:18 +0800476 crypto_gcm_init_common(req);
477 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800478 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000479
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800480 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800481 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800482}
483
Herbert Xuadcbc682015-06-16 13:54:18 +0800484static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800485{
Herbert Xuadcbc682015-06-16 13:54:18 +0800486 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800487 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800488 u8 *auth_tag = pctx->auth_tag;
489 u8 *iauth_tag = pctx->iauth_tag;
490 unsigned int authsize = crypto_aead_authsize(aead);
491 unsigned int cryptlen = req->cryptlen - authsize;
492
Huang Ying9382d972009-08-06 15:34:26 +1000493 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800494 scatterwalk_map_and_copy(iauth_tag, req->src,
495 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600496 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800497}
498
Huang Ying9382d972009-08-06 15:34:26 +1000499static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800500{
Herbert Xu84c91152007-12-17 21:42:08 +0800501 struct aead_request *req = areq->data;
502
503 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800504 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800505
506 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800507}
508
Herbert Xuadcbc682015-06-16 13:54:18 +0800509static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000510{
Huang Ying9382d972009-08-06 15:34:26 +1000511 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800512 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000513 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
514
Herbert Xuadcbc682015-06-16 13:54:18 +0800515 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800516 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
517 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000518}
519
Mikko Herranen28db8e32007-11-26 22:24:11 +0800520static int crypto_gcm_decrypt(struct aead_request *req)
521{
Herbert Xu6160b282007-12-04 19:17:50 +1100522 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800523 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000524 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100525 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000526 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800527 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800528
Herbert Xu6160b282007-12-04 19:17:50 +1100529 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800530
Herbert Xuadcbc682015-06-16 13:54:18 +0800531 crypto_gcm_init_common(req);
532
533 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000534 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800535 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000536
Herbert Xuadcbc682015-06-16 13:54:18 +0800537 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800538}
539
Herbert Xuadcbc682015-06-16 13:54:18 +0800540static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800541{
Herbert Xuadcbc682015-06-16 13:54:18 +0800542 struct aead_instance *inst = aead_alg_instance(tfm);
543 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
544 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800545 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000546 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800547 unsigned long align;
548 int err;
549
Huang Ying9382d972009-08-06 15:34:26 +1000550 ghash = crypto_spawn_ahash(&ictx->ghash);
551 if (IS_ERR(ghash))
552 return PTR_ERR(ghash);
553
Eric Biggers60425a82016-10-28 09:52:19 -0700554 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800555 err = PTR_ERR(ctr);
556 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000557 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800558
559 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000560 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800561
Herbert Xuadcbc682015-06-16 13:54:18 +0800562 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800563 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800564 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800565 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800566 max(sizeof(struct skcipher_request) +
567 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000568 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800569 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800570
571 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000572
573err_free_hash:
574 crypto_free_ahash(ghash);
575 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800576}
577
Herbert Xuadcbc682015-06-16 13:54:18 +0800578static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800579{
Herbert Xuadcbc682015-06-16 13:54:18 +0800580 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800581
Huang Ying9382d972009-08-06 15:34:26 +1000582 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800583 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800584}
585
Herbert Xu7b05a372015-07-09 07:17:30 +0800586static void crypto_gcm_free(struct aead_instance *inst)
587{
588 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
589
590 crypto_drop_skcipher(&ctx->ctr);
591 crypto_drop_ahash(&ctx->ghash);
592 kfree(inst);
593}
594
Herbert Xuadcbc682015-06-16 13:54:18 +0800595static int crypto_gcm_create_common(struct crypto_template *tmpl,
596 struct rtattr **tb,
Herbert Xuadcbc682015-06-16 13:54:18 +0800597 const char *ctr_name,
598 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800599{
Herbert Xud00aa192007-12-07 20:31:10 +0800600 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800601 struct aead_instance *inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800602 struct skcipher_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000603 struct crypto_alg *ghash_alg;
Herbert Xuadcbc682015-06-16 13:54:18 +0800604 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800605 struct gcm_instance_ctx *ctx;
606 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800607
Herbert Xud00aa192007-12-07 20:31:10 +0800608 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800609 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800610 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800611
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800612 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800613 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800614
Huang Ying9382d972009-08-06 15:34:26 +1000615 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
616 CRYPTO_ALG_TYPE_HASH,
Herbert Xub30bdfa2016-06-15 22:27:05 +0800617 CRYPTO_ALG_TYPE_AHASH_MASK |
618 crypto_requires_sync(algt->type,
619 algt->mask));
Huang Ying9382d972009-08-06 15:34:26 +1000620 if (IS_ERR(ghash_alg))
Herbert Xuadcbc682015-06-16 13:54:18 +0800621 return PTR_ERR(ghash_alg);
622
623 ghash = __crypto_hash_alg_common(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000624
625 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800626 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
627 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000628 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800629
Herbert Xuadcbc682015-06-16 13:54:18 +0800630 ctx = aead_instance_ctx(inst);
631 err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
632 aead_crypto_instance(inst));
Huang Ying9382d972009-08-06 15:34:26 +1000633 if (err)
634 goto err_free_inst;
635
Herbert Xuadcbc682015-06-16 13:54:18 +0800636 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700637 if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
638 ghash->digestsize != 16)
Herbert Xuadcbc682015-06-16 13:54:18 +0800639 goto err_drop_ghash;
640
641 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700642 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
643 crypto_requires_sync(algt->type,
644 algt->mask));
Herbert Xu1472e5e2007-12-07 19:26:11 +0800645 if (err)
Huang Ying9382d972009-08-06 15:34:26 +1000646 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800647
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800648 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800649
Eric Biggersf6995942019-04-18 14:43:02 -0700650 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000651 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700652 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
653 crypto_skcipher_alg_ivsize(ctr) != 16 ||
654 ctr->base.cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800655 goto out_put_ctr;
656
Mikko Herranen28db8e32007-11-26 22:24:11 +0800657 err = -ENAMETOOLONG;
Eric Biggersf6995942019-04-18 14:43:02 -0700658 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
659 "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
660 goto out_put_ctr;
661
Herbert Xuadcbc682015-06-16 13:54:18 +0800662 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800663 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Huang Ying9382d972009-08-06 15:34:26 +1000664 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800665 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800666 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800667
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800668 inst->alg.base.cra_flags = (ghash->base.cra_flags |
669 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800670 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800671 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800672 inst->alg.base.cra_blocksize = 1;
673 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800674 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800675 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200676 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800677 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800678 inst->alg.maxauthsize = 16;
679 inst->alg.init = crypto_gcm_init_tfm;
680 inst->alg.exit = crypto_gcm_exit_tfm;
681 inst->alg.setkey = crypto_gcm_setkey;
682 inst->alg.setauthsize = crypto_gcm_setauthsize;
683 inst->alg.encrypt = crypto_gcm_encrypt;
684 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800685
Herbert Xu7b05a372015-07-09 07:17:30 +0800686 inst->free = crypto_gcm_free;
687
Herbert Xuadcbc682015-06-16 13:54:18 +0800688 err = aead_register_instance(tmpl, inst);
689 if (err)
690 goto out_put_ctr;
691
692out_put_ghash:
Huang Ying9382d972009-08-06 15:34:26 +1000693 crypto_mod_put(ghash_alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800694 return err;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800695
696out_put_ctr:
697 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000698err_drop_ghash:
699 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800700err_free_inst:
701 kfree(inst);
Herbert Xuadcbc682015-06-16 13:54:18 +0800702 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800703}
704
Herbert Xuadcbc682015-06-16 13:54:18 +0800705static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800706{
Herbert Xud00aa192007-12-07 20:31:10 +0800707 const char *cipher_name;
708 char ctr_name[CRYPTO_MAX_ALG_NAME];
Herbert Xud00aa192007-12-07 20:31:10 +0800709
710 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800711 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800712 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800713
714 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
715 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800716 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800717
Eric Biggersf6995942019-04-18 14:43:02 -0700718 return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800719}
720
Herbert Xuadcbc682015-06-16 13:54:18 +0800721static int crypto_gcm_base_create(struct crypto_template *tmpl,
722 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800723{
Herbert Xud00aa192007-12-07 20:31:10 +0800724 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000725 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800726
727 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800728 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800729 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800730
Huang Ying9382d972009-08-06 15:34:26 +1000731 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000732 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800733 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000734
Eric Biggersf6995942019-04-18 14:43:02 -0700735 return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800736}
737
Herbert Xudadbc532007-12-17 15:33:17 +0800738static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
739 unsigned int keylen)
740{
741 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
742 struct crypto_aead *child = ctx->child;
743 int err;
744
745 if (keylen < 4)
746 return -EINVAL;
747
748 keylen -= 4;
749 memcpy(ctx->nonce, key + keylen, 4);
750
751 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
752 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
753 CRYPTO_TFM_REQ_MASK);
754 err = crypto_aead_setkey(child, key, keylen);
755 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
756 CRYPTO_TFM_RES_MASK);
757
758 return err;
759}
760
761static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
762 unsigned int authsize)
763{
764 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
765
766 switch (authsize) {
767 case 8:
768 case 12:
769 case 16:
770 break;
771 default:
772 return -EINVAL;
773 }
774
775 return crypto_aead_setauthsize(ctx->child, authsize);
776}
777
778static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
779{
Herbert Xu7b05a372015-07-09 07:17:30 +0800780 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800781 struct crypto_aead *aead = crypto_aead_reqtfm(req);
782 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800783 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800784 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800785 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800786 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
787 crypto_aead_alignmask(child) + 1);
788
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200789 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800790
Herbert Xudadbc532007-12-17 15:33:17 +0800791 memcpy(iv, ctx->nonce, 4);
792 memcpy(iv + 4, req->iv, 8);
793
Herbert Xu7b05a372015-07-09 07:17:30 +0800794 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200795 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800796 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
797 if (sg != rctx->src + 1)
798 sg_chain(rctx->src, 2, sg);
799
800 if (req->src != req->dst) {
801 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200802 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800803 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
804 if (sg != rctx->dst + 1)
805 sg_chain(rctx->dst, 2, sg);
806 }
807
Herbert Xudadbc532007-12-17 15:33:17 +0800808 aead_request_set_tfm(subreq, child);
809 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
810 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800811 aead_request_set_crypt(subreq, rctx->src,
812 req->src == req->dst ? rctx->src : rctx->dst,
813 req->cryptlen, iv);
814 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800815
816 return subreq;
817}
818
819static int crypto_rfc4106_encrypt(struct aead_request *req)
820{
Herbert Xu7b05a372015-07-09 07:17:30 +0800821 if (req->assoclen != 16 && req->assoclen != 20)
822 return -EINVAL;
823
Herbert Xudadbc532007-12-17 15:33:17 +0800824 req = crypto_rfc4106_crypt(req);
825
826 return crypto_aead_encrypt(req);
827}
828
829static int crypto_rfc4106_decrypt(struct aead_request *req)
830{
Herbert Xu7b05a372015-07-09 07:17:30 +0800831 if (req->assoclen != 16 && req->assoclen != 20)
832 return -EINVAL;
833
Herbert Xudadbc532007-12-17 15:33:17 +0800834 req = crypto_rfc4106_crypt(req);
835
836 return crypto_aead_decrypt(req);
837}
838
Herbert Xuadcbc682015-06-16 13:54:18 +0800839static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800840{
Herbert Xuadcbc682015-06-16 13:54:18 +0800841 struct aead_instance *inst = aead_alg_instance(tfm);
842 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
843 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800844 struct crypto_aead *aead;
845 unsigned long align;
846
847 aead = crypto_spawn_aead(spawn);
848 if (IS_ERR(aead))
849 return PTR_ERR(aead);
850
851 ctx->child = aead;
852
853 align = crypto_aead_alignmask(aead);
854 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800855 crypto_aead_set_reqsize(
856 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800857 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800858 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800859 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800860
861 return 0;
862}
863
Herbert Xuadcbc682015-06-16 13:54:18 +0800864static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800865{
Herbert Xuadcbc682015-06-16 13:54:18 +0800866 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800867
868 crypto_free_aead(ctx->child);
869}
870
Herbert Xu7b05a372015-07-09 07:17:30 +0800871static void crypto_rfc4106_free(struct aead_instance *inst)
872{
873 crypto_drop_aead(aead_instance_ctx(inst));
874 kfree(inst);
875}
876
Herbert Xuadcbc682015-06-16 13:54:18 +0800877static int crypto_rfc4106_create(struct crypto_template *tmpl,
878 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800879{
880 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800881 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800882 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800883 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800884 const char *ccm_name;
885 int err;
886
887 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800888 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800889 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800890
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800891 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800892 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800893
894 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800895 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800897
898 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
899 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800900 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800901
Herbert Xuadcbc682015-06-16 13:54:18 +0800902 spawn = aead_instance_ctx(inst);
903 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800904 err = crypto_grab_aead(spawn, ccm_name, 0,
905 crypto_requires_sync(algt->type, algt->mask));
906 if (err)
907 goto out_free_inst;
908
Herbert Xuadcbc682015-06-16 13:54:18 +0800909 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800910
911 err = -EINVAL;
912
Herbert Xuadcbc682015-06-16 13:54:18 +0800913 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200914 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xudadbc532007-12-17 15:33:17 +0800915 goto out_drop_alg;
916
917 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800918 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800919 goto out_drop_alg;
920
921 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800922 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
923 "rfc4106(%s)", alg->base.cra_name) >=
924 CRYPTO_MAX_ALG_NAME ||
925 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
926 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800927 CRYPTO_MAX_ALG_NAME)
928 goto out_drop_alg;
929
Herbert Xu7b05a372015-07-09 07:17:30 +0800930 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800931 inst->alg.base.cra_priority = alg->base.cra_priority;
932 inst->alg.base.cra_blocksize = 1;
933 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800934
Herbert Xuadcbc682015-06-16 13:54:18 +0800935 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800936
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200937 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800938 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800939 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800940
Herbert Xuadcbc682015-06-16 13:54:18 +0800941 inst->alg.init = crypto_rfc4106_init_tfm;
942 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800943
Herbert Xuadcbc682015-06-16 13:54:18 +0800944 inst->alg.setkey = crypto_rfc4106_setkey;
945 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
946 inst->alg.encrypt = crypto_rfc4106_encrypt;
947 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800948
Herbert Xu7b05a372015-07-09 07:17:30 +0800949 inst->free = crypto_rfc4106_free;
950
Herbert Xuadcbc682015-06-16 13:54:18 +0800951 err = aead_register_instance(tmpl, inst);
952 if (err)
953 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800954
955out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800956 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800957
958out_drop_alg:
959 crypto_drop_aead(spawn);
960out_free_inst:
961 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800962 goto out;
963}
964
Tobias Brunner73c89c12010-01-17 21:52:11 +1100965static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
966 unsigned int keylen)
967{
968 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
969 struct crypto_aead *child = ctx->child;
970 int err;
971
972 if (keylen < 4)
973 return -EINVAL;
974
975 keylen -= 4;
976 memcpy(ctx->nonce, key + keylen, 4);
977
978 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
979 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
980 CRYPTO_TFM_REQ_MASK);
981 err = crypto_aead_setkey(child, key, keylen);
982 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
983 CRYPTO_TFM_RES_MASK);
984
985 return err;
986}
987
988static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
989 unsigned int authsize)
990{
991 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
992
993 if (authsize != 16)
994 return -EINVAL;
995
996 return crypto_aead_setauthsize(ctx->child, authsize);
997}
998
Herbert Xuadcbc682015-06-16 13:54:18 +0800999static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001000{
1001 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1002 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001003 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001004 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001005 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001006 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1007 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001008 int err;
1009
1010 if (req->src != req->dst) {
1011 err = crypto_rfc4543_copy_src_to_dst(req, enc);
1012 if (err)
1013 return err;
1014 }
Tobias Brunner73c89c12010-01-17 21:52:11 +11001015
1016 memcpy(iv, ctx->nonce, 4);
1017 memcpy(iv + 4, req->iv, 8);
1018
Tobias Brunner73c89c12010-01-17 21:52:11 +11001019 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +08001020 aead_request_set_callback(subreq, req->base.flags,
1021 req->base.complete, req->base.data);
1022 aead_request_set_crypt(subreq, req->src, req->dst,
1023 enc ? 0 : authsize, iv);
1024 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1025 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001026
Herbert Xuadcbc682015-06-16 13:54:18 +08001027 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001028}
1029
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001030static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1031{
1032 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1033 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1034 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001035 unsigned int nbytes = req->assoclen + req->cryptlen -
1036 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -07001037 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001038
Kees Cook8d605392018-09-18 19:10:51 -07001039 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001040 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1041 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1042
1043 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001044}
1045
Tobias Brunner73c89c12010-01-17 21:52:11 +11001046static int crypto_rfc4543_encrypt(struct aead_request *req)
1047{
Herbert Xuadcbc682015-06-16 13:54:18 +08001048 return crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001049}
1050
1051static int crypto_rfc4543_decrypt(struct aead_request *req)
1052{
Herbert Xuadcbc682015-06-16 13:54:18 +08001053 return crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001054}
1055
Herbert Xuadcbc682015-06-16 13:54:18 +08001056static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001057{
Herbert Xuadcbc682015-06-16 13:54:18 +08001058 struct aead_instance *inst = aead_alg_instance(tfm);
1059 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001060 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001061 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001062 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -07001063 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001064 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001065 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001066
1067 aead = crypto_spawn_aead(spawn);
1068 if (IS_ERR(aead))
1069 return PTR_ERR(aead);
1070
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001071 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001072 err = PTR_ERR(null);
1073 if (IS_ERR(null))
1074 goto err_free_aead;
1075
Tobias Brunner73c89c12010-01-17 21:52:11 +11001076 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001077 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001078
1079 align = crypto_aead_alignmask(aead);
1080 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001081 crypto_aead_set_reqsize(
1082 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001083 sizeof(struct crypto_rfc4543_req_ctx) +
1084 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001085 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001086
1087 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001088
1089err_free_aead:
1090 crypto_free_aead(aead);
1091 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001092}
1093
Herbert Xuadcbc682015-06-16 13:54:18 +08001094static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001095{
Herbert Xuadcbc682015-06-16 13:54:18 +08001096 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001097
1098 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001099 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001100}
1101
Herbert Xu7b05a372015-07-09 07:17:30 +08001102static void crypto_rfc4543_free(struct aead_instance *inst)
1103{
1104 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1105
1106 crypto_drop_aead(&ctx->aead);
1107
1108 kfree(inst);
1109}
1110
Herbert Xuadcbc682015-06-16 13:54:18 +08001111static int crypto_rfc4543_create(struct crypto_template *tmpl,
1112 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001113{
1114 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +08001115 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001116 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001117 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001118 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001119 const char *ccm_name;
1120 int err;
1121
1122 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001123 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001124 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001126 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001127 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001128
1129 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001130 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001131 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001132
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001133 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001134 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001135 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001136
Herbert Xuadcbc682015-06-16 13:54:18 +08001137 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001138 spawn = &ctx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001139 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001140 err = crypto_grab_aead(spawn, ccm_name, 0,
1141 crypto_requires_sync(algt->type, algt->mask));
1142 if (err)
1143 goto out_free_inst;
1144
Herbert Xuadcbc682015-06-16 13:54:18 +08001145 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001146
1147 err = -EINVAL;
1148
Herbert Xuadcbc682015-06-16 13:54:18 +08001149 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001150 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xu17db8542015-05-21 15:11:11 +08001151 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001152
1153 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001154 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001155 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001156
1157 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001158 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1159 "rfc4543(%s)", alg->base.cra_name) >=
1160 CRYPTO_MAX_ALG_NAME ||
1161 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1162 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001163 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001164 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001165
Herbert Xuadcbc682015-06-16 13:54:18 +08001166 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1167 inst->alg.base.cra_priority = alg->base.cra_priority;
1168 inst->alg.base.cra_blocksize = 1;
1169 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001170
Herbert Xuadcbc682015-06-16 13:54:18 +08001171 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001172
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001173 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001174 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001175 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001176
Herbert Xuadcbc682015-06-16 13:54:18 +08001177 inst->alg.init = crypto_rfc4543_init_tfm;
1178 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001179
Herbert Xuadcbc682015-06-16 13:54:18 +08001180 inst->alg.setkey = crypto_rfc4543_setkey;
1181 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1182 inst->alg.encrypt = crypto_rfc4543_encrypt;
1183 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001184
Herbert Xu7b05a372015-07-09 07:17:30 +08001185 inst->free = crypto_rfc4543_free,
1186
Herbert Xuadcbc682015-06-16 13:54:18 +08001187 err = aead_register_instance(tmpl, inst);
1188 if (err)
1189 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001190
1191out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001192 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001193
1194out_drop_alg:
1195 crypto_drop_aead(spawn);
1196out_free_inst:
1197 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001198 goto out;
1199}
1200
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001201static struct crypto_template crypto_gcm_tmpls[] = {
1202 {
1203 .name = "gcm_base",
1204 .create = crypto_gcm_base_create,
1205 .module = THIS_MODULE,
1206 }, {
1207 .name = "gcm",
1208 .create = crypto_gcm_create,
1209 .module = THIS_MODULE,
1210 }, {
1211 .name = "rfc4106",
1212 .create = crypto_rfc4106_create,
1213 .module = THIS_MODULE,
1214 }, {
1215 .name = "rfc4543",
1216 .create = crypto_rfc4543_create,
1217 .module = THIS_MODULE,
1218 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001219};
1220
Mikko Herranen28db8e32007-11-26 22:24:11 +08001221static int __init crypto_gcm_module_init(void)
1222{
Herbert Xud00aa192007-12-07 20:31:10 +08001223 int err;
1224
Herbert Xuadcbc682015-06-16 13:54:18 +08001225 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001226 if (!gcm_zeroes)
1227 return -ENOMEM;
1228
Herbert Xuadcbc682015-06-16 13:54:18 +08001229 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1230
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001231 err = crypto_register_templates(crypto_gcm_tmpls,
1232 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001233 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001234 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001235
Huang Ying9382d972009-08-06 15:34:26 +10001236 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001237}
1238
1239static void __exit crypto_gcm_module_exit(void)
1240{
Huang Ying9382d972009-08-06 15:34:26 +10001241 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001242 crypto_unregister_templates(crypto_gcm_tmpls,
1243 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001244}
1245
Eric Biggersc4741b22019-04-11 21:57:42 -07001246subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001247module_exit(crypto_gcm_module_exit);
1248
1249MODULE_LICENSE("GPL");
1250MODULE_DESCRIPTION("Galois/Counter Mode");
1251MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001252MODULE_ALIAS_CRYPTO("gcm_base");
1253MODULE_ALIAS_CRYPTO("rfc4106");
1254MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001255MODULE_ALIAS_CRYPTO("gcm");