From: Philippe Reynes <philippe.reynes@softathome.com>
To: sjg+nodisclaimer@chromium.org, raymond.mao+nodisclaimer@linaro.org
Cc: u-boot+nodisclaimer@lists.denx.de,
Philippe Reynes <philippe.reynes@softathome.com>
Subject: [PATCH v6 4/9] lib: sha256: move common function to sha256_common.c
Date: Tue, 17 Dec 2024 22:36:08 +0100 [thread overview]
Message-ID: <20241217213613.286813-5-philippe.reynes@softathome.com> (raw)
In-Reply-To: <20241217213613.286813-1-philippe.reynes@softathome.com>
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@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
next prev parent reply other threads:[~2024-12-17 21:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-17 21:36 [PATCH v6 0/9] add the support of sha256_hmac and sha256_hkdf Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 1/9] tools: kwbimage.h: use linux/compiler_attributes.h Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 2/9] tools: renesas_spkgimage.h: " Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 3/9] mbedtls: enable support of hkdf Philippe Reynes
2024-12-17 21:36 ` Philippe Reynes [this message]
2024-12-17 21:36 ` [PATCH v6 5/9] lib: sha256: add feature sha256_hmac Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 6/9] test: lib: add test for sha256_hmac Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 7/9] lib: mbedtls: sha256: add support of key derivation Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 8/9] test: lib: add test for " Philippe Reynes
2024-12-17 21:36 ` [PATCH v6 9/9] configs: sandbox: enable mbedtls Philippe Reynes
2024-12-18 15:23 ` [PATCH v6 0/9] add the support of sha256_hmac and sha256_hkdf Raymond Mao
2024-12-19 12:21 ` Philippe REYNES
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241217213613.286813-5-philippe.reynes@softathome.com \
--to=philippe.reynes@softathome.com \
--cc=raymond.mao+nodisclaimer@linaro.org \
--cc=sjg+nodisclaimer@chromium.org \
--cc=u-boot+nodisclaimer@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.