blob: 338ee076974796423950edfaaf69abaa36c41959 [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:
Waiman Long453431a2020-08-06 23:18:13 -0700142 kfree_sensitive(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{
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800581 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800582 struct aead_instance *inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800583 struct gcm_instance_ctx *ctx;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800584 struct skcipher_alg *ctr;
585 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800586 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800587
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700588 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
589 if (err)
590 return err;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800591
Herbert Xu1472e5e2007-12-07 19:26:11 +0800592 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
593 if (!inst)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800594 return -ENOMEM;
Herbert Xuadcbc682015-06-16 13:54:18 +0800595 ctx = aead_instance_ctx(inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800596
597 err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
598 ghash_name, 0, mask);
Huang Ying9382d972009-08-06 15:34:26 +1000599 if (err)
600 goto err_free_inst;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800601 ghash = crypto_spawn_ahash_alg(&ctx->ghash);
Huang Ying9382d972009-08-06 15:34:26 +1000602
Herbert Xuadcbc682015-06-16 13:54:18 +0800603 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700604 if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
605 ghash->digestsize != 16)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800606 goto err_free_inst;
Herbert Xuadcbc682015-06-16 13:54:18 +0800607
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800608 err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
609 ctr_name, 0, mask);
Herbert Xu1472e5e2007-12-07 19:26:11 +0800610 if (err)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800611 goto err_free_inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800612 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800613
Eric Biggersf6995942019-04-18 14:43:02 -0700614 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000615 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700616 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
617 crypto_skcipher_alg_ivsize(ctr) != 16 ||
618 ctr->base.cra_blocksize != 1)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800619 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800620
Mikko Herranen28db8e32007-11-26 22:24:11 +0800621 err = -ENAMETOOLONG;
Eric Biggersf6995942019-04-18 14:43:02 -0700622 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
623 "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800624 goto err_free_inst;
Eric Biggersf6995942019-04-18 14:43:02 -0700625
Herbert Xuadcbc682015-06-16 13:54:18 +0800626 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800627 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Eric Biggersab6ffd32020-01-02 19:58:57 -0800628 ghash->base.cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800629 CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800630 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800631
Herbert Xuadcbc682015-06-16 13:54:18 +0800632 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800633 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800634 inst->alg.base.cra_blocksize = 1;
635 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800636 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800637 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200638 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800639 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800640 inst->alg.maxauthsize = 16;
641 inst->alg.init = crypto_gcm_init_tfm;
642 inst->alg.exit = crypto_gcm_exit_tfm;
643 inst->alg.setkey = crypto_gcm_setkey;
644 inst->alg.setauthsize = crypto_gcm_setauthsize;
645 inst->alg.encrypt = crypto_gcm_encrypt;
646 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800647
Herbert Xu7b05a372015-07-09 07:17:30 +0800648 inst->free = crypto_gcm_free;
649
Herbert Xuadcbc682015-06-16 13:54:18 +0800650 err = aead_register_instance(tmpl, inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800651 if (err) {
Mikko Herranen28db8e32007-11-26 22:24:11 +0800652err_free_inst:
Eric Biggersab6ffd32020-01-02 19:58:57 -0800653 crypto_gcm_free(inst);
654 }
655 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800656}
657
Herbert Xuadcbc682015-06-16 13:54:18 +0800658static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800659{
Herbert Xud00aa192007-12-07 20:31:10 +0800660 const char *cipher_name;
661 char ctr_name[CRYPTO_MAX_ALG_NAME];
Herbert Xud00aa192007-12-07 20:31:10 +0800662
663 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800664 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800665 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800666
667 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
668 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800669 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800670
Eric Biggersf6995942019-04-18 14:43:02 -0700671 return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800672}
673
Herbert Xuadcbc682015-06-16 13:54:18 +0800674static int crypto_gcm_base_create(struct crypto_template *tmpl,
675 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800676{
Herbert Xud00aa192007-12-07 20:31:10 +0800677 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000678 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800679
680 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800681 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800682 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800683
Huang Ying9382d972009-08-06 15:34:26 +1000684 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000685 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800686 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000687
Eric Biggersf6995942019-04-18 14:43:02 -0700688 return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800689}
690
Herbert Xudadbc532007-12-17 15:33:17 +0800691static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
692 unsigned int keylen)
693{
694 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
695 struct crypto_aead *child = ctx->child;
Herbert Xudadbc532007-12-17 15:33:17 +0800696
697 if (keylen < 4)
698 return -EINVAL;
699
700 keylen -= 4;
701 memcpy(ctx->nonce, key + keylen, 4);
702
703 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
704 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
705 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600706 return crypto_aead_setkey(child, key, keylen);
Herbert Xudadbc532007-12-17 15:33:17 +0800707}
708
709static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
710 unsigned int authsize)
711{
712 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
Iuliana Prodan65526f62019-07-31 16:05:54 +0300713 int err;
Herbert Xudadbc532007-12-17 15:33:17 +0800714
Iuliana Prodan65526f62019-07-31 16:05:54 +0300715 err = crypto_rfc4106_check_authsize(authsize);
716 if (err)
717 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800718
719 return crypto_aead_setauthsize(ctx->child, authsize);
720}
721
722static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
723{
Herbert Xu7b05a372015-07-09 07:17:30 +0800724 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800725 struct crypto_aead *aead = crypto_aead_reqtfm(req);
726 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800727 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800728 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800729 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800730 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
731 crypto_aead_alignmask(child) + 1);
732
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200733 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800734
Herbert Xudadbc532007-12-17 15:33:17 +0800735 memcpy(iv, ctx->nonce, 4);
736 memcpy(iv + 4, req->iv, 8);
737
Herbert Xu7b05a372015-07-09 07:17:30 +0800738 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200739 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800740 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
741 if (sg != rctx->src + 1)
742 sg_chain(rctx->src, 2, sg);
743
744 if (req->src != req->dst) {
745 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200746 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800747 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
748 if (sg != rctx->dst + 1)
749 sg_chain(rctx->dst, 2, sg);
750 }
751
Herbert Xudadbc532007-12-17 15:33:17 +0800752 aead_request_set_tfm(subreq, child);
753 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
754 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800755 aead_request_set_crypt(subreq, rctx->src,
756 req->src == req->dst ? rctx->src : rctx->dst,
757 req->cryptlen, iv);
758 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800759
760 return subreq;
761}
762
763static int crypto_rfc4106_encrypt(struct aead_request *req)
764{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300765 int err;
766
767 err = crypto_ipsec_check_assoclen(req->assoclen);
768 if (err)
769 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800770
Herbert Xudadbc532007-12-17 15:33:17 +0800771 req = crypto_rfc4106_crypt(req);
772
773 return crypto_aead_encrypt(req);
774}
775
776static int crypto_rfc4106_decrypt(struct aead_request *req)
777{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300778 int err;
779
780 err = crypto_ipsec_check_assoclen(req->assoclen);
781 if (err)
782 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800783
Herbert Xudadbc532007-12-17 15:33:17 +0800784 req = crypto_rfc4106_crypt(req);
785
786 return crypto_aead_decrypt(req);
787}
788
Herbert Xuadcbc682015-06-16 13:54:18 +0800789static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800790{
Herbert Xuadcbc682015-06-16 13:54:18 +0800791 struct aead_instance *inst = aead_alg_instance(tfm);
792 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
793 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800794 struct crypto_aead *aead;
795 unsigned long align;
796
797 aead = crypto_spawn_aead(spawn);
798 if (IS_ERR(aead))
799 return PTR_ERR(aead);
800
801 ctx->child = aead;
802
803 align = crypto_aead_alignmask(aead);
804 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800805 crypto_aead_set_reqsize(
806 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800807 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800808 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800809 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800810
811 return 0;
812}
813
Herbert Xuadcbc682015-06-16 13:54:18 +0800814static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800815{
Herbert Xuadcbc682015-06-16 13:54:18 +0800816 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800817
818 crypto_free_aead(ctx->child);
819}
820
Herbert Xu7b05a372015-07-09 07:17:30 +0800821static void crypto_rfc4106_free(struct aead_instance *inst)
822{
823 crypto_drop_aead(aead_instance_ctx(inst));
824 kfree(inst);
825}
826
Herbert Xuadcbc682015-06-16 13:54:18 +0800827static int crypto_rfc4106_create(struct crypto_template *tmpl,
828 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800829{
Eric Biggerscd900f02020-01-02 19:58:46 -0800830 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800831 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800832 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800833 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800834 int err;
835
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700836 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
837 if (err)
838 return err;
Eric Biggerscd900f02020-01-02 19:58:46 -0800839
Herbert Xudadbc532007-12-17 15:33:17 +0800840 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
841 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800842 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800843
Herbert Xuadcbc682015-06-16 13:54:18 +0800844 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800845 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
Eric Biggers959ac1c2020-02-25 20:59:18 -0800846 crypto_attr_alg_name(tb[1]), 0, mask);
Herbert Xudadbc532007-12-17 15:33:17 +0800847 if (err)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800848 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800849
Herbert Xuadcbc682015-06-16 13:54:18 +0800850 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800851
852 err = -EINVAL;
853
Herbert Xuadcbc682015-06-16 13:54:18 +0800854 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200855 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800856 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800857
858 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800859 if (alg->base.cra_blocksize != 1)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800860 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800861
862 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800863 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
864 "rfc4106(%s)", alg->base.cra_name) >=
865 CRYPTO_MAX_ALG_NAME ||
866 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
867 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800868 CRYPTO_MAX_ALG_NAME)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800869 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800870
Herbert Xuadcbc682015-06-16 13:54:18 +0800871 inst->alg.base.cra_priority = alg->base.cra_priority;
872 inst->alg.base.cra_blocksize = 1;
873 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800874
Herbert Xuadcbc682015-06-16 13:54:18 +0800875 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800876
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200877 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800878 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800879 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800880
Herbert Xuadcbc682015-06-16 13:54:18 +0800881 inst->alg.init = crypto_rfc4106_init_tfm;
882 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800883
Herbert Xuadcbc682015-06-16 13:54:18 +0800884 inst->alg.setkey = crypto_rfc4106_setkey;
885 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
886 inst->alg.encrypt = crypto_rfc4106_encrypt;
887 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800888
Herbert Xu7b05a372015-07-09 07:17:30 +0800889 inst->free = crypto_rfc4106_free;
890
Herbert Xuadcbc682015-06-16 13:54:18 +0800891 err = aead_register_instance(tmpl, inst);
Eric Biggers959ac1c2020-02-25 20:59:18 -0800892 if (err) {
893err_free_inst:
894 crypto_rfc4106_free(inst);
895 }
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800897}
898
Tobias Brunner73c89c12010-01-17 21:52:11 +1100899static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
900 unsigned int keylen)
901{
902 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
903 struct crypto_aead *child = ctx->child;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100904
905 if (keylen < 4)
906 return -EINVAL;
907
908 keylen -= 4;
909 memcpy(ctx->nonce, key + keylen, 4);
910
911 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
912 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
913 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600914 return crypto_aead_setkey(child, key, keylen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100915}
916
917static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
918 unsigned int authsize)
919{
920 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
921
922 if (authsize != 16)
923 return -EINVAL;
924
925 return crypto_aead_setauthsize(ctx->child, authsize);
926}
927
Herbert Xuadcbc682015-06-16 13:54:18 +0800928static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100929{
930 struct crypto_aead *aead = crypto_aead_reqtfm(req);
931 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800932 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100933 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100934 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100935 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
936 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800937 int err;
938
939 if (req->src != req->dst) {
940 err = crypto_rfc4543_copy_src_to_dst(req, enc);
941 if (err)
942 return err;
943 }
Tobias Brunner73c89c12010-01-17 21:52:11 +1100944
945 memcpy(iv, ctx->nonce, 4);
946 memcpy(iv + 4, req->iv, 8);
947
Tobias Brunner73c89c12010-01-17 21:52:11 +1100948 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +0800949 aead_request_set_callback(subreq, req->base.flags,
950 req->base.complete, req->base.data);
951 aead_request_set_crypt(subreq, req->src, req->dst,
952 enc ? 0 : authsize, iv);
953 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
954 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100955
Herbert Xuadcbc682015-06-16 13:54:18 +0800956 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100957}
958
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300959static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
960{
961 struct crypto_aead *aead = crypto_aead_reqtfm(req);
962 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
963 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800964 unsigned int nbytes = req->assoclen + req->cryptlen -
965 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -0700966 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300967
Kees Cook8d605392018-09-18 19:10:51 -0700968 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800969 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
970 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
971
972 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300973}
974
Tobias Brunner73c89c12010-01-17 21:52:11 +1100975static int crypto_rfc4543_encrypt(struct aead_request *req)
976{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300977 return crypto_ipsec_check_assoclen(req->assoclen) ?:
978 crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100979}
980
981static int crypto_rfc4543_decrypt(struct aead_request *req)
982{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300983 return crypto_ipsec_check_assoclen(req->assoclen) ?:
984 crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100985}
986
Herbert Xuadcbc682015-06-16 13:54:18 +0800987static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100988{
Herbert Xuadcbc682015-06-16 13:54:18 +0800989 struct aead_instance *inst = aead_alg_instance(tfm);
990 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300991 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +0800992 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100993 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -0700994 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100995 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300996 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100997
998 aead = crypto_spawn_aead(spawn);
999 if (IS_ERR(aead))
1000 return PTR_ERR(aead);
1001
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001002 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001003 err = PTR_ERR(null);
1004 if (IS_ERR(null))
1005 goto err_free_aead;
1006
Tobias Brunner73c89c12010-01-17 21:52:11 +11001007 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001008 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001009
1010 align = crypto_aead_alignmask(aead);
1011 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001012 crypto_aead_set_reqsize(
1013 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001014 sizeof(struct crypto_rfc4543_req_ctx) +
1015 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001016 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001017
1018 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001019
1020err_free_aead:
1021 crypto_free_aead(aead);
1022 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001023}
1024
Herbert Xuadcbc682015-06-16 13:54:18 +08001025static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001026{
Herbert Xuadcbc682015-06-16 13:54:18 +08001027 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001028
1029 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001030 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001031}
1032
Herbert Xu7b05a372015-07-09 07:17:30 +08001033static void crypto_rfc4543_free(struct aead_instance *inst)
1034{
1035 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1036
1037 crypto_drop_aead(&ctx->aead);
1038
1039 kfree(inst);
1040}
1041
Herbert Xuadcbc682015-06-16 13:54:18 +08001042static int crypto_rfc4543_create(struct crypto_template *tmpl,
1043 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001044{
Eric Biggerscd900f02020-01-02 19:58:46 -08001045 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +08001046 struct aead_instance *inst;
Herbert Xuadcbc682015-06-16 13:54:18 +08001047 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001048 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001049 int err;
1050
Eric Biggers7bcb2c92020-07-09 23:20:38 -07001051 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
1052 if (err)
1053 return err;
Eric Biggerscd900f02020-01-02 19:58:46 -08001054
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001055 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001056 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001057 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001058
Herbert Xuadcbc682015-06-16 13:54:18 +08001059 ctx = aead_instance_ctx(inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001060 err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst),
1061 crypto_attr_alg_name(tb[1]), 0, mask);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001062 if (err)
Eric Biggersc4caa562020-02-25 20:59:19 -08001063 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001064
Eric Biggersc4caa562020-02-25 20:59:19 -08001065 alg = crypto_spawn_aead_alg(&ctx->aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001066
1067 err = -EINVAL;
1068
Herbert Xuadcbc682015-06-16 13:54:18 +08001069 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001070 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggersc4caa562020-02-25 20:59:19 -08001071 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001072
1073 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001074 if (alg->base.cra_blocksize != 1)
Eric Biggersc4caa562020-02-25 20:59:19 -08001075 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001076
1077 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001078 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1079 "rfc4543(%s)", alg->base.cra_name) >=
1080 CRYPTO_MAX_ALG_NAME ||
1081 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1082 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001083 CRYPTO_MAX_ALG_NAME)
Eric Biggersc4caa562020-02-25 20:59:19 -08001084 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001085
Herbert Xuadcbc682015-06-16 13:54:18 +08001086 inst->alg.base.cra_priority = alg->base.cra_priority;
1087 inst->alg.base.cra_blocksize = 1;
1088 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001089
Herbert Xuadcbc682015-06-16 13:54:18 +08001090 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001091
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001092 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001093 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001094 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001095
Herbert Xuadcbc682015-06-16 13:54:18 +08001096 inst->alg.init = crypto_rfc4543_init_tfm;
1097 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001098
Herbert Xuadcbc682015-06-16 13:54:18 +08001099 inst->alg.setkey = crypto_rfc4543_setkey;
1100 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1101 inst->alg.encrypt = crypto_rfc4543_encrypt;
1102 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001103
Eric Biggersc4caa562020-02-25 20:59:19 -08001104 inst->free = crypto_rfc4543_free;
Herbert Xu7b05a372015-07-09 07:17:30 +08001105
Herbert Xuadcbc682015-06-16 13:54:18 +08001106 err = aead_register_instance(tmpl, inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001107 if (err) {
1108err_free_inst:
1109 crypto_rfc4543_free(inst);
1110 }
Herbert Xuadcbc682015-06-16 13:54:18 +08001111 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001112}
1113
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001114static struct crypto_template crypto_gcm_tmpls[] = {
1115 {
1116 .name = "gcm_base",
1117 .create = crypto_gcm_base_create,
1118 .module = THIS_MODULE,
1119 }, {
1120 .name = "gcm",
1121 .create = crypto_gcm_create,
1122 .module = THIS_MODULE,
1123 }, {
1124 .name = "rfc4106",
1125 .create = crypto_rfc4106_create,
1126 .module = THIS_MODULE,
1127 }, {
1128 .name = "rfc4543",
1129 .create = crypto_rfc4543_create,
1130 .module = THIS_MODULE,
1131 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001132};
1133
Mikko Herranen28db8e32007-11-26 22:24:11 +08001134static int __init crypto_gcm_module_init(void)
1135{
Herbert Xud00aa192007-12-07 20:31:10 +08001136 int err;
1137
Herbert Xuadcbc682015-06-16 13:54:18 +08001138 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001139 if (!gcm_zeroes)
1140 return -ENOMEM;
1141
Herbert Xuadcbc682015-06-16 13:54:18 +08001142 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1143
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001144 err = crypto_register_templates(crypto_gcm_tmpls,
1145 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001146 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001147 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001148
Huang Ying9382d972009-08-06 15:34:26 +10001149 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001150}
1151
1152static void __exit crypto_gcm_module_exit(void)
1153{
Huang Ying9382d972009-08-06 15:34:26 +10001154 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001155 crypto_unregister_templates(crypto_gcm_tmpls,
1156 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001157}
1158
Eric Biggersc4741b22019-04-11 21:57:42 -07001159subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001160module_exit(crypto_gcm_module_exit);
1161
1162MODULE_LICENSE("GPL");
1163MODULE_DESCRIPTION("Galois/Counter Mode");
1164MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001165MODULE_ALIAS_CRYPTO("gcm_base");
1166MODULE_ALIAS_CRYPTO("rfc4106");
1167MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001168MODULE_ALIAS_CRYPTO("gcm");