diff mbox series

[v2,3/6] crypto: ahash - Enforce MAX_SYNC_HASH_REQSIZE for sync ahash

Message ID d3b9bc6074465ecaeb50ae962ba6d4498edd8a94.1746365585.git.herbert@gondor.apana.org.au
State New
Headers show
Series [v2,1/6] crypto: shash - Cap state size to HASH_MAX_STATESIZE | expand

Commit Message

Herbert Xu May 4, 2025, 1:33 p.m. UTC
As sync ahash algorithms (currently there are none) are used without
a fallback, ensure that they obey the MAX_SYNC_HASH_REQSIZE rule
just like shash algorithms.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---
 crypto/ahash.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/crypto/ahash.c b/crypto/ahash.c
index 57c131a13067..736e9fb5d0a4 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -760,23 +760,28 @@  static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
 
 	tfm->exit = crypto_ahash_exit_tfm;
 
-	if (!alg->init_tfm) {
-		if (!tfm->__crt_alg->cra_init)
-			return 0;
-
+	if (alg->init_tfm)
+		err = alg->init_tfm(hash);
+	else if (tfm->__crt_alg->cra_init)
 		err = tfm->__crt_alg->cra_init(tfm);
-		if (err)
-			goto out_free_sync_hash;
-
+	else
 		return 0;
-	}
 
-	err = alg->init_tfm(hash);
 	if (err)
 		goto out_free_sync_hash;
 
+	if (!ahash_is_async(hash) && crypto_ahash_reqsize(hash) >
+				     MAX_SYNC_HASH_REQSIZE)
+		goto out_exit_tfm;
+
 	return 0;
 
+out_exit_tfm:
+	if (alg->exit_tfm)
+		alg->exit_tfm(hash);
+	else if (tfm->__crt_alg->cra_exit)
+		tfm->__crt_alg->cra_exit(tfm);
+	err = -EINVAL;
 out_free_sync_hash:
 	crypto_free_ahash(fb);
 	return err;
@@ -954,6 +959,10 @@  static int ahash_prepare_alg(struct ahash_alg *alg)
 	if (base->cra_reqsize && base->cra_reqsize < alg->halg.statesize)
 		return -EINVAL;
 
+	if (!(base->cra_flags & CRYPTO_ALG_ASYNC) &&
+	    base->cra_reqsize > MAX_SYNC_HASH_REQSIZE)
+		return -EINVAL;
+
 	err = hash_prepare_alg(&alg->halg);
 	if (err)
 		return err;