Skip to content

PSA tests: use a few common test macros #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions tests/suites/helpers.function
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ typedef struct data_tag
} \
} while( 0 )

/** Evaluate two expressions and fail the test case if they have different
* values.
*
* \param expr1 An expression to evaluate.
* \param expr2 The expected value of \p expr1. This can be any
* expression, but it is typically a constant.
*/
#define TEST_EQUAL( expr1, expr2 ) \
TEST_ASSERT( ( expr1 ) == ( expr2 ) )

/** Evaluate an expression and fail the test case if it returns an error.
*
* \param expr The expression to evaluate. This is typically a call
* to a \c psa_xxx function that returns a value of type
* #psa_status_t.
*/
#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )

/** Allocate memory dynamically and fail the test case if this fails.
*
* You must set \p pointer to \c NULL before calling this macro and
Expand Down Expand Up @@ -150,6 +168,58 @@ typedef struct data_tag
mbedtls_exit( 1 ); \
}

#if defined(__GNUC__)
/* Test if arg and &(arg)[0] have the same type. This is true if arg is
* an array but not if it's a pointer. */
#define IS_ARRAY_NOT_POINTER( arg ) \
( ! __builtin_types_compatible_p( __typeof__( arg ), \
__typeof__( &( arg )[0] ) ) )
#else
/* On platforms where we don't know how to implement this check,
* omit it. Oh well, a non-portable check is better than nothing. */
#define IS_ARRAY_NOT_POINTER( arg ) 1
#endif

/* A compile-time constant with the value 0. If `const_expr` is not a
* compile-time constant with a nonzero value, cause a compile-time error. */
#define STATIC_ASSERT_EXPR( const_expr ) \
( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
/* Return the scalar value `value` (possibly promoted). This is a compile-time
* constant if `value` is. `condition` must be a compile-time constant.
* If `condition` is false, arrange to cause a compile-time error. */
#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )

#define ARRAY_LENGTH_UNSAFE( array ) \
( sizeof( array ) / sizeof( *( array ) ) )
/** Return the number of elements of a static or stack array.
*
* \param array A value of array (not pointer) type.
*
* \return The number of elements of the array.
*/
#define ARRAY_LENGTH( array ) \
( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
ARRAY_LENGTH_UNSAFE( array ) ) )

/** Return the smaller of two values.
*
* \param x An integer-valued expression without side effects.
* \param y An integer-valued expression without side effects.
*
* \return The smaller of \p x and \p y.
*/
#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )

/** Return the larger of two values.
*
* \param x An integer-valued expression without side effects.
* \param y An integer-valued expression without side effects.
*
* \return The larger of \p x and \p y.
*/
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )

/*
* 32-bit integer manipulation macros (big endian)
*/
Expand Down
2,138 changes: 995 additions & 1,143 deletions tests/suites/test_suite_psa_crypto.function

Large diffs are not rendered by default.

27 changes: 11 additions & 16 deletions tests/suites/test_suite_psa_crypto_entropy.function
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
#include "mbedtls/entropy.h"
#include "mbedtls/entropy_poll.h"

/* MAX value support macro */
#if !defined(MAX)
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif

/* Calculating the minimum allowed entropy size in bytes */
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)

Expand Down Expand Up @@ -52,12 +47,12 @@ void validate_entropy_seed_injection( int seed_length_a,
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
status = mbedtls_psa_inject_entropy( seed, seed_length_a );
TEST_ASSERT( status == expected_status_a );
TEST_EQUAL( status, expected_status_a );
status = mbedtls_psa_inject_entropy( seed, seed_length_b );
TEST_ASSERT( status == expected_status_b );
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
TEST_ASSERT( psa_generate_random( output,
sizeof( output ) ) == PSA_SUCCESS );
TEST_EQUAL( status, expected_status_b );
PSA_ASSERT( psa_crypto_init( ) );
PSA_ASSERT( psa_generate_random( output,
sizeof( output ) ) );
TEST_ASSERT( memcmp( output, zeros, sizeof( output ) ) != 0 );
exit:
mbedtls_free( seed );
Expand All @@ -82,19 +77,19 @@ void run_entropy_inject_with_crypto_init( )
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
its_status = psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID );
TEST_ASSERT( its_status == PSA_ITS_SUCCESS );
TEST_EQUAL( its_status, PSA_ITS_SUCCESS );
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_ENTROPY );
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_ENTROPY );
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
mbedtls_psa_crypto_free( );
/* The seed is written by nv_seed callback functions therefore the injection will fail */
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
exit:
psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID );
mbedtls_psa_crypto_free( );
Expand Down
51 changes: 25 additions & 26 deletions tests/suites/test_suite_psa_crypto_hash.function
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
* END_DEPENDENCIES
*/

/* BEGIN_CASE */
/* BEGIN_CASE */
void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
{
psa_algorithm_t alg = alg_arg;
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
size_t actual_hash_length;
psa_hash_operation_t operation;

TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
PSA_ASSERT( psa_crypto_init( ) );

TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
TEST_ASSERT( psa_hash_update( &operation,
input->x, input->len ) == PSA_SUCCESS );
TEST_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ),
&actual_hash_length ) == PSA_SUCCESS );
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
PSA_ASSERT( psa_hash_update( &operation,
input->x, input->len ) );
PSA_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ),
&actual_hash_length ) );
ASSERT_COMPARE( expected_hash->x, expected_hash->len,
actual_hash, actual_hash_length );

Expand All @@ -45,15 +45,15 @@ void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
psa_algorithm_t alg = alg_arg;
psa_hash_operation_t operation;

TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
PSA_ASSERT( psa_crypto_init( ) );

TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
TEST_ASSERT( psa_hash_update( &operation,
input->x,
input->len ) == PSA_SUCCESS );
TEST_ASSERT( psa_hash_verify( &operation,
expected_hash->x,
expected_hash->len ) == PSA_SUCCESS );
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
PSA_ASSERT( psa_hash_update( &operation,
input->x,
input->len ) );
PSA_ASSERT( psa_hash_verify( &operation,
expected_hash->x,
expected_hash->len ) );

exit:
mbedtls_psa_crypto_free( );
Expand All @@ -69,22 +69,21 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
psa_hash_operation_t operation;
uint32_t len = 0;

TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
PSA_ASSERT( psa_crypto_init( ) );

do
{
memset( actual_hash, 0, sizeof( actual_hash ) );
TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
PSA_ASSERT( psa_hash_setup( &operation, alg ) );

TEST_ASSERT( psa_hash_update( &operation,
input->x, len ) == PSA_SUCCESS );
TEST_ASSERT( psa_hash_update( &operation,
input->x + len, input->len - len ) ==
PSA_SUCCESS );
PSA_ASSERT( psa_hash_update( &operation,
input->x, len ) );
PSA_ASSERT( psa_hash_update( &operation,
input->x + len, input->len - len ) );

TEST_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ),
&actual_hash_length ) == PSA_SUCCESS );
PSA_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ),
&actual_hash_length ) );

ASSERT_COMPARE( expected_hash->x, expected_hash->len,
actual_hash, actual_hash_length );
Expand Down
47 changes: 19 additions & 28 deletions tests/suites/test_suite_psa_crypto_init.function
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#include "mbedtls/entropy.h"
#include "mbedtls/entropy_poll.h"

#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )

#define ENTROPY_MIN_NV_SEED_SIZE \
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)

Expand Down Expand Up @@ -142,9 +139,9 @@ void init_deinit( int count )
for( i = 0; i < count; i++ )
{
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
mbedtls_psa_crypto_free( );
}
}
Expand All @@ -156,7 +153,7 @@ void deinit_without_init( int count )
int i;
for( i = 0; i < count; i++ )
{
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
PSA_ASSERT( psa_crypto_init( ) );
mbedtls_psa_crypto_free( );
}
mbedtls_psa_crypto_free( );
Expand All @@ -172,11 +169,11 @@ void validate_module_init_generate_random( int count )
for( i = 0; i < count; i++ )
{
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
mbedtls_psa_crypto_free( );
}
status = psa_generate_random( random, sizeof( random ) );
TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
}
/* END_CASE */

Expand All @@ -189,11 +186,11 @@ void validate_module_init_key_based( int count )
for( i = 0; i < count; i++ )
{
status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS );
PSA_ASSERT( status );
mbedtls_psa_crypto_free( );
}
status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
}
/* END_CASE */

Expand All @@ -204,16 +201,14 @@ void custom_entropy_sources( int sources_arg, int expected_init_status_arg )
uint8_t random[10] = { 0 };

custom_entropy_sources_mask = sources_arg;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) ==
PSA_SUCCESS );
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) );

TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS )
goto exit;

TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
PSA_SUCCESS );
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );

exit:
mbedtls_psa_crypto_free( );
Expand Down Expand Up @@ -246,16 +241,14 @@ void fake_entropy_source( int threshold,
fake_entropy_state.length_sequence = lengths;

custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) ==
PSA_SUCCESS );
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) );

TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS )
goto exit;

TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
PSA_SUCCESS );
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );

exit:
mbedtls_psa_crypto_free( );
Expand All @@ -275,16 +268,14 @@ void entropy_from_nv_seed( int seed_size_arg,
TEST_ASSERT( mbedtls_nv_seed_write( seed, seed_size ) >= 0 );

custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) ==
PSA_SUCCESS );
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) );

TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS )
goto exit;

TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
PSA_SUCCESS );
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );

exit:
mbedtls_free( seed );
Expand Down
Loading