blob: 72649b8cbf2a4523952423bcc3089f3958f539c1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mikko Herranen28db8e32007-11-26 22:24:11 +08002/*
3 * GCM: Galois/Counter Mode.
4 *
5 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
Mikko Herranen28db8e32007-11-26 22:24:11 +08006 */
7
Mikko Herranen28db8e32007-11-26 22:24:11 +08008#include <crypto/gf128mul.h>
Herbert Xudadbc532007-12-17 15:33:17 +08009#include <crypto/internal/aead.h>
Herbert Xu1472e5e2007-12-07 19:26:11 +080010#include <crypto/internal/skcipher.h>
Huang Ying9382d972009-08-06 15:34:26 +100011#include <crypto/internal/hash.h>
Herbert Xu17db8542015-05-21 15:11:11 +080012#include <crypto/null.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080013#include <crypto/scatterwalk.h>
Corentin LABBEe0ab7e92017-08-22 10:08:17 +020014#include <crypto/gcm.h>
Huang Ying9382d972009-08-06 15:34:26 +100015#include <crypto/hash.h>
16#include "internal.h"
Mikko Herranen28db8e32007-11-26 22:24:11 +080017#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22
Mikko Herranen28db8e32007-11-26 22:24:11 +080023struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080024 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100025 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080026};
27
28struct crypto_gcm_ctx {
Herbert Xu16f37ecd2016-07-12 13:17:39 +080029 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100030 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080031};
32
Herbert Xudadbc532007-12-17 15:33:17 +080033struct crypto_rfc4106_ctx {
34 struct crypto_aead *child;
35 u8 nonce[4];
36};
37
Herbert Xu7b05a372015-07-09 07:17:30 +080038struct crypto_rfc4106_req_ctx {
39 struct scatterlist src[3];
40 struct scatterlist dst[3];
41 struct aead_request subreq;
42};
43
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030044struct crypto_rfc4543_instance_ctx {
45 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030046};
47
Tobias Brunner73c89c12010-01-17 21:52:11 +110048struct crypto_rfc4543_ctx {
49 struct crypto_aead *child;
Kees Cook8d605392018-09-18 19:10:51 -070050 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110051 u8 nonce[4];
52};
53
54struct crypto_rfc4543_req_ctx {
Tobias Brunner73c89c12010-01-17 21:52:11 +110055 struct aead_request subreq;
56};
57
Mikko Herranen28db8e32007-11-26 22:24:11 +080058struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100059 unsigned int cryptlen;
60 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080061 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080062};
63
64struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080065 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080066 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110067 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080068 struct scatterlist src[3];
69 struct scatterlist dst[3];
70 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100071 struct crypto_gcm_ghash_ctx ghash_ctx;
72 union {
73 struct ahash_request ahreq;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080074 struct skcipher_request skreq;
Huang Ying9382d972009-08-06 15:34:26 +100075 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080076};
77
Herbert Xuadcbc682015-06-16 13:54:18 +080078static struct {
79 u8 buf[16];
80 struct scatterlist sg;
81} *gcm_zeroes;
82
83static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100084
Herbert Xu25894692007-12-12 19:16:38 +080085static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
86 struct aead_request *req)
87{
88 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
89
90 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
91}
92
Mikko Herranen28db8e32007-11-26 22:24:11 +080093static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
94 unsigned int keylen)
95{
96 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +100097 struct crypto_ahash *ghash = ctx->ghash;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080098 struct crypto_skcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +080099 struct {
100 be128 hash;
Ondrej Mosnáček50d2e6d2016-09-23 10:47:32 +0200101 u8 iv[16];
Herbert Xu84c91152007-12-17 21:42:08 +0800102
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100103 struct crypto_wait wait;
Herbert Xu84c91152007-12-17 21:42:08 +0800104
105 struct scatterlist sg[1];
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800106 struct skcipher_request req;
Herbert Xu84c91152007-12-17 21:42:08 +0800107 } *data;
108 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800109
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800110 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
111 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
112 CRYPTO_TFM_REQ_MASK);
113 err = crypto_skcipher_setkey(ctr, key, keylen);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800114 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800115 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800116
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800117 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
Herbert Xu84c91152007-12-17 21:42:08 +0800118 GFP_KERNEL);
119 if (!data)
120 return -ENOMEM;
121
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100122 crypto_init_wait(&data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800123 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800124 skcipher_request_set_tfm(&data->req, ctr);
125 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
126 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100127 crypto_req_done,
128 &data->wait);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800129 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
130 sizeof(data->hash), data->iv);
Herbert Xu84c91152007-12-17 21:42:08 +0800131
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100132 err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
133 &data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800134
Mikko Herranen28db8e32007-11-26 22:24:11 +0800135 if (err)
136 goto out;
137
Huang Ying9382d972009-08-06 15:34:26 +1000138 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
139 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
140 CRYPTO_TFM_REQ_MASK);
141 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
Herbert Xu84c91152007-12-17 21:42:08 +0800142out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800143 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800144 return err;
145}
146
Herbert Xudadbc532007-12-17 15:33:17 +0800147static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
148 unsigned int authsize)
149{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300150 return crypto_gcm_check_authsize(authsize);
Herbert Xudadbc532007-12-17 15:33:17 +0800151}
152
Herbert Xuadcbc682015-06-16 13:54:18 +0800153static void crypto_gcm_init_common(struct aead_request *req)
154{
155 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
156 __be32 counter = cpu_to_be32(1);
157 struct scatterlist *sg;
158
159 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200160 memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
161 memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
Herbert Xuadcbc682015-06-16 13:54:18 +0800162
163 sg_init_table(pctx->src, 3);
164 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
165 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
166 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200167 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800168
169 if (req->src != req->dst) {
170 sg_init_table(pctx->dst, 3);
171 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
172 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
173 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200174 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800175 }
176}
177
178static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800179 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800180{
181 struct crypto_aead *aead = crypto_aead_reqtfm(req);
182 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800183 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800184 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800185 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800186
Herbert Xuadcbc682015-06-16 13:54:18 +0800187 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800188
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800189 skcipher_request_set_tfm(skreq, ctx->ctr);
190 skcipher_request_set_crypt(skreq, pctx->src, dst,
Herbert Xu84c91152007-12-17 21:42:08 +0800191 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800192 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800193}
194
Huang Ying9382d972009-08-06 15:34:26 +1000195static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800196{
Huang Ying9382d972009-08-06 15:34:26 +1000197 len &= 0xfU;
198 return len ? 16 - len : 0;
199}
200
201static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000202
203static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700204 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000205 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800206 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000207{
Herbert Xuadcbc682015-06-16 13:54:18 +0800208 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000209 struct ahash_request *ahreq = &pctx->u.ahreq;
210
Herbert Xuadcbc682015-06-16 13:54:18 +0800211 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000212 ahash_request_set_crypt(ahreq, src, NULL, len);
213
214 return crypto_ahash_update(ahreq);
215}
216
217static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000218 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800219 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000220{
Herbert Xuadcbc682015-06-16 13:54:18 +0800221 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000222}
223
Herbert Xuadcbc682015-06-16 13:54:18 +0800224static int gcm_hash_len(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000225{
Herbert Xuadcbc682015-06-16 13:54:18 +0800226 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000227 struct ahash_request *ahreq = &pctx->u.ahreq;
228 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Eric Biggers18666552019-01-10 12:17:52 -0800229 be128 lengths;
Huang Ying9382d972009-08-06 15:34:26 +1000230
231 lengths.a = cpu_to_be64(req->assoclen * 8);
232 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
233 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800234 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
235 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
236 ahash_request_set_crypt(ahreq, &pctx->sg,
237 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000238
Herbert Xuadcbc682015-06-16 13:54:18 +0800239 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000240}
241
Herbert Xuadcbc682015-06-16 13:54:18 +0800242static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000243{
Herbert Xu25894692007-12-12 19:16:38 +0800244 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000245 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800246
Herbert Xuadcbc682015-06-16 13:54:18 +0800247 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000248}
249
Huang Ying62c55932009-11-16 21:52:22 +0800250static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000251{
252 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800253
Herbert Xuadcbc682015-06-16 13:54:18 +0800254 if (err)
255 goto out;
256
257 err = gcm_hash_len_continue(req, 0);
258 if (err == -EINPROGRESS)
259 return;
260
261out:
262 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800263}
264
Herbert Xuadcbc682015-06-16 13:54:18 +0800265static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800266{
Herbert Xuadcbc682015-06-16 13:54:18 +0800267 return gcm_hash_len(req, flags) ?:
268 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000269}
270
Huang Ying62c55932009-11-16 21:52:22 +0800271static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
272 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000273{
274 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800275
Herbert Xuadcbc682015-06-16 13:54:18 +0800276 if (err)
277 goto out;
278
279 err = gcm_hash_crypt_remain_continue(req, 0);
280 if (err == -EINPROGRESS)
281 return;
282
283out:
284 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800285}
286
Herbert Xuadcbc682015-06-16 13:54:18 +0800287static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800288{
Huang Ying9382d972009-08-06 15:34:26 +1000289 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
290 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
291 unsigned int remain;
292
Herbert Xuadcbc682015-06-16 13:54:18 +0800293 remain = gcm_remain(gctx->cryptlen);
294 if (remain)
295 return gcm_hash_remain(req, remain,
296 gcm_hash_crypt_remain_done, flags) ?:
297 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000298
Herbert Xuadcbc682015-06-16 13:54:18 +0800299 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000300}
301
Huang Ying62c55932009-11-16 21:52:22 +0800302static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000303{
304 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800305
Herbert Xuadcbc682015-06-16 13:54:18 +0800306 if (err)
307 goto out;
308
309 err = gcm_hash_crypt_continue(req, 0);
310 if (err == -EINPROGRESS)
311 return;
312
313out:
314 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800315}
316
Herbert Xuadcbc682015-06-16 13:54:18 +0800317static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800318{
Huang Ying9382d972009-08-06 15:34:26 +1000319 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
320 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000321
Herbert Xuadcbc682015-06-16 13:54:18 +0800322 if (gctx->cryptlen)
323 return gcm_hash_update(req, gcm_hash_crypt_done,
324 gctx->src, gctx->cryptlen, flags) ?:
325 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000326
Herbert Xuadcbc682015-06-16 13:54:18 +0800327 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000328}
329
Huang Ying62c55932009-11-16 21:52:22 +0800330static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
331 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000332{
333 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800334
Herbert Xuadcbc682015-06-16 13:54:18 +0800335 if (err)
336 goto out;
337
338 err = gcm_hash_assoc_remain_continue(req, 0);
339 if (err == -EINPROGRESS)
340 return;
341
342out:
343 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800344}
345
Herbert Xuadcbc682015-06-16 13:54:18 +0800346static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800347{
Huang Ying9382d972009-08-06 15:34:26 +1000348 unsigned int remain;
349
Herbert Xuadcbc682015-06-16 13:54:18 +0800350 remain = gcm_remain(req->assoclen);
351 if (remain)
352 return gcm_hash_remain(req, remain,
353 gcm_hash_assoc_remain_done, flags) ?:
354 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000355
Herbert Xuadcbc682015-06-16 13:54:18 +0800356 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000357}
358
Huang Ying62c55932009-11-16 21:52:22 +0800359static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000360{
361 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800362
Herbert Xuadcbc682015-06-16 13:54:18 +0800363 if (err)
364 goto out;
365
366 err = gcm_hash_assoc_continue(req, 0);
367 if (err == -EINPROGRESS)
368 return;
369
370out:
371 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800372}
373
Herbert Xuadcbc682015-06-16 13:54:18 +0800374static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800375{
Herbert Xuadcbc682015-06-16 13:54:18 +0800376 if (req->assoclen)
377 return gcm_hash_update(req, gcm_hash_assoc_done,
378 req->src, req->assoclen, flags) ?:
379 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000380
Herbert Xuadcbc682015-06-16 13:54:18 +0800381 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800382}
383
384static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
385{
386 struct aead_request *req = areq->data;
387
Herbert Xuadcbc682015-06-16 13:54:18 +0800388 if (err)
389 goto out;
390
391 err = gcm_hash_init_continue(req, 0);
392 if (err == -EINPROGRESS)
393 return;
394
395out:
396 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000397}
398
Herbert Xuadcbc682015-06-16 13:54:18 +0800399static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000400{
Herbert Xuadcbc682015-06-16 13:54:18 +0800401 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000402 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800403 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000404
405 ahash_request_set_tfm(ahreq, ctx->ghash);
406
Herbert Xuadcbc682015-06-16 13:54:18 +0800407 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
408 return crypto_ahash_init(ahreq) ?:
409 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100410}
411
Herbert Xuadcbc682015-06-16 13:54:18 +0800412static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000413{
Herbert Xuadcbc682015-06-16 13:54:18 +0800414 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000415 struct crypto_aead *aead = crypto_aead_reqtfm(req);
416 u8 *auth_tag = pctx->auth_tag;
417
Herbert Xuadcbc682015-06-16 13:54:18 +0800418 crypto_xor(auth_tag, pctx->iauth_tag, 16);
419 scatterwalk_map_and_copy(auth_tag, req->dst,
420 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000421 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800422 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000423}
424
Herbert Xuadcbc682015-06-16 13:54:18 +0800425static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100426{
Huang Ying9382d972009-08-06 15:34:26 +1000427 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800428 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100429
Herbert Xuadcbc682015-06-16 13:54:18 +0800430 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
431 gctx->cryptlen = req->cryptlen;
432 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100433
Herbert Xuadcbc682015-06-16 13:54:18 +0800434 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800435}
436
Huang Ying62c55932009-11-16 21:52:22 +0800437static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000438{
439 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000440
Herbert Xuadcbc682015-06-16 13:54:18 +0800441 if (err)
442 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000443
Herbert Xuadcbc682015-06-16 13:54:18 +0800444 err = gcm_encrypt_continue(req, 0);
445 if (err == -EINPROGRESS)
446 return;
447
448out:
Huang Ying62c55932009-11-16 21:52:22 +0800449 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000450}
451
Mikko Herranen28db8e32007-11-26 22:24:11 +0800452static int crypto_gcm_encrypt(struct aead_request *req)
453{
Herbert Xu25894692007-12-12 19:16:38 +0800454 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800455 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800456 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800457
Herbert Xuadcbc682015-06-16 13:54:18 +0800458 crypto_gcm_init_common(req);
459 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800460 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000461
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800462 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800463 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800464}
465
Herbert Xuadcbc682015-06-16 13:54:18 +0800466static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800467{
Herbert Xuadcbc682015-06-16 13:54:18 +0800468 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800469 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800470 u8 *auth_tag = pctx->auth_tag;
471 u8 *iauth_tag = pctx->iauth_tag;
472 unsigned int authsize = crypto_aead_authsize(aead);
473 unsigned int cryptlen = req->cryptlen - authsize;
474
Huang Ying9382d972009-08-06 15:34:26 +1000475 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800476 scatterwalk_map_and_copy(iauth_tag, req->src,
477 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600478 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800479}
480
Huang Ying9382d972009-08-06 15:34:26 +1000481static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800482{
Herbert Xu84c91152007-12-17 21:42:08 +0800483 struct aead_request *req = areq->data;
484
485 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800486 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800487
488 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800489}
490
Herbert Xuadcbc682015-06-16 13:54:18 +0800491static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000492{
Huang Ying9382d972009-08-06 15:34:26 +1000493 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800494 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000495 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
496
Herbert Xuadcbc682015-06-16 13:54:18 +0800497 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800498 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
499 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000500}
501
Mikko Herranen28db8e32007-11-26 22:24:11 +0800502static int crypto_gcm_decrypt(struct aead_request *req)
503{
Herbert Xu6160b282007-12-04 19:17:50 +1100504 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800505 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000506 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100507 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000508 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800509 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800510
Herbert Xu6160b282007-12-04 19:17:50 +1100511 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800512
Herbert Xuadcbc682015-06-16 13:54:18 +0800513 crypto_gcm_init_common(req);
514
515 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000516 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800517 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000518
Herbert Xuadcbc682015-06-16 13:54:18 +0800519 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800520}
521
Herbert Xuadcbc682015-06-16 13:54:18 +0800522static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800523{
Herbert Xuadcbc682015-06-16 13:54:18 +0800524 struct aead_instance *inst = aead_alg_instance(tfm);
525 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
526 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800527 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000528 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800529 unsigned long align;
530 int err;
531
Huang Ying9382d972009-08-06 15:34:26 +1000532 ghash = crypto_spawn_ahash(&ictx->ghash);
533 if (IS_ERR(ghash))
534 return PTR_ERR(ghash);
535
Eric Biggers60425a82016-10-28 09:52:19 -0700536 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800537 err = PTR_ERR(ctr);
538 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000539 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800540
541 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000542 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800543
Herbert Xuadcbc682015-06-16 13:54:18 +0800544 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800545 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800546 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800547 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800548 max(sizeof(struct skcipher_request) +
549 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000550 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800551 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800552
553 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000554
555err_free_hash:
556 crypto_free_ahash(ghash);
557 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800558}
559
Herbert Xuadcbc682015-06-16 13:54:18 +0800560static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800561{
Herbert Xuadcbc682015-06-16 13:54:18 +0800562 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800563
Huang Ying9382d972009-08-06 15:34:26 +1000564 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800565 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800566}
567
Herbert Xu7b05a372015-07-09 07:17:30 +0800568static void crypto_gcm_free(struct aead_instance *inst)
569{
570 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
571
572 crypto_drop_skcipher(&ctx->ctr);
573 crypto_drop_ahash(&ctx->ghash);
574 kfree(inst);
575}
576
Herbert Xuadcbc682015-06-16 13:54:18 +0800577static int crypto_gcm_create_common(struct crypto_template *tmpl,
578 struct rtattr **tb,
Herbert Xuadcbc682015-06-16 13:54:18 +0800579 const char *ctr_name,
580 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800581{
Herbert Xud00aa192007-12-07 20:31:10 +0800582 struct crypto_attr_type *algt;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800583 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800584 struct aead_instance *inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800585 struct skcipher_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000586 struct crypto_alg *ghash_alg;
Herbert Xuadcbc682015-06-16 13:54:18 +0800587 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800588 struct gcm_instance_ctx *ctx;
589 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800590
Herbert Xud00aa192007-12-07 20:31:10 +0800591 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800592 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800593 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800594
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800595 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800596 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800597
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800598 mask = crypto_requires_sync(algt->type, algt->mask);
599
Huang Ying9382d972009-08-06 15:34:26 +1000600 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
601 CRYPTO_ALG_TYPE_HASH,
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800602 CRYPTO_ALG_TYPE_AHASH_MASK | mask);
Huang Ying9382d972009-08-06 15:34:26 +1000603 if (IS_ERR(ghash_alg))
Herbert Xuadcbc682015-06-16 13:54:18 +0800604 return PTR_ERR(ghash_alg);
605
606 ghash = __crypto_hash_alg_common(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000607
608 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800609 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
610 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000611 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800612
Herbert Xuadcbc682015-06-16 13:54:18 +0800613 ctx = aead_instance_ctx(inst);
614 err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
615 aead_crypto_instance(inst));
Huang Ying9382d972009-08-06 15:34:26 +1000616 if (err)
617 goto err_free_inst;
618
Herbert Xuadcbc682015-06-16 13:54:18 +0800619 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700620 if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
621 ghash->digestsize != 16)
Herbert Xuadcbc682015-06-16 13:54:18 +0800622 goto err_drop_ghash;
623
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800624 err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
625 ctr_name, 0, mask);
Herbert Xu1472e5e2007-12-07 19:26:11 +0800626 if (err)
Huang Ying9382d972009-08-06 15:34:26 +1000627 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800628
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800629 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800630
Eric Biggersf6995942019-04-18 14:43:02 -0700631 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000632 err = -EINVAL;
Eric Biggersf6995942019-04-18 14:43:02 -0700633 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
634 crypto_skcipher_alg_ivsize(ctr) != 16 ||
635 ctr->base.cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800636 goto out_put_ctr;
637
Mikko Herranen28db8e32007-11-26 22:24:11 +0800638 err = -ENAMETOOLONG;
Eric Biggersf6995942019-04-18 14:43:02 -0700639 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
640 "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
641 goto out_put_ctr;
642
Herbert Xuadcbc682015-06-16 13:54:18 +0800643 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800644 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Huang Ying9382d972009-08-06 15:34:26 +1000645 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800646 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800647 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800648
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800649 inst->alg.base.cra_flags = (ghash->base.cra_flags |
650 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800651 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800652 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800653 inst->alg.base.cra_blocksize = 1;
654 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800655 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800656 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200657 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800658 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800659 inst->alg.maxauthsize = 16;
660 inst->alg.init = crypto_gcm_init_tfm;
661 inst->alg.exit = crypto_gcm_exit_tfm;
662 inst->alg.setkey = crypto_gcm_setkey;
663 inst->alg.setauthsize = crypto_gcm_setauthsize;
664 inst->alg.encrypt = crypto_gcm_encrypt;
665 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800666
Herbert Xu7b05a372015-07-09 07:17:30 +0800667 inst->free = crypto_gcm_free;
668
Herbert Xuadcbc682015-06-16 13:54:18 +0800669 err = aead_register_instance(tmpl, inst);
670 if (err)
671 goto out_put_ctr;
672
673out_put_ghash:
Huang Ying9382d972009-08-06 15:34:26 +1000674 crypto_mod_put(ghash_alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800675 return err;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800676
677out_put_ctr:
678 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000679err_drop_ghash:
680 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800681err_free_inst:
682 kfree(inst);
Herbert Xuadcbc682015-06-16 13:54:18 +0800683 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800684}
685
Herbert Xuadcbc682015-06-16 13:54:18 +0800686static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800687{
Herbert Xud00aa192007-12-07 20:31:10 +0800688 const char *cipher_name;
689 char ctr_name[CRYPTO_MAX_ALG_NAME];
Herbert Xud00aa192007-12-07 20:31:10 +0800690
691 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800692 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800693 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800694
695 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
696 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800697 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800698
Eric Biggersf6995942019-04-18 14:43:02 -0700699 return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800700}
701
Herbert Xuadcbc682015-06-16 13:54:18 +0800702static int crypto_gcm_base_create(struct crypto_template *tmpl,
703 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800704{
Herbert Xud00aa192007-12-07 20:31:10 +0800705 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000706 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800707
708 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800709 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800710 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800711
Huang Ying9382d972009-08-06 15:34:26 +1000712 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000713 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800714 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000715
Eric Biggersf6995942019-04-18 14:43:02 -0700716 return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800717}
718
Herbert Xudadbc532007-12-17 15:33:17 +0800719static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
720 unsigned int keylen)
721{
722 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
723 struct crypto_aead *child = ctx->child;
Herbert Xudadbc532007-12-17 15:33:17 +0800724
725 if (keylen < 4)
726 return -EINVAL;
727
728 keylen -= 4;
729 memcpy(ctx->nonce, key + keylen, 4);
730
731 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
732 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
733 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600734 return crypto_aead_setkey(child, key, keylen);
Herbert Xudadbc532007-12-17 15:33:17 +0800735}
736
737static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
738 unsigned int authsize)
739{
740 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
Iuliana Prodan65526f62019-07-31 16:05:54 +0300741 int err;
Herbert Xudadbc532007-12-17 15:33:17 +0800742
Iuliana Prodan65526f62019-07-31 16:05:54 +0300743 err = crypto_rfc4106_check_authsize(authsize);
744 if (err)
745 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800746
747 return crypto_aead_setauthsize(ctx->child, authsize);
748}
749
750static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
751{
Herbert Xu7b05a372015-07-09 07:17:30 +0800752 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800753 struct crypto_aead *aead = crypto_aead_reqtfm(req);
754 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800755 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800756 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800757 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800758 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
759 crypto_aead_alignmask(child) + 1);
760
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200761 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800762
Herbert Xudadbc532007-12-17 15:33:17 +0800763 memcpy(iv, ctx->nonce, 4);
764 memcpy(iv + 4, req->iv, 8);
765
Herbert Xu7b05a372015-07-09 07:17:30 +0800766 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200767 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800768 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
769 if (sg != rctx->src + 1)
770 sg_chain(rctx->src, 2, sg);
771
772 if (req->src != req->dst) {
773 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200774 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800775 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
776 if (sg != rctx->dst + 1)
777 sg_chain(rctx->dst, 2, sg);
778 }
779
Herbert Xudadbc532007-12-17 15:33:17 +0800780 aead_request_set_tfm(subreq, child);
781 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
782 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800783 aead_request_set_crypt(subreq, rctx->src,
784 req->src == req->dst ? rctx->src : rctx->dst,
785 req->cryptlen, iv);
786 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800787
788 return subreq;
789}
790
791static int crypto_rfc4106_encrypt(struct aead_request *req)
792{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300793 int err;
794
795 err = crypto_ipsec_check_assoclen(req->assoclen);
796 if (err)
797 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800798
Herbert Xudadbc532007-12-17 15:33:17 +0800799 req = crypto_rfc4106_crypt(req);
800
801 return crypto_aead_encrypt(req);
802}
803
804static int crypto_rfc4106_decrypt(struct aead_request *req)
805{
Iuliana Prodan65526f62019-07-31 16:05:54 +0300806 int err;
807
808 err = crypto_ipsec_check_assoclen(req->assoclen);
809 if (err)
810 return err;
Herbert Xu7b05a372015-07-09 07:17:30 +0800811
Herbert Xudadbc532007-12-17 15:33:17 +0800812 req = crypto_rfc4106_crypt(req);
813
814 return crypto_aead_decrypt(req);
815}
816
Herbert Xuadcbc682015-06-16 13:54:18 +0800817static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800818{
Herbert Xuadcbc682015-06-16 13:54:18 +0800819 struct aead_instance *inst = aead_alg_instance(tfm);
820 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
821 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800822 struct crypto_aead *aead;
823 unsigned long align;
824
825 aead = crypto_spawn_aead(spawn);
826 if (IS_ERR(aead))
827 return PTR_ERR(aead);
828
829 ctx->child = aead;
830
831 align = crypto_aead_alignmask(aead);
832 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800833 crypto_aead_set_reqsize(
834 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800835 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800836 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800837 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800838
839 return 0;
840}
841
Herbert Xuadcbc682015-06-16 13:54:18 +0800842static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800843{
Herbert Xuadcbc682015-06-16 13:54:18 +0800844 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800845
846 crypto_free_aead(ctx->child);
847}
848
Herbert Xu7b05a372015-07-09 07:17:30 +0800849static void crypto_rfc4106_free(struct aead_instance *inst)
850{
851 crypto_drop_aead(aead_instance_ctx(inst));
852 kfree(inst);
853}
854
Herbert Xuadcbc682015-06-16 13:54:18 +0800855static int crypto_rfc4106_create(struct crypto_template *tmpl,
856 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800857{
858 struct crypto_attr_type *algt;
Eric Biggerscd900f02020-01-02 19:58:46 -0800859 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800860 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800861 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800862 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800863 const char *ccm_name;
864 int err;
865
866 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800867 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800868 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800869
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800870 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800871 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800872
Eric Biggerscd900f02020-01-02 19:58:46 -0800873 mask = crypto_requires_sync(algt->type, algt->mask);
874
Herbert Xudadbc532007-12-17 15:33:17 +0800875 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800876 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800877 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800878
879 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
880 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800881 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800882
Herbert Xuadcbc682015-06-16 13:54:18 +0800883 spawn = aead_instance_ctx(inst);
Eric Biggerscd900f02020-01-02 19:58:46 -0800884 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
885 ccm_name, 0, mask);
Herbert Xudadbc532007-12-17 15:33:17 +0800886 if (err)
887 goto out_free_inst;
888
Herbert Xuadcbc682015-06-16 13:54:18 +0800889 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800890
891 err = -EINVAL;
892
Herbert Xuadcbc682015-06-16 13:54:18 +0800893 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200894 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xudadbc532007-12-17 15:33:17 +0800895 goto out_drop_alg;
896
897 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800898 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800899 goto out_drop_alg;
900
901 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800902 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
903 "rfc4106(%s)", alg->base.cra_name) >=
904 CRYPTO_MAX_ALG_NAME ||
905 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
906 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800907 CRYPTO_MAX_ALG_NAME)
908 goto out_drop_alg;
909
Herbert Xu7b05a372015-07-09 07:17:30 +0800910 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800911 inst->alg.base.cra_priority = alg->base.cra_priority;
912 inst->alg.base.cra_blocksize = 1;
913 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800914
Herbert Xuadcbc682015-06-16 13:54:18 +0800915 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800916
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200917 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800918 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800919 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800920
Herbert Xuadcbc682015-06-16 13:54:18 +0800921 inst->alg.init = crypto_rfc4106_init_tfm;
922 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800923
Herbert Xuadcbc682015-06-16 13:54:18 +0800924 inst->alg.setkey = crypto_rfc4106_setkey;
925 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
926 inst->alg.encrypt = crypto_rfc4106_encrypt;
927 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800928
Herbert Xu7b05a372015-07-09 07:17:30 +0800929 inst->free = crypto_rfc4106_free;
930
Herbert Xuadcbc682015-06-16 13:54:18 +0800931 err = aead_register_instance(tmpl, inst);
932 if (err)
933 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800934
935out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800936 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800937
938out_drop_alg:
939 crypto_drop_aead(spawn);
940out_free_inst:
941 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800942 goto out;
943}
944
Tobias Brunner73c89c12010-01-17 21:52:11 +1100945static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
946 unsigned int keylen)
947{
948 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
949 struct crypto_aead *child = ctx->child;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100950
951 if (keylen < 4)
952 return -EINVAL;
953
954 keylen -= 4;
955 memcpy(ctx->nonce, key + keylen, 4);
956
957 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
958 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
959 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600960 return crypto_aead_setkey(child, key, keylen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100961}
962
963static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
964 unsigned int authsize)
965{
966 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
967
968 if (authsize != 16)
969 return -EINVAL;
970
971 return crypto_aead_setauthsize(ctx->child, authsize);
972}
973
Herbert Xuadcbc682015-06-16 13:54:18 +0800974static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100975{
976 struct crypto_aead *aead = crypto_aead_reqtfm(req);
977 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800978 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100979 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100980 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100981 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
982 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800983 int err;
984
985 if (req->src != req->dst) {
986 err = crypto_rfc4543_copy_src_to_dst(req, enc);
987 if (err)
988 return err;
989 }
Tobias Brunner73c89c12010-01-17 21:52:11 +1100990
991 memcpy(iv, ctx->nonce, 4);
992 memcpy(iv + 4, req->iv, 8);
993
Tobias Brunner73c89c12010-01-17 21:52:11 +1100994 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +0800995 aead_request_set_callback(subreq, req->base.flags,
996 req->base.complete, req->base.data);
997 aead_request_set_crypt(subreq, req->src, req->dst,
998 enc ? 0 : authsize, iv);
999 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1000 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001001
Herbert Xuadcbc682015-06-16 13:54:18 +08001002 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001003}
1004
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001005static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1006{
1007 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1008 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1009 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001010 unsigned int nbytes = req->assoclen + req->cryptlen -
1011 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -07001012 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001013
Kees Cook8d605392018-09-18 19:10:51 -07001014 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001015 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1016 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1017
1018 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001019}
1020
Tobias Brunner73c89c12010-01-17 21:52:11 +11001021static int crypto_rfc4543_encrypt(struct aead_request *req)
1022{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +03001023 return crypto_ipsec_check_assoclen(req->assoclen) ?:
1024 crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001025}
1026
1027static int crypto_rfc4543_decrypt(struct aead_request *req)
1028{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +03001029 return crypto_ipsec_check_assoclen(req->assoclen) ?:
1030 crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001031}
1032
Herbert Xuadcbc682015-06-16 13:54:18 +08001033static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001034{
Herbert Xuadcbc682015-06-16 13:54:18 +08001035 struct aead_instance *inst = aead_alg_instance(tfm);
1036 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001037 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001038 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001039 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -07001040 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001041 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001042 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001043
1044 aead = crypto_spawn_aead(spawn);
1045 if (IS_ERR(aead))
1046 return PTR_ERR(aead);
1047
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001048 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001049 err = PTR_ERR(null);
1050 if (IS_ERR(null))
1051 goto err_free_aead;
1052
Tobias Brunner73c89c12010-01-17 21:52:11 +11001053 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001054 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001055
1056 align = crypto_aead_alignmask(aead);
1057 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001058 crypto_aead_set_reqsize(
1059 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001060 sizeof(struct crypto_rfc4543_req_ctx) +
1061 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001062 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001063
1064 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001065
1066err_free_aead:
1067 crypto_free_aead(aead);
1068 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001069}
1070
Herbert Xuadcbc682015-06-16 13:54:18 +08001071static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001072{
Herbert Xuadcbc682015-06-16 13:54:18 +08001073 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001074
1075 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001076 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001077}
1078
Herbert Xu7b05a372015-07-09 07:17:30 +08001079static void crypto_rfc4543_free(struct aead_instance *inst)
1080{
1081 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1082
1083 crypto_drop_aead(&ctx->aead);
1084
1085 kfree(inst);
1086}
1087
Herbert Xuadcbc682015-06-16 13:54:18 +08001088static int crypto_rfc4543_create(struct crypto_template *tmpl,
1089 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001090{
1091 struct crypto_attr_type *algt;
Eric Biggerscd900f02020-01-02 19:58:46 -08001092 u32 mask;
Herbert Xuadcbc682015-06-16 13:54:18 +08001093 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001094 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001095 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001096 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001097 const char *ccm_name;
1098 int err;
1099
1100 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001101 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001102 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001103
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001104 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001105 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001106
Eric Biggerscd900f02020-01-02 19:58:46 -08001107 mask = crypto_requires_sync(algt->type, algt->mask);
1108
Tobias Brunner73c89c12010-01-17 21:52:11 +11001109 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001110 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001111 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001112
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001113 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001114 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001115 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001116
Herbert Xuadcbc682015-06-16 13:54:18 +08001117 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001118 spawn = &ctx->aead;
Eric Biggerscd900f02020-01-02 19:58:46 -08001119 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
1120 ccm_name, 0, mask);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001121 if (err)
1122 goto out_free_inst;
1123
Herbert Xuadcbc682015-06-16 13:54:18 +08001124 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125
1126 err = -EINVAL;
1127
Herbert Xuadcbc682015-06-16 13:54:18 +08001128 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001129 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xu17db8542015-05-21 15:11:11 +08001130 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001131
1132 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001133 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001134 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001135
1136 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001137 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1138 "rfc4543(%s)", alg->base.cra_name) >=
1139 CRYPTO_MAX_ALG_NAME ||
1140 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1141 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001142 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001143 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001144
Herbert Xuadcbc682015-06-16 13:54:18 +08001145 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1146 inst->alg.base.cra_priority = alg->base.cra_priority;
1147 inst->alg.base.cra_blocksize = 1;
1148 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001149
Herbert Xuadcbc682015-06-16 13:54:18 +08001150 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001151
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001152 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001153 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001154 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001155
Herbert Xuadcbc682015-06-16 13:54:18 +08001156 inst->alg.init = crypto_rfc4543_init_tfm;
1157 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001158
Herbert Xuadcbc682015-06-16 13:54:18 +08001159 inst->alg.setkey = crypto_rfc4543_setkey;
1160 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1161 inst->alg.encrypt = crypto_rfc4543_encrypt;
1162 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001163
Herbert Xu7b05a372015-07-09 07:17:30 +08001164 inst->free = crypto_rfc4543_free,
1165
Herbert Xuadcbc682015-06-16 13:54:18 +08001166 err = aead_register_instance(tmpl, inst);
1167 if (err)
1168 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001169
1170out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001171 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001172
1173out_drop_alg:
1174 crypto_drop_aead(spawn);
1175out_free_inst:
1176 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001177 goto out;
1178}
1179
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001180static struct crypto_template crypto_gcm_tmpls[] = {
1181 {
1182 .name = "gcm_base",
1183 .create = crypto_gcm_base_create,
1184 .module = THIS_MODULE,
1185 }, {
1186 .name = "gcm",
1187 .create = crypto_gcm_create,
1188 .module = THIS_MODULE,
1189 }, {
1190 .name = "rfc4106",
1191 .create = crypto_rfc4106_create,
1192 .module = THIS_MODULE,
1193 }, {
1194 .name = "rfc4543",
1195 .create = crypto_rfc4543_create,
1196 .module = THIS_MODULE,
1197 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001198};
1199
Mikko Herranen28db8e32007-11-26 22:24:11 +08001200static int __init crypto_gcm_module_init(void)
1201{
Herbert Xud00aa192007-12-07 20:31:10 +08001202 int err;
1203
Herbert Xuadcbc682015-06-16 13:54:18 +08001204 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001205 if (!gcm_zeroes)
1206 return -ENOMEM;
1207
Herbert Xuadcbc682015-06-16 13:54:18 +08001208 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1209
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001210 err = crypto_register_templates(crypto_gcm_tmpls,
1211 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001212 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001213 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001214
Huang Ying9382d972009-08-06 15:34:26 +10001215 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001216}
1217
1218static void __exit crypto_gcm_module_exit(void)
1219{
Huang Ying9382d972009-08-06 15:34:26 +10001220 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001221 crypto_unregister_templates(crypto_gcm_tmpls,
1222 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001223}
1224
Eric Biggersc4741b22019-04-11 21:57:42 -07001225subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001226module_exit(crypto_gcm_module_exit);
1227
1228MODULE_LICENSE("GPL");
1229MODULE_DESCRIPTION("Galois/Counter Mode");
1230MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001231MODULE_ALIAS_CRYPTO("gcm_base");
1232MODULE_ALIAS_CRYPTO("rfc4106");
1233MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001234MODULE_ALIAS_CRYPTO("gcm");