[Concept,07/24] mbedtls: hash: Provide the mbedtls hash type in the hash interface

Message ID 20251031065439.3251464-8-sjg@u-boot.org
State New
Headers
Series luks: Provide basic support for unlocking a LUKS1 partition |

Commit Message

Simon Glass Oct. 31, 2025, 6:54 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

The mbedtls hashing has not been integrated into U-Boot's hash API. As a
first step, add the mbedtls hash type into struct hash_algo and provide
the values. This allows looking up the type by its name.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/hash.c  |  5 +++++
 include/hash.h | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)
  

Patch

diff --git a/common/hash.c b/common/hash.c
index 0c45992d5c7..2e3b12c109a 100644
--- a/common/hash.c
+++ b/common/hash.c
@@ -212,6 +212,7 @@  static struct hash_algo hash_algo[] = {
 		.digest_size	= MD5_SUM_LEN,
 		.chunk_size	= CHUNKSZ_MD5,
 		.hash_func_ws	= md5_wd,
+		HASH_MBEDTLS_TYPE(MBEDTLS_MD_MD5)
 	},
 #endif
 #if CONFIG_IS_ENABLED(SHA1)
@@ -233,6 +234,7 @@  static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_sha1,
 		.hash_finish	= hash_finish_sha1,
 #endif
+		HASH_MBEDTLS_TYPE(MBEDTLS_MD_SHA1)
 	},
 #endif
 #if CONFIG_IS_ENABLED(SHA256)
@@ -254,6 +256,7 @@  static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_sha256,
 		.hash_finish	= hash_finish_sha256,
 #endif
+		HASH_MBEDTLS_TYPE(MBEDTLS_MD_SHA256)
 	},
 #endif
 #if CONFIG_IS_ENABLED(SHA384)
@@ -275,6 +278,7 @@  static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_sha384,
 		.hash_finish	= hash_finish_sha384,
 #endif
+		HASH_MBEDTLS_TYPE(MBEDTLS_MD_SHA384)
 	},
 #endif
 #if CONFIG_IS_ENABLED(SHA512)
@@ -296,6 +300,7 @@  static struct hash_algo hash_algo[] = {
 		.hash_update	= hash_update_sha512,
 		.hash_finish	= hash_finish_sha512,
 #endif
+		HASH_MBEDTLS_TYPE(MBEDTLS_MD_SHA512)
 	},
 #endif
 #if CONFIG_IS_ENABLED(CRC16)
diff --git a/include/hash.h b/include/hash.h
index 8b3f79ec473..46a2be72f7e 100644
--- a/include/hash.h
+++ b/include/hash.h
@@ -10,6 +10,15 @@ 
 #include <linux/kconfig.h>
 #endif
 
+#if !defined(USE_HOSTCC) && CONFIG_IS_ENABLED(MBEDTLS_LIB)
+#include <mbedtls_options.h>
+#include <mbedtls/md.h>
+
+#define HASH_MBEDTLS_TYPE(_val)	.md_type	= _val,
+#else
+#define HASH_MBEDTLS_TYPE(_val)
+#endif
+
 struct cmd_tbl;
 
 /*
@@ -44,6 +53,9 @@  struct hash_algo {
 	void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
 		unsigned char *output, unsigned int chunk_sz);
 	int chunk_size;				/* Watchdog chunk size */
+#if !defined(USE_HOSTCC) && CONFIG_IS_ENABLED(MBEDTLS_LIB)
+	mbedtls_md_type_t md_type;		/* mbedtls hash type */
+#endif
 	/*
 	 * hash_init: Create the context for progressive hashing
 	 *
@@ -120,7 +132,26 @@  int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
 int hash_block(const char *algo_name, const void *data, unsigned int len,
 	       uint8_t *output, int *output_size);
 
-#endif /* !USE_HOSTCC */
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB)
+static inline mbedtls_md_type_t hash_mbedtls_type(struct hash_algo *algo)
+{
+	return algo->md_type;
+}
+#else
+static inline int hash_mbedtls_type(struct hash_algo *algo)
+{
+	return 0;
+}
+#endif
+
+#else /* USE_HOSTCC*/
+
+static inline int hash_mbedtls_type(struct hash_algo *algo)
+{
+	return 0;
+}
+
+#endif /* USE_HOSTCC */
 
 /**
  * hash_lookup_algo() - Look up the hash_algo struct for an algorithm