md: Add parameter validation
Improve documentation on parameter constraints
diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h
index 8bcf766..e860de2 100644
--- a/include/mbedtls/md.h
+++ b/include/mbedtls/md.h
@@ -109,7 +109,7 @@
* \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.
+ * \param md_name The name of the digest to search for. This must not be \c NULL.
*
* \return The message-digest information associated with \p md_name.
* \return NULL if the associated message-digest information is not found.
@@ -120,7 +120,7 @@
* \brief This function returns the message-digest information
* associated with the given digest type.
*
- * \param md_type The type of digest to search for.
+ * \param md_type The type of digest to search for. This must not be \c NULL.
*
* \return The message-digest information associated with \p md_type.
* \return NULL if the associated message-digest information is not found.
@@ -133,7 +133,7 @@
*
* This function should always be called first. It prepares the
* context for mbedtls_md_setup() for binding it to a
- * message-digest algorithm.
+ * message-digest algorithm. The \p ctx must not be \c NULL.
*/
void mbedtls_md_init( mbedtls_md_context_t *ctx );
@@ -148,7 +148,7 @@
* Calling this function if you have previously
* called mbedtls_md_init() and nothing else is optional.
* You must not call this function if you have not called
- * mbedtls_md_init().
+ * mbedtls_md_init(). The \p ctx must not be \c NULL.
*/
void mbedtls_md_free( mbedtls_md_context_t *ctx );
@@ -167,7 +167,7 @@
*
* \deprecated Superseded by mbedtls_md_setup() in 2.0.0
*
- * \param ctx The context to set up.
+ * \param ctx The context to set up. This must not be \c NULL.
* \param md_info The information structure of the message-digest algorithm
* to use.
*
@@ -188,9 +188,9 @@
* mbedtls_md_free(). Makes it necessary to call
* mbedtls_md_free() later.
*
- * \param ctx The context to set up.
+ * \param ctx The context to set up. This must not be \c NULL.
* \param md_info The information structure of the message-digest algorithm
- * to use.
+ * to use. This must not be \c NULL.
* \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
* or non-zero: HMAC is used with this context.
*
@@ -214,8 +214,8 @@
* \warning This function clones the message-digest state, not the
* HMAC state.
*
- * \param dst The destination context.
- * \param src The context to be cloned.
+ * \param dst The destination context. This must not be \c NULL.
+ * \param src The context to be cloned. This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
@@ -228,7 +228,7 @@
* message-digest information structure.
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
+ * to use. This must not be \c NULL.
*
* \return The size of the message-digest output in Bytes.
*/
@@ -239,7 +239,7 @@
* message-digest information structure.
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
+ * to use. This must not be \c NULL.
*
* \return The type of the message digest.
*/
@@ -250,7 +250,7 @@
* message-digest information structure.
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
+ * to use. This must not be \c NULL.
*
* \return The name of the message digest.
*/
@@ -280,7 +280,7 @@
* Afterwards, call mbedtls_md_finish().
*
* \param ctx The generic message-digest context.
- * \param input The buffer holding the input data.
+ * \param input The buffer holding the input data. This must not be \c NULL.
* \param ilen The length of the input data.
*
* \return \c 0 on success.
@@ -302,6 +302,7 @@
*
* \param ctx The generic message-digest context.
* \param output The buffer for the generic message-digest checksum result.
+ * This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
@@ -318,10 +319,11 @@
* Output = message_digest(input buffer).
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
+ * to use. This must not be \c NULL.
* \param input The buffer holding the data.
* \param ilen The length of the input data.
* \param output The generic message-digest checksum result.
+ * This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
@@ -339,9 +341,10 @@
* Output = message_digest(file contents).
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
- * \param path The input file name.
+ * to use. This must not be \c NULL.
+ * \param path The input file name. This must not be \c NULL.
* \param output The generic message-digest checksum result.
+ * This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
@@ -363,7 +366,7 @@
*
* \param ctx The message digest context containing an embedded HMAC
* context.
- * \param key The HMAC secret key.
+ * \param key The HMAC secret key. This must not be \c NULL.
* \param keylen The length of the HMAC key in Bytes.
*
* \return \c 0 on success.
@@ -407,7 +410,7 @@
*
* \param ctx The message digest context containing an embedded HMAC
* context.
- * \param output The generic HMAC checksum result.
+ * \param output The generic HMAC checksum result. This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
@@ -424,7 +427,7 @@
* input.
*
* \param ctx The message digest context containing an embedded HMAC
- * context.
+ * context. This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
@@ -443,12 +446,12 @@
* output = generic HMAC(hmac key, input buffer).
*
* \param md_info The information structure of the message-digest algorithm
- * to use.
- * \param key The HMAC secret key.
+ * to use. This must not be \c NULL.
+ * \param key The HMAC secret key. This must not be \c NULL.
* \param keylen The length of the HMAC secret key in Bytes.
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
- * \param output The generic HMAC result.
+ * \param output The generic HMAC result. This must not be \c NULL.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
diff --git a/library/md.c b/library/md.c
index 303cdcb..821a5e8 100644
--- a/library/md.c
+++ b/library/md.c
@@ -49,6 +49,12 @@
#include <stdio.h>
#endif
+#define MBEDTLS_MD_VALIDATE_RET(cond) \
+ MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MD_BAD_INPUT_DATA )
+#define MBEDTLS_MD_VALIDATE_RET_NULL(cond) \
+ MBEDTLS_INTERNAL_VALIDATE_RET( cond, NULL )
+#define MBEDTLS_MD_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
+
/*
* Reminder: update profiles in x509_crt.c when adding a new hash!
*/
@@ -94,8 +100,7 @@
const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
{
- if( NULL == md_name )
- return( NULL );
+ MBEDTLS_MD_VALIDATE_RET_NULL( md_name != NULL );
/* Get the appropriate digest information */
#if defined(MBEDTLS_MD2_C)
@@ -176,6 +181,7 @@
void mbedtls_md_init( mbedtls_md_context_t *ctx )
{
+ MBEDTLS_MD_VALIDATE( ctx != NULL );
memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
}
@@ -200,12 +206,11 @@
int mbedtls_md_clone( mbedtls_md_context_t *dst,
const mbedtls_md_context_t *src )
{
- if( dst == NULL || dst->md_info == NULL ||
- src == NULL || src->md_info == NULL ||
- dst->md_info != src->md_info )
- {
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- }
+ MBEDTLS_MD_VALIDATE_RET( dst != NULL );
+ MBEDTLS_MD_VALIDATE_RET( src != NULL );
+ MBEDTLS_MD_VALIDATE_RET( dst->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( src->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( dst->md_info == src->md_info );
dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
@@ -221,8 +226,8 @@
int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
{
- if( md_info == NULL || ctx == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
@@ -244,24 +249,26 @@
int mbedtls_md_starts( mbedtls_md_context_t *ctx )
{
- if( ctx == NULL || ctx->md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
return( ctx->md_info->starts_func( ctx->md_ctx ) );
}
int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
{
- if( ctx == NULL || ctx->md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( input != NULL );
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
}
int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
{
- if( ctx == NULL || ctx->md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( output != NULL );
return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
}
@@ -269,8 +276,8 @@
int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output )
{
- if( md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( output != NULL );
return( md_info->digest_func( input, ilen, output ) );
}
@@ -284,8 +291,9 @@
mbedtls_md_context_t ctx;
unsigned char buf[1024];
- if( md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( path != NULL );
+ MBEDTLS_MD_VALIDATE_RET( output != NULL );
if( ( f = fopen( path, "rb" ) ) == NULL )
return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
@@ -323,8 +331,10 @@
unsigned char *ipad, *opad;
size_t i;
- if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( key != NULL );
if( keylen > (size_t) ctx->md_info->block_size )
{
@@ -365,8 +375,9 @@
int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
{
- if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
}
@@ -377,8 +388,10 @@
unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
unsigned char *opad;
- if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( output != NULL );
opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
@@ -400,8 +413,9 @@
int ret;
unsigned char *ipad;
- if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->hmac_ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
ipad = (unsigned char *) ctx->hmac_ctx;
@@ -419,8 +433,9 @@
mbedtls_md_context_t ctx;
int ret;
- if( md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( md_info != NULL );
+ MBEDTLS_MD_VALIDATE_RET( key != NULL );
+ MBEDTLS_MD_VALIDATE_RET( output != NULL );
mbedtls_md_init( &ctx );
@@ -442,32 +457,29 @@
int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
{
- if( ctx == NULL || ctx->md_info == NULL )
- return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ MBEDTLS_MD_VALIDATE_RET( ctx != NULL );
+ MBEDTLS_MD_VALIDATE_RET( ctx->md_info != NULL );
return( ctx->md_info->process_func( ctx->md_ctx, data ) );
}
unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
{
- if( md_info == NULL )
- return( 0 );
+ MBEDTLS_INTERNAL_VALIDATE_RET( md_info != NULL, 0 );
return md_info->size;
}
mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
{
- if( md_info == NULL )
- return( MBEDTLS_MD_NONE );
+ MBEDTLS_INTERNAL_VALIDATE_RET( md_info != NULL, MBEDTLS_MD_NONE );
return md_info->type;
}
const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
{
- if( md_info == NULL )
- return( NULL );
+ MBEDTLS_MD_VALIDATE_RET_NULL( md_info != NULL );
return md_info->name;
}
diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function
index 11cf88a..d72c038 100644
--- a/tests/suites/test_suite_md.function
+++ b/tests/suites/test_suite_md.function
@@ -39,7 +39,7 @@
}
/* END_CASE */
-/* BEGIN_CASE */
+/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void md_null_args( )
{
mbedtls_md_context_t ctx;
@@ -48,53 +48,46 @@
mbedtls_md_init( &ctx );
- TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
- TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
- TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
+ TEST_INVALID_PARAM( mbedtls_md_get_size( NULL ) == 0 );
+ TEST_INVALID_PARAM( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
+ TEST_INVALID_PARAM( mbedtls_md_get_name( NULL ) == NULL );
- TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
+ TEST_INVALID_PARAM( mbedtls_md_info_from_string( NULL ) == 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 );
+ TEST_INVALID_PARAM( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
#if defined(MBEDTLS_FS_IO)
- TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
#endif
- TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_starts( NULL, buf, 1 ) );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_starts( &ctx, buf, 1 ) );
- TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_update( NULL, buf, 1 ) );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_update( &ctx, buf, 1 ) );
- TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_finish( NULL, buf ) );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_finish( &ctx, buf ) );
- TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_reset( NULL ) );
+ TEST_INVALID_PARAM( mbedtls_md_hmac_reset( &ctx ) );
- TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
- == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf ) );
- TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
- TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
+ TEST_INVALID_PARAM( mbedtls_md_process( NULL, buf ) );
+ TEST_INVALID_PARAM( mbedtls_md_process( &ctx, buf ) );
/* Ok, this is not NULL arg but NULL return... */
TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );