crypto: shash - optimize the default digest and finup
authorEric Biggers <ebiggers@google.com>
Mon, 9 Oct 2023 07:32:13 +0000 (00:32 -0700)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 20 Oct 2023 05:39:25 +0000 (13:39 +0800)
For an shash algorithm that doesn't implement ->digest, currently
crypto_shash_digest() with aligned input makes 5 indirect calls: 1 to
shash_digest_unaligned(), 1 to ->init, 2 to ->update ('alignmask + 1'
bytes, then the rest), then 1 to ->final.  This is true even if the
algorithm implements ->finup.  This is caused by an unnecessary fallback
to code meant to handle unaligned inputs.  In fact,
crypto_shash_digest() already does the needed alignment check earlier.
Therefore, optimize the number of indirect calls for aligned inputs to 3
when the algorithm implements ->finup.  It remains at 5 when the
algorithm implements neither ->finup nor ->digest.

Similarly, for an shash algorithm that doesn't implement ->finup,
currently crypto_shash_finup() with aligned input makes 4 indirect
calls: 1 to shash_finup_unaligned(), 2 to ->update, and
1 to ->final.  Optimize this to 3 calls.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/shash.c

index 1fadb6b59bdcc1afdd61b280813249327b4025d5..d99dc2f94c65f52afd770ced9751f6750d872c03 100644 (file)
@@ -191,6 +191,15 @@ static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
               shash_final_unaligned(desc, out);
 }
 
+static int shash_default_finup(struct shash_desc *desc, const u8 *data,
+                              unsigned int len, u8 *out)
+{
+       struct shash_alg *shash = crypto_shash_alg(desc->tfm);
+
+       return shash->update(desc, data, len) ?:
+              shash->final(desc, out);
+}
+
 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
                       unsigned int len, u8 *out)
 {
@@ -224,6 +233,15 @@ static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
               shash_final_unaligned(desc, out);
 }
 
+static int shash_default_digest(struct shash_desc *desc, const u8 *data,
+                               unsigned int len, u8 *out)
+{
+       struct shash_alg *shash = crypto_shash_alg(desc->tfm);
+
+       return shash->init(desc) ?:
+              shash->finup(desc, data, len, out);
+}
+
 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
                        unsigned int len, u8 *out)
 {
@@ -656,9 +674,9 @@ static int shash_prepare_alg(struct shash_alg *alg)
        base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
 
        if (!alg->finup)
-               alg->finup = shash_finup_unaligned;
+               alg->finup = shash_default_finup;
        if (!alg->digest)
-               alg->digest = shash_digest_unaligned;
+               alg->digest = shash_default_digest;
        if (!alg->export) {
                alg->export = shash_default_export;
                alg->import = shash_default_import;