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_wrap.c
cmac.c
common.c
ctr_drbg.c
des.c
dhm.c

View file

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

View file

@ -17,11 +17,20 @@
* limitations under the License.
*/
#include "common.h"
#include <stddef.h>
#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

View file

@ -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__ ) )

View file

@ -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();