From 0e9cddbf1af8b92912e45a0a356a7a75ea4f972f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 10 Dec 2018 16:37:51 +0100 Subject: [PATCH] Introduce generic validation macros Avoid duplicating source code for each module. --- include/mbedtls/aes.h | 16 ---------------- include/mbedtls/platform_util.h | 27 +++++++++++++++++++++++++++ library/aes.c | 16 +++++++++++----- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/include/mbedtls/aes.h b/include/mbedtls/aes.h index 62c1f9234..cfb20c4fc 100644 --- a/include/mbedtls/aes.h +++ b/include/mbedtls/aes.h @@ -67,22 +67,6 @@ /* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */ #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */ -#if defined( MBEDTLS_CHECK_PARAMS ) -#define MBEDTLS_AES_VALIDATE_RET( cond ) do{ if( !(cond) ) { \ - MBEDTLS_PARAM_FAILED( #cond ); \ - return MBEDTLS_ERR_AES_BAD_INPUT_DATA;} \ - } while(0); - -#define MBEDTLS_AES_VALIDATE( cond ) do{ if( !(cond) ) { \ - MBEDTLS_PARAM_FAILED( #cond ); \ - return; } \ - } while(0); -#else -/* No validation of parameters will be performed */ -#define MBEDTLS_AES_VALIDATE_RET( cond ) -#define MBEDTLS_AES_VALIDATE( cond) -#endif - #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ !defined(inline) && !defined(__cplusplus) #define inline __inline diff --git a/include/mbedtls/platform_util.h b/include/mbedtls/platform_util.h index ed21ffa91..1ac16d45f 100644 --- a/include/mbedtls/platform_util.h +++ b/include/mbedtls/platform_util.h @@ -81,6 +81,33 @@ void mbedtls_param_failed( const char *failure_condition, const char *file, int line ); #endif /* MBEDTLS_PARAM_FAILED */ + +/* Internal macro meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ + do { \ + if( !(cond) ) \ + { \ + MBEDTLS_PARAM_FAILED( #cond ); \ + return( ret ); \ + } \ + } while( 0 ) + +/* Internal macro meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE( cond ) \ + do { \ + if( !(cond) ) \ + { \ + MBEDTLS_PARAM_FAILED( #cond ); \ + return; \ + } \ + } while( 0 ) + +#else /* MBEDTLS_CHECK_PARAMS */ + +/* Internal macros meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) +#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) + #endif /* MBEDTLS_CHECK_PARAMS */ /** diff --git a/library/aes.c b/library/aes.c index 7a364a0f6..6ff39d74c 100644 --- a/library/aes.c +++ b/library/aes.c @@ -56,6 +56,12 @@ #if !defined(MBEDTLS_AES_ALT) +/* Parameter validation macros based on platform_util.h */ +#define AES_VALIDATE_RET( cond ) \ + MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_AES_BAD_INPUT_DATA) +#define AES_VALIDATE( cond ) \ + MBEDTLS_INTERNAL_VALIDATE( cond ) + /* * 32-bit integer manipulation macros (little endian) */ @@ -511,7 +517,7 @@ static void aes_gen_tables( void ) void mbedtls_aes_init( mbedtls_aes_context *ctx ) { - MBEDTLS_AES_VALIDATE( ctx != NULL ); + AES_VALIDATE( ctx != NULL ); memset( ctx, 0, sizeof( mbedtls_aes_context ) ); } @@ -527,7 +533,7 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx ) #if defined(MBEDTLS_CIPHER_MODE_XTS) void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ) { - MBEDTLS_AES_VALIDATE( ctx != NULL ); + AES_VALIDATE( ctx != NULL ); mbedtls_aes_init( &ctx->crypt ); mbedtls_aes_init( &ctx->tweak ); @@ -535,7 +541,7 @@ void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ) void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ) { - MBEDTLS_AES_VALIDATE( ctx != NULL ); + AES_VALIDATE( ctx != NULL ); mbedtls_aes_free( &ctx->crypt ); mbedtls_aes_free( &ctx->tweak ); @@ -552,7 +558,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, unsigned int i; uint32_t *RK; - MBEDTLS_AES_VALIDATE_RET( ctx != NULL && key != NULL ); + AES_VALIDATE_RET( ctx != NULL && key != NULL ); switch( keybits ) { @@ -670,7 +676,7 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, uint32_t *RK; uint32_t *SK; - MBEDTLS_AES_VALIDATE_RET( ctx != NULL && key != NULL ); + AES_VALIDATE_RET( ctx != NULL && key != NULL ); mbedtls_aes_init( &cty );