Allow defining MD information structs through macros

In builds enabling only a single MD digest, we want to be able to
implement the MD info getter functions by returning compile-time
constants matching the fields of the MD info structures used so far.

To avoid information duplication hardening maintainability, this
commit introduces the possibility of providing the various aspects
of a particular digest implementation by defining macros
MBEDTLS_MD_INFO_DIGEST_FIELD (e.g. MBEDTLS_MD_INFO_SHA256_SIZE)
and to generate the corresponding mbedtls_md_info instance from
this set of macros, via the new macro MBEDTLS_MD_INFO().

This way, we'll be able to switch between MD info based builds
and single-digest builds without information duplication.
This commit is contained in:
Hanno Becker 2019-07-17 14:14:01 +01:00
parent 0e7fc3166d
commit 62a44d43b0
2 changed files with 73 additions and 14 deletions

View File

@ -40,6 +40,59 @@
extern "C" { extern "C" {
#endif #endif
/*
* Message-digest information macro definition
*/
/* SHA-256 */
#define MBEDTLS_MD_INFO_SHA256_TYPE MBEDTLS_MD_SHA256
#define MBEDTLS_MD_INFO_SHA256_NAME "SHA256"
#define MBEDTLS_MD_INFO_SHA256_SIZE 32
#define MBEDTLS_MD_INFO_SHA256_BLOCKSIZE 64
#define MBEDTLS_MD_INFO_SHA256_STARTS_FUNC sha256_starts_wrap
#define MBEDTLS_MD_INFO_SHA256_UPDATE_FUNC sha224_update_wrap
#define MBEDTLS_MD_INFO_SHA256_FINISH_FUNC sha224_finish_wrap
#define MBEDTLS_MD_INFO_SHA256_DIGEST_FUNC sha256_wrap
#define MBEDTLS_MD_INFO_SHA256_ALLOC_FUNC sha224_ctx_alloc
#define MBEDTLS_MD_INFO_SHA256_FREE_FUNC sha224_ctx_free
#define MBEDTLS_MD_INFO_SHA256_CLONE_FUNC sha224_clone_wrap
#define MBEDTLS_MD_INFO_SHA256_PROCESS_FUNC sha224_process_wrap
/*
* Helper macros to extract fields from ciphersuites.
*/
#define MBEDTLS_MD_INFO_TYPE_T( MD ) MD ## _TYPE
#define MBEDTLS_MD_INFO_NAME_T( MD ) MD ## _NAME
#define MBEDTLS_MD_INFO_SIZE_T( MD ) MD ## _SIZE
#define MBEDTLS_MD_INFO_BLOCKSIZE_T( MD ) MD ## _BLOCKSIZE
#define MBEDTLS_MD_INFO_STARTS_FUNC_T( MD ) MD ## _STARTS_FUNC
#define MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD ) MD ## _UPDATE_FUNC
#define MBEDTLS_MD_INFO_FINISH_FUNC_T( MD ) MD ## _FINISH_FUNC
#define MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD ) MD ## _DIGEST_FUNC
#define MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD ) MD ## _ALLOC_FUNC
#define MBEDTLS_MD_INFO_FREE_FUNC_T( MD ) MD ## _FREE_FUNC
#define MBEDTLS_MD_INFO_CLONE_FUNC_T( MD ) MD ## _CLONE_FUNC
#define MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD ) MD ## _PROCESS_FUNC
/* Wrapper around MBEDTLS_MD_INFO_XXX_T() which makes sure that
* the argument is macro-expanded before concatenated with the
* field name. This allows to call these macros as
* MBEDTLS_MD_INFO_XXX( MBEDTLS_SSL_CONF_SINGLE_HASH ).
* where MBEDTLS_SSL_CONF_SINGLE_HASH expands to MBEDTLS_MD_INFO_XXX. */
#define MBEDTLS_MD_INFO_TYPE( MD ) MBEDTLS_MD_INFO_TYPE_T( MD )
#define MBEDTLS_MD_INFO_NAME( MD ) MBEDTLS_MD_INFO_NAME_T( MD )
#define MBEDTLS_MD_INFO_SIZE( MD ) MBEDTLS_MD_INFO_SIZE_T( MD )
#define MBEDTLS_MD_INFO_BLOCKSIZE( MD ) MBEDTLS_MD_INFO_BLOCKSIZE_T( MD )
#define MBEDTLS_MD_INFO_STARTS_FUNC( MD ) MBEDTLS_MD_INFO_STARTS_FUNC_T( MD )
#define MBEDTLS_MD_INFO_UPDATE_FUNC( MD ) MBEDTLS_MD_INFO_UPDATE_FUNC_T( MD )
#define MBEDTLS_MD_INFO_FINISH_FUNC( MD ) MBEDTLS_MD_INFO_FINISH_FUNC_T( MD )
#define MBEDTLS_MD_INFO_DIGEST_FUNC( MD ) MBEDTLS_MD_INFO_DIGEST_FUNC_T( MD )
#define MBEDTLS_MD_INFO_ALLOC_FUNC( MD ) MBEDTLS_MD_INFO_ALLOC_FUNC_T( MD )
#define MBEDTLS_MD_INFO_FREE_FUNC( MD ) MBEDTLS_MD_INFO_FREE_FUNC_T( MD )
#define MBEDTLS_MD_INFO_CLONE_FUNC( MD ) MBEDTLS_MD_INFO_CLONE_FUNC_T( MD )
#define MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) MBEDTLS_MD_INFO_PROCESS_FUNC_T( MD )
/** /**
* Message digest information. * Message digest information.
* Allows message digest functions to be called in a generic way. * Allows message digest functions to be called in a generic way.
@ -98,6 +151,24 @@ struct mbedtls_md_info_t
mbedtls_md_process_func_t *process_func; mbedtls_md_process_func_t *process_func;
}; };
/**
* \brief This macro builds an instance of ::mbedtls_md_info_t
* from an \c MBEDTLS_MD_INFO_XXX identifier.
*/
#define MBEDTLS_MD_INFO( MD ) \
{ MBEDTLS_MD_INFO_TYPE( MD ), \
MBEDTLS_MD_INFO_NAME( MD ), \
MBEDTLS_MD_INFO_SIZE( MD ), \
MBEDTLS_MD_INFO_BLOCKSIZE( MD ), \
MBEDTLS_MD_INFO_STARTS_FUNC( MD ), \
MBEDTLS_MD_INFO_UPDATE_FUNC( MD ), \
MBEDTLS_MD_INFO_FINISH_FUNC( MD ), \
MBEDTLS_MD_INFO_DIGEST_FUNC( MD ), \
MBEDTLS_MD_INFO_ALLOC_FUNC( MD ), \
MBEDTLS_MD_INFO_FREE_FUNC( MD ), \
MBEDTLS_MD_INFO_CLONE_FUNC( MD ), \
MBEDTLS_MD_INFO_PROCESS_FUNC( MD ) }
/* /*
* Getter functions for MD info structure. * Getter functions for MD info structure.
*/ */

View File

@ -475,20 +475,8 @@ static int sha256_wrap( const unsigned char *input, size_t ilen,
return( mbedtls_sha256_ret( input, ilen, output, 0 ) ); return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
} }
const mbedtls_md_info_t mbedtls_sha256_info = { const mbedtls_md_info_t mbedtls_sha256_info =
MBEDTLS_MD_SHA256, MBEDTLS_MD_INFO( MBEDTLS_MD_INFO_SHA256 );
"SHA256",
32,
64,
sha256_starts_wrap,
sha224_update_wrap,
sha224_finish_wrap,
sha256_wrap,
sha224_ctx_alloc,
sha224_ctx_free,
sha224_clone_wrap,
sha224_process_wrap,
};
#endif /* MBEDTLS_SHA256_C */ #endif /* MBEDTLS_SHA256_C */