From 52e36bc1a1d40d023f66cfec371200ac4a089d5d Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 5 Sep 2019 13:02:52 +0100 Subject: [PATCH] MD: Embed digest context structure into MD wrapper context --- include/mbedtls/md.h | 13 +++++++++++-- library/md.c | 11 ++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 277656884..942d1f53e 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -108,6 +108,8 @@ typedef int mbedtls_md_handle_t; #endif /* !MBEDTLS_MD_SINGLE_HASH */ +#include "md_internal.h" + /** * The generic message-digest context. */ @@ -118,11 +120,20 @@ typedef struct mbedtls_md_context_t mbedtls_md_handle_t md_info; #endif +#if !defined(MBEDTLS_MD_SINGLE_HASH) /** The digest-specific context. */ void *md_ctx; /** The HMAC part of the context. */ void *hmac_ctx; +#else + unsigned char md_ctx[ sizeof( MBEDTLS_MD_INFO_CTX_TYPE( + MBEDTLS_MD_SINGLE_HASH ) ) ]; + + unsigned char hmac_ctx[ 2 * MBEDTLS_MD_INFO_BLOCKSIZE( + MBEDTLS_MD_SINGLE_HASH ) ]; + +#endif /* MBEDTLS_MD_SINGLE_HASH */ } mbedtls_md_context_t; #if !defined(MBEDTLS_MD_SINGLE_HASH) @@ -140,8 +151,6 @@ static inline mbedtls_md_handle_t mbedtls_md_get_handle( } #endif /* !MBEDTLS_MD_SINGLE_HASH */ -#include "md_internal.h" - /** * \brief This function returns the list of digests supported by the * generic digest module. diff --git a/library/md.c b/library/md.c index 5e9c5b404..accf301ba 100644 --- a/library/md.c +++ b/library/md.c @@ -388,6 +388,11 @@ mbedtls_md_handle_t mbedtls_md_info_from_type( mbedtls_md_type_t md_type ) void mbedtls_md_init( mbedtls_md_context_t *ctx ) { memset( ctx, 0, sizeof( mbedtls_md_context_t ) ); + +#if defined(MBEDTLS_MD_SINGLE_HASH) + mbedtls_md_info_init( mbedtls_md_get_handle( ctx ), + ctx->md_ctx ); +#endif } void mbedtls_md_free( mbedtls_md_context_t *ctx ) @@ -395,6 +400,7 @@ void mbedtls_md_free( mbedtls_md_context_t *ctx ) if( ctx == NULL || mbedtls_md_get_handle( ctx ) == MBEDTLS_MD_INVALID_HANDLE ) return; +#if !defined(MBEDTLS_MD_SINGLE_HASH) if( ctx->md_ctx != NULL ) { mbedtls_md_info_ctx_free( mbedtls_md_get_handle( ctx ), ctx->md_ctx ); @@ -406,6 +412,7 @@ void mbedtls_md_free( mbedtls_md_context_t *ctx ) 2 * mbedtls_md_info_block_size( mbedtls_md_get_handle( ctx ) ) ); mbedtls_free( ctx->hmac_ctx ); } +#endif /* MBEDTLS_MD_SINGLE_HASH */ mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) ); } @@ -437,6 +444,7 @@ int mbedtls_md_setup( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info, in if( md_info == MBEDTLS_MD_INVALID_HANDLE || ctx == NULL ) return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); +#if !defined(MBEDTLS_MD_SINGLE_HASH) ctx->md_ctx = mbedtls_md_info_ctx_alloc( md_info ); if( ctx->md_ctx == NULL ) return( MBEDTLS_ERR_MD_ALLOC_FAILED ); @@ -452,8 +460,9 @@ int mbedtls_md_setup( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info, in } } -#if !defined(MBEDTLS_MD_SINGLE_HASH) ctx->md_info = md_info; +#else + ((void) hmac); #endif return( 0 );