On Mon, Apr 28, 2025 at 10:00:26AM -0700, Eric Biggers wrote:
> From: Eric Biggers <[email protected]>
> 
> As has been done for various other algorithms, rework the design of the
> SHA-256 library to support arch-optimized implementations, and make
> crypto/sha256.c expose both generic and arch-optimized shash algorithms
> that wrap the library functions.
> 
> This allows users of the SHA-256 library functions to take advantage of
> the arch-optimized code, and this makes it much simpler to integrate
> SHA-256 for each architecture.
> 
> Note that sha256_base.h is not used in the new design.  It will be
> removed once all the architecture-specific code has been updated.
> 
> Move the generic block function into its own module to avoid a circular
> dependency from libsha256.ko => sha256-$ARCH.ko => libsha256.ko.
> 
> Signed-off-by: Eric Biggers <[email protected]>
> ---
>  crypto/Kconfig                 |   1 +
>  crypto/Makefile                |   3 +-
>  crypto/sha256.c                | 201 +++++++++++++++++++++++++++++++++
>  crypto/sha256_generic.c        | 102 -----------------
>  include/crypto/internal/sha2.h |  28 +++++
>  include/crypto/sha2.h          |  15 +--
>  include/crypto/sha256_base.h   |   9 +-
>  lib/crypto/Kconfig             |  19 ++++
>  lib/crypto/Makefile            |   3 +
>  lib/crypto/sha256-generic.c    | 137 ++++++++++++++++++++++
>  lib/crypto/sha256.c            | 196 ++++++++++++++------------------
>  11 files changed, 487 insertions(+), 227 deletions(-)
>  create mode 100644 crypto/sha256.c
>  delete mode 100644 crypto/sha256_generic.c
>  create mode 100644 include/crypto/internal/sha2.h
>  create mode 100644 lib/crypto/sha256-generic.c

This is the patch that I will fold in here to maintain the existing
export format:

diff --git a/crypto/sha256.c b/crypto/sha256.c
index 1c2edcf9453d..c2588d08ee3e 100644
--- a/crypto/sha256.c
+++ b/crypto/sha256.c
@@ -116,6 +116,32 @@ static int crypto_sha224_final_arch(struct shash_desc 
*desc, u8 *out)
        return 0;
 }
 
+static int crypto_sha256_import_lib(struct shash_desc *desc, const void *in)
+{
+       struct sha256_state *sctx = shash_desc_ctx(desc);
+       const u8 *p = in;
+
+       memcpy(sctx, p, sizeof(*sctx));
+       p += sizeof(*sctx);
+       sctx->count += *p;
+       return 0;
+}
+
+static int crypto_sha256_export_lib(struct shash_desc *desc, void *out)
+{
+       struct sha256_state *sctx0 = shash_desc_ctx(desc);
+       struct sha256_state sctx = *sctx0;
+       unsigned int partial;
+       u8 *p = out;
+
+       partial = sctx.count % SHA256_BLOCK_SIZE;
+       sctx.count -= partial;
+       memcpy(p, &sctx, sizeof(sctx));
+       p += sizeof(sctx);
+       *p = partial;
+       return 0;
+}
+
 static struct shash_alg algs[] = {
        {
                .base.cra_name          = "sha256",
@@ -130,6 +156,10 @@ static struct shash_alg algs[] = {
                .finup                  = crypto_sha256_finup_generic,
                .digest                 = crypto_sha256_digest_generic,
                .descsize               = sizeof(struct sha256_state),
+               .statesize              = sizeof(struct crypto_sha256_state) +
+                                         SHA256_BLOCK_SIZE + 1,
+               .import                 = crypto_sha256_import_lib,
+               .export                 = crypto_sha256_export_lib,
        },
        {
                .base.cra_name          = "sha224",
@@ -142,6 +172,10 @@ static struct shash_alg algs[] = {
                .update                 = crypto_sha256_update_generic,
                .final                  = crypto_sha224_final_generic,
                .descsize               = sizeof(struct sha256_state),
+               .statesize              = sizeof(struct crypto_sha256_state) +
+                                         SHA256_BLOCK_SIZE + 1,
+               .import                 = crypto_sha256_import_lib,
+               .export                 = crypto_sha256_export_lib,
        },
        {
                .base.cra_name          = "sha256",
@@ -156,6 +190,10 @@ static struct shash_alg algs[] = {
                .finup                  = crypto_sha256_finup_arch,
                .digest                 = crypto_sha256_digest_arch,
                .descsize               = sizeof(struct sha256_state),
+               .statesize              = sizeof(struct crypto_sha256_state) +
+                                         SHA256_BLOCK_SIZE + 1,
+               .import                 = crypto_sha256_import_lib,
+               .export                 = crypto_sha256_export_lib,
        },
        {
                .base.cra_name          = "sha224",
@@ -168,6 +206,10 @@ static struct shash_alg algs[] = {
                .update                 = crypto_sha256_update_arch,
                .final                  = crypto_sha224_final_arch,
                .descsize               = sizeof(struct sha256_state),
+               .statesize              = sizeof(struct crypto_sha256_state) +
+                                         SHA256_BLOCK_SIZE + 1,
+               .import                 = crypto_sha256_import_lib,
+               .export                 = crypto_sha256_export_lib,
        },
 };

Cheers,
-- 
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

Reply via email to