]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - crypto/authencesn.c
crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN
[mirror_ubuntu-jammy-kernel.git] / crypto / authencesn.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
4 * derived from authenc.c
5 *
6 * Copyright (C) 2010 secunet Security Networks AG
7 * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
8 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
9 */
10
11 #include <crypto/internal/aead.h>
12 #include <crypto/internal/hash.h>
13 #include <crypto/internal/skcipher.h>
14 #include <crypto/authenc.h>
15 #include <crypto/null.h>
16 #include <crypto/scatterwalk.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24
25 struct authenc_esn_instance_ctx {
26 struct crypto_ahash_spawn auth;
27 struct crypto_skcipher_spawn enc;
28 };
29
30 struct crypto_authenc_esn_ctx {
31 unsigned int reqoff;
32 struct crypto_ahash *auth;
33 struct crypto_skcipher *enc;
34 struct crypto_sync_skcipher *null;
35 };
36
37 struct authenc_esn_request_ctx {
38 struct scatterlist src[2];
39 struct scatterlist dst[2];
40 char tail[];
41 };
42
43 static void authenc_esn_request_complete(struct aead_request *req, int err)
44 {
45 if (err != -EINPROGRESS)
46 aead_request_complete(req, err);
47 }
48
49 static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
50 unsigned int authsize)
51 {
52 if (authsize > 0 && authsize < 4)
53 return -EINVAL;
54
55 return 0;
56 }
57
58 static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
59 unsigned int keylen)
60 {
61 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
62 struct crypto_ahash *auth = ctx->auth;
63 struct crypto_skcipher *enc = ctx->enc;
64 struct crypto_authenc_keys keys;
65 int err = -EINVAL;
66
67 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
68 goto out;
69
70 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
71 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
72 CRYPTO_TFM_REQ_MASK);
73 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
74 crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
75 CRYPTO_TFM_RES_MASK);
76
77 if (err)
78 goto out;
79
80 crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
81 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
82 CRYPTO_TFM_REQ_MASK);
83 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
84 crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) &
85 CRYPTO_TFM_RES_MASK);
86
87 out:
88 memzero_explicit(&keys, sizeof(keys));
89 return err;
90 }
91
92 static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
93 unsigned int flags)
94 {
95 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
96 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
97 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
98 struct crypto_ahash *auth = ctx->auth;
99 u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
100 crypto_ahash_alignmask(auth) + 1);
101 unsigned int authsize = crypto_aead_authsize(authenc_esn);
102 unsigned int assoclen = req->assoclen;
103 unsigned int cryptlen = req->cryptlen;
104 struct scatterlist *dst = req->dst;
105 u32 tmp[2];
106
107 /* Move high-order bits of sequence number back. */
108 scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
109 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
110 scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
111
112 scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
113 return 0;
114 }
115
116 static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
117 int err)
118 {
119 struct aead_request *req = areq->data;
120
121 err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
122 aead_request_complete(req, err);
123 }
124
125 static int crypto_authenc_esn_genicv(struct aead_request *req,
126 unsigned int flags)
127 {
128 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
129 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
130 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
131 struct crypto_ahash *auth = ctx->auth;
132 u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
133 crypto_ahash_alignmask(auth) + 1);
134 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
135 unsigned int authsize = crypto_aead_authsize(authenc_esn);
136 unsigned int assoclen = req->assoclen;
137 unsigned int cryptlen = req->cryptlen;
138 struct scatterlist *dst = req->dst;
139 u32 tmp[2];
140
141 if (!authsize)
142 return 0;
143
144 /* Move high-order bits of sequence number to the end. */
145 scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
146 scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
147 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
148
149 sg_init_table(areq_ctx->dst, 2);
150 dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
151
152 ahash_request_set_tfm(ahreq, auth);
153 ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
154 ahash_request_set_callback(ahreq, flags,
155 authenc_esn_geniv_ahash_done, req);
156
157 return crypto_ahash_digest(ahreq) ?:
158 crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
159 }
160
161
162 static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
163 int err)
164 {
165 struct aead_request *areq = req->data;
166
167 if (!err)
168 err = crypto_authenc_esn_genicv(areq, 0);
169
170 authenc_esn_request_complete(areq, err);
171 }
172
173 static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
174 {
175 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
176 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
177 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
178
179 skcipher_request_set_sync_tfm(skreq, ctx->null);
180 skcipher_request_set_callback(skreq, aead_request_flags(req),
181 NULL, NULL);
182 skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL);
183
184 return crypto_skcipher_encrypt(skreq);
185 }
186
187 static int crypto_authenc_esn_encrypt(struct aead_request *req)
188 {
189 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
190 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
191 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
192 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
193 ctx->reqoff);
194 struct crypto_skcipher *enc = ctx->enc;
195 unsigned int assoclen = req->assoclen;
196 unsigned int cryptlen = req->cryptlen;
197 struct scatterlist *src, *dst;
198 int err;
199
200 sg_init_table(areq_ctx->src, 2);
201 src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
202 dst = src;
203
204 if (req->src != req->dst) {
205 err = crypto_authenc_esn_copy(req, assoclen);
206 if (err)
207 return err;
208
209 sg_init_table(areq_ctx->dst, 2);
210 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
211 }
212
213 skcipher_request_set_tfm(skreq, enc);
214 skcipher_request_set_callback(skreq, aead_request_flags(req),
215 crypto_authenc_esn_encrypt_done, req);
216 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
217
218 err = crypto_skcipher_encrypt(skreq);
219 if (err)
220 return err;
221
222 return crypto_authenc_esn_genicv(req, aead_request_flags(req));
223 }
224
225 static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
226 unsigned int flags)
227 {
228 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
229 unsigned int authsize = crypto_aead_authsize(authenc_esn);
230 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
231 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
232 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
233 ctx->reqoff);
234 struct crypto_ahash *auth = ctx->auth;
235 u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
236 crypto_ahash_alignmask(auth) + 1);
237 unsigned int cryptlen = req->cryptlen - authsize;
238 unsigned int assoclen = req->assoclen;
239 struct scatterlist *dst = req->dst;
240 u8 *ihash = ohash + crypto_ahash_digestsize(auth);
241 u32 tmp[2];
242
243 if (!authsize)
244 goto decrypt;
245
246 /* Move high-order bits of sequence number back. */
247 scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
248 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
249 scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
250
251 if (crypto_memneq(ihash, ohash, authsize))
252 return -EBADMSG;
253
254 decrypt:
255
256 sg_init_table(areq_ctx->dst, 2);
257 dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
258
259 skcipher_request_set_tfm(skreq, ctx->enc);
260 skcipher_request_set_callback(skreq, flags,
261 req->base.complete, req->base.data);
262 skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
263
264 return crypto_skcipher_decrypt(skreq);
265 }
266
267 static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
268 int err)
269 {
270 struct aead_request *req = areq->data;
271
272 err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
273 authenc_esn_request_complete(req, err);
274 }
275
276 static int crypto_authenc_esn_decrypt(struct aead_request *req)
277 {
278 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
279 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
280 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
281 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
282 unsigned int authsize = crypto_aead_authsize(authenc_esn);
283 struct crypto_ahash *auth = ctx->auth;
284 u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
285 crypto_ahash_alignmask(auth) + 1);
286 unsigned int assoclen = req->assoclen;
287 unsigned int cryptlen = req->cryptlen;
288 u8 *ihash = ohash + crypto_ahash_digestsize(auth);
289 struct scatterlist *dst = req->dst;
290 u32 tmp[2];
291 int err;
292
293 cryptlen -= authsize;
294
295 if (req->src != dst) {
296 err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
297 if (err)
298 return err;
299 }
300
301 scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
302 authsize, 0);
303
304 if (!authsize)
305 goto tail;
306
307 /* Move high-order bits of sequence number to the end. */
308 scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
309 scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
310 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
311
312 sg_init_table(areq_ctx->dst, 2);
313 dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
314
315 ahash_request_set_tfm(ahreq, auth);
316 ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
317 ahash_request_set_callback(ahreq, aead_request_flags(req),
318 authenc_esn_verify_ahash_done, req);
319
320 err = crypto_ahash_digest(ahreq);
321 if (err)
322 return err;
323
324 tail:
325 return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
326 }
327
328 static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
329 {
330 struct aead_instance *inst = aead_alg_instance(tfm);
331 struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
332 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
333 struct crypto_ahash *auth;
334 struct crypto_skcipher *enc;
335 struct crypto_sync_skcipher *null;
336 int err;
337
338 auth = crypto_spawn_ahash(&ictx->auth);
339 if (IS_ERR(auth))
340 return PTR_ERR(auth);
341
342 enc = crypto_spawn_skcipher(&ictx->enc);
343 err = PTR_ERR(enc);
344 if (IS_ERR(enc))
345 goto err_free_ahash;
346
347 null = crypto_get_default_null_skcipher();
348 err = PTR_ERR(null);
349 if (IS_ERR(null))
350 goto err_free_skcipher;
351
352 ctx->auth = auth;
353 ctx->enc = enc;
354 ctx->null = null;
355
356 ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
357 crypto_ahash_alignmask(auth) + 1);
358
359 crypto_aead_set_reqsize(
360 tfm,
361 sizeof(struct authenc_esn_request_ctx) +
362 ctx->reqoff +
363 max_t(unsigned int,
364 crypto_ahash_reqsize(auth) +
365 sizeof(struct ahash_request),
366 sizeof(struct skcipher_request) +
367 crypto_skcipher_reqsize(enc)));
368
369 return 0;
370
371 err_free_skcipher:
372 crypto_free_skcipher(enc);
373 err_free_ahash:
374 crypto_free_ahash(auth);
375 return err;
376 }
377
378 static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
379 {
380 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
381
382 crypto_free_ahash(ctx->auth);
383 crypto_free_skcipher(ctx->enc);
384 crypto_put_default_null_skcipher();
385 }
386
387 static void crypto_authenc_esn_free(struct aead_instance *inst)
388 {
389 struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
390
391 crypto_drop_skcipher(&ctx->enc);
392 crypto_drop_ahash(&ctx->auth);
393 kfree(inst);
394 }
395
396 static int crypto_authenc_esn_create(struct crypto_template *tmpl,
397 struct rtattr **tb)
398 {
399 struct crypto_attr_type *algt;
400 struct aead_instance *inst;
401 struct hash_alg_common *auth;
402 struct crypto_alg *auth_base;
403 struct skcipher_alg *enc;
404 struct authenc_esn_instance_ctx *ctx;
405 const char *enc_name;
406 int err;
407
408 algt = crypto_get_attr_type(tb);
409 if (IS_ERR(algt))
410 return PTR_ERR(algt);
411
412 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
413 return -EINVAL;
414
415 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
416 CRYPTO_ALG_TYPE_AHASH_MASK |
417 crypto_requires_sync(algt->type, algt->mask));
418 if (IS_ERR(auth))
419 return PTR_ERR(auth);
420
421 auth_base = &auth->base;
422
423 enc_name = crypto_attr_alg_name(tb[2]);
424 err = PTR_ERR(enc_name);
425 if (IS_ERR(enc_name))
426 goto out_put_auth;
427
428 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
429 err = -ENOMEM;
430 if (!inst)
431 goto out_put_auth;
432
433 ctx = aead_instance_ctx(inst);
434
435 err = crypto_init_ahash_spawn(&ctx->auth, auth,
436 aead_crypto_instance(inst));
437 if (err)
438 goto err_free_inst;
439
440 crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
441 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
442 crypto_requires_sync(algt->type,
443 algt->mask));
444 if (err)
445 goto err_drop_auth;
446
447 enc = crypto_spawn_skcipher_alg(&ctx->enc);
448
449 err = -ENAMETOOLONG;
450 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
451 "authencesn(%s,%s)", auth_base->cra_name,
452 enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
453 goto err_drop_enc;
454
455 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
456 "authencesn(%s,%s)", auth_base->cra_driver_name,
457 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
458 goto err_drop_enc;
459
460 inst->alg.base.cra_flags = (auth_base->cra_flags |
461 enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
462 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
463 auth_base->cra_priority;
464 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
465 inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
466 enc->base.cra_alignmask;
467 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
468
469 inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
470 inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
471 inst->alg.maxauthsize = auth->digestsize;
472
473 inst->alg.init = crypto_authenc_esn_init_tfm;
474 inst->alg.exit = crypto_authenc_esn_exit_tfm;
475
476 inst->alg.setkey = crypto_authenc_esn_setkey;
477 inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
478 inst->alg.encrypt = crypto_authenc_esn_encrypt;
479 inst->alg.decrypt = crypto_authenc_esn_decrypt;
480
481 inst->free = crypto_authenc_esn_free,
482
483 err = aead_register_instance(tmpl, inst);
484 if (err)
485 goto err_drop_enc;
486
487 out:
488 crypto_mod_put(auth_base);
489 return err;
490
491 err_drop_enc:
492 crypto_drop_skcipher(&ctx->enc);
493 err_drop_auth:
494 crypto_drop_ahash(&ctx->auth);
495 err_free_inst:
496 kfree(inst);
497 out_put_auth:
498 goto out;
499 }
500
501 static struct crypto_template crypto_authenc_esn_tmpl = {
502 .name = "authencesn",
503 .create = crypto_authenc_esn_create,
504 .module = THIS_MODULE,
505 };
506
507 static int __init crypto_authenc_esn_module_init(void)
508 {
509 return crypto_register_template(&crypto_authenc_esn_tmpl);
510 }
511
512 static void __exit crypto_authenc_esn_module_exit(void)
513 {
514 crypto_unregister_template(&crypto_authenc_esn_tmpl);
515 }
516
517 subsys_initcall(crypto_authenc_esn_module_init);
518 module_exit(crypto_authenc_esn_module_exit);
519
520 MODULE_LICENSE("GPL");
521 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
522 MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
523 MODULE_ALIAS_CRYPTO("authencesn");