blob: 887f472734b66c2d26b1de2002244fbee40a5d27 [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;
Herbert Xuadcbc682015-06-16 13:54:18 +0800859 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800860 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800861 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800862 const char *ccm_name;
863 int err;
864
865 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800866 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800867 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800868
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800869 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800870 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800871
872 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800873 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800874 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800875
876 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
877 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800878 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800879
Herbert Xuadcbc682015-06-16 13:54:18 +0800880 spawn = aead_instance_ctx(inst);
881 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800882 err = crypto_grab_aead(spawn, ccm_name, 0,
883 crypto_requires_sync(algt->type, algt->mask));
884 if (err)
885 goto out_free_inst;
886
Herbert Xuadcbc682015-06-16 13:54:18 +0800887 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800888
889 err = -EINVAL;
890
Herbert Xuadcbc682015-06-16 13:54:18 +0800891 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200892 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xudadbc532007-12-17 15:33:17 +0800893 goto out_drop_alg;
894
895 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800896 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800897 goto out_drop_alg;
898
899 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800900 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
901 "rfc4106(%s)", alg->base.cra_name) >=
902 CRYPTO_MAX_ALG_NAME ||
903 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
904 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800905 CRYPTO_MAX_ALG_NAME)
906 goto out_drop_alg;
907
Herbert Xu7b05a372015-07-09 07:17:30 +0800908 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800909 inst->alg.base.cra_priority = alg->base.cra_priority;
910 inst->alg.base.cra_blocksize = 1;
911 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800912
Herbert Xuadcbc682015-06-16 13:54:18 +0800913 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800914
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200915 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800916 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800917 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800918
Herbert Xuadcbc682015-06-16 13:54:18 +0800919 inst->alg.init = crypto_rfc4106_init_tfm;
920 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800921
Herbert Xuadcbc682015-06-16 13:54:18 +0800922 inst->alg.setkey = crypto_rfc4106_setkey;
923 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
924 inst->alg.encrypt = crypto_rfc4106_encrypt;
925 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800926
Herbert Xu7b05a372015-07-09 07:17:30 +0800927 inst->free = crypto_rfc4106_free;
928
Herbert Xuadcbc682015-06-16 13:54:18 +0800929 err = aead_register_instance(tmpl, inst);
930 if (err)
931 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800932
933out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800934 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800935
936out_drop_alg:
937 crypto_drop_aead(spawn);
938out_free_inst:
939 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800940 goto out;
941}
942
Tobias Brunner73c89c12010-01-17 21:52:11 +1100943static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
944 unsigned int keylen)
945{
946 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
947 struct crypto_aead *child = ctx->child;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100948
949 if (keylen < 4)
950 return -EINVAL;
951
952 keylen -= 4;
953 memcpy(ctx->nonce, key + keylen, 4);
954
955 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
956 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
957 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600958 return crypto_aead_setkey(child, key, keylen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100959}
960
961static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
962 unsigned int authsize)
963{
964 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
965
966 if (authsize != 16)
967 return -EINVAL;
968
969 return crypto_aead_setauthsize(ctx->child, authsize);
970}
971
Herbert Xuadcbc682015-06-16 13:54:18 +0800972static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +1100973{
974 struct crypto_aead *aead = crypto_aead_reqtfm(req);
975 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +0800976 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100977 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +1100978 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100979 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
980 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800981 int err;
982
983 if (req->src != req->dst) {
984 err = crypto_rfc4543_copy_src_to_dst(req, enc);
985 if (err)
986 return err;
987 }
Tobias Brunner73c89c12010-01-17 21:52:11 +1100988
989 memcpy(iv, ctx->nonce, 4);
990 memcpy(iv + 4, req->iv, 8);
991
Tobias Brunner73c89c12010-01-17 21:52:11 +1100992 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +0800993 aead_request_set_callback(subreq, req->base.flags,
994 req->base.complete, req->base.data);
995 aead_request_set_crypt(subreq, req->src, req->dst,
996 enc ? 0 : authsize, iv);
997 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
998 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +1100999
Herbert Xuadcbc682015-06-16 13:54:18 +08001000 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001001}
1002
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001003static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1004{
1005 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1006 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1007 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001008 unsigned int nbytes = req->assoclen + req->cryptlen -
1009 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -07001010 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001011
Kees Cook8d605392018-09-18 19:10:51 -07001012 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001013 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1014 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1015
1016 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001017}
1018
Tobias Brunner73c89c12010-01-17 21:52:11 +11001019static int crypto_rfc4543_encrypt(struct aead_request *req)
1020{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +03001021 return crypto_ipsec_check_assoclen(req->assoclen) ?:
1022 crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001023}
1024
1025static int crypto_rfc4543_decrypt(struct aead_request *req)
1026{
Iuliana Prodan74bf81d2019-08-02 11:47:33 +03001027 return crypto_ipsec_check_assoclen(req->assoclen) ?:
1028 crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001029}
1030
Herbert Xuadcbc682015-06-16 13:54:18 +08001031static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001032{
Herbert Xuadcbc682015-06-16 13:54:18 +08001033 struct aead_instance *inst = aead_alg_instance(tfm);
1034 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001035 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001036 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001037 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -07001038 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001039 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001040 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001041
1042 aead = crypto_spawn_aead(spawn);
1043 if (IS_ERR(aead))
1044 return PTR_ERR(aead);
1045
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001046 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001047 err = PTR_ERR(null);
1048 if (IS_ERR(null))
1049 goto err_free_aead;
1050
Tobias Brunner73c89c12010-01-17 21:52:11 +11001051 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001052 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001053
1054 align = crypto_aead_alignmask(aead);
1055 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001056 crypto_aead_set_reqsize(
1057 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001058 sizeof(struct crypto_rfc4543_req_ctx) +
1059 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001060 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001061
1062 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001063
1064err_free_aead:
1065 crypto_free_aead(aead);
1066 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001067}
1068
Herbert Xuadcbc682015-06-16 13:54:18 +08001069static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001070{
Herbert Xuadcbc682015-06-16 13:54:18 +08001071 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001072
1073 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001074 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001075}
1076
Herbert Xu7b05a372015-07-09 07:17:30 +08001077static void crypto_rfc4543_free(struct aead_instance *inst)
1078{
1079 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1080
1081 crypto_drop_aead(&ctx->aead);
1082
1083 kfree(inst);
1084}
1085
Herbert Xuadcbc682015-06-16 13:54:18 +08001086static int crypto_rfc4543_create(struct crypto_template *tmpl,
1087 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001088{
1089 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +08001090 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001091 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001092 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001093 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001094 const char *ccm_name;
1095 int err;
1096
1097 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001098 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001099 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001100
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001101 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001102 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001103
1104 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001105 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001106 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001107
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001108 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001109 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001110 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001111
Herbert Xuadcbc682015-06-16 13:54:18 +08001112 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001113 spawn = &ctx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001114 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001115 err = crypto_grab_aead(spawn, ccm_name, 0,
1116 crypto_requires_sync(algt->type, algt->mask));
1117 if (err)
1118 goto out_free_inst;
1119
Herbert Xuadcbc682015-06-16 13:54:18 +08001120 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001121
1122 err = -EINVAL;
1123
Herbert Xuadcbc682015-06-16 13:54:18 +08001124 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001125 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xu17db8542015-05-21 15:11:11 +08001126 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001127
1128 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001129 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001130 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001131
1132 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001133 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1134 "rfc4543(%s)", alg->base.cra_name) >=
1135 CRYPTO_MAX_ALG_NAME ||
1136 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1137 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001138 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001139 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001140
Herbert Xuadcbc682015-06-16 13:54:18 +08001141 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1142 inst->alg.base.cra_priority = alg->base.cra_priority;
1143 inst->alg.base.cra_blocksize = 1;
1144 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001145
Herbert Xuadcbc682015-06-16 13:54:18 +08001146 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001147
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001148 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001149 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001150 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001151
Herbert Xuadcbc682015-06-16 13:54:18 +08001152 inst->alg.init = crypto_rfc4543_init_tfm;
1153 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001154
Herbert Xuadcbc682015-06-16 13:54:18 +08001155 inst->alg.setkey = crypto_rfc4543_setkey;
1156 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1157 inst->alg.encrypt = crypto_rfc4543_encrypt;
1158 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001159
Herbert Xu7b05a372015-07-09 07:17:30 +08001160 inst->free = crypto_rfc4543_free,
1161
Herbert Xuadcbc682015-06-16 13:54:18 +08001162 err = aead_register_instance(tmpl, inst);
1163 if (err)
1164 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001165
1166out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001167 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001168
1169out_drop_alg:
1170 crypto_drop_aead(spawn);
1171out_free_inst:
1172 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001173 goto out;
1174}
1175
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001176static struct crypto_template crypto_gcm_tmpls[] = {
1177 {
1178 .name = "gcm_base",
1179 .create = crypto_gcm_base_create,
1180 .module = THIS_MODULE,
1181 }, {
1182 .name = "gcm",
1183 .create = crypto_gcm_create,
1184 .module = THIS_MODULE,
1185 }, {
1186 .name = "rfc4106",
1187 .create = crypto_rfc4106_create,
1188 .module = THIS_MODULE,
1189 }, {
1190 .name = "rfc4543",
1191 .create = crypto_rfc4543_create,
1192 .module = THIS_MODULE,
1193 },
Tobias Brunner73c89c12010-01-17 21:52:11 +11001194};
1195
Mikko Herranen28db8e32007-11-26 22:24:11 +08001196static int __init crypto_gcm_module_init(void)
1197{
Herbert Xud00aa192007-12-07 20:31:10 +08001198 int err;
1199
Herbert Xuadcbc682015-06-16 13:54:18 +08001200 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001201 if (!gcm_zeroes)
1202 return -ENOMEM;
1203
Herbert Xuadcbc682015-06-16 13:54:18 +08001204 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1205
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001206 err = crypto_register_templates(crypto_gcm_tmpls,
1207 ARRAY_SIZE(crypto_gcm_tmpls));
Herbert Xud00aa192007-12-07 20:31:10 +08001208 if (err)
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001209 kfree(gcm_zeroes);
Herbert Xud00aa192007-12-07 20:31:10 +08001210
Huang Ying9382d972009-08-06 15:34:26 +10001211 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001212}
1213
1214static void __exit crypto_gcm_module_exit(void)
1215{
Huang Ying9382d972009-08-06 15:34:26 +10001216 kfree(gcm_zeroes);
Xiongfeng Wang56a00d92019-01-18 13:58:13 +08001217 crypto_unregister_templates(crypto_gcm_tmpls,
1218 ARRAY_SIZE(crypto_gcm_tmpls));
Mikko Herranen28db8e32007-11-26 22:24:11 +08001219}
1220
Eric Biggersc4741b22019-04-11 21:57:42 -07001221subsys_initcall(crypto_gcm_module_init);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001222module_exit(crypto_gcm_module_exit);
1223
1224MODULE_LICENSE("GPL");
1225MODULE_DESCRIPTION("Galois/Counter Mode");
1226MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001227MODULE_ALIAS_CRYPTO("gcm_base");
1228MODULE_ALIAS_CRYPTO("rfc4106");
1229MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001230MODULE_ALIAS_CRYPTO("gcm");