Skip to contentSkip to conversation

crypto: shash - Remove dynamic descsize

PATCH New Herbert Xu 1 participant 5 messages Apr 12, 2025

Subsystems (3)
lore
Share & tools
lore

Herbert Xu

author maintainer
#0 lore raw

cover crypto: shash - Remove dynamic descsize

Dynamic desc sizes were created for fallbacks. However, it's actually never needed in practice since the only shash fallbacks that can be used all have the same descsize.

Once the fallback cases go away, its use in hmac becomes gratuitous as the spawn descsize is constant.

Remove the two fallback users and then fix hmac to use the spawn descsize. This allows the dynamic descsize to be removed.

Herbert Xu (4):
crypto: padlock-sha - Make descsize an algorithm attribute
crypto: zynqmp-sha - Make descsize an algorithm attribute
crypto: hmac - Make descsize an algorithm attribute
crypto: shash - Remove dynamic descsize

 crypto/hmac.c                      |  4 +---
 crypto/shash.c                     | 18 +-----------------
 drivers/crypto/padlock-sha.c       | 14 +++++++++++---
 drivers/crypto/xilinx/zynqmp-sha.c | 11 +++++++++--
 include/crypto/hash.h              |  3 +--
 5 files changed, 23 insertions(+), 27 deletions(-)
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 1/4 crypto: padlock-sha - Make descsize an algorithm attribute

Rather than setting descsize in init_tfm, set it statically and double-check it in init_tfm.

---
 drivers/crypto/padlock-sha.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/crypto/padlock-sha.c +11 -3
index db9e84c0c9fb..466493907d48 100644
@@ -206,8 +206,14 @@ static int padlock_init_tfm(struct crypto_shash *hash)
 		return PTR_ERR(fallback_tfm);
 	}
 
+	if (crypto_shash_descsize(hash) < sizeof(struct padlock_sha_desc) +
+					  crypto_shash_descsize(fallback_tfm)) {
+		crypto_free_shash(fallback_tfm);
+		return -EINVAL;
+	}
+
 	ctx->fallback = fallback_tfm;
-	hash->descsize += crypto_shash_descsize(fallback_tfm);
+
 	return 0;
 }
 
@@ -228,7 +234,8 @@ static struct shash_alg sha1_alg = {
 	.import		=	padlock_sha_import,
 	.init_tfm	=	padlock_init_tfm,
 	.exit_tfm	=	padlock_exit_tfm,
-	.descsize	=	sizeof(struct padlock_sha_desc),
+	.descsize	=	sizeof(struct padlock_sha_desc) +
+				sizeof(struct sha1_state),
 	.statesize	=	sizeof(struct sha1_state),
 	.base		=	{
 		.cra_name		=	"sha1",
@@ -251,7 +258,8 @@ static struct shash_alg sha256_alg = {
 	.import		=	padlock_sha_import,
 	.init_tfm	=	padlock_init_tfm,
 	.exit_tfm	=	padlock_exit_tfm,
-	.descsize	=	sizeof(struct padlock_sha_desc),
+	.descsize	=	sizeof(struct padlock_sha_desc) +
+				sizeof(struct sha256_state),
 	.statesize	=	sizeof(struct sha256_state),
 	.base		=	{
 		.cra_name		=	"sha256",
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/4 crypto: zynqmp-sha - Make descsize an algorithm attribute

In reply to Herbert Xu

Rather than setting descsize in init_tfm, set it statically and double-check it in init_tfm.

---
 drivers/crypto/xilinx/zynqmp-sha.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/crypto/xilinx/zynqmp-sha.c +9 -2
index 580649f9bff8..f66e0a4c1169 100644
@@ -60,8 +60,14 @@ static int zynqmp_sha_init_tfm(struct crypto_shash *hash)
 	if (IS_ERR(fallback_tfm))
 		return PTR_ERR(fallback_tfm);
 
+	if (crypto_shash_descsize(hash) <
+	    sizeof(struct zynqmp_sha_desc_ctx) +
+	    crypto_shash_descsize(tfm_ctx->fbk_tfm)) {
+		crypto_free_shash(fallback_tfm);
+		return -EINVAL;
+	}
+
 	tfm_ctx->fbk_tfm = fallback_tfm;
-	hash->descsize += crypto_shash_descsize(tfm_ctx->fbk_tfm);
 
 	return 0;
 }
@@ -170,7 +176,8 @@ static struct zynqmp_sha_drv_ctx sha3_drv_ctx = {
 		.import = zynqmp_sha_import,
 		.init_tfm = zynqmp_sha_init_tfm,
 		.exit_tfm = zynqmp_sha_exit_tfm,
-		.descsize = sizeof(struct zynqmp_sha_desc_ctx),
+		.descsize = sizeof(struct zynqmp_sha_desc_ctx) +
+			    sizeof(struct sha3_state),
 		.statesize = sizeof(struct sha3_state),
 		.digestsize = SHA3_384_DIGEST_SIZE,
 		.base = {
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH 3/4 crypto: hmac - Make descsize an algorithm attribute

In reply to Herbert Xu

Rather than setting descsize in init_tfm, make it an algorithm attribute and set it during instance construction.

---
 crypto/hmac.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/hmac.c +1 -3
index 7cec25ff9889..dfb153511865 100644
@@ -146,9 +146,6 @@ static int hmac_init_tfm(struct crypto_shash *parent)
 	if (IS_ERR(hash))
 		return PTR_ERR(hash);
 
-	parent->descsize = sizeof(struct shash_desc) +
-			   crypto_shash_descsize(hash);
-
 	tctx->hash = hash;
 	return 0;
 }
@@ -222,6 +219,7 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
 
 	inst->alg.digestsize = ds;
 	inst->alg.statesize = ss;
+	inst->alg.descsize = sizeof(struct shash_desc) + salg->descsize;
 	inst->alg.init = hmac_init;
 	inst->alg.update = hmac_update;
 	inst->alg.final = hmac_final;
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

PATCH 4/4 crypto: shash - Remove dynamic descsize

In reply to Herbert Xu

As all users of the dynamic descsize have been converted to use a static one instead, remove support for dynamic descsize.

---
 crypto/shash.c        | 18 +-----------------
 include/crypto/hash.h |  3 +--
 2 files changed, 2 insertions(+), 19 deletions(-)
crypto/shash.c +1 -17
index a2a7d6609172..f23bd9cb1873 100644
@@ -153,9 +153,6 @@ static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
 {
 	struct crypto_shash *hash = __crypto_shash_cast(tfm);
 	struct shash_alg *alg = crypto_shash_alg(hash);
-	int err;
-
-	hash->descsize = alg->descsize;
 
 	shash_set_needkey(hash, alg);
 
@@ -165,18 +162,7 @@ static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
 	if (!alg->init_tfm)
 		return 0;
 
-	err = alg->init_tfm(hash);
-	if (err)
-		return err;
-
-	/* ->init_tfm() may have increased the descsize. */
-	if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) {
-		if (alg->exit_tfm)
-			alg->exit_tfm(hash);
-		return -EINVAL;
-	}
-
-	return 0;
+	return alg->init_tfm(hash);
 }
 
 static void crypto_shash_free_instance(struct crypto_instance *inst)
@@ -274,8 +260,6 @@ struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
 	if (IS_ERR(nhash))
 		return nhash;
 
-	nhash->descsize = hash->descsize;
-
 	if (alg->clone_tfm) {
 		err = alg->clone_tfm(nhash, hash);
 		if (err) {
include/crypto/hash.h +1 -2
index eceb2ed04f26..87518cf3b2d8 100644
@@ -235,7 +235,6 @@ struct crypto_ahash {
 };
 
 struct crypto_shash {
-	unsigned int descsize;
 	struct crypto_tfm base;
 };
 
@@ -810,7 +809,7 @@ static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
  */
 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
 {
-	return tfm->descsize;
+	return crypto_shash_alg(tfm)->descsize;
 }
 
 static inline void *shash_desc_ctx(struct shash_desc *desc)
Signature · 1 line
2.39.5