| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Authenc: Simple AEAD wrapper for IPsec |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
|---|
| 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 as published by the Free |
|---|
| 8 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 9 | | - * any later version. |
|---|
| 10 | | - * |
|---|
| 11 | 6 | */ |
|---|
| 12 | 7 | |
|---|
| 13 | 8 | #include <crypto/internal/aead.h> |
|---|
| .. | .. |
|---|
| 33 | 28 | struct crypto_authenc_ctx { |
|---|
| 34 | 29 | struct crypto_ahash *auth; |
|---|
| 35 | 30 | struct crypto_skcipher *enc; |
|---|
| 36 | | - struct crypto_skcipher *null; |
|---|
| 31 | + struct crypto_sync_skcipher *null; |
|---|
| 37 | 32 | }; |
|---|
| 38 | 33 | |
|---|
| 39 | 34 | struct authenc_request_ctx { |
|---|
| .. | .. |
|---|
| 96 | 91 | int err = -EINVAL; |
|---|
| 97 | 92 | |
|---|
| 98 | 93 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
|---|
| 99 | | - goto badkey; |
|---|
| 94 | + goto out; |
|---|
| 100 | 95 | |
|---|
| 101 | 96 | crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
|---|
| 102 | 97 | crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) & |
|---|
| 103 | 98 | CRYPTO_TFM_REQ_MASK); |
|---|
| 104 | 99 | err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen); |
|---|
| 105 | | - crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) & |
|---|
| 106 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 107 | | - |
|---|
| 108 | 100 | if (err) |
|---|
| 109 | 101 | goto out; |
|---|
| 110 | 102 | |
|---|
| .. | .. |
|---|
| 112 | 104 | crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) & |
|---|
| 113 | 105 | CRYPTO_TFM_REQ_MASK); |
|---|
| 114 | 106 | err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen); |
|---|
| 115 | | - crypto_aead_set_flags(authenc, crypto_skcipher_get_flags(enc) & |
|---|
| 116 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 117 | | - |
|---|
| 118 | 107 | out: |
|---|
| 119 | 108 | memzero_explicit(&keys, sizeof(keys)); |
|---|
| 120 | 109 | return err; |
|---|
| 121 | | - |
|---|
| 122 | | -badkey: |
|---|
| 123 | | - crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); |
|---|
| 124 | | - goto out; |
|---|
| 125 | 110 | } |
|---|
| 126 | 111 | |
|---|
| 127 | 112 | static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err) |
|---|
| .. | .. |
|---|
| 193 | 178 | { |
|---|
| 194 | 179 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
|---|
| 195 | 180 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
|---|
| 196 | | - SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null); |
|---|
| 181 | + SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null); |
|---|
| 197 | 182 | |
|---|
| 198 | | - skcipher_request_set_tfm(skreq, ctx->null); |
|---|
| 183 | + skcipher_request_set_sync_tfm(skreq, ctx->null); |
|---|
| 199 | 184 | skcipher_request_set_callback(skreq, aead_request_flags(req), |
|---|
| 200 | 185 | NULL, NULL); |
|---|
| 201 | 186 | skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen, |
|---|
| .. | .. |
|---|
| 268 | 253 | dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen); |
|---|
| 269 | 254 | |
|---|
| 270 | 255 | skcipher_request_set_tfm(skreq, ctx->enc); |
|---|
| 271 | | - skcipher_request_set_callback(skreq, aead_request_flags(req), |
|---|
| 256 | + skcipher_request_set_callback(skreq, flags, |
|---|
| 272 | 257 | req->base.complete, req->base.data); |
|---|
| 273 | 258 | skcipher_request_set_crypt(skreq, src, dst, |
|---|
| 274 | 259 | req->cryptlen - authsize, req->iv); |
|---|
| .. | .. |
|---|
| 326 | 311 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
|---|
| 327 | 312 | struct crypto_ahash *auth; |
|---|
| 328 | 313 | struct crypto_skcipher *enc; |
|---|
| 329 | | - struct crypto_skcipher *null; |
|---|
| 314 | + struct crypto_sync_skcipher *null; |
|---|
| 330 | 315 | int err; |
|---|
| 331 | 316 | |
|---|
| 332 | 317 | auth = crypto_spawn_ahash(&ictx->auth); |
|---|
| .. | .. |
|---|
| 387 | 372 | static int crypto_authenc_create(struct crypto_template *tmpl, |
|---|
| 388 | 373 | struct rtattr **tb) |
|---|
| 389 | 374 | { |
|---|
| 390 | | - struct crypto_attr_type *algt; |
|---|
| 375 | + u32 mask; |
|---|
| 391 | 376 | struct aead_instance *inst; |
|---|
| 377 | + struct authenc_instance_ctx *ctx; |
|---|
| 392 | 378 | struct hash_alg_common *auth; |
|---|
| 393 | 379 | struct crypto_alg *auth_base; |
|---|
| 394 | 380 | struct skcipher_alg *enc; |
|---|
| 395 | | - struct authenc_instance_ctx *ctx; |
|---|
| 396 | | - const char *enc_name; |
|---|
| 397 | 381 | int err; |
|---|
| 398 | 382 | |
|---|
| 399 | | - algt = crypto_get_attr_type(tb); |
|---|
| 400 | | - if (IS_ERR(algt)) |
|---|
| 401 | | - return PTR_ERR(algt); |
|---|
| 402 | | - |
|---|
| 403 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
|---|
| 404 | | - return -EINVAL; |
|---|
| 405 | | - |
|---|
| 406 | | - auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, |
|---|
| 407 | | - CRYPTO_ALG_TYPE_AHASH_MASK | |
|---|
| 408 | | - crypto_requires_sync(algt->type, algt->mask)); |
|---|
| 409 | | - if (IS_ERR(auth)) |
|---|
| 410 | | - return PTR_ERR(auth); |
|---|
| 411 | | - |
|---|
| 412 | | - auth_base = &auth->base; |
|---|
| 413 | | - |
|---|
| 414 | | - enc_name = crypto_attr_alg_name(tb[2]); |
|---|
| 415 | | - err = PTR_ERR(enc_name); |
|---|
| 416 | | - if (IS_ERR(enc_name)) |
|---|
| 417 | | - goto out_put_auth; |
|---|
| 383 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
|---|
| 384 | + if (err) |
|---|
| 385 | + return err; |
|---|
| 418 | 386 | |
|---|
| 419 | 387 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
|---|
| 420 | | - err = -ENOMEM; |
|---|
| 421 | 388 | if (!inst) |
|---|
| 422 | | - goto out_put_auth; |
|---|
| 423 | | - |
|---|
| 389 | + return -ENOMEM; |
|---|
| 424 | 390 | ctx = aead_instance_ctx(inst); |
|---|
| 425 | 391 | |
|---|
| 426 | | - err = crypto_init_ahash_spawn(&ctx->auth, auth, |
|---|
| 427 | | - aead_crypto_instance(inst)); |
|---|
| 392 | + err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst), |
|---|
| 393 | + crypto_attr_alg_name(tb[1]), 0, mask); |
|---|
| 428 | 394 | if (err) |
|---|
| 429 | 395 | goto err_free_inst; |
|---|
| 396 | + auth = crypto_spawn_ahash_alg(&ctx->auth); |
|---|
| 397 | + auth_base = &auth->base; |
|---|
| 430 | 398 | |
|---|
| 431 | | - crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst)); |
|---|
| 432 | | - err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, |
|---|
| 433 | | - crypto_requires_sync(algt->type, |
|---|
| 434 | | - algt->mask)); |
|---|
| 399 | + err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), |
|---|
| 400 | + crypto_attr_alg_name(tb[2]), 0, mask); |
|---|
| 435 | 401 | if (err) |
|---|
| 436 | | - goto err_drop_auth; |
|---|
| 437 | | - |
|---|
| 402 | + goto err_free_inst; |
|---|
| 438 | 403 | enc = crypto_spawn_skcipher_alg(&ctx->enc); |
|---|
| 439 | 404 | |
|---|
| 440 | 405 | ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask, |
|---|
| .. | .. |
|---|
| 445 | 410 | "authenc(%s,%s)", auth_base->cra_name, |
|---|
| 446 | 411 | enc->base.cra_name) >= |
|---|
| 447 | 412 | CRYPTO_MAX_ALG_NAME) |
|---|
| 448 | | - goto err_drop_enc; |
|---|
| 413 | + goto err_free_inst; |
|---|
| 449 | 414 | |
|---|
| 450 | 415 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
|---|
| 451 | 416 | "authenc(%s,%s)", auth_base->cra_driver_name, |
|---|
| 452 | 417 | enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
|---|
| 453 | | - goto err_drop_enc; |
|---|
| 418 | + goto err_free_inst; |
|---|
| 454 | 419 | |
|---|
| 455 | | - inst->alg.base.cra_flags = (auth_base->cra_flags | |
|---|
| 456 | | - enc->base.cra_flags) & CRYPTO_ALG_ASYNC; |
|---|
| 457 | 420 | inst->alg.base.cra_priority = enc->base.cra_priority * 10 + |
|---|
| 458 | 421 | auth_base->cra_priority; |
|---|
| 459 | 422 | inst->alg.base.cra_blocksize = enc->base.cra_blocksize; |
|---|
| .. | .. |
|---|
| 475 | 438 | inst->free = crypto_authenc_free; |
|---|
| 476 | 439 | |
|---|
| 477 | 440 | err = aead_register_instance(tmpl, inst); |
|---|
| 478 | | - if (err) |
|---|
| 479 | | - goto err_drop_enc; |
|---|
| 480 | | - |
|---|
| 481 | | -out: |
|---|
| 482 | | - crypto_mod_put(auth_base); |
|---|
| 483 | | - return err; |
|---|
| 484 | | - |
|---|
| 485 | | -err_drop_enc: |
|---|
| 486 | | - crypto_drop_skcipher(&ctx->enc); |
|---|
| 487 | | -err_drop_auth: |
|---|
| 488 | | - crypto_drop_ahash(&ctx->auth); |
|---|
| 441 | + if (err) { |
|---|
| 489 | 442 | err_free_inst: |
|---|
| 490 | | - kfree(inst); |
|---|
| 491 | | -out_put_auth: |
|---|
| 492 | | - goto out; |
|---|
| 443 | + crypto_authenc_free(inst); |
|---|
| 444 | + } |
|---|
| 445 | + return err; |
|---|
| 493 | 446 | } |
|---|
| 494 | 447 | |
|---|
| 495 | 448 | static struct crypto_template crypto_authenc_tmpl = { |
|---|
| .. | .. |
|---|
| 508 | 461 | crypto_unregister_template(&crypto_authenc_tmpl); |
|---|
| 509 | 462 | } |
|---|
| 510 | 463 | |
|---|
| 511 | | -module_init(crypto_authenc_module_init); |
|---|
| 464 | +subsys_initcall(crypto_authenc_module_init); |
|---|
| 512 | 465 | module_exit(crypto_authenc_module_exit); |
|---|
| 513 | 466 | |
|---|
| 514 | 467 | MODULE_LICENSE("GPL"); |
|---|