crypto: sha256-generic - Use API partial block handling
authorHerbert Xu <herbert@gondor.apana.org.au>
Fri, 18 Apr 2025 02:59:48 +0000 (10:59 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 23 Apr 2025 07:52:45 +0000 (15:52 +0800)
Use the Crypto API partial block handling.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/sha256_generic.c
include/crypto/sha2.h

index b00521f1a6d4555adf5b64ac745a1390b587239c..05084e5bbaec8f072bba0a403d17e1b28e133a4f 100644 (file)
@@ -8,14 +8,10 @@
  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  */
 #include <crypto/internal/hash.h>
-#include <linux/init.h>
-#include <linux/module.h>
-#include <linux/mm.h>
-#include <linux/types.h>
 #include <crypto/sha2.h>
 #include <crypto/sha256_base.h>
-#include <asm/byteorder.h>
-#include <linux/unaligned.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
 
 const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
        0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
@@ -33,42 +29,37 @@ const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
 };
 EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
 
-int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
-                         unsigned int len)
+static void sha256_block(struct crypto_sha256_state *sctx, const u8 *input,
+                        int blocks)
 {
-       sha256_update(shash_desc_ctx(desc), data, len);
-       return 0;
+       sha256_transform_blocks(sctx, input, blocks);
 }
-EXPORT_SYMBOL(crypto_sha256_update);
 
-static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
+static int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
+                               unsigned int len)
 {
-       if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
-               sha224_final(shash_desc_ctx(desc), out);
-       else
-               sha256_final(shash_desc_ctx(desc), out);
-       return 0;
+       return sha256_base_do_update_blocks(desc, data, len, sha256_block);
 }
 
-int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
-                       unsigned int len, u8 *hash)
+static int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
+                              unsigned int len, u8 *hash)
 {
-       sha256_update(shash_desc_ctx(desc), data, len);
-       return crypto_sha256_final(desc, hash);
+       sha256_base_do_finup(desc, data, len, sha256_block);
+       return sha256_base_finish(desc, hash);
 }
-EXPORT_SYMBOL(crypto_sha256_finup);
 
 static struct shash_alg sha256_algs[2] = { {
        .digestsize     =       SHA256_DIGEST_SIZE,
        .init           =       sha256_base_init,
        .update         =       crypto_sha256_update,
-       .final          =       crypto_sha256_final,
        .finup          =       crypto_sha256_finup,
-       .descsize       =       sizeof(struct sha256_state),
+       .descsize       =       sizeof(struct crypto_sha256_state),
        .base           =       {
                .cra_name       =       "sha256",
                .cra_driver_name=       "sha256-generic",
                .cra_priority   =       100,
+               .cra_flags      =       CRYPTO_AHASH_ALG_BLOCK_ONLY |
+                                       CRYPTO_AHASH_ALG_FINUP_MAX,
                .cra_blocksize  =       SHA256_BLOCK_SIZE,
                .cra_module     =       THIS_MODULE,
        }
@@ -76,13 +67,14 @@ static struct shash_alg sha256_algs[2] = { {
        .digestsize     =       SHA224_DIGEST_SIZE,
        .init           =       sha224_base_init,
        .update         =       crypto_sha256_update,
-       .final          =       crypto_sha256_final,
        .finup          =       crypto_sha256_finup,
-       .descsize       =       sizeof(struct sha256_state),
+       .descsize       =       sizeof(struct crypto_sha256_state),
        .base           =       {
                .cra_name       =       "sha224",
                .cra_driver_name=       "sha224-generic",
                .cra_priority   =       100,
+               .cra_flags      =       CRYPTO_AHASH_ALG_BLOCK_ONLY |
+                                       CRYPTO_AHASH_ALG_FINUP_MAX,
                .cra_blocksize  =       SHA224_BLOCK_SIZE,
                .cra_module     =       THIS_MODULE,
        }
index d9b1b99323938519ce17aa7390acaedcdc40d180..a913bad5dd3b7e089b55235c60edcd0fa7803ebd 100644 (file)
@@ -83,12 +83,6 @@ struct sha512_state {
 
 struct shash_desc;
 
-extern int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
-                             unsigned int len);
-
-extern int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
-                              unsigned int len, u8 *hash);
-
 extern int crypto_sha512_update(struct shash_desc *desc, const u8 *data,
                              unsigned int len);