From 0bb38336a5c47e4cf7f614e17c0c132819af0f48 Mon Sep 17 00:00:00 2001 From: Max Fillinger Date: Tue, 28 Dec 2021 16:32:00 +0100 Subject: [PATCH 1/3] Add function to get md info from md context Signed-off-by: Max Fillinger --- include/mbedtls/md.h | 14 ++++++++++++++ library/md.c | 9 +++++++++ tests/suites/test_suite_md.function | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 2b668f55e3..243e57ac61 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -137,6 +137,20 @@ 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. diff --git a/library/md.c b/library/md.c index a228789653..f2c1a90f86 100644 --- a/library/md.c +++ b/library/md.c @@ -227,6 +227,15 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type ) } } +const mbedtls_md_info_t *mbedtls_md_info_from_ctx( + const mbedtls_md_context_t *ctx ) +{ + if( ctx == NULL ) + return NULL; + + return( ctx->MBEDTLS_PRIVATE(md_info) ); +} + void mbedtls_md_init( mbedtls_md_context_t *ctx ) { memset( ctx, 0, sizeof( mbedtls_md_context_t ) ); diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index d918ce3b96..1f92a60af5 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -53,6 +53,8 @@ void md_null_args( ) TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL ); TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL ); + TEST_ASSERT( mbedtls_md_info_from_ctx( NULL ) == NULL ); + TEST_ASSERT( mbedtls_md_info_from_ctx( &ctx ) == NULL ); TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); @@ -202,6 +204,8 @@ void md_text_multi( char * text_md_name, char * text_src_string, TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); + TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info ); + TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx_copy ) == md_info ); TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) ); TEST_ASSERT ( ctx.md_ctx != NULL ); @@ -249,6 +253,8 @@ void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash ) TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); + TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info ); + TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx_copy ) == md_info ); halfway = src_str->len / 2; @@ -321,6 +327,7 @@ void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str, md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) ); + TEST_ASSERT ( mbedtls_md_info_from_ctx( &ctx ) == md_info ); halfway = src_str->len / 2; From 8737ec240765dcdd146547c0ce1b0149220a686d Mon Sep 17 00:00:00 2001 From: Max Fillinger Date: Tue, 28 Dec 2021 16:53:40 +0100 Subject: [PATCH 2/3] Add ChangeLog entry for md_info getter Signed-off-by: Max Fillinger --- ChangeLog.d/md_info_from_ctx.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ChangeLog.d/md_info_from_ctx.txt diff --git a/ChangeLog.d/md_info_from_ctx.txt b/ChangeLog.d/md_info_from_ctx.txt new file mode 100644 index 0000000000..f8ec1a0d5f --- /dev/null +++ b/ChangeLog.d/md_info_from_ctx.txt @@ -0,0 +1,3 @@ +Features + * Add a function to extract message digest information from a message + digest context. From 62e5514b8be7ae07dcd4b4e92280a01b652e5948 Mon Sep 17 00:00:00 2001 From: Max Fillinger Date: Tue, 28 Dec 2021 17:42:30 +0100 Subject: [PATCH 3/3] Remove extra newline Signed-off-by: Max Fillinger --- include/mbedtls/md.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 243e57ac61..612061d152 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -150,7 +150,6 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type ); 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.