Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: ccm - use crypto_grab_ahash() and simplify error paths

Make the ccm and ccm_base templates use the new function
crypto_grab_ahash() to initialize their ahash spawn.

This is needed to make all spawns be initialized in a consistent way.

Also simplify the error handling by taking advantage of crypto_drop_*()
now accepting (as a no-op) spawns that haven't been initialized yet.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
05b3bbb5 ab6ffd36

+21 -42
+21 -42
crypto/ccm.c
··· 15 15 #include <linux/module.h> 16 16 #include <linux/slab.h> 17 17 18 - #include "internal.h" 19 - 20 18 struct ccm_instance_ctx { 21 19 struct crypto_skcipher_spawn ctr; 22 20 struct crypto_ahash_spawn mac; ··· 450 452 struct crypto_attr_type *algt; 451 453 u32 mask; 452 454 struct aead_instance *inst; 453 - struct skcipher_alg *ctr; 454 - struct crypto_alg *mac_alg; 455 - struct hash_alg_common *mac; 456 455 struct ccm_instance_ctx *ictx; 456 + struct skcipher_alg *ctr; 457 + struct hash_alg_common *mac; 457 458 int err; 458 459 459 460 algt = crypto_get_attr_type(tb); ··· 464 467 465 468 mask = crypto_requires_sync(algt->type, algt->mask); 466 469 467 - mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type, 468 - CRYPTO_ALG_TYPE_HASH, 469 - CRYPTO_ALG_TYPE_AHASH_MASK | 470 - CRYPTO_ALG_ASYNC); 471 - if (IS_ERR(mac_alg)) 472 - return PTR_ERR(mac_alg); 470 + inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); 471 + if (!inst) 472 + return -ENOMEM; 473 + ictx = aead_instance_ctx(inst); 473 474 474 - mac = __crypto_hash_alg_common(mac_alg); 475 + err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst), 476 + mac_name, 0, CRYPTO_ALG_ASYNC); 477 + if (err) 478 + goto err_free_inst; 479 + mac = crypto_spawn_ahash_alg(&ictx->mac); 480 + 475 481 err = -EINVAL; 476 482 if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 || 477 483 mac->digestsize != 16) 478 - goto out_put_mac; 479 - 480 - inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); 481 - err = -ENOMEM; 482 - if (!inst) 483 - goto out_put_mac; 484 - 485 - ictx = aead_instance_ctx(inst); 486 - err = crypto_init_ahash_spawn(&ictx->mac, mac, 487 - aead_crypto_instance(inst)); 488 - if (err) 489 484 goto err_free_inst; 490 485 491 486 err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst), 492 487 ctr_name, 0, mask); 493 488 if (err) 494 - goto err_drop_mac; 495 - 489 + goto err_free_inst; 496 490 ctr = crypto_spawn_skcipher_alg(&ictx->ctr); 497 491 498 492 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ ··· 491 503 if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || 492 504 crypto_skcipher_alg_ivsize(ctr) != 16 || 493 505 ctr->base.cra_blocksize != 1) 494 - goto err_drop_ctr; 506 + goto err_free_inst; 495 507 496 508 /* ctr and cbcmac must use the same underlying block cipher. */ 497 509 if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0) 498 - goto err_drop_ctr; 510 + goto err_free_inst; 499 511 500 512 err = -ENAMETOOLONG; 501 513 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, 502 514 "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) 503 - goto err_drop_ctr; 515 + goto err_free_inst; 504 516 505 517 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, 506 518 "ccm_base(%s,%s)", ctr->base.cra_driver_name, 507 519 mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) 508 - goto err_drop_ctr; 520 + goto err_free_inst; 509 521 510 522 inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC; 511 523 inst->alg.base.cra_priority = (mac->base.cra_priority + ··· 527 539 inst->free = crypto_ccm_free; 528 540 529 541 err = aead_register_instance(tmpl, inst); 530 - if (err) 531 - goto err_drop_ctr; 532 - 533 - out_put_mac: 534 - crypto_mod_put(mac_alg); 535 - return err; 536 - 537 - err_drop_ctr: 538 - crypto_drop_skcipher(&ictx->ctr); 539 - err_drop_mac: 540 - crypto_drop_ahash(&ictx->mac); 542 + if (err) { 541 543 err_free_inst: 542 - kfree(inst); 543 - goto out_put_mac; 544 + crypto_ccm_free(inst); 545 + } 546 + return err; 544 547 } 545 548 546 549 static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)