summaryrefslogtreecommitdiff
path: root/lib/sha256_common.c
diff options
context:
space:
mode:
authorPhilippe Reynes <philippe.reynes@softathome.com>2024-12-19 14:05:49 +0100
committerTom Rini <trini@konsulko.com>2025-01-18 17:12:47 -0600
commitccc5e166836c2fa204a58fe9ac87c5fce72b5e7b (patch)
treee3e00e5c88bd32888e0d407207af89ddb6a84f33 /lib/sha256_common.c
parent70a42bf2170eadd2b8b99175785435f209faca0a (diff)
lib: sha256: move common function to sha256_common.c
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 Reviewed-by: Raymond Mao <raymond.mao@linaro.org> Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
Diffstat (limited to 'lib/sha256_common.c')
-rw-r--r--lib/sha256_common.c50
1 files changed, 50 insertions, 0 deletions
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);
+}