.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers, |
---|
3 | 4 | * derived from authenc.c |
---|
.. | .. |
---|
5 | 6 | * Copyright (C) 2010 secunet Security Networks AG |
---|
6 | 7 | * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com> |
---|
7 | 8 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify it |
---|
10 | | - * under the terms of the GNU General Public License as published by the Free |
---|
11 | | - * Software Foundation; either version 2 of the License, or (at your option) |
---|
12 | | - * any later version. |
---|
13 | | - * |
---|
14 | 9 | */ |
---|
15 | 10 | |
---|
16 | 11 | #include <crypto/internal/aead.h> |
---|
.. | .. |
---|
36 | 31 | unsigned int reqoff; |
---|
37 | 32 | struct crypto_ahash *auth; |
---|
38 | 33 | struct crypto_skcipher *enc; |
---|
39 | | - struct crypto_skcipher *null; |
---|
| 34 | + struct crypto_sync_skcipher *null; |
---|
40 | 35 | }; |
---|
41 | 36 | |
---|
42 | 37 | struct authenc_esn_request_ctx { |
---|
.. | .. |
---|
70 | 65 | int err = -EINVAL; |
---|
71 | 66 | |
---|
72 | 67 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
---|
73 | | - goto badkey; |
---|
| 68 | + goto out; |
---|
74 | 69 | |
---|
75 | 70 | crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
---|
76 | 71 | crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) & |
---|
77 | 72 | CRYPTO_TFM_REQ_MASK); |
---|
78 | 73 | err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen); |
---|
79 | | - crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) & |
---|
80 | | - CRYPTO_TFM_RES_MASK); |
---|
81 | | - |
---|
82 | 74 | if (err) |
---|
83 | 75 | goto out; |
---|
84 | 76 | |
---|
.. | .. |
---|
86 | 78 | crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) & |
---|
87 | 79 | CRYPTO_TFM_REQ_MASK); |
---|
88 | 80 | err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen); |
---|
89 | | - crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) & |
---|
90 | | - CRYPTO_TFM_RES_MASK); |
---|
91 | | - |
---|
92 | 81 | out: |
---|
93 | 82 | memzero_explicit(&keys, sizeof(keys)); |
---|
94 | 83 | return err; |
---|
95 | | - |
---|
96 | | -badkey: |
---|
97 | | - crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN); |
---|
98 | | - goto out; |
---|
99 | 84 | } |
---|
100 | 85 | |
---|
101 | 86 | static int crypto_authenc_esn_genicv_tail(struct aead_request *req, |
---|
.. | .. |
---|
183 | 168 | { |
---|
184 | 169 | struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req); |
---|
185 | 170 | struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn); |
---|
186 | | - SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null); |
---|
| 171 | + SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null); |
---|
187 | 172 | |
---|
188 | | - skcipher_request_set_tfm(skreq, ctx->null); |
---|
| 173 | + skcipher_request_set_sync_tfm(skreq, ctx->null); |
---|
189 | 174 | skcipher_request_set_callback(skreq, aead_request_flags(req), |
---|
190 | 175 | NULL, NULL); |
---|
191 | 176 | skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL); |
---|
.. | .. |
---|
341 | 326 | struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm); |
---|
342 | 327 | struct crypto_ahash *auth; |
---|
343 | 328 | struct crypto_skcipher *enc; |
---|
344 | | - struct crypto_skcipher *null; |
---|
| 329 | + struct crypto_sync_skcipher *null; |
---|
345 | 330 | int err; |
---|
346 | 331 | |
---|
347 | 332 | auth = crypto_spawn_ahash(&ictx->auth); |
---|
.. | .. |
---|
405 | 390 | static int crypto_authenc_esn_create(struct crypto_template *tmpl, |
---|
406 | 391 | struct rtattr **tb) |
---|
407 | 392 | { |
---|
408 | | - struct crypto_attr_type *algt; |
---|
| 393 | + u32 mask; |
---|
409 | 394 | struct aead_instance *inst; |
---|
| 395 | + struct authenc_esn_instance_ctx *ctx; |
---|
410 | 396 | struct hash_alg_common *auth; |
---|
411 | 397 | struct crypto_alg *auth_base; |
---|
412 | 398 | struct skcipher_alg *enc; |
---|
413 | | - struct authenc_esn_instance_ctx *ctx; |
---|
414 | | - const char *enc_name; |
---|
415 | 399 | int err; |
---|
416 | 400 | |
---|
417 | | - algt = crypto_get_attr_type(tb); |
---|
418 | | - if (IS_ERR(algt)) |
---|
419 | | - return PTR_ERR(algt); |
---|
420 | | - |
---|
421 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
---|
422 | | - return -EINVAL; |
---|
423 | | - |
---|
424 | | - auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, |
---|
425 | | - CRYPTO_ALG_TYPE_AHASH_MASK | |
---|
426 | | - crypto_requires_sync(algt->type, algt->mask)); |
---|
427 | | - if (IS_ERR(auth)) |
---|
428 | | - return PTR_ERR(auth); |
---|
429 | | - |
---|
430 | | - auth_base = &auth->base; |
---|
431 | | - |
---|
432 | | - enc_name = crypto_attr_alg_name(tb[2]); |
---|
433 | | - err = PTR_ERR(enc_name); |
---|
434 | | - if (IS_ERR(enc_name)) |
---|
435 | | - goto out_put_auth; |
---|
| 401 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
---|
| 402 | + if (err) |
---|
| 403 | + return err; |
---|
436 | 404 | |
---|
437 | 405 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
---|
438 | | - err = -ENOMEM; |
---|
439 | 406 | if (!inst) |
---|
440 | | - goto out_put_auth; |
---|
441 | | - |
---|
| 407 | + return -ENOMEM; |
---|
442 | 408 | ctx = aead_instance_ctx(inst); |
---|
443 | 409 | |
---|
444 | | - err = crypto_init_ahash_spawn(&ctx->auth, auth, |
---|
445 | | - aead_crypto_instance(inst)); |
---|
| 410 | + err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst), |
---|
| 411 | + crypto_attr_alg_name(tb[1]), 0, mask); |
---|
446 | 412 | if (err) |
---|
447 | 413 | goto err_free_inst; |
---|
| 414 | + auth = crypto_spawn_ahash_alg(&ctx->auth); |
---|
| 415 | + auth_base = &auth->base; |
---|
448 | 416 | |
---|
449 | | - crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst)); |
---|
450 | | - err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, |
---|
451 | | - crypto_requires_sync(algt->type, |
---|
452 | | - algt->mask)); |
---|
| 417 | + err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), |
---|
| 418 | + crypto_attr_alg_name(tb[2]), 0, mask); |
---|
453 | 419 | if (err) |
---|
454 | | - goto err_drop_auth; |
---|
455 | | - |
---|
| 420 | + goto err_free_inst; |
---|
456 | 421 | enc = crypto_spawn_skcipher_alg(&ctx->enc); |
---|
457 | 422 | |
---|
458 | 423 | err = -ENAMETOOLONG; |
---|
459 | 424 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
---|
460 | 425 | "authencesn(%s,%s)", auth_base->cra_name, |
---|
461 | 426 | enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME) |
---|
462 | | - goto err_drop_enc; |
---|
| 427 | + goto err_free_inst; |
---|
463 | 428 | |
---|
464 | 429 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
---|
465 | 430 | "authencesn(%s,%s)", auth_base->cra_driver_name, |
---|
466 | 431 | enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
---|
467 | | - goto err_drop_enc; |
---|
| 432 | + goto err_free_inst; |
---|
468 | 433 | |
---|
469 | | - inst->alg.base.cra_flags = (auth_base->cra_flags | |
---|
470 | | - enc->base.cra_flags) & CRYPTO_ALG_ASYNC; |
---|
471 | 434 | inst->alg.base.cra_priority = enc->base.cra_priority * 10 + |
---|
472 | 435 | auth_base->cra_priority; |
---|
473 | 436 | inst->alg.base.cra_blocksize = enc->base.cra_blocksize; |
---|
.. | .. |
---|
487 | 450 | inst->alg.encrypt = crypto_authenc_esn_encrypt; |
---|
488 | 451 | inst->alg.decrypt = crypto_authenc_esn_decrypt; |
---|
489 | 452 | |
---|
490 | | - inst->free = crypto_authenc_esn_free, |
---|
| 453 | + inst->free = crypto_authenc_esn_free; |
---|
491 | 454 | |
---|
492 | 455 | err = aead_register_instance(tmpl, inst); |
---|
493 | | - if (err) |
---|
494 | | - goto err_drop_enc; |
---|
495 | | - |
---|
496 | | -out: |
---|
497 | | - crypto_mod_put(auth_base); |
---|
498 | | - return err; |
---|
499 | | - |
---|
500 | | -err_drop_enc: |
---|
501 | | - crypto_drop_skcipher(&ctx->enc); |
---|
502 | | -err_drop_auth: |
---|
503 | | - crypto_drop_ahash(&ctx->auth); |
---|
| 456 | + if (err) { |
---|
504 | 457 | err_free_inst: |
---|
505 | | - kfree(inst); |
---|
506 | | -out_put_auth: |
---|
507 | | - goto out; |
---|
| 458 | + crypto_authenc_esn_free(inst); |
---|
| 459 | + } |
---|
| 460 | + return err; |
---|
508 | 461 | } |
---|
509 | 462 | |
---|
510 | 463 | static struct crypto_template crypto_authenc_esn_tmpl = { |
---|
.. | .. |
---|
523 | 476 | crypto_unregister_template(&crypto_authenc_esn_tmpl); |
---|
524 | 477 | } |
---|
525 | 478 | |
---|
526 | | -module_init(crypto_authenc_esn_module_init); |
---|
| 479 | +subsys_initcall(crypto_authenc_esn_module_init); |
---|
527 | 480 | module_exit(crypto_authenc_esn_module_exit); |
---|
528 | 481 | |
---|
529 | 482 | MODULE_LICENSE("GPL"); |
---|