diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 220fbf92b7..e25fe57e58 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -27,6 +27,7 @@ set(src_crypto cipher.c cipher_wrap.c cmac.c + common.c ctr_drbg.c des.c dhm.c diff --git a/library/Makefile b/library/Makefile index 13b0b29348..66110166a2 100644 --- a/library/Makefile +++ b/library/Makefile @@ -84,6 +84,7 @@ OBJS_CRYPTO= \ cipher.o \ cipher_wrap.o \ cmac.o \ + common.o \ ctr_drbg.o \ des.o \ dhm.o \ diff --git a/library/common.c b/library/common.c index 9f901f006a..4273600c46 100644 --- a/library/common.c +++ b/library/common.c @@ -17,11 +17,20 @@ * limitations under the License. */ +#include "common.h" + +#include + #if defined(MBEDTLS_TEST_HOOKS) -void (*mbedtls_test_err_add_hook)( int, int, const char *, int ); -int mbedtls_err_add( int high, int low, const char *file, int line ) { - if( mbedtls_test_err_add_hook != NULL ) - (*mbedtls_test_err_add_hook)( high, low, file, line ); +static void (*err_add_hook)( int, int, const char *, int ); +void mbedtls_set_err_add_hook(void *hook) +{ + err_add_hook = hook; +} +int mbedtls_err_add( int high, int low, const char *file, int line ) +{ + if( err_add_hook != NULL ) + (*err_add_hook)( high, low, file, line ); return ( high + low ); } #endif diff --git a/library/common.h b/library/common.h index f4cef97b91..a9b6187b3c 100644 --- a/library/common.h +++ b/library/common.h @@ -48,12 +48,18 @@ #define MBEDTLS_STATIC_TESTABLE /** Helper macro and function to combine a high and low level error code. - * + * * This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive * testing of its inputs. This is used in the test infrastructure to report * on errors when combining two error codes of the same level (e.g: two high * or two low level errors). + * + * To set a hook use + * ``` + * mbedtls_set_err_add_hook(&mbedtls_check_foo); + * ``` */ +void mbedtls_set_err_add_hook( void *hook ); int mbedtls_err_add( int high, int low, const char *file, int line ); #define MBEDTLS_ERR_ADD( high, low ) \ ( mbedtls_err_add( high, low, __FILE__, __LINE__ ) ) diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index b35b1437f7..09927fe7b4 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -33,6 +33,10 @@ #include "psa/crypto.h" #endif /* MBEDTLS_USE_PSA_CRYPTO */ +#if defined(MBEDTLS_TEST_HOOKS) +#include "common.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 @@ -280,7 +284,7 @@ $platform_code int main( int argc, const char *argv[] ) { #if defined(MBEDTLS_TEST_HOOKS) - mbedtls_test_err_add_hook = &mbedtls_test_err_add_check; + mbedtls_set_err_add_hook( &mbedtls_test_err_add_check ); #endif int ret = mbedtls_test_platform_setup();