blob: 0103d28c541eb4137240c45fa74be7bde2638fb8 [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>
Mikko Herranen28db8e32007-11-26 22:24:11 +080016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21
Mikko Herranen28db8e32007-11-26 22:24:11 +080022struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080023 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100024 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080025};
26
27struct crypto_gcm_ctx {
Herbert Xu16f37ecd2016-07-12 13:17:39 +080028 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100029 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080030};
31
Herbert Xudadbc532007-12-17 15:33:17 +080032struct crypto_rfc4106_ctx {
33 struct crypto_aead *child;
34 u8 nonce[4];
35};
36
Herbert Xu7b05a372015-07-09 07:17:30 +080037struct crypto_rfc4106_req_ctx {
38 struct scatterlist src[3];
39 struct scatterlist dst[3];
40 struct aead_request subreq;
41};
42
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030043struct crypto_rfc4543_instance_ctx {
44 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030045};
46
Tobias Brunner73c89c12010-01-17 21:52:11 +110047struct crypto_rfc4543_ctx {
48 struct crypto_aead *child;
Kees Cook8d605392018-09-18 19:10:51 -070049 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110050 u8 nonce[4];
51};
52
53struct crypto_rfc4543_req_ctx {
Tobias Brunner73c89c12010-01-17 21:52:11 +110054 struct aead_request subreq;
55};
56
Mikko Herranen28db8e32007-11-26 22:24:11 +080057struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100058 unsigned int cryptlen;
59 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080060 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080061};
62
63struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080064 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080065 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110066 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080067 struct scatterlist src[3];
68 struct scatterlist dst[3];
69 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100070 struct crypto_gcm_ghash_ctx ghash_ctx;
71 union {
72 struct ahash_request ahreq;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080073 struct skcipher_request skreq;
Huang Ying9382d972009-08-06 15:34:26 +100074 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080075};
76
Herbert Xuadcbc682015-06-16 13:54:18 +080077static struct {
78 u8 buf[16];
79 struct scatterlist sg;
80} *gcm_zeroes;
81
82static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100083
Herbert Xu25894692007-12-12 19:16:38 +080084static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
85 struct aead_request *req)
86{
87 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
88
89 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
90}
91
Mikko Herranen28db8e32007-11-26 22:24:11 +080092static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
93 unsigned int keylen)
94{
95 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +100096 struct crypto_ahash *ghash = ctx->ghash;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080097 struct crypto_skcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +080098 struct {
99 be128 hash;
Ondrej Mosnáček50d2e6d2016-09-23 10:47:32 +0200100 u8 iv[16];
Herbert Xu84c91152007-12-17 21:42:08 +0800101
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100102 struct crypto_wait wait;
Herbert Xu84c91152007-12-17 21:42:08 +0800103
104 struct scatterlist sg[1];
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800105 struct skcipher_request req;
Herbert Xu84c91152007-12-17 21:42:08 +0800106 } *data;
107 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800108
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800109 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
110 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
111 CRYPTO_TFM_REQ_MASK);
112 err = crypto_skcipher_setkey(ctr, key, keylen);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800113 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800114 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800115
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800116 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
Herbert Xu84c91152007-12-17 21:42:08 +0800117 GFP_KERNEL);
118 if (!data)
119 return -ENOMEM;
120
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100121 crypto_init_wait(&data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800122 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800123 skcipher_request_set_tfm(&data->req, ctr);
124 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
125 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100126 crypto_req_done,
127 &data->wait);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800128 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
129 sizeof(data->hash), data->iv);
Herbert Xu84c91152007-12-17 21:42:08 +0800130
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100131 err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
132 &data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800133
Mikko Herranen28db8e32007-11-26 22:24:11 +0800134 if (err)
135 goto out;
136
Huang Ying9382d972009-08-06 15:34:26 +1000137 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
138 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
139 CRYPTO_TFM_REQ_MASK);
140 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
Herbert Xu84c91152007-12-17 21:42:08 +0800141out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800142 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800143 return err;
144}
145
Herbert Xudadbc532007-12-17 15:33:17 +0800146static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
147 unsigned int authsize)
148{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300149 return crypto_gcm_check_authsize(authsize);
Herbert Xudadbc532007-12-17 15:33:17 +0800150}
151
Herbert Xuadcbc682015-06-16 13:54:18 +0800152static void crypto_gcm_init_common(struct aead_request *req)
153{
154 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
155 __be32 counter = cpu_to_be32(1);
156 struct scatterlist *sg;
157
158 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200159 memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
160 memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
Herbert Xuadcbc682015-06-16 13:54:18 +0800161
162 sg_init_table(pctx->src, 3);
163 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
164 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
165 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200166 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800167
168 if (req->src != req->dst) {
169 sg_init_table(pctx->dst, 3);
170 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
171 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
172 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200173 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800174 }
175}
176
177static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800178 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800179{
180 struct crypto_aead *aead = crypto_aead_reqtfm(req);
181 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800182 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800183 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800184 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800185
Herbert Xuadcbc682015-06-16 13:54:18 +0800186 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800187
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800188 skcipher_request_set_tfm(skreq, ctx->ctr);
189 skcipher_request_set_crypt(skreq, pctx->src, dst,
Herbert Xu84c91152007-12-17 21:42:08 +0800190 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800191 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800192}
193
Huang Ying9382d972009-08-06 15:34:26 +1000194static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800195{
Huang Ying9382d972009-08-06 15:34:26 +1000196 len &= 0xfU;
197 return len ? 16 - len : 0;
198}
199
200static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000201
202static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700203 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000204 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800205 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000206{
Herbert Xuadcbc682015-06-16 13:54:18 +0800207 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000208 struct ahash_request *ahreq = &pctx->u.ahreq;
209
Herbert Xuadcbc682015-06-16 13:54:18 +0800210 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000211 ahash_request_set_crypt(ahreq, src, NULL, len);
212
213 return crypto_ahash_update(ahreq);
214}
215
216static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000217 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800218 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000219{
Herbert Xuadcbc682015-06-16 13:54:18 +0800220 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000221}
222
Herbert Xuadcbc682015-06-16 13:54:18 +0800223static int gcm_hash_len(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000224{
Herbert Xuadcbc682015-06-16 13:54:18 +0800225 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000226 struct ahash_request *ahreq = &pctx->u.ahreq;
227 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Eric Biggers18666552019-01-10 12:17:52 -0800228 be128 lengths;
Huang Ying9382d972009-08-06 15:34:26 +1000229
230 lengths.a = cpu_to_be64(req->assoclen * 8);
231 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
232 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800233 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
234 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
235 ahash_request_set_crypt(ahreq, &pctx->sg,
236 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000237
Herbert Xuadcbc682015-06-16 13:54:18 +0800238 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000239}
240
Herbert Xuadcbc682015-06-16 13:54:18 +0800241static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000242{
Herbert Xu25894692007-12-12 19:16:38 +0800243 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000244 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800245
Herbert Xuadcbc682015-06-16 13:54:18 +0800246 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000247}
248
Huang Ying62c55932009-11-16 21:52:22 +0800249static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000250{
251 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800252
Herbert Xuadcbc682015-06-16 13:54:18 +0800253 if (err)
254 goto out;
255
256 err = gcm_hash_len_continue(req, 0);
257 if (err == -EINPROGRESS)
258 return;
259
260out:
261 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800262}
263
Herbert Xuadcbc682015-06-16 13:54:18 +0800264static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800265{
Herbert Xuadcbc682015-06-16 13:54:18 +0800266 return gcm_hash_len(req, flags) ?:
267 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000268}
269
Huang Ying62c55932009-11-16 21:52:22 +0800270static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
271 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_crypt_remain_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_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800287{
Huang Ying9382d972009-08-06 15:34:26 +1000288 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
289 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
290 unsigned int remain;
291
Herbert Xuadcbc682015-06-16 13:54:18 +0800292 remain = gcm_remain(gctx->cryptlen);
293 if (remain)
294 return gcm_hash_remain(req, remain,
295 gcm_hash_crypt_remain_done, flags) ?:
296 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000297
Herbert Xuadcbc682015-06-16 13:54:18 +0800298 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000299}
300
Huang Ying62c55932009-11-16 21:52:22 +0800301static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000302{
303 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800304
Herbert Xuadcbc682015-06-16 13:54:18 +0800305 if (err)
306 goto out;
307
308 err = gcm_hash_crypt_continue(req, 0);
309 if (err == -EINPROGRESS)
310 return;
311
312out:
313 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800314}
315
Herbert Xuadcbc682015-06-16 13:54:18 +0800316static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800317{
Huang Ying9382d972009-08-06 15:34:26 +1000318 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
319 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000320
Herbert Xuadcbc682015-06-16 13:54:18 +0800321 if (gctx->cryptlen)
322 return gcm_hash_update(req, gcm_hash_crypt_done,
323 gctx->src, gctx->cryptlen, flags) ?:
324 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000325
Herbert Xuadcbc682015-06-16 13:54:18 +0800326 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000327}
328
Huang Ying62c55932009-11-16 21:52:22 +0800329static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
330 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000331{
332 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800333
Herbert Xuadcbc682015-06-16 13:54:18 +0800334 if (err)
335 goto out;
336
337 err = gcm_hash_assoc_remain_continue(req, 0);
338 if (err == -EINPROGRESS)
339 return;
340
341out:
342 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800343}
344
Herbert Xuadcbc682015-06-16 13:54:18 +0800345static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800346{
Huang Ying9382d972009-08-06 15:34:26 +1000347 unsigned int remain;
348
Herbert Xuadcbc682015-06-16 13:54:18 +0800349 remain = gcm_remain(req->assoclen);
350 if (remain)
351 return gcm_hash_remain(req, remain,
352 gcm_hash_assoc_remain_done, flags) ?:
353 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000354
Herbert Xuadcbc682015-06-16 13:54:18 +0800355 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000356}
357
Huang Ying62c55932009-11-16 21:52:22 +0800358static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000359{
360 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800361
Herbert Xuadcbc682015-06-16 13:54:18 +0800362 if (err)
363 goto out;
364
365 err = gcm_hash_assoc_continue(req, 0);
366 if (err == -EINPROGRESS)
367 return;
368
369out:
370 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800371}
372
Herbert Xuadcbc682015-06-16 13:54:18 +0800373static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800374{
Herbert Xuadcbc682015-06-16 13:54:18 +0800375 if (req->assoclen)
376 return gcm_hash_update(req, gcm_hash_assoc_done,
377 req->src, req->assoclen, flags) ?:
378 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000379
Herbert Xuadcbc682015-06-16 13:54:18 +0800380 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800381}
382
383static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
384{
385 struct aead_request *req = areq->data;
386
Herbert Xuadcbc682015-06-16 13:54:18 +0800387 if (err)
388 goto out;
389
390 err = gcm_hash_init_continue(req, 0);
391 if (err == -EINPROGRESS)
392 return;
393
394out:
395 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000396}
397
Herbert Xuadcbc682015-06-16 13:54:18 +0800398static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000399{
Herbert Xuadcbc682015-06-16 13:54:18 +0800400 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000401 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800402 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000403
404 ahash_request_set_tfm(ahreq, ctx->ghash);
405
Herbert Xuadcbc682015-06-16 13:54:18 +0800406 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
407 return crypto_ahash_init(ahreq) ?:
408 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100409}
410
Herbert Xuadcbc682015-06-16 13:54:18 +0800411static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000412{
Herbert Xuadcbc682015-06-16 13:54:18 +0800413 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000414 struct crypto_aead *aead = crypto_aead_reqtfm(req);
415 u8 *auth_tag = pctx->auth_tag;
416
Herbert Xuadcbc682015-06-16 13:54:18 +0800417 crypto_xor(auth_tag, pctx->iauth_tag, 16);
418 scatterwalk_map_and_copy(auth_tag, req->dst,
419 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000420 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800421 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000422}
423
Herbert Xuadcbc682015-06-16 13:54:18 +0800424static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100425{
Huang Ying9382d972009-08-06 15:34:26 +1000426 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800427 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100428
Herbert Xuadcbc682015-06-16 13:54:18 +0800429 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
430 gctx->cryptlen = req->cryptlen;
431 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100432
Herbert Xuadcbc682015-06-16 13:54:18 +0800433 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800434}
435
Huang Ying62c55932009-11-16 21:52:22 +0800436static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000437{
438 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000439
Herbert Xuadcbc682015-06-16 13:54:18 +0800440 if (err)
441 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000442
Herbert Xuadcbc682015-06-16 13:54:18 +0800443 err = gcm_encrypt_continue(req, 0);
444 if (err == -EINPROGRESS)
445 return;
446
447out:
Huang Ying62c55932009-11-16 21:52:22 +0800448 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000449}
450
Mikko Herranen28db8e32007-11-26 22:24:11 +0800451static int crypto_gcm_encrypt(struct aead_request *req)
452{
Herbert Xu25894692007-12-12 19:16:38 +0800453 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800454 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800455 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800456
Herbert Xuadcbc682015-06-16 13:54:18 +0800457 crypto_gcm_init_common(req);
458 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800459 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000460
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800461 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800462 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800463}
464
Herbert Xuadcbc682015-06-16 13:54:18 +0800465static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800466{
Herbert Xuadcbc682015-06-16 13:54:18 +0800467 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800468 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800469 u8 *auth_tag = pctx->auth_tag;
470 u8 *iauth_tag = pctx->iauth_tag;
471 unsigned int authsize = crypto_aead_authsize(aead);
472 unsigned int cryptlen = req->cryptlen - authsize;
473
Huang Ying9382d972009-08-06 15:34:26 +1000474 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800475 scatterwalk_map_and_copy(iauth_tag, req->src,
476 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600477 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800478}
479
Huang Ying9382d972009-08-06 15:34:26 +1000480static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800481{
Herbert Xu84c91152007-12-17 21:42:08 +0800482 struct aead_request *req = areq->data;
483
484 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800485 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800486
487 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800488}
489
Herbert Xuadcbc682015-06-16 13:54:18 +0800490static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000491{
Huang Ying9382d972009-08-06 15:34:26 +1000492 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800493 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000494 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
495
Herbert Xuadcbc682015-06-16 13:54:18 +0800496 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800497 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
498 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000499}
500
Mikko Herranen28db8e32007-11-26 22:24:11 +0800501static int crypto_gcm_decrypt(struct aead_request *req)
502{
Herbert Xu6160b282007-12-04 19:17:50 +1100503 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800504 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000505 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100506 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000507 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800508 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800509
Herbert Xu6160b282007-12-04 19:17:50 +1100510 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800511
Herbert Xuadcbc682015-06-16 13:54:18 +0800512 crypto_gcm_init_common(req);
513
514 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000515 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800516 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000517
Herbert Xuadcbc682015-06-16 13:54:18 +0800518 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800519}
520
Herbert Xuadcbc682015-06-16 13:54:18 +0800521static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800522{
Herbert Xuadcbc682015-06-16 13:54:18 +0800523 struct aead_instance *inst = aead_alg_instance(tfm);
524 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
525 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800526 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000527 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800528 unsigned long align;
529 int err;
530
Huang Ying9382d972009-08-06 15:34:26 +1000531 ghash = crypto_spawn_ahash(&ictx->ghash);
532 if (IS_ERR(ghash))
533 return PTR_ERR(ghash);
534
Eric Biggers60425a82016-10-28 09:52:19 -0700535 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800536 err = PTR_ERR(ctr);
537 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000538 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800539
540 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000541 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800542
Herbert Xuadcbc682015-06-16 13:54:18 +0800543 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800544 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800545 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800546 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800547 max(sizeof(struct skcipher_request) +
548 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000549 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800550 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800551
552 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000553
554err_free_hash:
555 crypto_free_ahash(ghash);
556 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800557}
558
Herbert Xuadcbc682015-06-16 13:54:18 +0800559static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800560{
Herbert Xuadcbc682015-06-16 13:54:18 +0800561 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800562
Huang Ying9382d972009-08-06 15:34:26 +1000563 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800564 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800565}
566
Herbert Xu7b05a372015-07-09 07:17:30 +0800567static void crypto_gcm_free(struct aead_instance *inst)
568{
569 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
570
571 crypto_drop_skcipher(&ctx->ctr);
572 crypto_drop_ahash(&ctx->ghash);
573 kfree(inst);
574}
575
Herbert Xuadcbc682015-06-16 13:54:18 +0800576static int crypto_gcm_create_common(struct crypto_template *tmpl,
577 struct rtattr **tb,
Herbert Xuadcbc682015-06-16 13:54:18 +0800578 const char *ctr_name,
579 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800580{
Herbert Xud00aa192007-12-07 20:31:10 +0800581 struct crypto_attr_type *algt;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800582 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800583 struct aead_instance *inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800584 struct gcm_instance_ctx *ctx;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800585 struct skcipher_alg *ctr;
586 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800587 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800588
Herbert Xud00aa192007-12-07 20:31:10 +0800589 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800590 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800591 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800592
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800593 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800594 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800595
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800596 mask = crypto_requires_sync(algt->type, algt->mask);
597
Herbert Xu1472e5e2007-12-07 19:26:11 +0800598 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
599 if (!inst)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800600 return -ENOMEM;
Herbert Xuadcbc682015-06-16 13:54:18 +0800601 ctx = aead_instance_ctx(inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800602
603 err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
604 ghash_name, 0, mask);
Huang Ying9382d972009-08-06 15:34:26 +1000605 if (err)
606 goto err_free_inst;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800607 ghash = crypto_spawn_ahash_alg(&ctx->ghash);
Huang Ying9382d972009-08-06 15:34:26 +1000608
Herbert Xuadcbc682015-06-16 13:54:18 +0800609 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700610 if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
611 ghash->digestsize != 16)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800612 goto err_free_inst;
Herbert Xuadcbc682015-06-16 13:54:18 +0800613
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800614 err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
615 ctr_name, 0, mask);
Herbert Xu1472e5e2007-12-07 19:26:11 +0800616 if (err)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800617 goto err_free_inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800618 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800619
Eric Biggersf6995942019-04-18 14:43:02 -0700620 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000621 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700622 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
623 crypto_skcipher_alg_ivsize(ctr) != 16 ||
624 ctr->base.cra_blocksize != 1)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800625 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800626
Mikko Herranen28db8e32007-11-26 22:24:11 +0800627 err = -ENAMETOOLONG;
Eric Biggersf6995942019-04-18 14:43:02 -0700628 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
629 "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800630 goto err_free_inst;
Eric Biggersf6995942019-04-18 14:43:02 -0700631
Herbert Xuadcbc682015-06-16 13:54:18 +0800632 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800633 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Eric Biggersab6ffd32020-01-02 19:58:57 -0800634 ghash->base.cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800635 CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800636 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800637
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800638 inst->alg.base.cra_flags = (ghash->base.cra_flags |
639 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800640 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800641 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800642 inst->alg.base.cra_blocksize = 1;
643 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800644 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800645 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200646 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800647 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800648 inst->alg.maxauthsize = 16;
649 inst->alg.init = crypto_gcm_init_tfm;
650 inst->alg.exit = crypto_gcm_exit_tfm;
651 inst->alg.setkey = crypto_gcm_setkey;
652 inst->alg.setauthsize = crypto_gcm_setauthsize;
653 inst->alg.encrypt = crypto_gcm_encrypt;
654 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800655
Herbert Xu7b05a372015-07-09 07:17:30 +0800656 inst->free = crypto_gcm_free;
657
Herbert Xuadcbc682015-06-16 13:54:18 +0800658 err = aead_register_instance(tmpl, inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800659 if (err) {
Mikko Herranen28db8e32007-11-26 22:24:11 +0800660err_free_inst:
Eric Biggersab6ffd32020-01-02 19:58:57 -0800661 crypto_gcm_free(inst);
662 }
663 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800664}
665
Herbert Xuadcbc682015-06-16 13:54:18 +0800666static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800667{
Herbert Xud00aa192007-12-07 20:31:10 +0800668 const char *cipher_name;
669 char ctr_name[CRYPTO_MAX_ALG_NAME];
Herbert Xud00aa192007-12-07 20:31:10 +0800670
671 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800672 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800673 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800674
675 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
676 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800677 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800678
Eric Biggersf6995942019-04-18 14:43:02 -0700679 return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800680}
681
Herbert Xuadcbc682015-06-16 13:54:18 +0800682static int crypto_gcm_base_create(struct crypto_template *tmpl,
683 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800684{
Herbert Xud00aa192007-12-07 20:31:10 +0800685 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000686 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800687
688 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800689 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800690 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800691
Huang Ying9382d972009-08-06 15:34:26 +1000692 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000693 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800694 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000695
Eric Biggersf6995942019-04-18 14:43:02 -0700696 return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800697}
698
Herbert Xudadbc532007-12-17 15:33:17 +0800699static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
700 unsigned int keylen)
701{
702 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
703 struct crypto_aead *child = ctx->child;
Herbert Xudadbc532007-12-17 15:33:17 +0800704
705 if (keylen < 4)
706 return -EINVAL;
707
708 keylen -= 4;
709 memcpy(ctx->nonce, key + keylen, 4);
710
711 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
712 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
713 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600714 return crypto_aead_setkey(child, key, keylen);
Herbert Xudadbc532007-12-17 15:33:17 +0800715}
716
717static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
718 unsigned int authsize)
719{
720 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
Iuliana Prodan65526f62019-07-31 16:05:54 +0300721 int err;
Herbert Xudadbc532007-12-17 15:33:17 +0800722
Iuliana Prodan65526f62019-07-31 16:05:54 +0300723 err = crypto_rfc4106_check_authsize(authsize);
724 if (err)
725 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800726
727 return crypto_aead_setauthsize(ctx->child, authsize);
728}
729
730static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
731{
Herbert Xu7b05a372015-07-09 07:17:30 +0800732 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800733 struct crypto_aead *aead = crypto_aead_reqtfm(req);
734 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800735 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800736 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800737 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800738 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
739 crypto_aead_alignmask(child) + 1);
740
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200741 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800742
Herbert Xudadbc532007-12-17 15:33:17 +0800743 memcpy(iv, ctx->nonce, 4);
744 memcpy(iv + 4, req->iv, 8);
745
Herbert Xu7b05a372015-07-09 07:17:30 +0800746 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200747 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800748 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
749 if (sg != rctx->src + 1)
750 sg_chain(rctx->src, 2, sg);
751
752 if (req->src != req->dst) {
753 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200754 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800755 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
756 if (sg != rctx->dst + 1)
757 sg_chain(rctx->dst, 2, sg);
758 }
759
Herbert Xudadbc532007-12-17 15:33:17 +0800760 aead_request_set_tfm(subreq, child);
761 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
762 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800763 aead_request_set_crypt(subreq, rctx->src,
764 req->src == req->dst ? rctx->src : rctx->dst,
765 req->cryptlen, iv);
766 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800767
768 return subreq;
769}
770
771static int crypto_rfc4106_encrypt(struct aead_request *req)
772{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300773 int err;
774
775 err = crypto_ipsec_check_assoclen(req->assoclen);
776 if (err)
777 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800778
Herbert Xudadbc532007-12-17 15:33:17 +0800779 req = crypto_rfc4106_crypt(req);
780
781 return crypto_aead_encrypt(req);
782}
783
784static int crypto_rfc4106_decrypt(struct aead_request *req)
785{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300786 int err;
787
788 err = crypto_ipsec_check_assoclen(req->assoclen);
789 if (err)
790 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800791
Herbert Xudadbc532007-12-17 15:33:17 +0800792 req = crypto_rfc4106_crypt(req);
793
794 return crypto_aead_decrypt(req);
795}
796
Herbert Xuadcbc682015-06-16 13:54:18 +0800797static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800798{
Herbert Xuadcbc682015-06-16 13:54:18 +0800799 struct aead_instance *inst = aead_alg_instance(tfm);
800 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
801 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800802 struct crypto_aead *aead;
803 unsigned long align;
804
805 aead = crypto_spawn_aead(spawn);
806 if (IS_ERR(aead))
807 return PTR_ERR(aead);
808
809 ctx->child = aead;
810
811 align = crypto_aead_alignmask(aead);
812 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800813 crypto_aead_set_reqsize(
814 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800815 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800816 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800817 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800818
819 return 0;
820}
821
Herbert Xuadcbc682015-06-16 13:54:18 +0800822static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800823{
Herbert Xuadcbc682015-06-16 13:54:18 +0800824 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800825
826 crypto_free_aead(ctx->child);
827}
828
Herbert Xu7b05a372015-07-09 07:17:30 +0800829static void crypto_rfc4106_free(struct aead_instance *inst)
830{
831 crypto_drop_aead(aead_instance_ctx(inst));
832 kfree(inst);
833}
834
Herbert Xuadcbc682015-06-16 13:54:18 +0800835static int crypto_rfc4106_create(struct crypto_template *tmpl,
836 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800837{
838 struct crypto_attr_type *algt;
Eric Biggerscd900f02020-01-02 19:58:46 -0800839 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800840 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800841 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800842 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800843 int err;
844
845 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800846 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800847 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800848
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800849 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800850 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800851
Eric Biggerscd900f02020-01-02 19:58:46 -0800852 mask = crypto_requires_sync(algt->type, algt->mask);
853
Herbert Xudadbc532007-12-17 15:33:17 +0800854 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
855 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800856 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800857
Herbert Xuadcbc682015-06-16 13:54:18 +0800858 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800859 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
Eric Biggers959ac1c2020-02-25 20:59:18 -0800860 crypto_attr_alg_name(tb[1]), 0, mask);
Herbert Xudadbc532007-12-17 15:33:17 +0800861 if (err)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800862 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800863
Herbert Xuadcbc682015-06-16 13:54:18 +0800864 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800865
866 err = -EINVAL;
867
Herbert Xuadcbc682015-06-16 13:54:18 +0800868 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200869 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800870 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800871
872 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800873 if (alg->base.cra_blocksize != 1)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800874 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800875
876 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800877 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
878 "rfc4106(%s)", alg->base.cra_name) >=
879 CRYPTO_MAX_ALG_NAME ||
880 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
881 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800882 CRYPTO_MAX_ALG_NAME)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800883 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800884
Herbert Xu7b05a372015-07-09 07:17:30 +0800885 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800886 inst->alg.base.cra_priority = alg->base.cra_priority;
887 inst->alg.base.cra_blocksize = 1;
888 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800889
Herbert Xuadcbc682015-06-16 13:54:18 +0800890 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800891
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200892 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800893 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800894 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800895
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 inst->alg.init = crypto_rfc4106_init_tfm;
897 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800898
Herbert Xuadcbc682015-06-16 13:54:18 +0800899 inst->alg.setkey = crypto_rfc4106_setkey;
900 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
901 inst->alg.encrypt = crypto_rfc4106_encrypt;
902 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800903
Herbert Xu7b05a372015-07-09 07:17:30 +0800904 inst->free = crypto_rfc4106_free;
905
Herbert Xuadcbc682015-06-16 13:54:18 +0800906 err = aead_register_instance(tmpl, inst);
Eric Biggers959ac1c2020-02-25 20:59:18 -0800907 if (err) {
908err_free_inst:
909 crypto_rfc4106_free(inst);
910 }
Herbert Xuadcbc682015-06-16 13:54:18 +0800911 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800912}
913
Tobias Brunner73c89c12010-01-17 21:52:11 +1100914static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
915 unsigned int keylen)
916{
917 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
918 struct crypto_aead *child = ctx->child;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100919
920 if (keylen < 4)
921 return -EINVAL;
922
923 keylen -= 4;
924 memcpy(ctx->nonce, key + keylen, 4);
925
926 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
927 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
928 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600929 return crypto_aead_setkey(child, key, keylen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100930}
931
932static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
933 unsigned int authsize)
934{
935 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
936
937 if (authsize != 16)
938 return -EINVAL;
939
940 return crypto_aead_setauthsize(ctx->child, authsize);
941}
942
Herbert Xuadcbc682015-06-16 13:54:18 +0800943static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100944{
945 struct crypto_aead *aead = crypto_aead_reqtfm(req);
946 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800947 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100948 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100949 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100950 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
951 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800952 int err;
953
954 if (req->src != req->dst) {
955 err = crypto_rfc4543_copy_src_to_dst(req, enc);
956 if (err)
957 return err;
958 }
Tobias Brunner73c89c12010-01-17 21:52:11 +1100959
960 memcpy(iv, ctx->nonce, 4);
961 memcpy(iv + 4, req->iv, 8);
962
Tobias Brunner73c89c12010-01-17 21:52:11 +1100963 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +0800964 aead_request_set_callback(subreq, req->base.flags,
965 req->base.complete, req->base.data);
966 aead_request_set_crypt(subreq, req->src, req->dst,
967 enc ? 0 : authsize, iv);
968 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
969 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100970
Herbert Xuadcbc682015-06-16 13:54:18 +0800971 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100972}
973
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300974static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
975{
976 struct crypto_aead *aead = crypto_aead_reqtfm(req);
977 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
978 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800979 unsigned int nbytes = req->assoclen + req->cryptlen -
980 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -0700981 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300982
Kees Cook8d605392018-09-18 19:10:51 -0700983 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800984 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
985 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
986
987 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300988}
989
Tobias Brunner73c89c12010-01-17 21:52:11 +1100990static int crypto_rfc4543_encrypt(struct aead_request *req)
991{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300992 return crypto_ipsec_check_assoclen(req->assoclen) ?:
993 crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100994}
995
996static int crypto_rfc4543_decrypt(struct aead_request *req)
997{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300998 return crypto_ipsec_check_assoclen(req->assoclen) ?:
999 crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001000}
1001
Herbert Xuadcbc682015-06-16 13:54:18 +08001002static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001003{
Herbert Xuadcbc682015-06-16 13:54:18 +08001004 struct aead_instance *inst = aead_alg_instance(tfm);
1005 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001006 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001007 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001008 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -07001009 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001010 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001011 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001012
1013 aead = crypto_spawn_aead(spawn);
1014 if (IS_ERR(aead))
1015 return PTR_ERR(aead);
1016
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001017 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001018 err = PTR_ERR(null);
1019 if (IS_ERR(null))
1020 goto err_free_aead;
1021
Tobias Brunner73c89c12010-01-17 21:52:11 +11001022 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001023 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001024
1025 align = crypto_aead_alignmask(aead);
1026 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001027 crypto_aead_set_reqsize(
1028 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001029 sizeof(struct crypto_rfc4543_req_ctx) +
1030 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001031 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001032
1033 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001034
1035err_free_aead:
1036 crypto_free_aead(aead);
1037 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001038}
1039
Herbert Xuadcbc682015-06-16 13:54:18 +08001040static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001041{
Herbert Xuadcbc682015-06-16 13:54:18 +08001042 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001043
1044 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001045 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001046}
1047
Herbert Xu7b05a372015-07-09 07:17:30 +08001048static void crypto_rfc4543_free(struct aead_instance *inst)
1049{
1050 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1051
1052 crypto_drop_aead(&ctx->aead);
1053
1054 kfree(inst);
1055}
1056
Herbert Xuadcbc682015-06-16 13:54:18 +08001057static int crypto_rfc4543_create(struct crypto_template *tmpl,
1058 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001059{
1060 struct crypto_attr_type *algt;
Eric Biggerscd900f02020-01-02 19:58:46 -08001061 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +08001062 struct aead_instance *inst;
Herbert Xuadcbc682015-06-16 13:54:18 +08001063 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001064 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001065 int err;
1066
1067 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001068 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001069 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001070
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001071 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001072 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001073
Eric Biggerscd900f02020-01-02 19:58:46 -08001074 mask = crypto_requires_sync(algt->type, algt->mask);
1075
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001076 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001077 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001078 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001079
Herbert Xuadcbc682015-06-16 13:54:18 +08001080 ctx = aead_instance_ctx(inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001081 err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst),
1082 crypto_attr_alg_name(tb[1]), 0, mask);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001083 if (err)
Eric Biggersc4caa562020-02-25 20:59:19 -08001084 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001085
Eric Biggersc4caa562020-02-25 20:59:19 -08001086 alg = crypto_spawn_aead_alg(&ctx->aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001087
1088 err = -EINVAL;
1089
Herbert Xuadcbc682015-06-16 13:54:18 +08001090 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001091 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggersc4caa562020-02-25 20:59:19 -08001092 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001093
1094 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001095 if (alg->base.cra_blocksize != 1)
Eric Biggersc4caa562020-02-25 20:59:19 -08001096 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001097
1098 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001099 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1100 "rfc4543(%s)", alg->base.cra_name) >=
1101 CRYPTO_MAX_ALG_NAME ||
1102 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1103 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001104 CRYPTO_MAX_ALG_NAME)
Eric Biggersc4caa562020-02-25 20:59:19 -08001105 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001106
Herbert Xuadcbc682015-06-16 13:54:18 +08001107 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1108 inst->alg.base.cra_priority = alg->base.cra_priority;
1109 inst->alg.base.cra_blocksize = 1;
1110 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001111
Herbert Xuadcbc682015-06-16 13:54:18 +08001112 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001113
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001114 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001115 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001116 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001117
Herbert Xuadcbc682015-06-16 13:54:18 +08001118 inst->alg.init = crypto_rfc4543_init_tfm;
1119 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001120
Herbert Xuadcbc682015-06-16 13:54:18 +08001121 inst->alg.setkey = crypto_rfc4543_setkey;
1122 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1123 inst->alg.encrypt = crypto_rfc4543_encrypt;
1124 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125
Eric Biggersc4caa562020-02-25 20:59:19 -08001126 inst->free = crypto_rfc4543_free;
Herbert Xu7b05a372015-07-09 07:17:30 +08001127
Herbert Xuadcbc682015-06-16 13:54:18 +08001128 err = aead_register_instance(tmpl, inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001129 if (err) {
1130err_free_inst:
1131 crypto_rfc4543_free(inst);
1132 }
Herbert Xuadcbc682015-06-16 13:54:18 +08001133 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001134}
1135
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001136static struct crypto_template crypto_gcm_tmpls[] = {
1137 {
1138 .name = "gcm_base",
1139 .create = crypto_gcm_base_create,
1140 .module = THIS_MODULE,
1141 }, {
1142 .name = "gcm",
1143 .create = crypto_gcm_create,
1144 .module = THIS_MODULE,
1145 }, {
1146 .name = "rfc4106",
1147 .create = crypto_rfc4106_create,
1148 .module = THIS_MODULE,
1149 }, {
1150 .name = "rfc4543",
1151 .create = crypto_rfc4543_create,
1152 .module = THIS_MODULE,
1153 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001154};
1155
Mikko Herranen28db8e32007-11-26 22:24:11 +08001156static int __init crypto_gcm_module_init(void)
1157{
Herbert Xud00aa192007-12-07 20:31:10 +08001158 int err;
1159
Herbert Xuadcbc682015-06-16 13:54:18 +08001160 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001161 if (!gcm_zeroes)
1162 return -ENOMEM;
1163
Herbert Xuadcbc682015-06-16 13:54:18 +08001164 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1165
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001166 err = crypto_register_templates(crypto_gcm_tmpls,
1167 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001168 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001169 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001170
Huang Ying9382d972009-08-06 15:34:26 +10001171 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001172}
1173
1174static void __exit crypto_gcm_module_exit(void)
1175{
Huang Ying9382d972009-08-06 15:34:26 +10001176 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001177 crypto_unregister_templates(crypto_gcm_tmpls,
1178 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001179}
1180
Eric Biggersc4741b22019-04-11 21:57:42 -07001181subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001182module_exit(crypto_gcm_module_exit);
1183
1184MODULE_LICENSE("GPL");
1185MODULE_DESCRIPTION("Galois/Counter Mode");
1186MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001187MODULE_ALIAS_CRYPTO("gcm_base");
1188MODULE_ALIAS_CRYPTO("rfc4106");
1189MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001190MODULE_ALIAS_CRYPTO("gcm");