All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Subject: [PATCH v2 18/28] crypto: ccm - use crypto_grab_ahash() and simplify error paths
Date: Thu,  2 Jan 2020 19:58:58 -0800	[thread overview]
Message-ID: <20200103035908.12048-19-ebiggers@kernel.org> (raw)
In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org>

From: Eric Biggers <ebiggers@google.com>

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>
---
 crypto/ccm.c | 61 +++++++++++++++++-----------------------------------
 1 file changed, 20 insertions(+), 41 deletions(-)

diff --git a/crypto/ccm.c b/crypto/ccm.c
index 9c377976581d..48c2b2c565a6 100644
--- a/crypto/ccm.c
+++ b/crypto/ccm.c
@@ -15,8 +15,6 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 
-#include "internal.h"
-
 struct ccm_instance_ctx {
 	struct crypto_skcipher_spawn ctr;
 	struct crypto_ahash_spawn mac;
@@ -459,10 +457,9 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
 	struct crypto_attr_type *algt;
 	u32 mask;
 	struct aead_instance *inst;
+	struct ccm_instance_ctx *ictx;
 	struct skcipher_alg *ctr;
-	struct crypto_alg *mac_alg;
 	struct hash_alg_common *mac;
-	struct ccm_instance_ctx *ictx;
 	int err;
 
 	algt = crypto_get_attr_type(tb);
@@ -474,35 +471,26 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
 
 	mask = crypto_requires_sync(algt->type, algt->mask);
 
-	mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type,
-				  CRYPTO_ALG_TYPE_HASH,
-				  CRYPTO_ALG_TYPE_AHASH_MASK |
-				  CRYPTO_ALG_ASYNC);
-	if (IS_ERR(mac_alg))
-		return PTR_ERR(mac_alg);
-
-	mac = __crypto_hash_alg_common(mac_alg);
-	err = -EINVAL;
-	if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
-	    mac->digestsize != 16)
-		goto out_put_mac;
-
 	inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
-	err = -ENOMEM;
 	if (!inst)
-		goto out_put_mac;
-
+		return -ENOMEM;
 	ictx = aead_instance_ctx(inst);
-	err = crypto_init_ahash_spawn(&ictx->mac, mac,
-				      aead_crypto_instance(inst));
+
+	err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
+				mac_name, 0, CRYPTO_ALG_ASYNC);
 	if (err)
 		goto err_free_inst;
+	mac = crypto_spawn_ahash_alg(&ictx->mac);
+
+	err = -EINVAL;
+	if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
+	    mac->digestsize != 16)
+		goto err_free_inst;
 
 	err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst),
 				   ctr_name, 0, mask);
 	if (err)
-		goto err_drop_mac;
-
+		goto err_free_inst;
 	ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
 
 	/* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
@@ -510,21 +498,21 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
 	if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
 	    crypto_skcipher_alg_ivsize(ctr) != 16 ||
 	    ctr->base.cra_blocksize != 1)
-		goto err_drop_ctr;
+		goto err_free_inst;
 
 	/* ctr and cbcmac must use the same underlying block cipher. */
 	if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
-		goto err_drop_ctr;
+		goto err_free_inst;
 
 	err = -ENAMETOOLONG;
 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
 		     "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
-		goto err_drop_ctr;
+		goto err_free_inst;
 
 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
 		     "ccm_base(%s,%s)", ctr->base.cra_driver_name,
 		     mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
-		goto err_drop_ctr;
+		goto err_free_inst;
 
 	inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
 	inst->alg.base.cra_priority = (mac->base.cra_priority +
@@ -546,20 +534,11 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
 	inst->free = crypto_ccm_free;
 
 	err = aead_register_instance(tmpl, inst);
-	if (err)
-		goto err_drop_ctr;
-
-out_put_mac:
-	crypto_mod_put(mac_alg);
-	return err;
-
-err_drop_ctr:
-	crypto_drop_skcipher(&ictx->ctr);
-err_drop_mac:
-	crypto_drop_ahash(&ictx->mac);
+	if (err) {
 err_free_inst:
-	kfree(inst);
-	goto out_put_mac;
+		crypto_ccm_free(inst);
+	}
+	return err;
 }
 
 static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
-- 
2.24.1


  parent reply	other threads:[~2020-01-03  4:01 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-03  3:58 [PATCH v2 00/28] crypto: template instantiation cleanup Eric Biggers
2020-01-03  3:58 ` [PATCH v2 01/28] crypto: algapi - make crypto_drop_spawn() a no-op on uninitialized spawns Eric Biggers
2020-01-03  3:58 ` [PATCH v2 02/28] crypto: algapi - make crypto_grab_spawn() handle an ERR_PTR() name Eric Biggers
2020-01-03  3:58 ` [PATCH v2 03/28] crypto: shash - make struct shash_instance be the full size Eric Biggers
2020-01-03  3:58 ` [PATCH v2 04/28] crypto: ahash - make struct ahash_instance " Eric Biggers
2020-01-03  3:58 ` [PATCH v2 05/28] crypto: skcipher - pass instance to crypto_grab_skcipher() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 06/28] crypto: aead - pass instance to crypto_grab_aead() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 07/28] crypto: akcipher - pass instance to crypto_grab_akcipher() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 08/28] crypto: algapi - pass instance to crypto_grab_spawn() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 09/28] crypto: shash - introduce crypto_grab_shash() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 10/28] crypto: ahash - introduce crypto_grab_ahash() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 11/28] crypto: cipher - introduce crypto_cipher_spawn and crypto_grab_cipher() Eric Biggers
2020-01-03  3:58 ` [PATCH v2 12/28] crypto: adiantum - use crypto_grab_{cipher,shash} and simplify error paths Eric Biggers
2020-01-03  3:58 ` [PATCH v2 13/28] crypto: cryptd - use crypto_grab_shash() " Eric Biggers
2020-01-03  3:58 ` [PATCH v2 14/28] crypto: hmac " Eric Biggers
2020-01-03  3:58 ` [PATCH v2 15/28] crypto: authenc - use crypto_grab_ahash() " Eric Biggers
2020-01-03  3:58 ` [PATCH v2 16/28] crypto: authencesn " Eric Biggers
2020-01-03  3:58 ` [PATCH v2 17/28] crypto: gcm " Eric Biggers
2020-01-03  3:58 ` Eric Biggers [this message]
2020-01-03  3:58 ` [PATCH v2 19/28] crypto: chacha20poly1305 " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 20/28] crypto: skcipher - use crypto_grab_cipher() " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 21/28] crypto: cbcmac " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 22/28] crypto: cmac " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 23/28] crypto: vmac " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 24/28] crypto: xcbc " Eric Biggers
2020-01-03  3:59 ` [PATCH v2 25/28] crypto: cipher - make crypto_spawn_cipher() take a crypto_cipher_spawn Eric Biggers
2020-01-03  3:59 ` [PATCH v2 26/28] crypto: algapi - remove obsoleted instance creation helpers Eric Biggers
2020-01-03  3:59 ` [PATCH v2 27/28] crypto: ahash - unexport crypto_ahash_type Eric Biggers
2020-01-03  3:59 ` [PATCH v2 28/28] crypto: algapi - fold crypto_init_spawn() into crypto_grab_spawn() Eric Biggers
2020-01-09  5:14 ` [PATCH v2 00/28] crypto: template instantiation cleanup Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200103035908.12048-19-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.