summaryrefslogtreecommitdiff
path: root/lib/mbedtls/md5.c
diff options
context:
space:
mode:
authorRaymond Mao <raymond.mao@linaro.org>2024-10-03 14:50:19 -0700
committerTom Rini <trini@konsulko.com>2024-10-14 17:58:31 -0600
commitbdc3f44a6bf4de61d99c818b3e8e71b07a9eea52 (patch)
treeeea6e753771c0f31203026663bc22b5fe4d514cb /lib/mbedtls/md5.c
parent988e749d9770bad0a8d8fbf45bfa8af3ca88da38 (diff)
mbedtls: add digest shim layer for MbedTLS
Implement digest shim layer on top of MbedTLS crypto library. Introduce <alg>_MBEDTLS kconfig for MbedTLS crypto implementations. Signed-off-by: Raymond Mao <raymond.mao@linaro.org>
Diffstat (limited to 'lib/mbedtls/md5.c')
-rw-r--r--lib/mbedtls/md5.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/mbedtls/md5.c b/lib/mbedtls/md5.c
new file mode 100644
index 00000000000..04388fce249
--- /dev/null
+++ b/lib/mbedtls/md5.c
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Hash shim layer on MbedTLS Crypto library
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao <raymond.mao@linaro.org>
+ */
+#include "compiler.h"
+
+#ifndef USE_HOSTCC
+#include <watchdog.h>
+#endif /* USE_HOSTCC */
+#include <u-boot/md5.h>
+
+void MD5Init(MD5Context *ctx)
+{
+ mbedtls_md5_init(ctx);
+ mbedtls_md5_starts(ctx);
+}
+
+void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
+{
+ mbedtls_md5_update(ctx, buf, len);
+}
+
+void MD5Final(unsigned char digest[16], MD5Context *ctx)
+{
+ mbedtls_md5_finish(ctx, digest);
+ mbedtls_md5_free(ctx);
+}
+
+void md5_wd(const unsigned char *input, unsigned int len,
+ unsigned char output[16], unsigned int chunk_sz)
+{
+ MD5Context context;
+
+ MD5Init(&context);
+
+ if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
+ const unsigned char *curr = input;
+ const unsigned char *end = input + len;
+ int chunk;
+
+ while (curr < end) {
+ chunk = end - curr;
+ if (chunk > chunk_sz)
+ chunk = chunk_sz;
+ MD5Update(&context, curr, chunk);
+ curr += chunk;
+ schedule();
+ }
+ } else {
+ MD5Update(&context, input, len);
+ }
+
+ MD5Final(output, &context);
+}