Add macro for error code addition

Adds a macro (`MBEDTLS_ERR_ADD`) to add error codes together and check that the
result will not be corrupted. This additional check is only enabled during
testing when `MBEDTLS_TEST_HOOKS` is defined.

Also includes a reference usage example in `rsa.c` where two high-level error
codes could be incorrectly added together under the right conditions. This now
ensures that when this error occurs during testing it will be correctly
reported.

Signed-off-by: Chris Jones <christopher.jones@arm.com>
This commit is contained in:
Chris Jones 2021-01-08 17:04:59 +00:00
parent 528b0a6b18
commit 96ae73b0ea
6 changed files with 49 additions and 1 deletions

View file

@ -278,4 +278,14 @@ void mbedtls_test_mutex_usage_init( void );
void mbedtls_test_mutex_usage_check( void );
#endif /* MBEDTLS_TEST_MUTEX_USAGE */
#if defined(MBEDTLS_TEST_HOOKS)
/**
* \brief Check that a pure high-level error code is being combined with a
* pure low-level error code as otherwise the resultant error code
* would be corrupted.
*/
void mbedtls_test_err_add_check( int high, int low,
const char *file, int line);
#endif
#endif /* TEST_HELPERS_H */

View file

@ -282,3 +282,16 @@ void mbedtls_param_failed( const char *failure_condition,
}
}
#endif /* MBEDTLS_CHECK_PARAMS */
#if defined(MBEDTLS_TEST_HOOKS)
void mbedtls_test_err_add_check( int high, int low,
const char *file, int line )
{
if ( high < -0x0FFF && low > -0x007F )
{
mbedtls_fprintf( stderr, "\nIncorrect error code addition at %s:%d\n",
file, line );
mbedtls_exit( 1 );
}
}
#endif /* MBEDTLS_TEST_HOOKS */

View file

@ -33,6 +33,10 @@
#include "psa/crypto.h"
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_TEST_HOOKS)
#include "mbedtls/error.h"
#endif
/* Test code may use deprecated identifiers only if the preprocessor symbol
* MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
* MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
@ -279,6 +283,10 @@ $platform_code
*/
int main( int argc, const char *argv[] )
{
#if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_err_add_hook = &mbedtls_test_err_add_check;
#endif
int ret = mbedtls_test_platform_setup();
if( ret != 0 )
{