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 19/28] crypto: chacha20poly1305 - use crypto_grab_ahash() and simplify error paths
Date: Thu,  2 Jan 2020 19:58:59 -0800	[thread overview]
Message-ID: <20200103035908.12048-20-ebiggers@kernel.org> (raw)
In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org>

From: Eric Biggers <ebiggers@google.com>

Make the rfc7539 and rfc7539esp 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, and
by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 crypto/chacha20poly1305.c | 84 +++++++++++++--------------------------
 1 file changed, 27 insertions(+), 57 deletions(-)

diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c
index fcb8ec4ba083..72543cfa45ef 100644
--- a/crypto/chacha20poly1305.c
+++ b/crypto/chacha20poly1305.c
@@ -16,8 +16,6 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 
-#include "internal.h"
-
 struct chachapoly_instance_ctx {
 	struct crypto_skcipher_spawn chacha;
 	struct crypto_ahash_spawn poly;
@@ -565,11 +563,9 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 	struct crypto_attr_type *algt;
 	u32 mask;
 	struct aead_instance *inst;
-	struct skcipher_alg *chacha;
-	struct crypto_alg *poly;
-	struct hash_alg_common *poly_hash;
 	struct chachapoly_instance_ctx *ctx;
-	const char *chacha_name, *poly_name;
+	struct skcipher_alg *chacha;
+	struct hash_alg_common *poly;
 	int err;
 
 	if (ivsize > CHACHAPOLY_IV_SIZE)
@@ -584,68 +580,51 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 
 	mask = crypto_requires_sync(algt->type, algt->mask);
 
-	chacha_name = crypto_attr_alg_name(tb[1]);
-	if (IS_ERR(chacha_name))
-		return PTR_ERR(chacha_name);
-	poly_name = crypto_attr_alg_name(tb[2]);
-	if (IS_ERR(poly_name))
-		return PTR_ERR(poly_name);
-
-	poly = crypto_find_alg(poly_name, &crypto_ahash_type,
-			       CRYPTO_ALG_TYPE_HASH,
-			       CRYPTO_ALG_TYPE_AHASH_MASK | mask);
-	if (IS_ERR(poly))
-		return PTR_ERR(poly);
-	poly_hash = __crypto_hash_alg_common(poly);
-
-	err = -EINVAL;
-	if (poly_hash->digestsize != POLY1305_DIGEST_SIZE)
-		goto out_put_poly;
-
-	err = -ENOMEM;
 	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
 	if (!inst)
-		goto out_put_poly;
-
+		return -ENOMEM;
 	ctx = aead_instance_ctx(inst);
 	ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize;
-	err = crypto_init_ahash_spawn(&ctx->poly, poly_hash,
-				      aead_crypto_instance(inst));
-	if (err)
-		goto err_free_inst;
 
 	err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst),
-				   chacha_name, 0, mask);
+				   crypto_attr_alg_name(tb[1]), 0, mask);
 	if (err)
-		goto err_drop_poly;
-
+		goto err_free_inst;
 	chacha = crypto_spawn_skcipher_alg(&ctx->chacha);
 
+	err = crypto_grab_ahash(&ctx->poly, aead_crypto_instance(inst),
+				crypto_attr_alg_name(tb[2]), 0, mask);
+	if (err)
+		goto err_free_inst;
+	poly = crypto_spawn_ahash_alg(&ctx->poly);
+
 	err = -EINVAL;
+	if (poly->digestsize != POLY1305_DIGEST_SIZE)
+		goto err_free_inst;
 	/* Need 16-byte IV size, including Initial Block Counter value */
 	if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE)
-		goto out_drop_chacha;
+		goto err_free_inst;
 	/* Not a stream cipher? */
 	if (chacha->base.cra_blocksize != 1)
-		goto out_drop_chacha;
+		goto err_free_inst;
 
 	err = -ENAMETOOLONG;
 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
 		     "%s(%s,%s)", name, chacha->base.cra_name,
-		     poly->cra_name) >= CRYPTO_MAX_ALG_NAME)
-		goto out_drop_chacha;
+		     poly->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
+		goto err_free_inst;
 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
 		     "%s(%s,%s)", name, chacha->base.cra_driver_name,
-		     poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
-		goto out_drop_chacha;
+		     poly->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
+		goto err_free_inst;
 
-	inst->alg.base.cra_flags = (chacha->base.cra_flags | poly->cra_flags) &
-				   CRYPTO_ALG_ASYNC;
+	inst->alg.base.cra_flags = (chacha->base.cra_flags |
+				    poly->base.cra_flags) & CRYPTO_ALG_ASYNC;
 	inst->alg.base.cra_priority = (chacha->base.cra_priority +
-				       poly->cra_priority) / 2;
+				       poly->base.cra_priority) / 2;
 	inst->alg.base.cra_blocksize = 1;
 	inst->alg.base.cra_alignmask = chacha->base.cra_alignmask |
-				       poly->cra_alignmask;
+				       poly->base.cra_alignmask;
 	inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) +
 				     ctx->saltlen;
 	inst->alg.ivsize = ivsize;
@@ -661,20 +640,11 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 	inst->free = chachapoly_free;
 
 	err = aead_register_instance(tmpl, inst);
-	if (err)
-		goto out_drop_chacha;
-
-out_put_poly:
-	crypto_mod_put(poly);
-	return err;
-
-out_drop_chacha:
-	crypto_drop_skcipher(&ctx->chacha);
-err_drop_poly:
-	crypto_drop_ahash(&ctx->poly);
+	if (err) {
 err_free_inst:
-	kfree(inst);
-	goto out_put_poly;
+		chachapoly_free(inst);
+	}
+	return err;
 }
 
 static int rfc7539_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 ` [PATCH v2 18/28] crypto: ccm " Eric Biggers
2020-01-03  3:58 ` Eric Biggers [this message]
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-20-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.