blob: e438492db2ca2c745f941f2e89772a71ed157bf9 [file] [log] [blame]
Mikko Herranen28db8e32007-11-26 22:24:11 +08001/*
2 * GCM: Galois/Counter Mode.
3 *
4 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 */
10
Mikko Herranen28db8e32007-11-26 22:24:11 +080011#include <crypto/gf128mul.h>
Herbert Xudadbc532007-12-17 15:33:17 +080012#include <crypto/internal/aead.h>
Herbert Xu1472e5e2007-12-07 19:26:11 +080013#include <crypto/internal/skcipher.h>
Huang Ying9382d972009-08-06 15:34:26 +100014#include <crypto/internal/hash.h>
Herbert Xu17db8542015-05-21 15:11:11 +080015#include <crypto/null.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080016#include <crypto/scatterwalk.h>
Corentin LABBEe0ab7e92017-08-22 10:08:17 +020017#include <crypto/gcm.h>
Huang Ying9382d972009-08-06 15:34:26 +100018#include <crypto/hash.h>
19#include "internal.h"
Mikko Herranen28db8e32007-11-26 22:24:11 +080020#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25
Mikko Herranen28db8e32007-11-26 22:24:11 +080026struct gcm_instance_ctx {
Herbert Xu1472e5e2007-12-07 19:26:11 +080027 struct crypto_skcipher_spawn ctr;
Huang Ying9382d972009-08-06 15:34:26 +100028 struct crypto_ahash_spawn ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080029};
30
31struct crypto_gcm_ctx {
Herbert Xu16f37ecd2016-07-12 13:17:39 +080032 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +100033 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +080034};
35
Herbert Xudadbc532007-12-17 15:33:17 +080036struct crypto_rfc4106_ctx {
37 struct crypto_aead *child;
38 u8 nonce[4];
39};
40
Herbert Xu7b05a372015-07-09 07:17:30 +080041struct crypto_rfc4106_req_ctx {
42 struct scatterlist src[3];
43 struct scatterlist dst[3];
44 struct aead_request subreq;
45};
46
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030047struct crypto_rfc4543_instance_ctx {
48 struct crypto_aead_spawn aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +030049};
50
Tobias Brunner73c89c12010-01-17 21:52:11 +110051struct crypto_rfc4543_ctx {
52 struct crypto_aead *child;
Kees Cook8d605392018-09-18 19:10:51 -070053 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +110054 u8 nonce[4];
55};
56
57struct crypto_rfc4543_req_ctx {
Tobias Brunner73c89c12010-01-17 21:52:11 +110058 struct aead_request subreq;
59};
60
Mikko Herranen28db8e32007-11-26 22:24:11 +080061struct crypto_gcm_ghash_ctx {
Huang Ying9382d972009-08-06 15:34:26 +100062 unsigned int cryptlen;
63 struct scatterlist *src;
Herbert Xuadcbc682015-06-16 13:54:18 +080064 int (*complete)(struct aead_request *req, u32 flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +080065};
66
67struct crypto_gcm_req_priv_ctx {
Herbert Xuadcbc682015-06-16 13:54:18 +080068 u8 iv[16];
Mikko Herranen28db8e32007-11-26 22:24:11 +080069 u8 auth_tag[16];
Herbert Xu6160b282007-12-04 19:17:50 +110070 u8 iauth_tag[16];
Herbert Xuadcbc682015-06-16 13:54:18 +080071 struct scatterlist src[3];
72 struct scatterlist dst[3];
73 struct scatterlist sg;
Huang Ying9382d972009-08-06 15:34:26 +100074 struct crypto_gcm_ghash_ctx ghash_ctx;
75 union {
76 struct ahash_request ahreq;
Herbert Xu16f37ecd2016-07-12 13:17:39 +080077 struct skcipher_request skreq;
Huang Ying9382d972009-08-06 15:34:26 +100078 } u;
Mikko Herranen28db8e32007-11-26 22:24:11 +080079};
80
Herbert Xuadcbc682015-06-16 13:54:18 +080081static struct {
82 u8 buf[16];
83 struct scatterlist sg;
84} *gcm_zeroes;
85
86static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
Huang Ying9382d972009-08-06 15:34:26 +100087
Herbert Xu25894692007-12-12 19:16:38 +080088static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
89 struct aead_request *req)
90{
91 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
92
93 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
94}
95
Mikko Herranen28db8e32007-11-26 22:24:11 +080096static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
97 unsigned int keylen)
98{
99 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000100 struct crypto_ahash *ghash = ctx->ghash;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800101 struct crypto_skcipher *ctr = ctx->ctr;
Herbert Xu84c91152007-12-17 21:42:08 +0800102 struct {
103 be128 hash;
Ondrej Mosnáček50d2e6d2016-09-23 10:47:32 +0200104 u8 iv[16];
Herbert Xu84c91152007-12-17 21:42:08 +0800105
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100106 struct crypto_wait wait;
Herbert Xu84c91152007-12-17 21:42:08 +0800107
108 struct scatterlist sg[1];
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800109 struct skcipher_request req;
Herbert Xu84c91152007-12-17 21:42:08 +0800110 } *data;
111 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800112
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800113 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
114 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
115 CRYPTO_TFM_REQ_MASK);
116 err = crypto_skcipher_setkey(ctr, key, keylen);
117 crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
Herbert Xuadcbc682015-06-16 13:54:18 +0800118 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800119 if (err)
Herbert Xu84c91152007-12-17 21:42:08 +0800120 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800121
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800122 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
Herbert Xu84c91152007-12-17 21:42:08 +0800123 GFP_KERNEL);
124 if (!data)
125 return -ENOMEM;
126
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100127 crypto_init_wait(&data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800128 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800129 skcipher_request_set_tfm(&data->req, ctr);
130 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
131 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100132 crypto_req_done,
133 &data->wait);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800134 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
135 sizeof(data->hash), data->iv);
Herbert Xu84c91152007-12-17 21:42:08 +0800136
Gilad Ben-Yossef76c67392017-10-18 08:00:42 +0100137 err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
138 &data->wait);
Herbert Xu84c91152007-12-17 21:42:08 +0800139
Mikko Herranen28db8e32007-11-26 22:24:11 +0800140 if (err)
141 goto out;
142
Huang Ying9382d972009-08-06 15:34:26 +1000143 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
144 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
145 CRYPTO_TFM_REQ_MASK);
146 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
147 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
148 CRYPTO_TFM_RES_MASK);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800149
Herbert Xu84c91152007-12-17 21:42:08 +0800150out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800151 kzfree(data);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800152 return err;
153}
154
Herbert Xudadbc532007-12-17 15:33:17 +0800155static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
156 unsigned int authsize)
157{
158 switch (authsize) {
159 case 4:
160 case 8:
161 case 12:
162 case 13:
163 case 14:
164 case 15:
165 case 16:
166 break;
167 default:
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
Herbert Xuadcbc682015-06-16 13:54:18 +0800174static void crypto_gcm_init_common(struct aead_request *req)
175{
176 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
177 __be32 counter = cpu_to_be32(1);
178 struct scatterlist *sg;
179
180 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200181 memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
182 memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
Herbert Xuadcbc682015-06-16 13:54:18 +0800183
184 sg_init_table(pctx->src, 3);
185 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
186 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
187 if (sg != pctx->src + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200188 sg_chain(pctx->src, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800189
190 if (req->src != req->dst) {
191 sg_init_table(pctx->dst, 3);
192 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
193 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
194 if (sg != pctx->dst + 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200195 sg_chain(pctx->dst, 2, sg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800196 }
197}
198
199static void crypto_gcm_init_crypt(struct aead_request *req,
Herbert Xu84c91152007-12-17 21:42:08 +0800200 unsigned int cryptlen)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800201{
202 struct crypto_aead *aead = crypto_aead_reqtfm(req);
203 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu25894692007-12-12 19:16:38 +0800204 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800205 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xu84c91152007-12-17 21:42:08 +0800206 struct scatterlist *dst;
Herbert Xu84c91152007-12-17 21:42:08 +0800207
Herbert Xuadcbc682015-06-16 13:54:18 +0800208 dst = req->src == req->dst ? pctx->src : pctx->dst;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800209
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800210 skcipher_request_set_tfm(skreq, ctx->ctr);
211 skcipher_request_set_crypt(skreq, pctx->src, dst,
Herbert Xu84c91152007-12-17 21:42:08 +0800212 cryptlen + sizeof(pctx->auth_tag),
Herbert Xuadcbc682015-06-16 13:54:18 +0800213 pctx->iv);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800214}
215
Huang Ying9382d972009-08-06 15:34:26 +1000216static inline unsigned int gcm_remain(unsigned int len)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800217{
Huang Ying9382d972009-08-06 15:34:26 +1000218 len &= 0xfU;
219 return len ? 16 - len : 0;
220}
221
222static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
Huang Ying9382d972009-08-06 15:34:26 +1000223
224static int gcm_hash_update(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700225 crypto_completion_t compl,
Huang Ying9382d972009-08-06 15:34:26 +1000226 struct scatterlist *src,
Herbert Xuadcbc682015-06-16 13:54:18 +0800227 unsigned int len, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000228{
Herbert Xuadcbc682015-06-16 13:54:18 +0800229 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000230 struct ahash_request *ahreq = &pctx->u.ahreq;
231
Herbert Xuadcbc682015-06-16 13:54:18 +0800232 ahash_request_set_callback(ahreq, flags, compl, req);
Huang Ying9382d972009-08-06 15:34:26 +1000233 ahash_request_set_crypt(ahreq, src, NULL, len);
234
235 return crypto_ahash_update(ahreq);
236}
237
238static int gcm_hash_remain(struct aead_request *req,
Huang Ying9382d972009-08-06 15:34:26 +1000239 unsigned int remain,
Herbert Xuadcbc682015-06-16 13:54:18 +0800240 crypto_completion_t compl, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000241{
Herbert Xuadcbc682015-06-16 13:54:18 +0800242 return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000243}
244
Herbert Xuadcbc682015-06-16 13:54:18 +0800245static int gcm_hash_len(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000246{
Herbert Xuadcbc682015-06-16 13:54:18 +0800247 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000248 struct ahash_request *ahreq = &pctx->u.ahreq;
249 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
250 u128 lengths;
251
252 lengths.a = cpu_to_be64(req->assoclen * 8);
253 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
254 memcpy(pctx->iauth_tag, &lengths, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800255 sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
256 ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
257 ahash_request_set_crypt(ahreq, &pctx->sg,
258 pctx->iauth_tag, sizeof(lengths));
Huang Ying9382d972009-08-06 15:34:26 +1000259
Herbert Xuadcbc682015-06-16 13:54:18 +0800260 return crypto_ahash_finup(ahreq);
Huang Ying9382d972009-08-06 15:34:26 +1000261}
262
Herbert Xuadcbc682015-06-16 13:54:18 +0800263static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000264{
Herbert Xu25894692007-12-12 19:16:38 +0800265 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000266 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800267
Herbert Xuadcbc682015-06-16 13:54:18 +0800268 return gctx->complete(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000269}
270
Huang Ying62c55932009-11-16 21:52:22 +0800271static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000272{
273 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800274
Herbert Xuadcbc682015-06-16 13:54:18 +0800275 if (err)
276 goto out;
277
278 err = gcm_hash_len_continue(req, 0);
279 if (err == -EINPROGRESS)
280 return;
281
282out:
283 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800284}
285
Herbert Xuadcbc682015-06-16 13:54:18 +0800286static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800287{
Herbert Xuadcbc682015-06-16 13:54:18 +0800288 return gcm_hash_len(req, flags) ?:
289 gcm_hash_len_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000290}
291
Huang Ying62c55932009-11-16 21:52:22 +0800292static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
293 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000294{
295 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800296
Herbert Xuadcbc682015-06-16 13:54:18 +0800297 if (err)
298 goto out;
299
300 err = gcm_hash_crypt_remain_continue(req, 0);
301 if (err == -EINPROGRESS)
302 return;
303
304out:
305 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800306}
307
Herbert Xuadcbc682015-06-16 13:54:18 +0800308static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800309{
Huang Ying9382d972009-08-06 15:34:26 +1000310 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
311 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
312 unsigned int remain;
313
Herbert Xuadcbc682015-06-16 13:54:18 +0800314 remain = gcm_remain(gctx->cryptlen);
315 if (remain)
316 return gcm_hash_remain(req, remain,
317 gcm_hash_crypt_remain_done, flags) ?:
318 gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000319
Herbert Xuadcbc682015-06-16 13:54:18 +0800320 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000321}
322
Huang Ying62c55932009-11-16 21:52:22 +0800323static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000324{
325 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800326
Herbert Xuadcbc682015-06-16 13:54:18 +0800327 if (err)
328 goto out;
329
330 err = gcm_hash_crypt_continue(req, 0);
331 if (err == -EINPROGRESS)
332 return;
333
334out:
335 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800336}
337
Herbert Xuadcbc682015-06-16 13:54:18 +0800338static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800339{
Huang Ying9382d972009-08-06 15:34:26 +1000340 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
341 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Huang Ying9382d972009-08-06 15:34:26 +1000342
Herbert Xuadcbc682015-06-16 13:54:18 +0800343 if (gctx->cryptlen)
344 return gcm_hash_update(req, gcm_hash_crypt_done,
345 gctx->src, gctx->cryptlen, flags) ?:
346 gcm_hash_crypt_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000347
Herbert Xuadcbc682015-06-16 13:54:18 +0800348 return gcm_hash_crypt_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000349}
350
Huang Ying62c55932009-11-16 21:52:22 +0800351static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
352 int err)
Huang Ying9382d972009-08-06 15:34:26 +1000353{
354 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800355
Herbert Xuadcbc682015-06-16 13:54:18 +0800356 if (err)
357 goto out;
358
359 err = gcm_hash_assoc_remain_continue(req, 0);
360 if (err == -EINPROGRESS)
361 return;
362
363out:
364 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800365}
366
Herbert Xuadcbc682015-06-16 13:54:18 +0800367static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800368{
Huang Ying9382d972009-08-06 15:34:26 +1000369 unsigned int remain;
370
Herbert Xuadcbc682015-06-16 13:54:18 +0800371 remain = gcm_remain(req->assoclen);
372 if (remain)
373 return gcm_hash_remain(req, remain,
374 gcm_hash_assoc_remain_done, flags) ?:
375 gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000376
Herbert Xuadcbc682015-06-16 13:54:18 +0800377 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000378}
379
Huang Ying62c55932009-11-16 21:52:22 +0800380static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000381{
382 struct aead_request *req = areq->data;
Huang Ying62c55932009-11-16 21:52:22 +0800383
Herbert Xuadcbc682015-06-16 13:54:18 +0800384 if (err)
385 goto out;
386
387 err = gcm_hash_assoc_continue(req, 0);
388 if (err == -EINPROGRESS)
389 return;
390
391out:
392 aead_request_complete(req, err);
Huang Ying62c55932009-11-16 21:52:22 +0800393}
394
Herbert Xuadcbc682015-06-16 13:54:18 +0800395static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
Huang Ying62c55932009-11-16 21:52:22 +0800396{
Herbert Xuadcbc682015-06-16 13:54:18 +0800397 if (req->assoclen)
398 return gcm_hash_update(req, gcm_hash_assoc_done,
399 req->src, req->assoclen, flags) ?:
400 gcm_hash_assoc_continue(req, flags);
Huang Ying9382d972009-08-06 15:34:26 +1000401
Herbert Xuadcbc682015-06-16 13:54:18 +0800402 return gcm_hash_assoc_remain_continue(req, flags);
Huang Ying62c55932009-11-16 21:52:22 +0800403}
404
405static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
406{
407 struct aead_request *req = areq->data;
408
Herbert Xuadcbc682015-06-16 13:54:18 +0800409 if (err)
410 goto out;
411
412 err = gcm_hash_init_continue(req, 0);
413 if (err == -EINPROGRESS)
414 return;
415
416out:
417 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000418}
419
Herbert Xuadcbc682015-06-16 13:54:18 +0800420static int gcm_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000421{
Herbert Xuadcbc682015-06-16 13:54:18 +0800422 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000423 struct ahash_request *ahreq = &pctx->u.ahreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800424 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
Huang Ying9382d972009-08-06 15:34:26 +1000425
426 ahash_request_set_tfm(ahreq, ctx->ghash);
427
Herbert Xuadcbc682015-06-16 13:54:18 +0800428 ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
429 return crypto_ahash_init(ahreq) ?:
430 gcm_hash_init_continue(req, flags);
Herbert Xu6160b282007-12-04 19:17:50 +1100431}
432
Herbert Xuadcbc682015-06-16 13:54:18 +0800433static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000434{
Herbert Xuadcbc682015-06-16 13:54:18 +0800435 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000436 struct crypto_aead *aead = crypto_aead_reqtfm(req);
437 u8 *auth_tag = pctx->auth_tag;
438
Herbert Xuadcbc682015-06-16 13:54:18 +0800439 crypto_xor(auth_tag, pctx->iauth_tag, 16);
440 scatterwalk_map_and_copy(auth_tag, req->dst,
441 req->assoclen + req->cryptlen,
Huang Ying9382d972009-08-06 15:34:26 +1000442 crypto_aead_authsize(aead), 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800443 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000444}
445
Herbert Xuadcbc682015-06-16 13:54:18 +0800446static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
Herbert Xu6160b282007-12-04 19:17:50 +1100447{
Huang Ying9382d972009-08-06 15:34:26 +1000448 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xuadcbc682015-06-16 13:54:18 +0800449 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100450
Herbert Xuadcbc682015-06-16 13:54:18 +0800451 gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
452 gctx->cryptlen = req->cryptlen;
453 gctx->complete = gcm_enc_copy_hash;
Herbert Xu6160b282007-12-04 19:17:50 +1100454
Herbert Xuadcbc682015-06-16 13:54:18 +0800455 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800456}
457
Huang Ying62c55932009-11-16 21:52:22 +0800458static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
Huang Ying9382d972009-08-06 15:34:26 +1000459{
460 struct aead_request *req = areq->data;
Huang Ying9382d972009-08-06 15:34:26 +1000461
Herbert Xuadcbc682015-06-16 13:54:18 +0800462 if (err)
463 goto out;
Huang Ying9382d972009-08-06 15:34:26 +1000464
Herbert Xuadcbc682015-06-16 13:54:18 +0800465 err = gcm_encrypt_continue(req, 0);
466 if (err == -EINPROGRESS)
467 return;
468
469out:
Huang Ying62c55932009-11-16 21:52:22 +0800470 aead_request_complete(req, err);
Huang Ying9382d972009-08-06 15:34:26 +1000471}
472
Mikko Herranen28db8e32007-11-26 22:24:11 +0800473static int crypto_gcm_encrypt(struct aead_request *req)
474{
Herbert Xu25894692007-12-12 19:16:38 +0800475 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800476 struct skcipher_request *skreq = &pctx->u.skreq;
Herbert Xuadcbc682015-06-16 13:54:18 +0800477 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800478
Herbert Xuadcbc682015-06-16 13:54:18 +0800479 crypto_gcm_init_common(req);
480 crypto_gcm_init_crypt(req, req->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800481 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
Huang Ying9382d972009-08-06 15:34:26 +1000482
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800483 return crypto_skcipher_encrypt(skreq) ?:
Herbert Xuadcbc682015-06-16 13:54:18 +0800484 gcm_encrypt_continue(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800485}
486
Herbert Xuadcbc682015-06-16 13:54:18 +0800487static int crypto_gcm_verify(struct aead_request *req)
Herbert Xu84c91152007-12-17 21:42:08 +0800488{
Herbert Xuadcbc682015-06-16 13:54:18 +0800489 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800490 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800491 u8 *auth_tag = pctx->auth_tag;
492 u8 *iauth_tag = pctx->iauth_tag;
493 unsigned int authsize = crypto_aead_authsize(aead);
494 unsigned int cryptlen = req->cryptlen - authsize;
495
Huang Ying9382d972009-08-06 15:34:26 +1000496 crypto_xor(auth_tag, iauth_tag, 16);
Herbert Xuadcbc682015-06-16 13:54:18 +0800497 scatterwalk_map_and_copy(iauth_tag, req->src,
498 req->assoclen + cryptlen, authsize, 0);
James Yonan6bf37e52013-09-26 02:20:39 -0600499 return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
Herbert Xu84c91152007-12-17 21:42:08 +0800500}
501
Huang Ying9382d972009-08-06 15:34:26 +1000502static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800503{
Herbert Xu84c91152007-12-17 21:42:08 +0800504 struct aead_request *req = areq->data;
505
506 if (!err)
Herbert Xuadcbc682015-06-16 13:54:18 +0800507 err = crypto_gcm_verify(req);
Herbert Xu84c91152007-12-17 21:42:08 +0800508
509 aead_request_complete(req, err);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800510}
511
Herbert Xuadcbc682015-06-16 13:54:18 +0800512static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
Huang Ying9382d972009-08-06 15:34:26 +1000513{
Huang Ying9382d972009-08-06 15:34:26 +1000514 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800515 struct skcipher_request *skreq = &pctx->u.skreq;
Huang Ying9382d972009-08-06 15:34:26 +1000516 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
517
Herbert Xuadcbc682015-06-16 13:54:18 +0800518 crypto_gcm_init_crypt(req, gctx->cryptlen);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800519 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
520 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
Huang Ying9382d972009-08-06 15:34:26 +1000521}
522
Mikko Herranen28db8e32007-11-26 22:24:11 +0800523static int crypto_gcm_decrypt(struct aead_request *req)
524{
Herbert Xu6160b282007-12-04 19:17:50 +1100525 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu25894692007-12-12 19:16:38 +0800526 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
Huang Ying9382d972009-08-06 15:34:26 +1000527 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
Herbert Xu6160b282007-12-04 19:17:50 +1100528 unsigned int authsize = crypto_aead_authsize(aead);
Huang Ying9382d972009-08-06 15:34:26 +1000529 unsigned int cryptlen = req->cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800530 u32 flags = aead_request_flags(req);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800531
Herbert Xu6160b282007-12-04 19:17:50 +1100532 cryptlen -= authsize;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800533
Herbert Xuadcbc682015-06-16 13:54:18 +0800534 crypto_gcm_init_common(req);
535
536 gctx->src = sg_next(pctx->src);
Huang Ying9382d972009-08-06 15:34:26 +1000537 gctx->cryptlen = cryptlen;
Herbert Xuadcbc682015-06-16 13:54:18 +0800538 gctx->complete = gcm_dec_hash_continue;
Huang Ying9382d972009-08-06 15:34:26 +1000539
Herbert Xuadcbc682015-06-16 13:54:18 +0800540 return gcm_hash(req, flags);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800541}
542
Herbert Xuadcbc682015-06-16 13:54:18 +0800543static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800544{
Herbert Xuadcbc682015-06-16 13:54:18 +0800545 struct aead_instance *inst = aead_alg_instance(tfm);
546 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
547 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800548 struct crypto_skcipher *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000549 struct crypto_ahash *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800550 unsigned long align;
551 int err;
552
Huang Ying9382d972009-08-06 15:34:26 +1000553 ghash = crypto_spawn_ahash(&ictx->ghash);
554 if (IS_ERR(ghash))
555 return PTR_ERR(ghash);
556
Eric Biggers60425a82016-10-28 09:52:19 -0700557 ctr = crypto_spawn_skcipher(&ictx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800558 err = PTR_ERR(ctr);
559 if (IS_ERR(ctr))
Huang Ying9382d972009-08-06 15:34:26 +1000560 goto err_free_hash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800561
562 ctx->ctr = ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000563 ctx->ghash = ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800564
Herbert Xuadcbc682015-06-16 13:54:18 +0800565 align = crypto_aead_alignmask(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800566 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800567 crypto_aead_set_reqsize(tfm,
Herbert Xu5d723362015-05-11 17:47:59 +0800568 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800569 max(sizeof(struct skcipher_request) +
570 crypto_skcipher_reqsize(ctr),
Huang Ying9382d972009-08-06 15:34:26 +1000571 sizeof(struct ahash_request) +
Herbert Xu5d723362015-05-11 17:47:59 +0800572 crypto_ahash_reqsize(ghash)));
Mikko Herranen28db8e32007-11-26 22:24:11 +0800573
574 return 0;
Huang Ying9382d972009-08-06 15:34:26 +1000575
576err_free_hash:
577 crypto_free_ahash(ghash);
578 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800579}
580
Herbert Xuadcbc682015-06-16 13:54:18 +0800581static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800582{
Herbert Xuadcbc682015-06-16 13:54:18 +0800583 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800584
Huang Ying9382d972009-08-06 15:34:26 +1000585 crypto_free_ahash(ctx->ghash);
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800586 crypto_free_skcipher(ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800587}
588
Herbert Xu7b05a372015-07-09 07:17:30 +0800589static void crypto_gcm_free(struct aead_instance *inst)
590{
591 struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
592
593 crypto_drop_skcipher(&ctx->ctr);
594 crypto_drop_ahash(&ctx->ghash);
595 kfree(inst);
596}
597
Herbert Xuadcbc682015-06-16 13:54:18 +0800598static int crypto_gcm_create_common(struct crypto_template *tmpl,
599 struct rtattr **tb,
600 const char *full_name,
601 const char *ctr_name,
602 const char *ghash_name)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800603{
Herbert Xud00aa192007-12-07 20:31:10 +0800604 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800605 struct aead_instance *inst;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800606 struct skcipher_alg *ctr;
Huang Ying9382d972009-08-06 15:34:26 +1000607 struct crypto_alg *ghash_alg;
Herbert Xuadcbc682015-06-16 13:54:18 +0800608 struct hash_alg_common *ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800609 struct gcm_instance_ctx *ctx;
610 int err;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800611
Herbert Xud00aa192007-12-07 20:31:10 +0800612 algt = crypto_get_attr_type(tb);
Herbert Xud00aa192007-12-07 20:31:10 +0800613 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800614 return PTR_ERR(algt);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800615
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800616 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800617 return -EINVAL;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800618
Huang Ying9382d972009-08-06 15:34:26 +1000619 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
620 CRYPTO_ALG_TYPE_HASH,
Herbert Xub30bdfa2016-06-15 22:27:05 +0800621 CRYPTO_ALG_TYPE_AHASH_MASK |
622 crypto_requires_sync(algt->type,
623 algt->mask));
Huang Ying9382d972009-08-06 15:34:26 +1000624 if (IS_ERR(ghash_alg))
Herbert Xuadcbc682015-06-16 13:54:18 +0800625 return PTR_ERR(ghash_alg);
626
627 ghash = __crypto_hash_alg_common(ghash_alg);
Huang Ying9382d972009-08-06 15:34:26 +1000628
629 err = -ENOMEM;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800630 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
631 if (!inst)
Huang Ying9382d972009-08-06 15:34:26 +1000632 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800633
Herbert Xuadcbc682015-06-16 13:54:18 +0800634 ctx = aead_instance_ctx(inst);
635 err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
636 aead_crypto_instance(inst));
Huang Ying9382d972009-08-06 15:34:26 +1000637 if (err)
638 goto err_free_inst;
639
Herbert Xuadcbc682015-06-16 13:54:18 +0800640 err = -EINVAL;
641 if (ghash->digestsize != 16)
642 goto err_drop_ghash;
643
644 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700645 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
646 crypto_requires_sync(algt->type,
647 algt->mask));
Herbert Xu1472e5e2007-12-07 19:26:11 +0800648 if (err)
Huang Ying9382d972009-08-06 15:34:26 +1000649 goto err_drop_ghash;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800650
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800651 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800652
Herbert Xud00aa192007-12-07 20:31:10 +0800653 /* We only support 16-byte blocks. */
Wei Yongjun9b40f792016-10-17 15:10:06 +0000654 err = -EINVAL;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800655 if (crypto_skcipher_alg_ivsize(ctr) != 16)
Herbert Xud00aa192007-12-07 20:31:10 +0800656 goto out_put_ctr;
657
658 /* Not a stream cipher? */
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800659 if (ctr->base.cra_blocksize != 1)
Mikko Herranen28db8e32007-11-26 22:24:11 +0800660 goto out_put_ctr;
661
Mikko Herranen28db8e32007-11-26 22:24:11 +0800662 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800663 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800664 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
Huang Ying9382d972009-08-06 15:34:26 +1000665 ghash_alg->cra_driver_name) >=
Herbert Xud00aa192007-12-07 20:31:10 +0800666 CRYPTO_MAX_ALG_NAME)
Herbert Xu1472e5e2007-12-07 19:26:11 +0800667 goto out_put_ctr;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800668
Herbert Xuadcbc682015-06-16 13:54:18 +0800669 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
Herbert Xud00aa192007-12-07 20:31:10 +0800670
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800671 inst->alg.base.cra_flags = (ghash->base.cra_flags |
672 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800673 inst->alg.base.cra_priority = (ghash->base.cra_priority +
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800674 ctr->base.cra_priority) / 2;
Herbert Xuadcbc682015-06-16 13:54:18 +0800675 inst->alg.base.cra_blocksize = 1;
676 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800677 ctr->base.cra_alignmask;
Herbert Xuadcbc682015-06-16 13:54:18 +0800678 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200679 inst->alg.ivsize = GCM_AES_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800680 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
Herbert Xuadcbc682015-06-16 13:54:18 +0800681 inst->alg.maxauthsize = 16;
682 inst->alg.init = crypto_gcm_init_tfm;
683 inst->alg.exit = crypto_gcm_exit_tfm;
684 inst->alg.setkey = crypto_gcm_setkey;
685 inst->alg.setauthsize = crypto_gcm_setauthsize;
686 inst->alg.encrypt = crypto_gcm_encrypt;
687 inst->alg.decrypt = crypto_gcm_decrypt;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800688
Herbert Xu7b05a372015-07-09 07:17:30 +0800689 inst->free = crypto_gcm_free;
690
Herbert Xuadcbc682015-06-16 13:54:18 +0800691 err = aead_register_instance(tmpl, inst);
692 if (err)
693 goto out_put_ctr;
694
695out_put_ghash:
Huang Ying9382d972009-08-06 15:34:26 +1000696 crypto_mod_put(ghash_alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800697 return err;
Herbert Xu1472e5e2007-12-07 19:26:11 +0800698
699out_put_ctr:
700 crypto_drop_skcipher(&ctx->ctr);
Huang Ying9382d972009-08-06 15:34:26 +1000701err_drop_ghash:
702 crypto_drop_ahash(&ctx->ghash);
Mikko Herranen28db8e32007-11-26 22:24:11 +0800703err_free_inst:
704 kfree(inst);
Herbert Xuadcbc682015-06-16 13:54:18 +0800705 goto out_put_ghash;
Mikko Herranen28db8e32007-11-26 22:24:11 +0800706}
707
Herbert Xuadcbc682015-06-16 13:54:18 +0800708static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800709{
Herbert Xud00aa192007-12-07 20:31:10 +0800710 const char *cipher_name;
711 char ctr_name[CRYPTO_MAX_ALG_NAME];
712 char full_name[CRYPTO_MAX_ALG_NAME];
713
714 cipher_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800715 if (IS_ERR(cipher_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800716 return PTR_ERR(cipher_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800717
718 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
719 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800720 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800721
722 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
723 CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800724 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800725
Herbert Xuadcbc682015-06-16 13:54:18 +0800726 return crypto_gcm_create_common(tmpl, tb, full_name,
727 ctr_name, "ghash");
Herbert Xud00aa192007-12-07 20:31:10 +0800728}
729
Mikko Herranen28db8e32007-11-26 22:24:11 +0800730static struct crypto_template crypto_gcm_tmpl = {
731 .name = "gcm",
Herbert Xuadcbc682015-06-16 13:54:18 +0800732 .create = crypto_gcm_create,
Mikko Herranen28db8e32007-11-26 22:24:11 +0800733 .module = THIS_MODULE,
734};
735
Herbert Xuadcbc682015-06-16 13:54:18 +0800736static int crypto_gcm_base_create(struct crypto_template *tmpl,
737 struct rtattr **tb)
Herbert Xud00aa192007-12-07 20:31:10 +0800738{
Herbert Xud00aa192007-12-07 20:31:10 +0800739 const char *ctr_name;
Huang Ying9382d972009-08-06 15:34:26 +1000740 const char *ghash_name;
Herbert Xud00aa192007-12-07 20:31:10 +0800741 char full_name[CRYPTO_MAX_ALG_NAME];
742
743 ctr_name = crypto_attr_alg_name(tb[1]);
Herbert Xud00aa192007-12-07 20:31:10 +0800744 if (IS_ERR(ctr_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800745 return PTR_ERR(ctr_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800746
Huang Ying9382d972009-08-06 15:34:26 +1000747 ghash_name = crypto_attr_alg_name(tb[2]);
Huang Ying9382d972009-08-06 15:34:26 +1000748 if (IS_ERR(ghash_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800749 return PTR_ERR(ghash_name);
Huang Ying9382d972009-08-06 15:34:26 +1000750
751 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
752 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xuadcbc682015-06-16 13:54:18 +0800753 return -ENAMETOOLONG;
Herbert Xud00aa192007-12-07 20:31:10 +0800754
Herbert Xuadcbc682015-06-16 13:54:18 +0800755 return crypto_gcm_create_common(tmpl, tb, full_name,
756 ctr_name, ghash_name);
Herbert Xud00aa192007-12-07 20:31:10 +0800757}
758
759static struct crypto_template crypto_gcm_base_tmpl = {
760 .name = "gcm_base",
Herbert Xuadcbc682015-06-16 13:54:18 +0800761 .create = crypto_gcm_base_create,
Herbert Xud00aa192007-12-07 20:31:10 +0800762 .module = THIS_MODULE,
763};
764
Herbert Xudadbc532007-12-17 15:33:17 +0800765static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
766 unsigned int keylen)
767{
768 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
769 struct crypto_aead *child = ctx->child;
770 int err;
771
772 if (keylen < 4)
773 return -EINVAL;
774
775 keylen -= 4;
776 memcpy(ctx->nonce, key + keylen, 4);
777
778 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
779 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
780 CRYPTO_TFM_REQ_MASK);
781 err = crypto_aead_setkey(child, key, keylen);
782 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
783 CRYPTO_TFM_RES_MASK);
784
785 return err;
786}
787
788static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
789 unsigned int authsize)
790{
791 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
792
793 switch (authsize) {
794 case 8:
795 case 12:
796 case 16:
797 break;
798 default:
799 return -EINVAL;
800 }
801
802 return crypto_aead_setauthsize(ctx->child, authsize);
803}
804
805static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
806{
Herbert Xu7b05a372015-07-09 07:17:30 +0800807 struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
Herbert Xudadbc532007-12-17 15:33:17 +0800808 struct crypto_aead *aead = crypto_aead_reqtfm(req);
809 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu7b05a372015-07-09 07:17:30 +0800810 struct aead_request *subreq = &rctx->subreq;
Herbert Xudadbc532007-12-17 15:33:17 +0800811 struct crypto_aead *child = ctx->child;
Herbert Xu7b05a372015-07-09 07:17:30 +0800812 struct scatterlist *sg;
Herbert Xudadbc532007-12-17 15:33:17 +0800813 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
814 crypto_aead_alignmask(child) + 1);
815
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200816 scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
Herbert Xu7b05a372015-07-09 07:17:30 +0800817
Herbert Xudadbc532007-12-17 15:33:17 +0800818 memcpy(iv, ctx->nonce, 4);
819 memcpy(iv + 4, req->iv, 8);
820
Herbert Xu7b05a372015-07-09 07:17:30 +0800821 sg_init_table(rctx->src, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200822 sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800823 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
824 if (sg != rctx->src + 1)
825 sg_chain(rctx->src, 2, sg);
826
827 if (req->src != req->dst) {
828 sg_init_table(rctx->dst, 3);
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200829 sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
Herbert Xu7b05a372015-07-09 07:17:30 +0800830 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
831 if (sg != rctx->dst + 1)
832 sg_chain(rctx->dst, 2, sg);
833 }
834
Herbert Xudadbc532007-12-17 15:33:17 +0800835 aead_request_set_tfm(subreq, child);
836 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
837 req->base.data);
Herbert Xu7b05a372015-07-09 07:17:30 +0800838 aead_request_set_crypt(subreq, rctx->src,
839 req->src == req->dst ? rctx->src : rctx->dst,
840 req->cryptlen, iv);
841 aead_request_set_ad(subreq, req->assoclen - 8);
Herbert Xudadbc532007-12-17 15:33:17 +0800842
843 return subreq;
844}
845
846static int crypto_rfc4106_encrypt(struct aead_request *req)
847{
Herbert Xu7b05a372015-07-09 07:17:30 +0800848 if (req->assoclen != 16 && req->assoclen != 20)
849 return -EINVAL;
850
Herbert Xudadbc532007-12-17 15:33:17 +0800851 req = crypto_rfc4106_crypt(req);
852
853 return crypto_aead_encrypt(req);
854}
855
856static int crypto_rfc4106_decrypt(struct aead_request *req)
857{
Herbert Xu7b05a372015-07-09 07:17:30 +0800858 if (req->assoclen != 16 && req->assoclen != 20)
859 return -EINVAL;
860
Herbert Xudadbc532007-12-17 15:33:17 +0800861 req = crypto_rfc4106_crypt(req);
862
863 return crypto_aead_decrypt(req);
864}
865
Herbert Xuadcbc682015-06-16 13:54:18 +0800866static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800867{
Herbert Xuadcbc682015-06-16 13:54:18 +0800868 struct aead_instance *inst = aead_alg_instance(tfm);
869 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
870 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800871 struct crypto_aead *aead;
872 unsigned long align;
873
874 aead = crypto_spawn_aead(spawn);
875 if (IS_ERR(aead))
876 return PTR_ERR(aead);
877
878 ctx->child = aead;
879
880 align = crypto_aead_alignmask(aead);
881 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +0800882 crypto_aead_set_reqsize(
883 tfm,
Herbert Xu7b05a372015-07-09 07:17:30 +0800884 sizeof(struct crypto_rfc4106_req_ctx) +
Herbert Xu5d723362015-05-11 17:47:59 +0800885 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Herbert Xu7b05a372015-07-09 07:17:30 +0800886 align + 24);
Herbert Xudadbc532007-12-17 15:33:17 +0800887
888 return 0;
889}
890
Herbert Xuadcbc682015-06-16 13:54:18 +0800891static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
Herbert Xudadbc532007-12-17 15:33:17 +0800892{
Herbert Xuadcbc682015-06-16 13:54:18 +0800893 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudadbc532007-12-17 15:33:17 +0800894
895 crypto_free_aead(ctx->child);
896}
897
Herbert Xu7b05a372015-07-09 07:17:30 +0800898static void crypto_rfc4106_free(struct aead_instance *inst)
899{
900 crypto_drop_aead(aead_instance_ctx(inst));
901 kfree(inst);
902}
903
Herbert Xuadcbc682015-06-16 13:54:18 +0800904static int crypto_rfc4106_create(struct crypto_template *tmpl,
905 struct rtattr **tb)
Herbert Xudadbc532007-12-17 15:33:17 +0800906{
907 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +0800908 struct aead_instance *inst;
Herbert Xudadbc532007-12-17 15:33:17 +0800909 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +0800910 struct aead_alg *alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800911 const char *ccm_name;
912 int err;
913
914 algt = crypto_get_attr_type(tb);
Herbert Xudadbc532007-12-17 15:33:17 +0800915 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +0800916 return PTR_ERR(algt);
Herbert Xudadbc532007-12-17 15:33:17 +0800917
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800918 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +0800919 return -EINVAL;
Herbert Xudadbc532007-12-17 15:33:17 +0800920
921 ccm_name = crypto_attr_alg_name(tb[1]);
Herbert Xudadbc532007-12-17 15:33:17 +0800922 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +0800923 return PTR_ERR(ccm_name);
Herbert Xudadbc532007-12-17 15:33:17 +0800924
925 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
926 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +0800927 return -ENOMEM;
Herbert Xudadbc532007-12-17 15:33:17 +0800928
Herbert Xuadcbc682015-06-16 13:54:18 +0800929 spawn = aead_instance_ctx(inst);
930 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xudadbc532007-12-17 15:33:17 +0800931 err = crypto_grab_aead(spawn, ccm_name, 0,
932 crypto_requires_sync(algt->type, algt->mask));
933 if (err)
934 goto out_free_inst;
935
Herbert Xuadcbc682015-06-16 13:54:18 +0800936 alg = crypto_spawn_aead_alg(spawn);
Herbert Xudadbc532007-12-17 15:33:17 +0800937
938 err = -EINVAL;
939
Herbert Xuadcbc682015-06-16 13:54:18 +0800940 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200941 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xudadbc532007-12-17 15:33:17 +0800942 goto out_drop_alg;
943
944 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +0800945 if (alg->base.cra_blocksize != 1)
Herbert Xudadbc532007-12-17 15:33:17 +0800946 goto out_drop_alg;
947
948 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +0800949 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
950 "rfc4106(%s)", alg->base.cra_name) >=
951 CRYPTO_MAX_ALG_NAME ||
952 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
953 "rfc4106(%s)", alg->base.cra_driver_name) >=
Herbert Xudadbc532007-12-17 15:33:17 +0800954 CRYPTO_MAX_ALG_NAME)
955 goto out_drop_alg;
956
Herbert Xu7b05a372015-07-09 07:17:30 +0800957 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xuadcbc682015-06-16 13:54:18 +0800958 inst->alg.base.cra_priority = alg->base.cra_priority;
959 inst->alg.base.cra_blocksize = 1;
960 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xudadbc532007-12-17 15:33:17 +0800961
Herbert Xuadcbc682015-06-16 13:54:18 +0800962 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
Herbert Xudadbc532007-12-17 15:33:17 +0800963
Corentin LABBEe0ab7e92017-08-22 10:08:17 +0200964 inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +0800965 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +0800966 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xudadbc532007-12-17 15:33:17 +0800967
Herbert Xuadcbc682015-06-16 13:54:18 +0800968 inst->alg.init = crypto_rfc4106_init_tfm;
969 inst->alg.exit = crypto_rfc4106_exit_tfm;
Herbert Xudadbc532007-12-17 15:33:17 +0800970
Herbert Xuadcbc682015-06-16 13:54:18 +0800971 inst->alg.setkey = crypto_rfc4106_setkey;
972 inst->alg.setauthsize = crypto_rfc4106_setauthsize;
973 inst->alg.encrypt = crypto_rfc4106_encrypt;
974 inst->alg.decrypt = crypto_rfc4106_decrypt;
Herbert Xudadbc532007-12-17 15:33:17 +0800975
Herbert Xu7b05a372015-07-09 07:17:30 +0800976 inst->free = crypto_rfc4106_free;
977
Herbert Xuadcbc682015-06-16 13:54:18 +0800978 err = aead_register_instance(tmpl, inst);
979 if (err)
980 goto out_drop_alg;
Herbert Xudadbc532007-12-17 15:33:17 +0800981
982out:
Herbert Xuadcbc682015-06-16 13:54:18 +0800983 return err;
Herbert Xudadbc532007-12-17 15:33:17 +0800984
985out_drop_alg:
986 crypto_drop_aead(spawn);
987out_free_inst:
988 kfree(inst);
Herbert Xudadbc532007-12-17 15:33:17 +0800989 goto out;
990}
991
Herbert Xudadbc532007-12-17 15:33:17 +0800992static struct crypto_template crypto_rfc4106_tmpl = {
993 .name = "rfc4106",
Herbert Xuadcbc682015-06-16 13:54:18 +0800994 .create = crypto_rfc4106_create,
Herbert Xudadbc532007-12-17 15:33:17 +0800995 .module = THIS_MODULE,
996};
997
Tobias Brunner73c89c12010-01-17 21:52:11 +1100998static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
999 unsigned int keylen)
1000{
1001 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1002 struct crypto_aead *child = ctx->child;
1003 int err;
1004
1005 if (keylen < 4)
1006 return -EINVAL;
1007
1008 keylen -= 4;
1009 memcpy(ctx->nonce, key + keylen, 4);
1010
1011 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
1012 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
1013 CRYPTO_TFM_REQ_MASK);
1014 err = crypto_aead_setkey(child, key, keylen);
1015 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1016 CRYPTO_TFM_RES_MASK);
1017
1018 return err;
1019}
1020
1021static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1022 unsigned int authsize)
1023{
1024 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1025
1026 if (authsize != 16)
1027 return -EINVAL;
1028
1029 return crypto_aead_setauthsize(ctx->child, authsize);
1030}
1031
Herbert Xuadcbc682015-06-16 13:54:18 +08001032static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001033{
1034 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1035 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001036 struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001037 struct aead_request *subreq = &rctx->subreq;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001038 unsigned int authsize = crypto_aead_authsize(aead);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001039 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1040 crypto_aead_alignmask(ctx->child) + 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001041 int err;
1042
1043 if (req->src != req->dst) {
1044 err = crypto_rfc4543_copy_src_to_dst(req, enc);
1045 if (err)
1046 return err;
1047 }
Tobias Brunner73c89c12010-01-17 21:52:11 +11001048
1049 memcpy(iv, ctx->nonce, 4);
1050 memcpy(iv + 4, req->iv, 8);
1051
Tobias Brunner73c89c12010-01-17 21:52:11 +11001052 aead_request_set_tfm(subreq, ctx->child);
Herbert Xuadcbc682015-06-16 13:54:18 +08001053 aead_request_set_callback(subreq, req->base.flags,
1054 req->base.complete, req->base.data);
1055 aead_request_set_crypt(subreq, req->src, req->dst,
1056 enc ? 0 : authsize, iv);
1057 aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1058 subreq->cryptlen);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001059
Herbert Xuadcbc682015-06-16 13:54:18 +08001060 return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001061}
1062
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001063static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1064{
1065 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1066 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1067 unsigned int authsize = crypto_aead_authsize(aead);
Herbert Xuadcbc682015-06-16 13:54:18 +08001068 unsigned int nbytes = req->assoclen + req->cryptlen -
1069 (enc ? 0 : authsize);
Kees Cook8d605392018-09-18 19:10:51 -07001070 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001071
Kees Cook8d605392018-09-18 19:10:51 -07001072 skcipher_request_set_sync_tfm(nreq, ctx->null);
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001073 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1074 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1075
1076 return crypto_skcipher_encrypt(nreq);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001077}
1078
Tobias Brunner73c89c12010-01-17 21:52:11 +11001079static int crypto_rfc4543_encrypt(struct aead_request *req)
1080{
Herbert Xuadcbc682015-06-16 13:54:18 +08001081 return crypto_rfc4543_crypt(req, true);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001082}
1083
1084static int crypto_rfc4543_decrypt(struct aead_request *req)
1085{
Herbert Xuadcbc682015-06-16 13:54:18 +08001086 return crypto_rfc4543_crypt(req, false);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001087}
1088
Herbert Xuadcbc682015-06-16 13:54:18 +08001089static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001090{
Herbert Xuadcbc682015-06-16 13:54:18 +08001091 struct aead_instance *inst = aead_alg_instance(tfm);
1092 struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001093 struct crypto_aead_spawn *spawn = &ictx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001094 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001095 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -07001096 struct crypto_sync_skcipher *null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001097 unsigned long align;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001098 int err = 0;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001099
1100 aead = crypto_spawn_aead(spawn);
1101 if (IS_ERR(aead))
1102 return PTR_ERR(aead);
1103
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001104 null = crypto_get_default_null_skcipher();
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001105 err = PTR_ERR(null);
1106 if (IS_ERR(null))
1107 goto err_free_aead;
1108
Tobias Brunner73c89c12010-01-17 21:52:11 +11001109 ctx->child = aead;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001110 ctx->null = null;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001111
1112 align = crypto_aead_alignmask(aead);
1113 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xuadcbc682015-06-16 13:54:18 +08001114 crypto_aead_set_reqsize(
1115 tfm,
Herbert Xu5d723362015-05-11 17:47:59 +08001116 sizeof(struct crypto_rfc4543_req_ctx) +
1117 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001118 align + GCM_AES_IV_SIZE);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001119
1120 return 0;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001121
1122err_free_aead:
1123 crypto_free_aead(aead);
1124 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001125}
1126
Herbert Xuadcbc682015-06-16 13:54:18 +08001127static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001128{
Herbert Xuadcbc682015-06-16 13:54:18 +08001129 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001130
1131 crypto_free_aead(ctx->child);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -08001132 crypto_put_default_null_skcipher();
Tobias Brunner73c89c12010-01-17 21:52:11 +11001133}
1134
Herbert Xu7b05a372015-07-09 07:17:30 +08001135static void crypto_rfc4543_free(struct aead_instance *inst)
1136{
1137 struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1138
1139 crypto_drop_aead(&ctx->aead);
1140
1141 kfree(inst);
1142}
1143
Herbert Xuadcbc682015-06-16 13:54:18 +08001144static int crypto_rfc4543_create(struct crypto_template *tmpl,
1145 struct rtattr **tb)
Tobias Brunner73c89c12010-01-17 21:52:11 +11001146{
1147 struct crypto_attr_type *algt;
Herbert Xuadcbc682015-06-16 13:54:18 +08001148 struct aead_instance *inst;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001149 struct crypto_aead_spawn *spawn;
Herbert Xuadcbc682015-06-16 13:54:18 +08001150 struct aead_alg *alg;
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001151 struct crypto_rfc4543_instance_ctx *ctx;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001152 const char *ccm_name;
1153 int err;
1154
1155 algt = crypto_get_attr_type(tb);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001156 if (IS_ERR(algt))
Herbert Xuadcbc682015-06-16 13:54:18 +08001157 return PTR_ERR(algt);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001158
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001159 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xuadcbc682015-06-16 13:54:18 +08001160 return -EINVAL;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001161
1162 ccm_name = crypto_attr_alg_name(tb[1]);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001163 if (IS_ERR(ccm_name))
Herbert Xuadcbc682015-06-16 13:54:18 +08001164 return PTR_ERR(ccm_name);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001165
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001166 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001167 if (!inst)
Herbert Xuadcbc682015-06-16 13:54:18 +08001168 return -ENOMEM;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001169
Herbert Xuadcbc682015-06-16 13:54:18 +08001170 ctx = aead_instance_ctx(inst);
Jussi Kivilinna9489667d2013-04-07 16:43:41 +03001171 spawn = &ctx->aead;
Herbert Xuadcbc682015-06-16 13:54:18 +08001172 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Tobias Brunner73c89c12010-01-17 21:52:11 +11001173 err = crypto_grab_aead(spawn, ccm_name, 0,
1174 crypto_requires_sync(algt->type, algt->mask));
1175 if (err)
1176 goto out_free_inst;
1177
Herbert Xuadcbc682015-06-16 13:54:18 +08001178 alg = crypto_spawn_aead_alg(spawn);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001179
1180 err = -EINVAL;
1181
Herbert Xuadcbc682015-06-16 13:54:18 +08001182 /* Underlying IV size must be 12. */
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001183 if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
Herbert Xu17db8542015-05-21 15:11:11 +08001184 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001185
1186 /* Not a stream cipher? */
Herbert Xuadcbc682015-06-16 13:54:18 +08001187 if (alg->base.cra_blocksize != 1)
Herbert Xu17db8542015-05-21 15:11:11 +08001188 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001189
1190 err = -ENAMETOOLONG;
Herbert Xuadcbc682015-06-16 13:54:18 +08001191 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1192 "rfc4543(%s)", alg->base.cra_name) >=
1193 CRYPTO_MAX_ALG_NAME ||
1194 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1195 "rfc4543(%s)", alg->base.cra_driver_name) >=
Tobias Brunner73c89c12010-01-17 21:52:11 +11001196 CRYPTO_MAX_ALG_NAME)
Herbert Xu17db8542015-05-21 15:11:11 +08001197 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001198
Herbert Xuadcbc682015-06-16 13:54:18 +08001199 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1200 inst->alg.base.cra_priority = alg->base.cra_priority;
1201 inst->alg.base.cra_blocksize = 1;
1202 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001203
Herbert Xuadcbc682015-06-16 13:54:18 +08001204 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001205
Corentin LABBEe0ab7e92017-08-22 10:08:17 +02001206 inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
Herbert Xu16f37ecd2016-07-12 13:17:39 +08001207 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
Herbert Xuadcbc682015-06-16 13:54:18 +08001208 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001209
Herbert Xuadcbc682015-06-16 13:54:18 +08001210 inst->alg.init = crypto_rfc4543_init_tfm;
1211 inst->alg.exit = crypto_rfc4543_exit_tfm;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001212
Herbert Xuadcbc682015-06-16 13:54:18 +08001213 inst->alg.setkey = crypto_rfc4543_setkey;
1214 inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1215 inst->alg.encrypt = crypto_rfc4543_encrypt;
1216 inst->alg.decrypt = crypto_rfc4543_decrypt;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001217
Herbert Xu7b05a372015-07-09 07:17:30 +08001218 inst->free = crypto_rfc4543_free,
1219
Herbert Xuadcbc682015-06-16 13:54:18 +08001220 err = aead_register_instance(tmpl, inst);
1221 if (err)
1222 goto out_drop_alg;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001223
1224out:
Herbert Xuadcbc682015-06-16 13:54:18 +08001225 return err;
Tobias Brunner73c89c12010-01-17 21:52:11 +11001226
1227out_drop_alg:
1228 crypto_drop_aead(spawn);
1229out_free_inst:
1230 kfree(inst);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001231 goto out;
1232}
1233
Tobias Brunner73c89c12010-01-17 21:52:11 +11001234static struct crypto_template crypto_rfc4543_tmpl = {
1235 .name = "rfc4543",
Herbert Xuadcbc682015-06-16 13:54:18 +08001236 .create = crypto_rfc4543_create,
Tobias Brunner73c89c12010-01-17 21:52:11 +11001237 .module = THIS_MODULE,
1238};
1239
Mikko Herranen28db8e32007-11-26 22:24:11 +08001240static int __init crypto_gcm_module_init(void)
1241{
Herbert Xud00aa192007-12-07 20:31:10 +08001242 int err;
1243
Herbert Xuadcbc682015-06-16 13:54:18 +08001244 gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
Huang Ying9382d972009-08-06 15:34:26 +10001245 if (!gcm_zeroes)
1246 return -ENOMEM;
1247
Herbert Xuadcbc682015-06-16 13:54:18 +08001248 sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1249
Herbert Xud00aa192007-12-07 20:31:10 +08001250 err = crypto_register_template(&crypto_gcm_base_tmpl);
1251 if (err)
1252 goto out;
1253
1254 err = crypto_register_template(&crypto_gcm_tmpl);
1255 if (err)
1256 goto out_undo_base;
1257
Herbert Xudadbc532007-12-17 15:33:17 +08001258 err = crypto_register_template(&crypto_rfc4106_tmpl);
1259 if (err)
1260 goto out_undo_gcm;
1261
Tobias Brunner73c89c12010-01-17 21:52:11 +11001262 err = crypto_register_template(&crypto_rfc4543_tmpl);
1263 if (err)
1264 goto out_undo_rfc4106;
1265
Huang Ying9382d972009-08-06 15:34:26 +10001266 return 0;
Herbert Xud00aa192007-12-07 20:31:10 +08001267
Tobias Brunner73c89c12010-01-17 21:52:11 +11001268out_undo_rfc4106:
1269 crypto_unregister_template(&crypto_rfc4106_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001270out_undo_gcm:
1271 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001272out_undo_base:
1273 crypto_unregister_template(&crypto_gcm_base_tmpl);
Huang Ying9382d972009-08-06 15:34:26 +10001274out:
1275 kfree(gcm_zeroes);
1276 return err;
Mikko Herranen28db8e32007-11-26 22:24:11 +08001277}
1278
1279static void __exit crypto_gcm_module_exit(void)
1280{
Huang Ying9382d972009-08-06 15:34:26 +10001281 kfree(gcm_zeroes);
Tobias Brunner73c89c12010-01-17 21:52:11 +11001282 crypto_unregister_template(&crypto_rfc4543_tmpl);
Herbert Xudadbc532007-12-17 15:33:17 +08001283 crypto_unregister_template(&crypto_rfc4106_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001284 crypto_unregister_template(&crypto_gcm_tmpl);
Herbert Xud00aa192007-12-07 20:31:10 +08001285 crypto_unregister_template(&crypto_gcm_base_tmpl);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001286}
1287
1288module_init(crypto_gcm_module_init);
1289module_exit(crypto_gcm_module_exit);
1290
1291MODULE_LICENSE("GPL");
1292MODULE_DESCRIPTION("Galois/Counter Mode");
1293MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -08001294MODULE_ALIAS_CRYPTO("gcm_base");
1295MODULE_ALIAS_CRYPTO("rfc4106");
1296MODULE_ALIAS_CRYPTO("rfc4543");
Kees Cook4943ba12014-11-24 16:32:38 -08001297MODULE_ALIAS_CRYPTO("gcm");