Merge pull request #7811 from mpg/md-info
Optimize strings in MD
diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h
index 5831e12..dde95c5 100644
--- a/include/mbedtls/md.h
+++ b/include/mbedtls/md.h
@@ -467,8 +467,8 @@
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
/**
- * \brief This function extracts the message-digest name from the
- * message-digest information structure.
+ * \brief This function returns the name of the message digest for
+ * the message-digest information structure given.
*
* \param md_info The information structure of the message-digest algorithm
* to use.
diff --git a/library/md.c b/library/md.c
index 3589d63..2cac50c 100644
--- a/library/md.c
+++ b/library/md.c
@@ -76,102 +76,75 @@
#error "Internal error: MBEDTLS_MD_MAX_SIZE < PSA_HASH_MAX_SIZE"
#endif
+#if defined(MBEDTLS_MD_C)
+#define MD_INFO(type, out_size, block_size) type, out_size, block_size,
+#else
+#define MD_INFO(type, out_size, block_size) type, out_size,
+#endif
+
#if defined(MBEDTLS_MD_CAN_MD5)
-const mbedtls_md_info_t mbedtls_md5_info = {
- "MD5",
- MBEDTLS_MD_MD5,
- 16,
- 64,
+static const mbedtls_md_info_t mbedtls_md5_info = {
+ MD_INFO(MBEDTLS_MD_MD5, 16, 64)
};
#endif
#if defined(MBEDTLS_MD_CAN_RIPEMD160)
-const mbedtls_md_info_t mbedtls_ripemd160_info = {
- "RIPEMD160",
- MBEDTLS_MD_RIPEMD160,
- 20,
- 64,
+static const mbedtls_md_info_t mbedtls_ripemd160_info = {
+ MD_INFO(MBEDTLS_MD_RIPEMD160, 20, 64)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA1)
-const mbedtls_md_info_t mbedtls_sha1_info = {
- "SHA1",
- MBEDTLS_MD_SHA1,
- 20,
- 64,
+static const mbedtls_md_info_t mbedtls_sha1_info = {
+ MD_INFO(MBEDTLS_MD_SHA1, 20, 64)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA224)
-const mbedtls_md_info_t mbedtls_sha224_info = {
- "SHA224",
- MBEDTLS_MD_SHA224,
- 28,
- 64,
+static const mbedtls_md_info_t mbedtls_sha224_info = {
+ MD_INFO(MBEDTLS_MD_SHA224, 28, 64)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA256)
-const mbedtls_md_info_t mbedtls_sha256_info = {
- "SHA256",
- MBEDTLS_MD_SHA256,
- 32,
- 64,
+static const mbedtls_md_info_t mbedtls_sha256_info = {
+ MD_INFO(MBEDTLS_MD_SHA256, 32, 64)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA384)
-const mbedtls_md_info_t mbedtls_sha384_info = {
- "SHA384",
- MBEDTLS_MD_SHA384,
- 48,
- 128,
+static const mbedtls_md_info_t mbedtls_sha384_info = {
+ MD_INFO(MBEDTLS_MD_SHA384, 48, 128)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA512)
-const mbedtls_md_info_t mbedtls_sha512_info = {
- "SHA512",
- MBEDTLS_MD_SHA512,
- 64,
- 128,
+static const mbedtls_md_info_t mbedtls_sha512_info = {
+ MD_INFO(MBEDTLS_MD_SHA512, 64, 128)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA3_224)
-const mbedtls_md_info_t mbedtls_sha3_224_info = {
- "SHA3-224",
- MBEDTLS_MD_SHA3_224,
- 28,
- 144,
+static const mbedtls_md_info_t mbedtls_sha3_224_info = {
+ MD_INFO(MBEDTLS_MD_SHA3_224, 28, 144)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA3_256)
-const mbedtls_md_info_t mbedtls_sha3_256_info = {
- "SHA3-256",
- MBEDTLS_MD_SHA3_256,
- 32,
- 136,
+static const mbedtls_md_info_t mbedtls_sha3_256_info = {
+ MD_INFO(MBEDTLS_MD_SHA3_256, 32, 136)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA3_384)
-const mbedtls_md_info_t mbedtls_sha3_384_info = {
- "SHA3-384",
- MBEDTLS_MD_SHA3_384,
- 48,
- 104,
+static const mbedtls_md_info_t mbedtls_sha3_384_info = {
+ MD_INFO(MBEDTLS_MD_SHA3_384, 48, 104)
};
#endif
#if defined(MBEDTLS_MD_CAN_SHA3_512)
-const mbedtls_md_info_t mbedtls_sha3_512_info = {
- "SHA3-512",
- MBEDTLS_MD_SHA3_512,
- 64,
- 72,
+static const mbedtls_md_info_t mbedtls_sha3_512_info = {
+ MD_INFO(MBEDTLS_MD_SHA3_512, 64, 72)
};
#endif
@@ -856,69 +829,77 @@
return supported_digests;
}
+typedef struct {
+ const char *md_name;
+ mbedtls_md_type_t md_type;
+} md_name_entry;
+
+static const md_name_entry md_names[] = {
+#if defined(MBEDTLS_MD_CAN_MD5)
+ { "MD5", MBEDTLS_MD_MD5 },
+#endif
+#if defined(MBEDTLS_MD_CAN_RIPEMD160)
+ { "RIPEMD160", MBEDTLS_MD_RIPEMD160 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA1)
+ { "SHA1", MBEDTLS_MD_SHA1 },
+ { "SHA", MBEDTLS_MD_SHA1 }, // compatibility fallback
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA224)
+ { "SHA224", MBEDTLS_MD_SHA224 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA256)
+ { "SHA256", MBEDTLS_MD_SHA256 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA384)
+ { "SHA384", MBEDTLS_MD_SHA384 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA512)
+ { "SHA512", MBEDTLS_MD_SHA512 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA3_224)
+ { "SHA3-224", MBEDTLS_MD_SHA3_224 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA3_256)
+ { "SHA3-256", MBEDTLS_MD_SHA3_256 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA3_384)
+ { "SHA3-384", MBEDTLS_MD_SHA3_384 },
+#endif
+#if defined(MBEDTLS_MD_CAN_SHA3_512)
+ { "SHA3-512", MBEDTLS_MD_SHA3_512 },
+#endif
+ { NULL, MBEDTLS_MD_NONE },
+};
+
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
{
if (NULL == md_name) {
return NULL;
}
- /* Get the appropriate digest information */
-#if defined(MBEDTLS_MD_CAN_MD5)
- if (!strcmp("MD5", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
+ const md_name_entry *entry = md_names;
+ while (entry->md_name != NULL &&
+ strcmp(entry->md_name, md_name) != 0) {
+ ++entry;
}
-#endif
-#if defined(MBEDTLS_MD_CAN_RIPEMD160)
- if (!strcmp("RIPEMD160", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
+
+ return mbedtls_md_info_from_type(entry->md_type);
+}
+
+const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
+{
+ if (md_info == NULL) {
+ return NULL;
}
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA1)
- if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
+
+ const md_name_entry *entry = md_names;
+ while (entry->md_type != MBEDTLS_MD_NONE &&
+ entry->md_type != md_info->type) {
+ ++entry;
}
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA224)
- if (!strcmp("SHA224", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA256)
- if (!strcmp("SHA256", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA384)
- if (!strcmp("SHA384", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA512)
- if (!strcmp("SHA512", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA3_224)
- if (!strcmp("SHA3-224", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA3_224);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA3_256)
- if (!strcmp("SHA3-256", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA3_256);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA3_384)
- if (!strcmp("SHA3-384", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA3_384);
- }
-#endif
-#if defined(MBEDTLS_MD_CAN_SHA3_512)
- if (!strcmp("SHA3-512", md_name)) {
- return mbedtls_md_info_from_type(MBEDTLS_MD_SHA3_512);
- }
-#endif
- return NULL;
+
+ return entry->md_name;
}
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
@@ -1119,15 +1100,6 @@
return ret;
}
-const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
-{
- if (md_info == NULL) {
- return NULL;
- }
-
- return md_info->name;
-}
-
#endif /* MBEDTLS_MD_C */
#endif /* MBEDTLS_MD_LIGHT */
diff --git a/library/md_wrap.h b/library/md_wrap.h
index 5e1e327..166b43b 100644
--- a/library/md_wrap.h
+++ b/library/md_wrap.h
@@ -39,47 +39,18 @@
* Allows message digest functions to be called in a generic way.
*/
struct mbedtls_md_info_t {
- /** Name of the message digest */
- const char *name;
-
/** Digest identifier */
mbedtls_md_type_t type;
/** Output length of the digest function in bytes */
unsigned char size;
+#if defined(MBEDTLS_MD_C)
/** Block length of the digest function in bytes */
unsigned char block_size;
+#endif
};
-#if defined(MBEDTLS_MD5_C)
-extern const mbedtls_md_info_t mbedtls_md5_info;
-#endif
-#if defined(MBEDTLS_RIPEMD160_C)
-extern const mbedtls_md_info_t mbedtls_ripemd160_info;
-#endif
-#if defined(MBEDTLS_SHA1_C)
-extern const mbedtls_md_info_t mbedtls_sha1_info;
-#endif
-#if defined(MBEDTLS_SHA224_C)
-extern const mbedtls_md_info_t mbedtls_sha224_info;
-#endif
-#if defined(MBEDTLS_SHA256_C)
-extern const mbedtls_md_info_t mbedtls_sha256_info;
-#endif
-#if defined(MBEDTLS_SHA384_C)
-extern const mbedtls_md_info_t mbedtls_sha384_info;
-#endif
-#if defined(MBEDTLS_SHA512_C)
-extern const mbedtls_md_info_t mbedtls_sha512_info;
-#endif
-#if defined(MBEDTLS_SHA3_C)
-extern const mbedtls_md_info_t mbedtls_sha3_224_info;
-extern const mbedtls_md_info_t mbedtls_sha3_256_info;
-extern const mbedtls_md_info_t mbedtls_sha3_384_info;
-extern const mbedtls_md_info_t mbedtls_sha3_512_info;
-#endif
-
#ifdef __cplusplus
}
#endif
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index c890e12..3244831 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -72,7 +72,6 @@
#include "mbedtls/gcm.h"
#include "mbedtls/md5.h"
#include "mbedtls/md.h"
-#include "md_wrap.h"
#include "mbedtls/pk.h"
#include "pk_wrap.h"
#include "mbedtls/platform_util.h"
diff --git a/library/psa_crypto_hash.h b/library/psa_crypto_hash.h
index d6bbd3f..2dfb011 100644
--- a/library/psa_crypto_hash.h
+++ b/library/psa_crypto_hash.h
@@ -23,8 +23,6 @@
#include <psa/crypto.h>
-#include "md_wrap.h"
-
/** Calculate the hash (digest) of a message using Mbed TLS routines.
*
* \note The signature of this function is that of a PSA driver hash_compute