crypto: shash - Allow cloning on algorithms with no init_tfm
authorHerbert Xu <herbert@gondor.apana.org.au>
Fri, 19 May 2023 09:04:04 +0000 (17:04 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 24 May 2023 10:12:33 +0000 (18:12 +0800)
Some shash algorithms are so simple that they don't have an init_tfm
function.  These can be cloned trivially.  Check this before failing
in crypto_clone_shash.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/shash.c

index 717b42d..1fadb6b 100644 (file)
@@ -597,7 +597,7 @@ struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
                return hash;
        }
 
-       if (!alg->clone_tfm)
+       if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init))
                return ERR_PTR(-ENOSYS);
 
        nhash = crypto_clone_tfm(&crypto_shash_type, tfm);
@@ -606,10 +606,12 @@ struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
 
        nhash->descsize = hash->descsize;
 
-       err = alg->clone_tfm(nhash, hash);
-       if (err) {
-               crypto_free_shash(nhash);
-               return ERR_PTR(err);
+       if (alg->clone_tfm) {
+               err = alg->clone_tfm(nhash, hash);
+               if (err) {
+                       crypto_free_shash(nhash);
+                       return ERR_PTR(err);
+               }
        }
 
        return nhash;