[PATCH v4 4/8] lib: sha256: move common function to sha256_common.c

Raymond Mao raymond.mao at linaro.org
Fri Dec 13 17:42:19 CET 2024


Hi Philippe,

On Thu, 12 Dec 2024 at 08:37, Philippe Reynes <
philippe.reynes at softathome.com> wrote:

> The function sha256_csum_wd is defined in lib/sha256.c
> and in lib/mbedtls/sha256.c. To avoid duplicating this
> function (and future function), we move this function
> to the file lib/sha256_common.c
>
> Signed-off-by: Philippe Reynes <philippe.reynes at softathome.com>
> ---
>  lib/Makefile         |  1 +
>  lib/mbedtls/sha256.c | 27 ------------------------
>  lib/sha256.c         | 36 -------------------------------
>  lib/sha256_common.c  | 50 ++++++++++++++++++++++++++++++++++++++++++++
>  tools/Makefile       |  1 +
>  5 files changed, 52 insertions(+), 63 deletions(-)
>  create mode 100644 lib/sha256_common.c
>
> diff --git a/lib/Makefile b/lib/Makefile
> index d24ed629732..17201f66798 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -77,6 +77,7 @@ obj-$(CONFIG_BLAKE2) += blake2/blake2b.o
>
>  obj-$(CONFIG_$(XPL_)MD5_LEGACY) += md5.o
>  obj-$(CONFIG_$(XPL_)SHA1_LEGACY) += sha1.o
> +obj-$(CONFIG_$(XPL_)SHA256) += sha256_common.o
>  obj-$(CONFIG_$(XPL_)SHA256_LEGACY) += sha256.o
>  obj-$(CONFIG_$(XPL_)SHA512_LEGACY) += sha512.o
>
> diff --git a/lib/mbedtls/sha256.c b/lib/mbedtls/sha256.c
> index 24aa58fa674..2128e598834 100644
> --- a/lib/mbedtls/sha256.c
> +++ b/lib/mbedtls/sha256.c
> @@ -33,30 +33,3 @@ void sha256_finish(sha256_context *ctx, uint8_t
> digest[SHA256_SUM_LEN])
>         mbedtls_sha256_finish(ctx, digest);
>         mbedtls_sha256_free(ctx);
>  }
> -
> -void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
> -                   unsigned char *output, unsigned int chunk_sz)
> -{
> -       sha256_context ctx;
> -
> -       sha256_starts(&ctx);
> -
> -       if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG))
> {
> -               const unsigned char *curr = input;
> -               const unsigned char *end = input + ilen;
> -               int chunk;
> -
> -               while (curr < end) {
> -                       chunk = end - curr;
> -                       if (chunk > chunk_sz)
> -                               chunk = chunk_sz;
> -                       sha256_update(&ctx, curr, chunk);
> -                       curr += chunk;
> -                       schedule();
> -               }
> -       } else {
> -               sha256_update(&ctx, input, ilen);
> -       }
> -
> -       sha256_finish(&ctx, output);
> -}
> diff --git a/lib/sha256.c b/lib/sha256.c
> index fb195d988f1..827bd9a872b 100644
> --- a/lib/sha256.c
> +++ b/lib/sha256.c
> @@ -264,39 +264,3 @@ void sha256_finish(sha256_context * ctx, uint8_t
> digest[32])
>         PUT_UINT32_BE(ctx->state[6], digest, 24);
>         PUT_UINT32_BE(ctx->state[7], digest, 28);
>  }
> -
> -/*
> - * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz'
> - * bytes of input processed.
> - */
> -void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
> -               unsigned char *output, unsigned int chunk_sz)
> -{
> -       sha256_context ctx;
> -#if !defined(USE_HOSTCC) && \
> -    (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
> -       const unsigned char *end;
> -       unsigned char *curr;
> -       int chunk;
> -#endif
> -
> -       sha256_starts(&ctx);
> -
> -#if !defined(USE_HOSTCC) && \
> -    (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
> -       curr = (unsigned char *)input;
> -       end = input + ilen;
> -       while (curr < end) {
> -               chunk = end - curr;
> -               if (chunk > chunk_sz)
> -                       chunk = chunk_sz;
> -               sha256_update(&ctx, curr, chunk);
> -               curr += chunk;
> -               schedule();
> -       }
> -#else
> -       sha256_update(&ctx, input, ilen);
> -#endif
> -
> -       sha256_finish(&ctx, output);
> -}
> diff --git a/lib/sha256_common.c b/lib/sha256_common.c
> new file mode 100644
> index 00000000000..7041abd26d9
> --- /dev/null
> +++ b/lib/sha256_common.c
> @@ -0,0 +1,50 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * FIPS-180-2 compliant SHA-256 implementation
> + *
> + * Copyright (C) 2001-2003  Christophe Devine
> + */
> +
> +#ifndef USE_HOSTCC
> +#include <u-boot/schedule.h>
> +#endif /* USE_HOSTCC */
> +#include <string.h>
> +#include <u-boot/sha256.h>
> +
> +#include <linux/compiler_attributes.h>
> +
> +/*
> + * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz'
> + * bytes of input processed.
> + */
> +void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
> +                   unsigned char *output, unsigned int chunk_sz)
> +{
> +       sha256_context ctx;
> +#if !defined(USE_HOSTCC) && \
> +       (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
> +       const unsigned char *end;
> +       unsigned char *curr;
> +       int chunk;
> +#endif
> +
> +       sha256_starts(&ctx);
> +
> +#if !defined(USE_HOSTCC) && \
> +       (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
> +       curr = (unsigned char *)input;
> +       end = input + ilen;
> +       while (curr < end) {
> +               chunk = end - curr;
> +               if (chunk > chunk_sz)
> +                       chunk = chunk_sz;
> +               sha256_update(&ctx, curr, chunk);
> +               curr += chunk;
> +               schedule();
> +       }
> +#else
> +       sha256_update(&ctx, input, ilen);
> +#endif
> +
> +       sha256_finish(&ctx, output);
> +}
> diff --git a/tools/Makefile b/tools/Makefile
> index ee08a9675df..237fa900a24 100644
> --- a/tools/Makefile
> +++ b/tools/Makefile
> @@ -135,6 +135,7 @@ dumpimage-mkimage-objs := aisimage.o \
>                         generated/lib/hash-checksum.o \
>                         generated/lib/sha1.o \
>                         generated/lib/sha256.o \
> +                       generated/lib/sha256_common.o \
>                         generated/lib/sha512.o \
>                         generated/common/hash.o \
>                         ublimage.o \
> --
> 2.25.1
>
> Sounds good to me.
Reviewed-by: Raymond Mao <raymond.mao at linaro.org>

Regards,
Raymond


More information about the U-Boot mailing list