blob: 84f7c23d14e48354c953f21cda4e7423e21932f2 [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
Herbert Xu255e48e2023-02-08 13:58:44 +0800200static void gcm_hash_len_done(void *data, 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
Herbert Xu255e48e2023-02-08 13:58:44 +0800249static void gcm_hash_len_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000250{
Herbert Xu255e48e2023-02-08 13:58:44 +0800251 struct aead_request *req = 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
Herbert Xu255e48e2023-02-08 13:58:44 +0800270static void gcm_hash_crypt_remain_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000271{
Herbert Xu255e48e2023-02-08 13:58:44 +0800272 struct aead_request *req = data;
Huang Ying62c55932009-11-16 21:52:22 +0800273
Herbert Xuadcbc682015-06-16 13:54:18 +0800274 if (err)
275 goto out;
276
277 err = gcm_hash_crypt_remain_continue(req, 0);
278 if (err == -EINPROGRESS)
279 return;
280
281out:
282 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800283}
284
Herbert Xuadcbc682015-06-16 13:54:18 +0800285static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800286{
Huang Ying9382d972009-08-06 15:34:26 +1000287 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
288 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
289 unsigned int remain;
290
Herbert Xuadcbc682015-06-16 13:54:18 +0800291 remain = gcm_remain(gctx->cryptlen);
292 if (remain)
293 return gcm_hash_remain(req, remain,
294 gcm_hash_crypt_remain_done, flags) ?:
295 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000296
Herbert Xuadcbc682015-06-16 13:54:18 +0800297 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000298}
299
Herbert Xu255e48e2023-02-08 13:58:44 +0800300static void gcm_hash_crypt_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000301{
Herbert Xu255e48e2023-02-08 13:58:44 +0800302 struct aead_request *req = data;
Huang Ying62c55932009-11-16 21:52:22 +0800303
Herbert Xuadcbc682015-06-16 13:54:18 +0800304 if (err)
305 goto out;
306
307 err = gcm_hash_crypt_continue(req, 0);
308 if (err == -EINPROGRESS)
309 return;
310
311out:
312 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800313}
314
Herbert Xuadcbc682015-06-16 13:54:18 +0800315static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800316{
Huang Ying9382d972009-08-06 15:34:26 +1000317 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
318 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000319
Herbert Xuadcbc682015-06-16 13:54:18 +0800320 if (gctx->cryptlen)
321 return gcm_hash_update(req, gcm_hash_crypt_done,
322 gctx->src, gctx->cryptlen, flags) ?:
323 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000324
Herbert Xuadcbc682015-06-16 13:54:18 +0800325 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000326}
327
Herbert Xu255e48e2023-02-08 13:58:44 +0800328static void gcm_hash_assoc_remain_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000329{
Herbert Xu255e48e2023-02-08 13:58:44 +0800330 struct aead_request *req = data;
Huang Ying62c55932009-11-16 21:52:22 +0800331
Herbert Xuadcbc682015-06-16 13:54:18 +0800332 if (err)
333 goto out;
334
335 err = gcm_hash_assoc_remain_continue(req, 0);
336 if (err == -EINPROGRESS)
337 return;
338
339out:
340 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800341}
342
Herbert Xuadcbc682015-06-16 13:54:18 +0800343static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800344{
Huang Ying9382d972009-08-06 15:34:26 +1000345 unsigned int remain;
346
Herbert Xuadcbc682015-06-16 13:54:18 +0800347 remain = gcm_remain(req->assoclen);
348 if (remain)
349 return gcm_hash_remain(req, remain,
350 gcm_hash_assoc_remain_done, flags) ?:
351 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000352
Herbert Xuadcbc682015-06-16 13:54:18 +0800353 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000354}
355
Herbert Xu255e48e2023-02-08 13:58:44 +0800356static void gcm_hash_assoc_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000357{
Herbert Xu255e48e2023-02-08 13:58:44 +0800358 struct aead_request *req = data;
Huang Ying62c55932009-11-16 21:52:22 +0800359
Herbert Xuadcbc682015-06-16 13:54:18 +0800360 if (err)
361 goto out;
362
363 err = gcm_hash_assoc_continue(req, 0);
364 if (err == -EINPROGRESS)
365 return;
366
367out:
368 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800369}
370
Herbert Xuadcbc682015-06-16 13:54:18 +0800371static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800372{
Herbert Xuadcbc682015-06-16 13:54:18 +0800373 if (req->assoclen)
374 return gcm_hash_update(req, gcm_hash_assoc_done,
375 req->src, req->assoclen, flags) ?:
376 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000377
Herbert Xuadcbc682015-06-16 13:54:18 +0800378 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800379}
380
Herbert Xu255e48e2023-02-08 13:58:44 +0800381static void gcm_hash_init_done(void *data, int err)
Huang Ying62c55932009-11-16 21:52:22 +0800382{
Herbert Xu255e48e2023-02-08 13:58:44 +0800383 struct aead_request *req = data;
Huang Ying62c55932009-11-16 21:52:22 +0800384
Herbert Xuadcbc682015-06-16 13:54:18 +0800385 if (err)
386 goto out;
387
388 err = gcm_hash_init_continue(req, 0);
389 if (err == -EINPROGRESS)
390 return;
391
392out:
393 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000394}
395
Herbert Xuadcbc682015-06-16 13:54:18 +0800396static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000397{
Herbert Xuadcbc682015-06-16 13:54:18 +0800398 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000399 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800400 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000401
402 ahash_request_set_tfm(ahreq, ctx->ghash);
403
Herbert Xuadcbc682015-06-16 13:54:18 +0800404 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
405 return crypto_ahash_init(ahreq) ?:
406 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100407}
408
Herbert Xuadcbc682015-06-16 13:54:18 +0800409static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000410{
Herbert Xuadcbc682015-06-16 13:54:18 +0800411 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000412 struct crypto_aead *aead = crypto_aead_reqtfm(req);
413 u8 *auth_tag = pctx->auth_tag;
414
Herbert Xuadcbc682015-06-16 13:54:18 +0800415 crypto_xor(auth_tag, pctx->iauth_tag, 16);
416 scatterwalk_map_and_copy(auth_tag, req->dst,
417 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000418 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800419 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000420}
421
Herbert Xuadcbc682015-06-16 13:54:18 +0800422static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100423{
Huang Ying9382d972009-08-06 15:34:26 +1000424 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800425 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100426
Herbert Xuadcbc682015-06-16 13:54:18 +0800427 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
428 gctx->cryptlen = req->cryptlen;
429 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100430
Herbert Xuadcbc682015-06-16 13:54:18 +0800431 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800432}
433
Herbert Xu255e48e2023-02-08 13:58:44 +0800434static void gcm_encrypt_done(void *data, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000435{
Herbert Xu255e48e2023-02-08 13:58:44 +0800436 struct aead_request *req = data;
Huang Ying9382d972009-08-06 15:34:26 +1000437
Herbert Xuadcbc682015-06-16 13:54:18 +0800438 if (err)
439 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000440
Herbert Xuadcbc682015-06-16 13:54:18 +0800441 err = gcm_encrypt_continue(req, 0);
442 if (err == -EINPROGRESS)
443 return;
444
445out:
Huang Ying62c55932009-11-16 21:52:22 +0800446 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000447}
448
Mikko Herranen28db8e32007-11-26 22:24:11 +0800449static int crypto_gcm_encrypt(struct aead_request *req)
450{
Herbert Xu25894692007-12-12 19:16:38 +0800451 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800452 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800453 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800454
Herbert Xuadcbc682015-06-16 13:54:18 +0800455 crypto_gcm_init_common(req);
456 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800457 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000458
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800459 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800460 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800461}
462
Herbert Xuadcbc682015-06-16 13:54:18 +0800463static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800464{
Herbert Xuadcbc682015-06-16 13:54:18 +0800465 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800466 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800467 u8 *auth_tag = pctx->auth_tag;
468 u8 *iauth_tag = pctx->iauth_tag;
469 unsigned int authsize = crypto_aead_authsize(aead);
470 unsigned int cryptlen = req->cryptlen - authsize;
471
Huang Ying9382d972009-08-06 15:34:26 +1000472 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800473 scatterwalk_map_and_copy(iauth_tag, req->src,
474 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600475 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800476}
477
Herbert Xu255e48e2023-02-08 13:58:44 +0800478static void gcm_decrypt_done(void *data, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800479{
Herbert Xu255e48e2023-02-08 13:58:44 +0800480 struct aead_request *req = data;
Herbert Xu84c91152007-12-17 21:42:08 +0800481
482 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800483 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800484
485 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800486}
487
Herbert Xuadcbc682015-06-16 13:54:18 +0800488static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000489{
Huang Ying9382d972009-08-06 15:34:26 +1000490 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800491 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000492 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
493
Herbert Xuadcbc682015-06-16 13:54:18 +0800494 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800495 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
496 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000497}
498
Mikko Herranen28db8e32007-11-26 22:24:11 +0800499static int crypto_gcm_decrypt(struct aead_request *req)
500{
Herbert Xu6160b282007-12-04 19:17:50 +1100501 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800502 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000503 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100504 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000505 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800506 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800507
Herbert Xu6160b282007-12-04 19:17:50 +1100508 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800509
Herbert Xuadcbc682015-06-16 13:54:18 +0800510 crypto_gcm_init_common(req);
511
512 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000513 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800514 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000515
Herbert Xuadcbc682015-06-16 13:54:18 +0800516 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800517}
518
Herbert Xuadcbc682015-06-16 13:54:18 +0800519static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800520{
Herbert Xuadcbc682015-06-16 13:54:18 +0800521 struct aead_instance *inst = aead_alg_instance(tfm);
522 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
523 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800524 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000525 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800526 unsigned long align;
527 int err;
528
Huang Ying9382d972009-08-06 15:34:26 +1000529 ghash = crypto_spawn_ahash(&ictx->ghash);
530 if (IS_ERR(ghash))
531 return PTR_ERR(ghash);
532
Eric Biggers60425a82016-10-28 09:52:19 -0700533 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800534 err = PTR_ERR(ctr);
535 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000536 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800537
538 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000539 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800540
Herbert Xuadcbc682015-06-16 13:54:18 +0800541 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800542 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800543 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800544 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800545 max(sizeof(struct skcipher_request) +
546 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000547 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800548 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800549
550 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000551
552err_free_hash:
553 crypto_free_ahash(ghash);
554 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800555}
556
Herbert Xuadcbc682015-06-16 13:54:18 +0800557static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800558{
Herbert Xuadcbc682015-06-16 13:54:18 +0800559 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800560
Huang Ying9382d972009-08-06 15:34:26 +1000561 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800562 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800563}
564
Herbert Xu7b05a372015-07-09 07:17:30 +0800565static void crypto_gcm_free(struct aead_instance *inst)
566{
567 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
568
569 crypto_drop_skcipher(&ctx->ctr);
570 crypto_drop_ahash(&ctx->ghash);
571 kfree(inst);
572}
573
Herbert Xuadcbc682015-06-16 13:54:18 +0800574static int crypto_gcm_create_common(struct crypto_template *tmpl,
575 struct rtattr **tb,
Herbert Xuadcbc682015-06-16 13:54:18 +0800576 const char *ctr_name,
577 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800578{
Herbert Xu712c22a2023-10-03 11:43:29 +0800579 struct skcipher_alg_common *ctr;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800580 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800581 struct aead_instance *inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800582 struct gcm_instance_ctx *ctx;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800583 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800584 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800585
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700586 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
587 if (err)
588 return err;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800589
Herbert Xu1472e5e2007-12-07 19:26:11 +0800590 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
591 if (!inst)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800592 return -ENOMEM;
Herbert Xuadcbc682015-06-16 13:54:18 +0800593 ctx = aead_instance_ctx(inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800594
595 err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
596 ghash_name, 0, mask);
Huang Ying9382d972009-08-06 15:34:26 +1000597 if (err)
598 goto err_free_inst;
Eric Biggersab6ffd32020-01-02 19:58:57 -0800599 ghash = crypto_spawn_ahash_alg(&ctx->ghash);
Huang Ying9382d972009-08-06 15:34:26 +1000600
Herbert Xuadcbc682015-06-16 13:54:18 +0800601 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700602 if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
603 ghash->digestsize != 16)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800604 goto err_free_inst;
Herbert Xuadcbc682015-06-16 13:54:18 +0800605
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800606 err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
607 ctr_name, 0, mask);
Herbert Xu1472e5e2007-12-07 19:26:11 +0800608 if (err)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800609 goto err_free_inst;
Herbert Xu712c22a2023-10-03 11:43:29 +0800610 ctr = crypto_spawn_skcipher_alg_common(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800611
Eric Biggersf6995942019-04-18 14:43:02 -0700612 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000613 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700614 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
Herbert Xu712c22a2023-10-03 11:43:29 +0800615 ctr->ivsize != 16 || ctr->base.cra_blocksize != 1)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800616 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800617
Mikko Herranen28db8e32007-11-26 22:24:11 +0800618 err = -ENAMETOOLONG;
Eric Biggersf6995942019-04-18 14:43:02 -0700619 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
620 "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800621 goto err_free_inst;
Eric Biggersf6995942019-04-18 14:43:02 -0700622
Herbert Xuadcbc682015-06-16 13:54:18 +0800623 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800624 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Eric Biggersab6ffd32020-01-02 19:58:57 -0800625 ghash->base.cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800626 CRYPTO_MAX_ALG_NAME)
Eric Biggersab6ffd32020-01-02 19:58:57 -0800627 goto err_free_inst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800628
Herbert Xuadcbc682015-06-16 13:54:18 +0800629 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800630 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800631 inst->alg.base.cra_blocksize = 1;
Eric Biggers33fe2fb2023-10-22 01:10:52 -0700632 inst->alg.base.cra_alignmask = ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800633 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200634 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu712c22a2023-10-03 11:43:29 +0800635 inst->alg.chunksize = ctr->chunksize;
Herbert Xuadcbc682015-06-16 13:54:18 +0800636 inst->alg.maxauthsize = 16;
637 inst->alg.init = crypto_gcm_init_tfm;
638 inst->alg.exit = crypto_gcm_exit_tfm;
639 inst->alg.setkey = crypto_gcm_setkey;
640 inst->alg.setauthsize = crypto_gcm_setauthsize;
641 inst->alg.encrypt = crypto_gcm_encrypt;
642 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800643
Herbert Xu7b05a372015-07-09 07:17:30 +0800644 inst->free = crypto_gcm_free;
645
Herbert Xuadcbc682015-06-16 13:54:18 +0800646 err = aead_register_instance(tmpl, inst);
Eric Biggersab6ffd32020-01-02 19:58:57 -0800647 if (err) {
Mikko Herranen28db8e32007-11-26 22:24:11 +0800648err_free_inst:
Eric Biggersab6ffd32020-01-02 19:58:57 -0800649 crypto_gcm_free(inst);
650 }
651 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800652}
653
Herbert Xuadcbc682015-06-16 13:54:18 +0800654static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800655{
Herbert Xud00aa192007-12-07 20:31:10 +0800656 const char *cipher_name;
657 char ctr_name[CRYPTO_MAX_ALG_NAME];
Herbert Xud00aa192007-12-07 20:31:10 +0800658
659 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800660 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800661 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800662
663 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
664 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800665 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800666
Eric Biggersf6995942019-04-18 14:43:02 -0700667 return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800668}
669
Herbert Xuadcbc682015-06-16 13:54:18 +0800670static int crypto_gcm_base_create(struct crypto_template *tmpl,
671 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800672{
Herbert Xud00aa192007-12-07 20:31:10 +0800673 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000674 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800675
676 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800677 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800678 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800679
Huang Ying9382d972009-08-06 15:34:26 +1000680 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000681 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800682 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000683
Eric Biggersf6995942019-04-18 14:43:02 -0700684 return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800685}
686
Herbert Xudadbc532007-12-17 15:33:17 +0800687static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
688 unsigned int keylen)
689{
690 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
691 struct crypto_aead *child = ctx->child;
Herbert Xudadbc532007-12-17 15:33:17 +0800692
693 if (keylen < 4)
694 return -EINVAL;
695
696 keylen -= 4;
697 memcpy(ctx->nonce, key + keylen, 4);
698
699 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
700 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
701 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600702 return crypto_aead_setkey(child, key, keylen);
Herbert Xudadbc532007-12-17 15:33:17 +0800703}
704
705static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
706 unsigned int authsize)
707{
708 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
Iuliana Prodan65526f62019-07-31 16:05:54 +0300709 int err;
Herbert Xudadbc532007-12-17 15:33:17 +0800710
Iuliana Prodan65526f62019-07-31 16:05:54 +0300711 err = crypto_rfc4106_check_authsize(authsize);
712 if (err)
713 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800714
715 return crypto_aead_setauthsize(ctx->child, authsize);
716}
717
718static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
719{
Herbert Xu7b05a372015-07-09 07:17:30 +0800720 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800721 struct crypto_aead *aead = crypto_aead_reqtfm(req);
722 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800723 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800724 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800725 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800726 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
727 crypto_aead_alignmask(child) + 1);
728
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200729 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800730
Herbert Xudadbc532007-12-17 15:33:17 +0800731 memcpy(iv, ctx->nonce, 4);
732 memcpy(iv + 4, req->iv, 8);
733
Herbert Xu7b05a372015-07-09 07:17:30 +0800734 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200735 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800736 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
737 if (sg != rctx->src + 1)
738 sg_chain(rctx->src, 2, sg);
739
740 if (req->src != req->dst) {
741 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200742 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800743 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
744 if (sg != rctx->dst + 1)
745 sg_chain(rctx->dst, 2, sg);
746 }
747
Herbert Xudadbc532007-12-17 15:33:17 +0800748 aead_request_set_tfm(subreq, child);
749 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
750 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800751 aead_request_set_crypt(subreq, rctx->src,
752 req->src == req->dst ? rctx->src : rctx->dst,
753 req->cryptlen, iv);
754 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800755
756 return subreq;
757}
758
759static int crypto_rfc4106_encrypt(struct aead_request *req)
760{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300761 int err;
762
763 err = crypto_ipsec_check_assoclen(req->assoclen);
764 if (err)
765 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800766
Herbert Xudadbc532007-12-17 15:33:17 +0800767 req = crypto_rfc4106_crypt(req);
768
769 return crypto_aead_encrypt(req);
770}
771
772static int crypto_rfc4106_decrypt(struct aead_request *req)
773{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300774 int err;
775
776 err = crypto_ipsec_check_assoclen(req->assoclen);
777 if (err)
778 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800779
Herbert Xudadbc532007-12-17 15:33:17 +0800780 req = crypto_rfc4106_crypt(req);
781
782 return crypto_aead_decrypt(req);
783}
784
Herbert Xuadcbc682015-06-16 13:54:18 +0800785static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800786{
Herbert Xuadcbc682015-06-16 13:54:18 +0800787 struct aead_instance *inst = aead_alg_instance(tfm);
788 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
789 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800790 struct crypto_aead *aead;
791 unsigned long align;
792
793 aead = crypto_spawn_aead(spawn);
794 if (IS_ERR(aead))
795 return PTR_ERR(aead);
796
797 ctx->child = aead;
798
799 align = crypto_aead_alignmask(aead);
800 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800801 crypto_aead_set_reqsize(
802 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800803 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800804 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800805 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800806
807 return 0;
808}
809
Herbert Xuadcbc682015-06-16 13:54:18 +0800810static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800811{
Herbert Xuadcbc682015-06-16 13:54:18 +0800812 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800813
814 crypto_free_aead(ctx->child);
815}
816
Herbert Xu7b05a372015-07-09 07:17:30 +0800817static void crypto_rfc4106_free(struct aead_instance *inst)
818{
819 crypto_drop_aead(aead_instance_ctx(inst));
820 kfree(inst);
821}
822
Herbert Xuadcbc682015-06-16 13:54:18 +0800823static int crypto_rfc4106_create(struct crypto_template *tmpl,
824 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800825{
Eric Biggerscd900f02020-01-02 19:58:46 -0800826 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800827 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800828 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800829 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800830 int err;
831
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700832 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
833 if (err)
834 return err;
Eric Biggerscd900f02020-01-02 19:58:46 -0800835
Herbert Xudadbc532007-12-17 15:33:17 +0800836 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
837 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800838 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800839
Herbert Xuadcbc682015-06-16 13:54:18 +0800840 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800841 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
Eric Biggers959ac1c2020-02-25 20:59:18 -0800842 crypto_attr_alg_name(tb[1]), 0, mask);
Herbert Xudadbc532007-12-17 15:33:17 +0800843 if (err)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800844 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800845
Herbert Xuadcbc682015-06-16 13:54:18 +0800846 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800847
848 err = -EINVAL;
849
Herbert Xuadcbc682015-06-16 13:54:18 +0800850 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200851 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800852 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800853
854 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800855 if (alg->base.cra_blocksize != 1)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800856 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800857
858 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800859 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
860 "rfc4106(%s)", alg->base.cra_name) >=
861 CRYPTO_MAX_ALG_NAME ||
862 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
863 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800864 CRYPTO_MAX_ALG_NAME)
Eric Biggers959ac1c2020-02-25 20:59:18 -0800865 goto err_free_inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800866
Herbert Xuadcbc682015-06-16 13:54:18 +0800867 inst->alg.base.cra_priority = alg->base.cra_priority;
868 inst->alg.base.cra_blocksize = 1;
869 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800870
Herbert Xuadcbc682015-06-16 13:54:18 +0800871 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800872
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200873 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800874 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800875 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800876
Herbert Xuadcbc682015-06-16 13:54:18 +0800877 inst->alg.init = crypto_rfc4106_init_tfm;
878 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800879
Herbert Xuadcbc682015-06-16 13:54:18 +0800880 inst->alg.setkey = crypto_rfc4106_setkey;
881 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
882 inst->alg.encrypt = crypto_rfc4106_encrypt;
883 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800884
Herbert Xu7b05a372015-07-09 07:17:30 +0800885 inst->free = crypto_rfc4106_free;
886
Herbert Xuadcbc682015-06-16 13:54:18 +0800887 err = aead_register_instance(tmpl, inst);
Eric Biggers959ac1c2020-02-25 20:59:18 -0800888 if (err) {
889err_free_inst:
890 crypto_rfc4106_free(inst);
891 }
Herbert Xuadcbc682015-06-16 13:54:18 +0800892 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800893}
894
Tobias Brunner73c89c12010-01-17 21:52:11 +1100895static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
896 unsigned int keylen)
897{
898 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
899 struct crypto_aead *child = ctx->child;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100900
901 if (keylen < 4)
902 return -EINVAL;
903
904 keylen -= 4;
905 memcpy(ctx->nonce, key + keylen, 4);
906
907 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
908 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
909 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600910 return crypto_aead_setkey(child, key, keylen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100911}
912
913static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
914 unsigned int authsize)
915{
916 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
917
918 if (authsize != 16)
919 return -EINVAL;
920
921 return crypto_aead_setauthsize(ctx->child, authsize);
922}
923
Herbert Xuadcbc682015-06-16 13:54:18 +0800924static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100925{
926 struct crypto_aead *aead = crypto_aead_reqtfm(req);
927 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800928 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100929 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100930 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100931 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
932 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800933 int err;
934
935 if (req->src != req->dst) {
936 err = crypto_rfc4543_copy_src_to_dst(req, enc);
937 if (err)
938 return err;
939 }
Tobias Brunner73c89c12010-01-17 21:52:11 +1100940
941 memcpy(iv, ctx->nonce, 4);
942 memcpy(iv + 4, req->iv, 8);
943
Tobias Brunner73c89c12010-01-17 21:52:11 +1100944 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +0800945 aead_request_set_callback(subreq, req->base.flags,
946 req->base.complete, req->base.data);
947 aead_request_set_crypt(subreq, req->src, req->dst,
948 enc ? 0 : authsize, iv);
949 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
950 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100951
Herbert Xuadcbc682015-06-16 13:54:18 +0800952 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100953}
954
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300955static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
956{
957 struct crypto_aead *aead = crypto_aead_reqtfm(req);
958 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
959 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800960 unsigned int nbytes = req->assoclen + req->cryptlen -
961 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -0700962 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300963
Kees Cook8d605392018-09-18 19:10:51 -0700964 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800965 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
966 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
967
968 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300969}
970
Tobias Brunner73c89c12010-01-17 21:52:11 +1100971static int crypto_rfc4543_encrypt(struct aead_request *req)
972{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300973 return crypto_ipsec_check_assoclen(req->assoclen) ?:
974 crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100975}
976
977static int crypto_rfc4543_decrypt(struct aead_request *req)
978{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +0300979 return crypto_ipsec_check_assoclen(req->assoclen) ?:
980 crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100981}
982
Herbert Xuadcbc682015-06-16 13:54:18 +0800983static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100984{
Herbert Xuadcbc682015-06-16 13:54:18 +0800985 struct aead_instance *inst = aead_alg_instance(tfm);
986 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300987 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +0800988 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100989 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -0700990 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100991 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300992 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100993
994 aead = crypto_spawn_aead(spawn);
995 if (IS_ERR(aead))
996 return PTR_ERR(aead);
997
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800998 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +0300999 err = PTR_ERR(null);
1000 if (IS_ERR(null))
1001 goto err_free_aead;
1002
Tobias Brunner73c89c12010-01-17 21:52:11 +11001003 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001004 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001005
1006 align = crypto_aead_alignmask(aead);
1007 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001008 crypto_aead_set_reqsize(
1009 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001010 sizeof(struct crypto_rfc4543_req_ctx) +
1011 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001012 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001013
1014 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001015
1016err_free_aead:
1017 crypto_free_aead(aead);
1018 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001019}
1020
Herbert Xuadcbc682015-06-16 13:54:18 +08001021static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001022{
Herbert Xuadcbc682015-06-16 13:54:18 +08001023 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001024
1025 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001026 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001027}
1028
Herbert Xu7b05a372015-07-09 07:17:30 +08001029static void crypto_rfc4543_free(struct aead_instance *inst)
1030{
1031 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1032
1033 crypto_drop_aead(&ctx->aead);
1034
1035 kfree(inst);
1036}
1037
Herbert Xuadcbc682015-06-16 13:54:18 +08001038static int crypto_rfc4543_create(struct crypto_template *tmpl,
1039 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001040{
Eric Biggerscd900f02020-01-02 19:58:46 -08001041 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +08001042 struct aead_instance *inst;
Herbert Xuadcbc682015-06-16 13:54:18 +08001043 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001044 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001045 int err;
1046
Eric Biggers7bcb2c92020-07-09 23:20:38 -07001047 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
1048 if (err)
1049 return err;
Eric Biggerscd900f02020-01-02 19:58:46 -08001050
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001051 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001052 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001053 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001054
Herbert Xuadcbc682015-06-16 13:54:18 +08001055 ctx = aead_instance_ctx(inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001056 err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst),
1057 crypto_attr_alg_name(tb[1]), 0, mask);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001058 if (err)
Eric Biggersc4caa562020-02-25 20:59:19 -08001059 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001060
Eric Biggersc4caa562020-02-25 20:59:19 -08001061 alg = crypto_spawn_aead_alg(&ctx->aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001062
1063 err = -EINVAL;
1064
Herbert Xuadcbc682015-06-16 13:54:18 +08001065 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001066 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Eric Biggersc4caa562020-02-25 20:59:19 -08001067 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001068
1069 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001070 if (alg->base.cra_blocksize != 1)
Eric Biggersc4caa562020-02-25 20:59:19 -08001071 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001072
1073 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001074 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1075 "rfc4543(%s)", alg->base.cra_name) >=
1076 CRYPTO_MAX_ALG_NAME ||
1077 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1078 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001079 CRYPTO_MAX_ALG_NAME)
Eric Biggersc4caa562020-02-25 20:59:19 -08001080 goto err_free_inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001081
Herbert Xuadcbc682015-06-16 13:54:18 +08001082 inst->alg.base.cra_priority = alg->base.cra_priority;
1083 inst->alg.base.cra_blocksize = 1;
1084 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001085
Herbert Xuadcbc682015-06-16 13:54:18 +08001086 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001087
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001088 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001089 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001090 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001091
Herbert Xuadcbc682015-06-16 13:54:18 +08001092 inst->alg.init = crypto_rfc4543_init_tfm;
1093 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001094
Herbert Xuadcbc682015-06-16 13:54:18 +08001095 inst->alg.setkey = crypto_rfc4543_setkey;
1096 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1097 inst->alg.encrypt = crypto_rfc4543_encrypt;
1098 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001099
Eric Biggersc4caa562020-02-25 20:59:19 -08001100 inst->free = crypto_rfc4543_free;
Herbert Xu7b05a372015-07-09 07:17:30 +08001101
Herbert Xuadcbc682015-06-16 13:54:18 +08001102 err = aead_register_instance(tmpl, inst);
Eric Biggersc4caa562020-02-25 20:59:19 -08001103 if (err) {
1104err_free_inst:
1105 crypto_rfc4543_free(inst);
1106 }
Herbert Xuadcbc682015-06-16 13:54:18 +08001107 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001108}
1109
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001110static struct crypto_template crypto_gcm_tmpls[] = {
1111 {
1112 .name = "gcm_base",
1113 .create = crypto_gcm_base_create,
1114 .module = THIS_MODULE,
1115 }, {
1116 .name = "gcm",
1117 .create = crypto_gcm_create,
1118 .module = THIS_MODULE,
1119 }, {
1120 .name = "rfc4106",
1121 .create = crypto_rfc4106_create,
1122 .module = THIS_MODULE,
1123 }, {
1124 .name = "rfc4543",
1125 .create = crypto_rfc4543_create,
1126 .module = THIS_MODULE,
1127 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001128};
1129
Mikko Herranen28db8e32007-11-26 22:24:11 +08001130static int __init crypto_gcm_module_init(void)
1131{
Herbert Xud00aa192007-12-07 20:31:10 +08001132 int err;
1133
Herbert Xuadcbc682015-06-16 13:54:18 +08001134 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001135 if (!gcm_zeroes)
1136 return -ENOMEM;
1137
Herbert Xuadcbc682015-06-16 13:54:18 +08001138 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1139
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001140 err = crypto_register_templates(crypto_gcm_tmpls,
1141 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001142 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001143 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001144
Huang Ying9382d972009-08-06 15:34:26 +10001145 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001146}
1147
1148static void __exit crypto_gcm_module_exit(void)
1149{
Huang Ying9382d972009-08-06 15:34:26 +10001150 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001151 crypto_unregister_templates(crypto_gcm_tmpls,
1152 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001153}
1154
Eric Biggersc4741b22019-04-11 21:57:42 -07001155subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001156module_exit(crypto_gcm_module_exit);
1157
1158MODULE_LICENSE("GPL");
1159MODULE_DESCRIPTION("Galois/Counter Mode");
1160MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001161MODULE_ALIAS_CRYPTO("gcm_base");
1162MODULE_ALIAS_CRYPTO("rfc4106");
1163MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001164MODULE_ALIAS_CRYPTO("gcm");