Fix building with MBEDTLS_TEST_HOOKS enabled

Fix building by adding `common.c` to the build scripts (both make and Cmake).

Also reworks the hook function pointer (also renamed to `err_add_hook`) to be
a static local to `common.c` with a setter function to set the pointer to a
checking function.

Signed-off-by: Chris Jones <christopher.jones@arm.com>
This commit is contained in:
Chris Jones 2021-01-13 12:33:36 +00:00
parent 5e8805afeb
commit 808b7c8a8a
5 changed files with 27 additions and 6 deletions

View File

@ -27,6 +27,7 @@ set(src_crypto
cipher.c cipher.c
cipher_wrap.c cipher_wrap.c
cmac.c cmac.c
common.c
ctr_drbg.c ctr_drbg.c
des.c des.c
dhm.c dhm.c

View File

@ -84,6 +84,7 @@ OBJS_CRYPTO= \
cipher.o \ cipher.o \
cipher_wrap.o \ cipher_wrap.o \
cmac.o \ cmac.o \
common.o \
ctr_drbg.o \ ctr_drbg.o \
des.o \ des.o \
dhm.o \ dhm.o \

View File

@ -17,11 +17,20 @@
* limitations under the License. * limitations under the License.
*/ */
#include "common.h"
#include <stddef.h>
#if defined(MBEDTLS_TEST_HOOKS) #if defined(MBEDTLS_TEST_HOOKS)
void (*mbedtls_test_err_add_hook)( int, int, const char *, int ); static void (*err_add_hook)( int, int, const char *, int );
int mbedtls_err_add( int high, int low, const char *file, int line ) { void mbedtls_set_err_add_hook(void *hook)
if( mbedtls_test_err_add_hook != NULL ) {
(*mbedtls_test_err_add_hook)( high, low, file, line ); 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 ); return ( high + low );
} }
#endif #endif

View File

@ -48,12 +48,18 @@
#define MBEDTLS_STATIC_TESTABLE #define MBEDTLS_STATIC_TESTABLE
/** Helper macro and function to combine a high and low level error code. /** 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 * 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 * 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 * on errors when combining two error codes of the same level (e.g: two high
* or two low level errors). * 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 ); int mbedtls_err_add( int high, int low, const char *file, int line );
#define MBEDTLS_ERR_ADD( high, low ) \ #define MBEDTLS_ERR_ADD( high, low ) \
( mbedtls_err_add( high, low, __FILE__, __LINE__ ) ) ( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )

View File

@ -33,6 +33,10 @@
#include "psa/crypto.h" #include "psa/crypto.h"
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #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 /* Test code may use deprecated identifiers only if the preprocessor symbol
* MBEDTLS_TEST_DEPRECATED is defined. When building tests, set * MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
* MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is * MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
@ -280,7 +284,7 @@ $platform_code
int main( int argc, const char *argv[] ) int main( int argc, const char *argv[] )
{ {
#if defined(MBEDTLS_TEST_HOOKS) #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 #endif
int ret = mbedtls_test_platform_setup(); int ret = mbedtls_test_platform_setup();