Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define "MD light" subset of MD #7120

Merged
merged 11 commits into from
Mar 6, 2023
7 changes: 7 additions & 0 deletions include/mbedtls/build_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@
#include MBEDTLS_USER_CONFIG_FILE
#endif

/* Auto-enable MBEDTLS_MD_LIGHT based on MBEDTLS_MD_C.
* This allows checking for MD_LIGHT rather than MD_LIGHT || MD_C.
*/
#if defined(MBEDTLS_MD_C)
#define MBEDTLS_MD_LIGHT
#endif

/* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT
* is defined as well to include all PSA code.
*/
Expand Down
2 changes: 1 addition & 1 deletion include/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,7 @@
/**
* \def MBEDTLS_MD_C
*
* Enable the generic message digest layer.
* Enable the generic layer for message digest (hashing) and HMAC.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Enable the generic layer for message digest (hashing) and HMAC.
* Enable the generic layer for message digest (hashing) and HMAC.
* This will automatically enable `MBEDTLS_MD_LIGHT`.

*
* Requires: one of: MBEDTLS_MD5_C, MBEDTLS_RIPEMD160_C, MBEDTLS_SHA1_C,
* MBEDTLS_SHA224_C, MBEDTLS_SHA256_C, MBEDTLS_SHA384_C,
Expand Down
103 changes: 50 additions & 53 deletions include/mbedtls/md.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* \file md.h
*
* \brief This file contains the generic message-digest wrapper.
* \brief This file contains the generic functions for message-digest
* (hashing) and HMAC.
*
* \author Adriaan de Jong <dejong@fox-it.com>
*/
Expand Down Expand Up @@ -107,30 +108,6 @@ typedef struct mbedtls_md_context_t {
void *MBEDTLS_PRIVATE(hmac_ctx);
} mbedtls_md_context_t;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this line, but rather about this file:
Don't we want to guard the function declarations with MBEDTLS_MD_LIGHT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't guard function declarations in header files with the corresponding implementation macro. For example, before this PR they were not guarded with MBEDTLS_MD_C. Technically, the guards I'm adding wouldn't be necessary, but I think they're helpful to document what's part of MD-light and what's not.

/**
* \brief This function returns the list of digests supported by the
* generic digest module.
*
* \note The list starts with the strongest available hashes.
*
* \return A statically allocated array of digests. Each element
* in the returned list is an integer belonging to the
* message-digest enumeration #mbedtls_md_type_t.
* The last entry is 0.
*/
const int *mbedtls_md_list(void);

/**
* \brief This function returns the message-digest information
* associated with the given digest name.
*
* \param md_name The name of the digest to search for.
*
* \return The message-digest information associated with \p md_name.
* \return NULL if the associated message-digest information is not found.
*/
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);

/**
* \brief This function returns the message-digest information
* associated with the given digest type.
Expand All @@ -142,19 +119,6 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
*/
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);

/**
* \brief This function returns the message-digest information
* from the given context.
*
* \param ctx The context from which to extract the information.
* This must be initialized (or \c NULL).
*
* \return The message-digest information associated with \p ctx.
* \return \c NULL if \p ctx is \c NULL.
*/
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
const mbedtls_md_context_t *ctx);

/**
* \brief This function initializes a message-digest context without
* binding it to a particular message-digest algorithm.
Expand Down Expand Up @@ -248,17 +212,6 @@ unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
*/
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);

/**
* \brief This function extracts the message-digest name from the
* message-digest information structure.
*
* \param md_info The information structure of the message-digest algorithm
* to use.
*
* \return The name of the message digest.
*/
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);

/**
* \brief This function starts a message-digest computation.
*
Expand Down Expand Up @@ -337,6 +290,54 @@ MBEDTLS_CHECK_RETURN_TYPICAL
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output);

/**
* \brief This function returns the list of digests supported by the
* generic digest module.
*
* \note The list starts with the strongest available hashes.
*
* \return A statically allocated array of digests. Each element
* in the returned list is an integer belonging to the
* message-digest enumeration #mbedtls_md_type_t.
* The last entry is 0.
*/
const int *mbedtls_md_list(void);

/**
* \brief This function returns the message-digest information
* associated with the given digest name.
*
* \param md_name The name of the digest to search for.
*
* \return The message-digest information associated with \p md_name.
* \return NULL if the associated message-digest information is not found.
*/
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.
*
* \param md_info The information structure of the message-digest algorithm
* to use.
*
* \return The name of the message digest.
*/
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);

/**
* \brief This function returns the message-digest information
* from the given context.
*
* \param ctx The context from which to extract the information.
* This must be initialized (or \c NULL).
*
* \return The message-digest information associated with \p ctx.
* \return \c NULL if \p ctx is \c NULL.
*/
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
const mbedtls_md_context_t *ctx);

#if defined(MBEDTLS_FS_IO)
/**
* \brief This function calculates the message-digest checksum
Expand Down Expand Up @@ -471,10 +472,6 @@ int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key,
const unsigned char *input, size_t ilen,
unsigned char *output);

/* Internal use */
MBEDTLS_CHECK_RETURN_TYPICAL
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data);

#ifdef __cplusplus
}
#endif
Expand Down
Loading