Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / crypto / authenc.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
3c09f17c
HX
2/*
3 * Authenc: Simple AEAD wrapper for IPsec
4 *
92d95ba9 5 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
3c09f17c
HX
6 */
7
68acbf84 8#include <crypto/internal/aead.h>
5f7082ed 9#include <crypto/internal/hash.h>
9ffde35a 10#include <crypto/internal/skcipher.h>
e236d4a8 11#include <crypto/authenc.h>
42c271c6 12#include <crypto/scatterwalk.h>
3c09f17c
HX
13#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
e236d4a8 17#include <linux/rtnetlink.h>
3c09f17c
HX
18#include <linux/slab.h>
19#include <linux/spinlock.h>
20
3c09f17c 21struct authenc_instance_ctx {
cbdcf80d 22 struct crypto_ahash_spawn auth;
9ffde35a 23 struct crypto_skcipher_spawn enc;
92d95ba9 24 unsigned int reqoff;
3c09f17c
HX
25};
26
27struct crypto_authenc_ctx {
cbdcf80d 28 struct crypto_ahash *auth;
7217d49f 29 struct crypto_skcipher *enc;
3c09f17c
HX
30};
31
cbdcf80d 32struct authenc_request_ctx {
92d95ba9
HX
33 struct scatterlist src[2];
34 struct scatterlist dst[2];
cbdcf80d
SK
35 char tail[];
36};
37
180ce7e8
HX
38static void authenc_request_complete(struct aead_request *req, int err)
39{
40 if (err != -EINPROGRESS)
41 aead_request_complete(req, err);
42}
43
bc6e2bdb
MK
44int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
45 unsigned int keylen)
3c09f17c 46{
bc6e2bdb 47 struct rtattr *rta = (struct rtattr *)key;
e236d4a8 48 struct crypto_authenc_key_param *param;
3c09f17c 49
12dc5e62 50 if (!RTA_OK(rta, keylen))
bc6e2bdb 51 return -EINVAL;
e236d4a8 52 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
bc6e2bdb 53 return -EINVAL;
8f9c4693
EB
54
55 /*
56 * RTA_OK() didn't align the rtattr's payload when validating that it
57 * fits in the buffer. Yet, the keys should start on the next 4-byte
58 * aligned boundary. To avoid confusion, require that the rtattr
59 * payload be exactly the param struct, which has a 4-byte aligned size.
60 */
61 if (RTA_PAYLOAD(rta) != sizeof(*param))
bc6e2bdb 62 return -EINVAL;
8f9c4693 63 BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
e236d4a8
HX
64
65 param = RTA_DATA(rta);
bc6e2bdb 66 keys->enckeylen = be32_to_cpu(param->enckeylen);
e236d4a8 67
8f9c4693
EB
68 key += rta->rta_len;
69 keylen -= rta->rta_len;
e236d4a8 70
bc6e2bdb
MK
71 if (keylen < keys->enckeylen)
72 return -EINVAL;
73
74 keys->authkeylen = keylen - keys->enckeylen;
75 keys->authkey = key;
76 keys->enckey = key + keys->authkeylen;
e236d4a8 77
bc6e2bdb
MK
78 return 0;
79}
80EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
81
82static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
83 unsigned int keylen)
84{
85 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
86 struct crypto_ahash *auth = ctx->auth;
7217d49f 87 struct crypto_skcipher *enc = ctx->enc;
bc6e2bdb
MK
88 struct crypto_authenc_keys keys;
89 int err = -EINVAL;
90
91 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
674f368a 92 goto out;
3c09f17c 93
cbdcf80d
SK
94 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
95 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
3c09f17c 96 CRYPTO_TFM_REQ_MASK);
bc6e2bdb 97 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
3c09f17c
HX
98 if (err)
99 goto out;
100
7217d49f
HX
101 crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
102 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
103 CRYPTO_TFM_REQ_MASK);
104 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
3c09f17c 105out:
ad2fdcdf 106 memzero_explicit(&keys, sizeof(keys));
3c09f17c
HX
107 return err;
108}
109
255e48eb 110static void authenc_geniv_ahash_done(void *data, int err)
cbdcf80d 111{
255e48eb 112 struct aead_request *req = data;
cbdcf80d 113 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
92d95ba9
HX
114 struct aead_instance *inst = aead_alg_instance(authenc);
115 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
cbdcf80d 116 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
92d95ba9 117 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
cbdcf80d
SK
118
119 if (err)
120 goto out;
121
92d95ba9
HX
122 scatterwalk_map_and_copy(ahreq->result, req->dst,
123 req->assoclen + req->cryptlen,
cbdcf80d
SK
124 crypto_aead_authsize(authenc), 1);
125
126out:
127 aead_request_complete(req, err);
128}
129
92d95ba9 130static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
cbdcf80d
SK
131{
132 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
92d95ba9 133 struct aead_instance *inst = aead_alg_instance(authenc);
cbdcf80d 134 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
92d95ba9 135 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
cbdcf80d
SK
136 struct crypto_ahash *auth = ctx->auth;
137 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
92d95ba9 138 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
cbdcf80d
SK
139 u8 *hash = areq_ctx->tail;
140 int err;
3c09f17c 141
cbdcf80d 142 ahash_request_set_tfm(ahreq, auth);
92d95ba9
HX
143 ahash_request_set_crypt(ahreq, req->dst, hash,
144 req->assoclen + req->cryptlen);
145 ahash_request_set_callback(ahreq, flags,
146 authenc_geniv_ahash_done, req);
cbdcf80d
SK
147
148 err = crypto_ahash_digest(ahreq);
3c09f17c 149 if (err)
92d95ba9 150 return err;
3c09f17c 151
92d95ba9 152 scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
7ba683a6 153 crypto_aead_authsize(authenc), 1);
92d95ba9 154
3c09f17c
HX
155 return 0;
156}
157
255e48eb 158static void crypto_authenc_encrypt_done(void *data, int err)
3c09f17c 159{
255e48eb 160 struct aead_request *areq = data;
a697690b 161
92d95ba9
HX
162 if (err)
163 goto out;
e56dd564 164
92d95ba9 165 err = crypto_authenc_genicv(areq, 0);
3c09f17c 166
92d95ba9 167out:
180ce7e8 168 authenc_request_complete(areq, err);
3c09f17c
HX
169}
170
171static int crypto_authenc_encrypt(struct aead_request *req)
172{
173 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
92d95ba9 174 struct aead_instance *inst = aead_alg_instance(authenc);
3c09f17c 175 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
92d95ba9 176 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
50beceba 177 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
7217d49f 178 struct crypto_skcipher *enc = ctx->enc;
e56dd564 179 unsigned int cryptlen = req->cryptlen;
7217d49f
HX
180 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
181 ictx->reqoff);
92d95ba9 182 struct scatterlist *src, *dst;
3c09f17c
HX
183 int err;
184
92d95ba9
HX
185 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
186 dst = src;
187
188 if (req->src != req->dst) {
dbc4b145 189 memcpy_sglist(req->dst, req->src, req->assoclen);
92d95ba9
HX
190 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
191 }
192
7217d49f
HX
193 skcipher_request_set_tfm(skreq, enc);
194 skcipher_request_set_callback(skreq, aead_request_flags(req),
195 crypto_authenc_encrypt_done, req);
196 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
3c09f17c 197
7217d49f 198 err = crypto_skcipher_encrypt(skreq);
3c09f17c
HX
199 if (err)
200 return err;
201
92d95ba9 202 return crypto_authenc_genicv(req, aead_request_flags(req));
e56dd564
HX
203}
204
92d95ba9
HX
205static int crypto_authenc_decrypt_tail(struct aead_request *req,
206 unsigned int flags)
e56dd564 207{
92d95ba9
HX
208 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
209 struct aead_instance *inst = aead_alg_instance(authenc);
210 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
211 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
212 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
213 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
7217d49f
HX
214 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
215 ictx->reqoff);
92d95ba9
HX
216 unsigned int authsize = crypto_aead_authsize(authenc);
217 u8 *ihash = ahreq->result + authsize;
218 struct scatterlist *src, *dst;
e56dd564 219
92d95ba9 220 scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
e56dd564 221
92d95ba9
HX
222 if (crypto_memneq(ihash, ahreq->result, authsize))
223 return -EBADMSG;
e56dd564 224
92d95ba9
HX
225 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
226 dst = src;
e56dd564 227
c34252fd 228 if (req->src != req->dst)
92d95ba9 229 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
e56dd564 230
7217d49f 231 skcipher_request_set_tfm(skreq, ctx->enc);
66eae850 232 skcipher_request_set_callback(skreq, flags,
7217d49f
HX
233 req->base.complete, req->base.data);
234 skcipher_request_set_crypt(skreq, src, dst,
235 req->cryptlen - authsize, req->iv);
3c09f17c 236
7217d49f 237 return crypto_skcipher_decrypt(skreq);
3c09f17c
HX
238}
239
255e48eb 240static void authenc_verify_ahash_done(void *data, int err)
3c09f17c 241{
255e48eb 242 struct aead_request *req = data;
cbdcf80d 243
92d95ba9
HX
244 if (err)
245 goto out;
e56dd564 246
92d95ba9 247 err = crypto_authenc_decrypt_tail(req, 0);
cbdcf80d 248
92d95ba9
HX
249out:
250 authenc_request_complete(req, err);
3c09f17c
HX
251}
252
253static int crypto_authenc_decrypt(struct aead_request *req)
254{
255 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
481f34ae 256 unsigned int authsize = crypto_aead_authsize(authenc);
92d95ba9
HX
257 struct aead_instance *inst = aead_alg_instance(authenc);
258 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
259 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
260 struct crypto_ahash *auth = ctx->auth;
261 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
262 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
263 u8 *hash = areq_ctx->tail;
3c09f17c
HX
264 int err;
265
92d95ba9
HX
266 ahash_request_set_tfm(ahreq, auth);
267 ahash_request_set_crypt(ahreq, req->src, hash,
268 req->assoclen + req->cryptlen - authsize);
269 ahash_request_set_callback(ahreq, aead_request_flags(req),
270 authenc_verify_ahash_done, req);
271
272 err = crypto_ahash_digest(ahreq);
3c09f17c
HX
273 if (err)
274 return err;
275
92d95ba9 276 return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
3c09f17c
HX
277}
278
92d95ba9 279static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
3c09f17c 280{
92d95ba9
HX
281 struct aead_instance *inst = aead_alg_instance(tfm);
282 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
283 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
cbdcf80d 284 struct crypto_ahash *auth;
7217d49f 285 struct crypto_skcipher *enc;
3c09f17c
HX
286 int err;
287
cbdcf80d 288 auth = crypto_spawn_ahash(&ictx->auth);
3c09f17c
HX
289 if (IS_ERR(auth))
290 return PTR_ERR(auth);
291
60425a8b 292 enc = crypto_spawn_skcipher(&ictx->enc);
3c09f17c
HX
293 err = PTR_ERR(enc);
294 if (IS_ERR(enc))
cbdcf80d 295 goto err_free_ahash;
3c09f17c
HX
296
297 ctx->auth = auth;
298 ctx->enc = enc;
f3542e6d 299
92d95ba9
HX
300 crypto_aead_set_reqsize(
301 tfm,
25df9194 302 sizeof(struct authenc_request_ctx) +
92d95ba9 303 ictx->reqoff +
25df9194 304 max_t(unsigned int,
92d95ba9
HX
305 crypto_ahash_reqsize(auth) +
306 sizeof(struct ahash_request),
7217d49f
HX
307 sizeof(struct skcipher_request) +
308 crypto_skcipher_reqsize(enc)));
3c09f17c
HX
309
310 return 0;
311
cbdcf80d
SK
312err_free_ahash:
313 crypto_free_ahash(auth);
3c09f17c
HX
314 return err;
315}
316
92d95ba9 317static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
3c09f17c 318{
92d95ba9 319 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
3c09f17c 320
cbdcf80d 321 crypto_free_ahash(ctx->auth);
7217d49f 322 crypto_free_skcipher(ctx->enc);
3c09f17c
HX
323}
324
92d95ba9
HX
325static void crypto_authenc_free(struct aead_instance *inst)
326{
327 struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
328
329 crypto_drop_skcipher(&ctx->enc);
330 crypto_drop_ahash(&ctx->auth);
331 kfree(inst);
332}
333
334static int crypto_authenc_create(struct crypto_template *tmpl,
335 struct rtattr **tb)
3c09f17c 336{
b9f76ddd 337 u32 mask;
92d95ba9 338 struct aead_instance *inst;
37a861ad 339 struct authenc_instance_ctx *ctx;
cae33043 340 struct skcipher_alg_common *enc;
cbdcf80d
SK
341 struct hash_alg_common *auth;
342 struct crypto_alg *auth_base;
3c09f17c
HX
343 int err;
344
7bcb2c99
EB
345 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
346 if (err)
347 return err;
b9f76ddd 348
3c09f17c 349 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
3c09f17c 350 if (!inst)
37a861ad 351 return -ENOMEM;
92d95ba9 352 ctx = aead_instance_ctx(inst);
3c09f17c 353
37a861ad
EB
354 err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
355 crypto_attr_alg_name(tb[1]), 0, mask);
3c09f17c
HX
356 if (err)
357 goto err_free_inst;
37a861ad
EB
358 auth = crypto_spawn_ahash_alg(&ctx->auth);
359 auth_base = &auth->base;
3c09f17c 360
b9f76ddd 361 err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
37a861ad 362 crypto_attr_alg_name(tb[2]), 0, mask);
3c09f17c 363 if (err)
37a861ad 364 goto err_free_inst;
cae33043 365 enc = crypto_spawn_skcipher_alg_common(&ctx->enc);
9ffde35a 366
58e4bb5f 367 ctx->reqoff = 2 * auth->digestsize;
92d95ba9 368
9ffde35a 369 err = -ENAMETOOLONG;
92d95ba9 370 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
7217d49f
HX
371 "authenc(%s,%s)", auth_base->cra_name,
372 enc->base.cra_name) >=
9ffde35a 373 CRYPTO_MAX_ALG_NAME)
37a861ad 374 goto err_free_inst;
9ffde35a 375
92d95ba9 376 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
cbdcf80d 377 "authenc(%s,%s)", auth_base->cra_driver_name,
7217d49f 378 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
37a861ad 379 goto err_free_inst;
9ffde35a 380
7217d49f 381 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
92d95ba9 382 auth_base->cra_priority;
7217d49f 383 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
58e4bb5f 384 inst->alg.base.cra_alignmask = enc->base.cra_alignmask;
92d95ba9
HX
385 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
386
cae33043
HX
387 inst->alg.ivsize = enc->ivsize;
388 inst->alg.chunksize = enc->chunksize;
92d95ba9 389 inst->alg.maxauthsize = auth->digestsize;
3c09f17c 390
92d95ba9
HX
391 inst->alg.init = crypto_authenc_init_tfm;
392 inst->alg.exit = crypto_authenc_exit_tfm;
3c09f17c 393
92d95ba9
HX
394 inst->alg.setkey = crypto_authenc_setkey;
395 inst->alg.encrypt = crypto_authenc_encrypt;
396 inst->alg.decrypt = crypto_authenc_decrypt;
3c09f17c 397
92d95ba9 398 inst->free = crypto_authenc_free;
3c09f17c 399
92d95ba9 400 err = aead_register_instance(tmpl, inst);
37a861ad 401 if (err) {
3c09f17c 402err_free_inst:
37a861ad
EB
403 crypto_authenc_free(inst);
404 }
405 return err;
3c09f17c
HX
406}
407
3c09f17c
HX
408static struct crypto_template crypto_authenc_tmpl = {
409 .name = "authenc",
92d95ba9 410 .create = crypto_authenc_create,
3c09f17c
HX
411 .module = THIS_MODULE,
412};
413
414static int __init crypto_authenc_module_init(void)
415{
416 return crypto_register_template(&crypto_authenc_tmpl);
417}
418
419static void __exit crypto_authenc_module_exit(void)
420{
421 crypto_unregister_template(&crypto_authenc_tmpl);
422}
423
ef93f156 424module_init(crypto_authenc_module_init);
3c09f17c
HX
425module_exit(crypto_authenc_module_exit);
426
427MODULE_LICENSE("GPL");
428MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
4943ba16 429MODULE_ALIAS_CRYPTO("authenc");