Add function to get md info from md context
Signed-off-by: Max Fillinger <max@max-fillinger.net>
diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h
index 2b668f5..243e57a 100644
--- a/include/mbedtls/md.h
+++ b/include/mbedtls/md.h
@@ -138,6 +138,20 @@
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 a228789..f2c1a90 100644
--- a/library/md.c
+++ b/library/md.c
@@ -227,6 +227,15 @@
}
}
+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 d918ce3..1f92a60 100644
--- a/tests/suites/test_suite_md.function
+++ b/tests/suites/test_suite_md.function
@@ -53,6 +53,8 @@
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 @@
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 @@
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 @@
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;