Merge pull request #2 from gilles-peskine-arm/psa-test_macros

PSA tests: use a few common test macros
This commit is contained in:
Jaeden Amero 2019-01-02 10:19:55 +00:00 committed by GitHub
commit c9a0722855
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 1273 additions and 1375 deletions

View file

@ -90,6 +90,24 @@ typedef struct data_tag
} \ } \
} while( 0 ) } 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. /** Allocate memory dynamically and fail the test case if this fails.
* *
* You must set \p pointer to \c NULL before calling this macro and * You must set \p pointer to \c NULL before calling this macro and
@ -150,6 +168,58 @@ typedef struct data_tag
mbedtls_exit( 1 ); \ 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) * 32-bit integer manipulation macros (big endian)
*/ */

File diff suppressed because it is too large Load diff

View file

@ -6,11 +6,6 @@
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/entropy_poll.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 */ /* Calculating the minimum allowed entropy size in bytes */
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE) #define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
@ -52,12 +47,12 @@ void validate_entropy_seed_injection( int seed_length_a,
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) || TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) ); ( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
status = mbedtls_psa_inject_entropy( seed, seed_length_a ); 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 ); status = mbedtls_psa_inject_entropy( seed, seed_length_b );
TEST_ASSERT( status == expected_status_b ); TEST_EQUAL( status, expected_status_b );
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init( ) );
TEST_ASSERT( psa_generate_random( output, PSA_ASSERT( psa_generate_random( output,
sizeof( output ) ) == PSA_SUCCESS ); sizeof( output ) ) );
TEST_ASSERT( memcmp( output, zeros, sizeof( output ) ) != 0 ); TEST_ASSERT( memcmp( output, zeros, sizeof( output ) ) != 0 );
exit: exit:
mbedtls_free( seed ); mbedtls_free( seed );
@ -82,19 +77,19 @@ void run_entropy_inject_with_crypto_init( )
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) || TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) ); ( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) ); 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 ); 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( ); 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 ) ); status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
status = psa_crypto_init( ); status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
/* The seed is written by nv_seed callback functions therefore the injection will fail */ /* The seed is written by nv_seed callback functions therefore the injection will fail */
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) ); status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
exit: exit:
psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID ); psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );

View file

@ -15,7 +15,7 @@
* END_DEPENDENCIES * END_DEPENDENCIES
*/ */
/* BEGIN_CASE */ /* BEGIN_CASE */
void hash_finish( int alg_arg, data_t *input, data_t *expected_hash ) void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
{ {
psa_algorithm_t alg = alg_arg; psa_algorithm_t alg = alg_arg;
@ -23,14 +23,14 @@ void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
size_t actual_hash_length; size_t actual_hash_length;
psa_hash_operation_t operation; 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 ); PSA_ASSERT( psa_hash_setup( &operation, alg ) );
TEST_ASSERT( psa_hash_update( &operation, PSA_ASSERT( psa_hash_update( &operation,
input->x, input->len ) == PSA_SUCCESS ); input->x, input->len ) );
TEST_ASSERT( psa_hash_finish( &operation, PSA_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ), actual_hash, sizeof( actual_hash ),
&actual_hash_length ) == PSA_SUCCESS ); &actual_hash_length ) );
ASSERT_COMPARE( expected_hash->x, expected_hash->len, ASSERT_COMPARE( expected_hash->x, expected_hash->len,
actual_hash, actual_hash_length ); actual_hash, actual_hash_length );
@ -45,15 +45,15 @@ void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
psa_algorithm_t alg = alg_arg; psa_algorithm_t alg = alg_arg;
psa_hash_operation_t operation; 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 ); PSA_ASSERT( psa_hash_setup( &operation, alg ) );
TEST_ASSERT( psa_hash_update( &operation, PSA_ASSERT( psa_hash_update( &operation,
input->x, input->x,
input->len ) == PSA_SUCCESS ); input->len ) );
TEST_ASSERT( psa_hash_verify( &operation, PSA_ASSERT( psa_hash_verify( &operation,
expected_hash->x, expected_hash->x,
expected_hash->len ) == PSA_SUCCESS ); expected_hash->len ) );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -69,22 +69,21 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
psa_hash_operation_t operation; psa_hash_operation_t operation;
uint32_t len = 0; uint32_t len = 0;
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init( ) );
do do
{ {
memset( actual_hash, 0, sizeof( actual_hash ) ); 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, PSA_ASSERT( psa_hash_update( &operation,
input->x, len ) == PSA_SUCCESS ); input->x, len ) );
TEST_ASSERT( psa_hash_update( &operation, PSA_ASSERT( psa_hash_update( &operation,
input->x + len, input->len - len ) == input->x + len, input->len - len ) );
PSA_SUCCESS );
TEST_ASSERT( psa_hash_finish( &operation, PSA_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ), actual_hash, sizeof( actual_hash ),
&actual_hash_length ) == PSA_SUCCESS ); &actual_hash_length ) );
ASSERT_COMPARE( expected_hash->x, expected_hash->len, ASSERT_COMPARE( expected_hash->x, expected_hash->len,
actual_hash, actual_hash_length ); actual_hash, actual_hash_length );

View file

@ -12,9 +12,6 @@
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/entropy_poll.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 \ #define ENTROPY_MIN_NV_SEED_SIZE \
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE) MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
@ -142,9 +139,9 @@ void init_deinit( int count )
for( i = 0; i < count; i++ ) for( i = 0; i < count; i++ )
{ {
status = psa_crypto_init( ); status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
status = psa_crypto_init( ); status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
} }
} }
@ -156,7 +153,7 @@ void deinit_without_init( int count )
int i; int i;
for( i = 0; i < count; 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( );
} }
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -172,11 +169,11 @@ void validate_module_init_generate_random( int count )
for( i = 0; i < count; i++ ) for( i = 0; i < count; i++ )
{ {
status = psa_crypto_init( ); status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
} }
status = psa_generate_random( random, sizeof( random ) ); status = psa_generate_random( random, sizeof( random ) );
TEST_ASSERT( status == PSA_ERROR_BAD_STATE ); TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
} }
/* END_CASE */ /* END_CASE */
@ -189,11 +186,11 @@ void validate_module_init_key_based( int count )
for( i = 0; i < count; i++ ) for( i = 0; i < count; i++ )
{ {
status = psa_crypto_init( ); status = psa_crypto_init( );
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
} }
status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) ); 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 */ /* END_CASE */
@ -204,16 +201,14 @@ void custom_entropy_sources( int sources_arg, int expected_init_status_arg )
uint8_t random[10] = { 0 }; uint8_t random[10] = { 0 };
custom_entropy_sources_mask = sources_arg; custom_entropy_sources_mask = sources_arg;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources( PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) == custom_entropy_init, mbedtls_entropy_free ) );
PSA_SUCCESS );
TEST_ASSERT( psa_crypto_init( ) == expected_init_status ); TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS ) if( expected_init_status != PSA_SUCCESS )
goto exit; goto exit;
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) == PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
PSA_SUCCESS );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -246,16 +241,14 @@ void fake_entropy_source( int threshold,
fake_entropy_state.length_sequence = lengths; fake_entropy_state.length_sequence = lengths;
custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE; custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources( PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) == custom_entropy_init, mbedtls_entropy_free ) );
PSA_SUCCESS );
TEST_ASSERT( psa_crypto_init( ) == expected_init_status ); TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS ) if( expected_init_status != PSA_SUCCESS )
goto exit; goto exit;
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) == PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
PSA_SUCCESS );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -275,16 +268,14 @@ void entropy_from_nv_seed( int seed_size_arg,
TEST_ASSERT( mbedtls_nv_seed_write( seed, seed_size ) >= 0 ); TEST_ASSERT( mbedtls_nv_seed_write( seed, seed_size ) >= 0 );
custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED; custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED;
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources( PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
custom_entropy_init, mbedtls_entropy_free ) == custom_entropy_init, mbedtls_entropy_free ) );
PSA_SUCCESS );
TEST_ASSERT( psa_crypto_init( ) == expected_init_status ); TEST_EQUAL( psa_crypto_init( ), expected_init_status );
if( expected_init_status != PSA_SUCCESS ) if( expected_init_status != PSA_SUCCESS )
goto exit; goto exit;
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) == PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
PSA_SUCCESS );
exit: exit:
mbedtls_free( seed ); mbedtls_free( seed );

View file

@ -46,7 +46,7 @@
#define KEY_TYPE_IS_DSA ( 1u << 5 ) #define KEY_TYPE_IS_DSA ( 1u << 5 )
#define KEY_TYPE_IS_ECC ( 1u << 6 ) #define KEY_TYPE_IS_ECC ( 1u << 6 )
#define TEST_CLASSIFICATION_MACRO( flag, alg, flags ) \ #define TEST_CLASSIFICATION_MACRO( flag, alg, flags ) \
TEST_ASSERT( PSA_##flag( alg ) == !! ( ( flags ) & flag ) ) TEST_ASSERT( PSA_##flag( alg ) == !! ( ( flags ) & flag ) )
void algorithm_classification( psa_algorithm_t alg, unsigned flags ) void algorithm_classification( psa_algorithm_t alg, unsigned flags )
@ -83,15 +83,15 @@ void key_type_classification( psa_key_type_t type, unsigned flags )
TEST_CLASSIFICATION_MACRO( KEY_TYPE_IS_ECC, type, flags ); TEST_CLASSIFICATION_MACRO( KEY_TYPE_IS_ECC, type, flags );
/* Macros with derived semantics */ /* Macros with derived semantics */
TEST_ASSERT( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) == TEST_EQUAL( PSA_KEY_TYPE_IS_ASYMMETRIC( type ),
( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) || ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ||
PSA_KEY_TYPE_IS_KEYPAIR( type ) ) ); PSA_KEY_TYPE_IS_KEYPAIR( type ) ) );
TEST_ASSERT( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) == TEST_EQUAL( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ),
( PSA_KEY_TYPE_IS_ECC( type ) && ( PSA_KEY_TYPE_IS_ECC( type ) &&
PSA_KEY_TYPE_IS_KEYPAIR( type ) ) ); PSA_KEY_TYPE_IS_KEYPAIR( type ) ) );
TEST_ASSERT( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) == TEST_EQUAL( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ),
( PSA_KEY_TYPE_IS_ECC( type ) && ( PSA_KEY_TYPE_IS_ECC( type ) &&
PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) ); PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) );
exit: ; exit: ;
} }
@ -113,7 +113,7 @@ void mac_algorithm_core( psa_algorithm_t alg, int classification_flags,
algorithm_classification( alg, classification_flags ); algorithm_classification( alg, classification_flags );
/* Length */ /* Length */
TEST_ASSERT( length == PSA_MAC_FINAL_SIZE( key_type, key_bits, alg ) ); TEST_EQUAL( length, PSA_MAC_FINAL_SIZE( key_type, key_bits, alg ) );
exit: ; exit: ;
} }
@ -134,7 +134,7 @@ void aead_algorithm_core( psa_algorithm_t alg, int classification_flags,
algorithm_classification( alg, classification_flags ); algorithm_classification( alg, classification_flags );
/* Tag length */ /* Tag length */
TEST_ASSERT( tag_length == PSA_AEAD_TAG_LENGTH( alg ) ); TEST_EQUAL( tag_length, PSA_AEAD_TAG_LENGTH( alg ) );
exit: ; exit: ;
} }
@ -174,18 +174,18 @@ void hash_algorithm( int alg_arg, int length_arg )
algorithm_classification( alg, 0 ); algorithm_classification( alg, 0 );
/* Dependent algorithms */ /* Dependent algorithms */
TEST_ASSERT( PSA_ALG_HMAC_GET_HASH( hmac_alg ) == alg ); TEST_EQUAL( PSA_ALG_HMAC_GET_HASH( hmac_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( rsa_pkcs1v15_sign_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( rsa_pkcs1v15_sign_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( rsa_pss_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( rsa_pss_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( dsa_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( dsa_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( deterministic_dsa_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( deterministic_dsa_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( ecdsa_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( ecdsa_alg ), alg );
TEST_ASSERT( PSA_ALG_SIGN_GET_HASH( deterministic_ecdsa_alg ) == alg ); TEST_EQUAL( PSA_ALG_SIGN_GET_HASH( deterministic_ecdsa_alg ), alg );
TEST_ASSERT( PSA_ALG_RSA_OAEP_GET_HASH( rsa_oaep_alg ) == alg ); TEST_EQUAL( PSA_ALG_RSA_OAEP_GET_HASH( rsa_oaep_alg ), alg );
TEST_ASSERT( PSA_ALG_HKDF_GET_HASH( hkdf_alg ) == alg ); TEST_EQUAL( PSA_ALG_HKDF_GET_HASH( hkdf_alg ), alg );
/* Hash length */ /* Hash length */
TEST_ASSERT( length == PSA_HASH_SIZE( alg ) ); TEST_EQUAL( length, PSA_HASH_SIZE( alg ) );
TEST_ASSERT( length <= PSA_HASH_MAX_SIZE ); TEST_ASSERT( length <= PSA_HASH_MAX_SIZE );
} }
/* END_CASE */ /* END_CASE */
@ -203,7 +203,7 @@ void mac_algorithm( int alg_arg, int classification_flags,
mac_algorithm_core( alg, classification_flags, mac_algorithm_core( alg, classification_flags,
key_type, key_bits, length ); key_type, key_bits, length );
TEST_ASSERT( PSA_ALG_FULL_LENGTH_MAC( alg ) == alg ); TEST_EQUAL( PSA_ALG_FULL_LENGTH_MAC( alg ), alg );
TEST_ASSERT( length <= PSA_MAC_MAX_SIZE ); TEST_ASSERT( length <= PSA_MAC_MAX_SIZE );
/* Truncated versions */ /* Truncated versions */
@ -212,16 +212,16 @@ void mac_algorithm( int alg_arg, int classification_flags,
psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC( alg, n ); psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC( alg, n );
mac_algorithm_core( truncated_alg, classification_flags, mac_algorithm_core( truncated_alg, classification_flags,
key_type, key_bits, n ); key_type, key_bits, n );
TEST_ASSERT( PSA_ALG_FULL_LENGTH_MAC( truncated_alg ) == alg ); TEST_EQUAL( PSA_ALG_FULL_LENGTH_MAC( truncated_alg ), alg );
/* Check that calling PSA_ALG_TRUNCATED_MAC twice gives the length /* Check that calling PSA_ALG_TRUNCATED_MAC twice gives the length
* of the outer truncation (even if the outer length is smaller than * of the outer truncation (even if the outer length is smaller than
* the inner length). */ * the inner length). */
TEST_ASSERT( PSA_ALG_TRUNCATED_MAC( truncated_alg, 1 ) == TEST_EQUAL( PSA_ALG_TRUNCATED_MAC( truncated_alg, 1 ),
PSA_ALG_TRUNCATED_MAC( alg, 1 ) ); PSA_ALG_TRUNCATED_MAC( alg, 1 ) );
TEST_ASSERT( PSA_ALG_TRUNCATED_MAC( truncated_alg, length - 1 ) == TEST_EQUAL( PSA_ALG_TRUNCATED_MAC( truncated_alg, length - 1 ),
PSA_ALG_TRUNCATED_MAC( alg, length - 1) ); PSA_ALG_TRUNCATED_MAC( alg, length - 1) );
TEST_ASSERT( PSA_ALG_TRUNCATED_MAC( truncated_alg, length ) == TEST_EQUAL( PSA_ALG_TRUNCATED_MAC( truncated_alg, length ),
PSA_ALG_TRUNCATED_MAC( alg, length ) ); PSA_ALG_TRUNCATED_MAC( alg, length ) );
} }
} }
/* END_CASE */ /* END_CASE */
@ -238,7 +238,7 @@ void hmac_algorithm( int alg_arg,
size_t n; size_t n;
TEST_ASSERT( PSA_ALG_IS_HASH( hash_alg ) ); TEST_ASSERT( PSA_ALG_IS_HASH( hash_alg ) );
TEST_ASSERT( PSA_ALG_HMAC( hash_alg ) == alg ); TEST_EQUAL( PSA_ALG_HMAC( hash_alg ), alg );
TEST_ASSERT( block_size <= PSA_HMAC_MAX_HASH_BLOCK_SIZE ); TEST_ASSERT( block_size <= PSA_HMAC_MAX_HASH_BLOCK_SIZE );
@ -248,7 +248,7 @@ void hmac_algorithm( int alg_arg,
for( n = 1; n <= length; n++ ) for( n = 1; n <= length; n++ )
{ {
psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC( alg, n ); psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC( alg, n );
TEST_ASSERT( PSA_ALG_HMAC_GET_HASH( truncated_alg ) == hash_alg ); TEST_EQUAL( PSA_ALG_HMAC_GET_HASH( truncated_alg ), hash_alg );
} }
} }
/* END_CASE */ /* END_CASE */
@ -287,20 +287,17 @@ void aead_algorithm( int alg_arg, int classification_flags,
{ {
psa_algorithm_t truncated_alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, n ); psa_algorithm_t truncated_alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, n );
aead_algorithm_core( truncated_alg, classification_flags, n ); aead_algorithm_core( truncated_alg, classification_flags, n );
TEST_ASSERT( TEST_EQUAL( PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH( truncated_alg ),
PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH( truncated_alg ) == alg ); alg );
/* Check that calling PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH twice gives /* Check that calling PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH twice gives
* the length of the outer truncation (even if the outer length is * the length of the outer truncation (even if the outer length is
* smaller than the inner length). */ * smaller than the inner length). */
TEST_ASSERT( TEST_EQUAL( PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, 1 ),
PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, 1 ) == PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 1 ) );
PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 1 ) ); TEST_EQUAL( PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, tag_length - 1 ),
TEST_ASSERT( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, tag_length - 1) );
PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, tag_length - 1 ) == TEST_EQUAL( PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, tag_length ),
PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, tag_length - 1) ); PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, tag_length ) );
TEST_ASSERT(
PSA_ALG_AEAD_WITH_TAG_LENGTH( truncated_alg, tag_length ) ==
PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, tag_length ) );
} }
} }
/* END_CASE */ /* END_CASE */
@ -363,8 +360,8 @@ void key_derivation_algorithm( int alg_arg, int classification_flags )
/* Check combinations with key agreements */ /* Check combinations with key agreements */
TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_FFDH( alg ) ) ); TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_FFDH( alg ) ) );
TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_ECDH( alg ) ) ); TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_ECDH( alg ) ) );
TEST_ASSERT( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_ECDH( alg ) ) == alg ); TEST_EQUAL( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_ECDH( alg ) ), alg );
TEST_ASSERT( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_FFDH( alg ) ) == alg ); TEST_EQUAL( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_FFDH( alg ) ), alg );
} }
/* END_CASE */ /* END_CASE */
@ -388,8 +385,8 @@ void key_selection_algorithm( int alg_arg, int classification_flags )
/* Check combinations with key agreements */ /* Check combinations with key agreements */
TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_FFDH( alg ) ) ); TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_FFDH( alg ) ) );
TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_ECDH( alg ) ) ); TEST_ASSERT( PSA_ALG_IS_KEY_AGREEMENT( PSA_ALG_ECDH( alg ) ) );
TEST_ASSERT( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_ECDH( alg ) ) == alg ); TEST_EQUAL( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_ECDH( alg ) ), alg );
TEST_ASSERT( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_FFDH( alg ) ) == alg ); TEST_EQUAL( PSA_ALG_KEY_AGREEMENT_GET_KDF( PSA_ALG_FFDH( alg ) ), alg );
} }
/* END_CASE */ /* END_CASE */
@ -416,7 +413,7 @@ void key_agreement_algorithm( int alg_arg, int classification_flags,
/* Shared secret derivation properties */ /* Shared secret derivation properties */
TEST_ASSERT( PSA_ALG_IS_KEY_DERIVATION( actual_post_alg ) || TEST_ASSERT( PSA_ALG_IS_KEY_DERIVATION( actual_post_alg ) ||
PSA_ALG_IS_KEY_SELECTION( actual_post_alg ) ); PSA_ALG_IS_KEY_SELECTION( actual_post_alg ) );
TEST_ASSERT( actual_post_alg == expected_post_alg ); TEST_EQUAL( actual_post_alg, expected_post_alg );
} }
/* END_CASE */ /* END_CASE */
@ -431,22 +428,22 @@ void key_type( int type_arg, int classification_flags )
if( classification_flags & KEY_TYPE_IS_PUBLIC_KEY ) if( classification_flags & KEY_TYPE_IS_PUBLIC_KEY )
{ {
psa_key_type_t pair_type = PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( type ); psa_key_type_t pair_type = PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( type );
TEST_ASSERT( PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( pair_type ) == type ); TEST_EQUAL( PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( pair_type ), type );
key_type_classification( pair_type, key_type_classification( pair_type,
( classification_flags ( classification_flags
& ~KEY_TYPE_IS_PUBLIC_KEY ) & ~KEY_TYPE_IS_PUBLIC_KEY )
| KEY_TYPE_IS_KEYPAIR ); | KEY_TYPE_IS_KEYPAIR );
TEST_ASSERT( PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ) == type ); TEST_EQUAL( PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ), type );
} }
if( classification_flags & KEY_TYPE_IS_KEYPAIR ) if( classification_flags & KEY_TYPE_IS_KEYPAIR )
{ {
psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
TEST_ASSERT( PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( public_type ) == type ); TEST_EQUAL( PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( public_type ), type );
key_type_classification( public_type, key_type_classification( public_type,
( classification_flags ( classification_flags
& ~KEY_TYPE_IS_KEYPAIR ) & ~KEY_TYPE_IS_KEYPAIR )
| KEY_TYPE_IS_PUBLIC_KEY ); | KEY_TYPE_IS_PUBLIC_KEY );
TEST_ASSERT( PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( type ) == type ); TEST_EQUAL( PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY( type ), type );
} }
} }
/* END_CASE */ /* END_CASE */
@ -462,8 +459,8 @@ void ecc_key_types( int curve_arg, int curve_bits_arg )
test_key_type( public_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_PUBLIC_KEY ); test_key_type( public_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_PUBLIC_KEY );
test_key_type( pair_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_KEYPAIR ); test_key_type( pair_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_KEYPAIR );
TEST_ASSERT( PSA_KEY_TYPE_GET_CURVE( public_type ) == curve ); TEST_EQUAL( PSA_KEY_TYPE_GET_CURVE( public_type ), curve );
TEST_ASSERT( PSA_KEY_TYPE_GET_CURVE( pair_type ) == curve ); TEST_EQUAL( PSA_KEY_TYPE_GET_CURVE( pair_type ), curve );
/* Validate that the bit size is less than the maximum ECC bit size /* Validate that the bit size is less than the maximum ECC bit size
* in this implementation. There's no parameter that should be equal * in this implementation. There's no parameter that should be equal

View file

@ -66,13 +66,13 @@ void parse_storage_data_check( data_t *file_data,
&key_data, &key_data_length, &key_data, &key_data_length,
&key_type, &key_policy ); &key_type, &key_policy );
TEST_ASSERT( status == expected_status ); TEST_EQUAL( status, expected_status );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;
TEST_ASSERT( key_type == (psa_key_type_t) expected_key_type ); TEST_EQUAL( key_type, (psa_key_type_t) expected_key_type );
TEST_ASSERT( key_policy.usage == (uint32_t) expected_key_usage ); TEST_EQUAL( key_policy.usage, (uint32_t) expected_key_usage );
TEST_ASSERT( key_policy.alg == (uint32_t) expected_key_alg ); TEST_EQUAL( key_policy.alg, (uint32_t) expected_key_alg );
ASSERT_COMPARE( expected_key_data->x, expected_key_data->len, ASSERT_COMPARE( expected_key_data->x, expected_key_data->len,
key_data, key_data_length ); key_data, key_data_length );
@ -81,7 +81,6 @@ exit:
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void save_large_persistent_key( int data_too_large, int expected_status ) void save_large_persistent_key( int data_too_large, int expected_status )
{ {
@ -95,15 +94,16 @@ void save_large_persistent_key( int data_too_large, int expected_status )
ASSERT_ALLOC( data, data_length ); ASSERT_ALLOC( data, data_length );
TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init() );
TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
PSA_KEY_TYPE_RAW_DATA, PSA_KEY_TYPE_RAW_DATA,
PSA_BYTES_TO_BITS( data_length ), PSA_BYTES_TO_BITS( data_length ),
&handle ) == PSA_SUCCESS ); &handle ) );
TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_RAW_DATA, TEST_EQUAL( psa_import_key( handle, PSA_KEY_TYPE_RAW_DATA,
data, data_length ) == expected_status ); data, data_length ),
expected_status );
exit: exit:
mbedtls_free( data ); mbedtls_free( data );
@ -123,43 +123,43 @@ void persistent_key_destroy( int key_id_arg, int should_store,
psa_key_type_t first_type = (psa_key_type_t) first_type_arg; psa_key_type_t first_type = (psa_key_type_t) first_type_arg;
psa_key_type_t second_type = (psa_key_type_t) second_type_arg; psa_key_type_t second_type = (psa_key_type_t) second_type_arg;
TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init() );
psa_key_policy_init( &policy ); psa_key_policy_init( &policy );
TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
first_type, first_type,
PSA_BYTES_TO_BITS( first_data->len ), PSA_BYTES_TO_BITS( first_data->len ),
&handle ) == PSA_SUCCESS ); &handle ) );
if( should_store == 1 ) if( should_store == 1 )
{ {
TEST_ASSERT( psa_import_key( PSA_ASSERT( psa_import_key(
handle, first_type, handle, first_type,
first_data->x, first_data->len ) == PSA_SUCCESS ); first_data->x, first_data->len ) );
} }
/* Destroy the key */ /* Destroy the key */
TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); PSA_ASSERT( psa_destroy_key( handle ) );
/* Check key slot storage is removed */ /* Check key slot storage is removed */
TEST_ASSERT( psa_is_key_present_in_storage( key_id ) == 0 ); TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
TEST_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, TEST_EQUAL( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle ),
&handle ) == PSA_ERROR_EMPTY_SLOT ); PSA_ERROR_EMPTY_SLOT );
TEST_ASSERT( handle == 0 ); TEST_EQUAL( handle, 0 );
/* Shutdown and restart */ /* Shutdown and restart */
mbedtls_psa_crypto_free(); mbedtls_psa_crypto_free();
TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init() );
/* Create another key in the same slot */ /* Create another key in the same slot */
TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
second_type, second_type,
PSA_BYTES_TO_BITS( second_data->len ), PSA_BYTES_TO_BITS( second_data->len ),
&handle ) == PSA_SUCCESS ); &handle ) );
TEST_ASSERT( psa_import_key( PSA_ASSERT( psa_import_key(
handle, second_type, handle, second_type,
second_data->x, second_data->len ) == PSA_SUCCESS ); second_data->x, second_data->len ) );
exit: exit:
mbedtls_psa_crypto_free(); mbedtls_psa_crypto_free();
@ -177,24 +177,24 @@ void persistent_key_import( int key_id_arg, int type_arg, data_t *data,
psa_key_type_t type = (psa_key_type_t) type_arg; psa_key_type_t type = (psa_key_type_t) type_arg;
psa_key_handle_t handle = 0; psa_key_handle_t handle = 0;
TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init() );
TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
type, type,
PSA_BYTES_TO_BITS( data->len ), PSA_BYTES_TO_BITS( data->len ),
&handle ) == PSA_SUCCESS ); &handle ) );
psa_key_policy_init( &policy ); psa_key_policy_init( &policy );
TEST_ASSERT( psa_import_key( handle, type, TEST_EQUAL( psa_import_key( handle, type, data->x, data->len ),
data->x, data->len ) == expected_status ); expected_status );
if( expected_status != PSA_SUCCESS ) if( expected_status != PSA_SUCCESS )
{ {
TEST_ASSERT( psa_is_key_present_in_storage( key_id ) == 0 ); TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
goto exit; goto exit;
} }
TEST_ASSERT( psa_get_key_lifetime( handle, &lifetime ) == PSA_SUCCESS ); PSA_ASSERT( psa_get_key_lifetime( handle, &lifetime ) );
TEST_ASSERT( lifetime == PSA_KEY_LIFETIME_PERSISTENT ); TEST_EQUAL( lifetime, PSA_KEY_LIFETIME_PERSISTENT );
exit: exit:
psa_destroy_persistent_key( key_id ); psa_destroy_persistent_key( key_id );
@ -219,46 +219,46 @@ void import_export_persistent_key( data_t *data, int type_arg,
ASSERT_ALLOC( exported, export_size ); ASSERT_ALLOC( exported, export_size );
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); PSA_ASSERT( psa_crypto_init( ) );
TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
type, type,
PSA_BYTES_TO_BITS( data->len ), PSA_BYTES_TO_BITS( data->len ),
&handle ) == PSA_SUCCESS ); &handle ) );
psa_key_policy_init( &policy ); psa_key_policy_init( &policy );
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
PSA_ALG_VENDOR_FLAG ); PSA_ALG_VENDOR_FLAG );
TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
/* Import the key */ /* Import the key */
TEST_ASSERT( psa_import_key( handle, type, PSA_ASSERT( psa_import_key( handle, type,
data->x, data->len ) == PSA_SUCCESS ); data->x, data->len ) );
TEST_ASSERT( psa_get_key_lifetime( handle, &lifetime_get ) == PSA_SUCCESS ); PSA_ASSERT( psa_get_key_lifetime( handle, &lifetime_get ) );
TEST_ASSERT( lifetime_get == PSA_KEY_LIFETIME_PERSISTENT ); TEST_EQUAL( lifetime_get, PSA_KEY_LIFETIME_PERSISTENT );
/* Test the key information */ /* Test the key information */
TEST_ASSERT( psa_get_key_information( PSA_ASSERT( psa_get_key_information(
handle, &got_type, &got_bits ) == PSA_SUCCESS ); handle, &got_type, &got_bits ) );
TEST_ASSERT( got_type == type ); TEST_EQUAL( got_type, type );
TEST_ASSERT( got_bits == (size_t) expected_bits ); TEST_EQUAL( got_bits, (size_t) expected_bits );
TEST_ASSERT( psa_is_key_present_in_storage( key_id ) == 1 ); TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );
if( key_not_exist ) if( key_not_exist )
{ {
psa_destroy_persistent_key( key_id ); psa_destroy_persistent_key( key_id );
} }
/* Export the key */ /* Export the key */
TEST_ASSERT( psa_export_key( handle, exported, export_size, PSA_ASSERT( psa_export_key( handle, exported, export_size,
&exported_length ) == PSA_SUCCESS ); &exported_length ) );
ASSERT_COMPARE( data->x, data->len, exported, exported_length ); ASSERT_COMPARE( data->x, data->len, exported, exported_length );
/* Destroy the key */ /* Destroy the key */
TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); PSA_ASSERT( psa_destroy_key( handle ) );
TEST_ASSERT( psa_is_key_present_in_storage( key_id ) == 0 ); TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
exit: exit:
mbedtls_free( exported ); mbedtls_free( exported );

View file

@ -8,8 +8,6 @@
#include "psa_crypto_storage.h" #include "psa_crypto_storage.h"
#define PSA_ASSERT( expr ) TEST_ASSERT( ( expr ) == PSA_SUCCESS )
typedef enum typedef enum
{ {
CLOSE_BY_CLOSE, CLOSE_BY_CLOSE,
@ -91,7 +89,7 @@ void transient_slot_lifecycle( int type_arg, int max_bits_arg,
PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
PSA_ASSERT( psa_import_key( handle, type, key_data->x, key_data->len ) ); PSA_ASSERT( psa_import_key( handle, type, key_data->x, key_data->len ) );
PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) ); PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) );
TEST_ASSERT( read_type == type ); TEST_EQUAL( read_type, type );
/* Do something that invalidates the handle. */ /* Do something that invalidates the handle. */
switch( close_method ) switch( close_method )
@ -108,9 +106,9 @@ void transient_slot_lifecycle( int type_arg, int max_bits_arg,
break; break;
} }
/* Test that the handle is now invalid. */ /* Test that the handle is now invalid. */
TEST_ASSERT( psa_get_key_information( handle, &read_type, NULL ) == TEST_EQUAL( psa_get_key_information( handle, &read_type, NULL ),
PSA_ERROR_INVALID_HANDLE ); PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_close_key( handle ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -147,13 +145,13 @@ void persistent_slot_lifecycle( int lifetime_arg, int id_arg,
PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
PSA_ASSERT( psa_import_key( handle, type, key_data->x, key_data->len ) ); PSA_ASSERT( psa_import_key( handle, type, key_data->x, key_data->len ) );
PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) ); PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) );
TEST_ASSERT( read_type == type ); TEST_EQUAL( read_type, type );
/* Close the key and reopen it. */ /* Close the key and reopen it. */
PSA_ASSERT( psa_close_key( handle ) ); PSA_ASSERT( psa_close_key( handle ) );
PSA_ASSERT( psa_open_key( lifetime, id, &handle ) ); PSA_ASSERT( psa_open_key( lifetime, id, &handle ) );
PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) ); PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) );
TEST_ASSERT( read_type == type ); TEST_EQUAL( read_type, type );
/* Do something that invalidates the handle. */ /* Do something that invalidates the handle. */
switch( close_method ) switch( close_method )
@ -170,9 +168,9 @@ void persistent_slot_lifecycle( int lifetime_arg, int id_arg,
break; break;
} }
/* Test that the handle is now invalid. */ /* Test that the handle is now invalid. */
TEST_ASSERT( psa_get_key_information( handle, &read_type, NULL ) == TEST_EQUAL( psa_get_key_information( handle, &read_type, NULL ),
PSA_ERROR_INVALID_HANDLE ); PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_close_key( handle ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
/* Try to reopen the key. If we destroyed it, check that it doesn't /* Try to reopen the key. If we destroyed it, check that it doesn't
* exist, otherwise check that it still exists. */ * exist, otherwise check that it still exists. */
@ -182,11 +180,11 @@ void persistent_slot_lifecycle( int lifetime_arg, int id_arg,
case CLOSE_BY_SHUTDOWN: case CLOSE_BY_SHUTDOWN:
PSA_ASSERT( psa_open_key( lifetime, id, &handle ) ); PSA_ASSERT( psa_open_key( lifetime, id, &handle ) );
PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) ); PSA_ASSERT( psa_get_key_information( handle, &read_type, NULL ) );
TEST_ASSERT( read_type == type ); TEST_EQUAL( read_type, type );
break; break;
case CLOSE_BY_DESTROY: case CLOSE_BY_DESTROY:
TEST_ASSERT( psa_open_key( lifetime, id, &handle ) == TEST_EQUAL( psa_open_key( lifetime, id, &handle ),
PSA_ERROR_EMPTY_SLOT ); PSA_ERROR_EMPTY_SLOT );
break; break;
} }
@ -232,9 +230,9 @@ void create_existent( int lifetime_arg, int id_arg,
PSA_ASSERT( psa_close_key( handle1 ) ); PSA_ASSERT( psa_close_key( handle1 ) );
/* Attempt to create a new key in the same slot. */ /* Attempt to create a new key in the same slot. */
TEST_ASSERT( psa_create_key( lifetime, id, type2, bits1, &handle2 ) == TEST_EQUAL( psa_create_key( lifetime, id, type2, bits1, &handle2 ),
PSA_ERROR_OCCUPIED_SLOT ); PSA_ERROR_OCCUPIED_SLOT );
TEST_ASSERT( handle2 == 0 ); TEST_EQUAL( handle2, 0 );
if( reopen_policy == CLOSE_AFTER ) if( reopen_policy == CLOSE_AFTER )
PSA_ASSERT( psa_close_key( handle1 ) ); PSA_ASSERT( psa_close_key( handle1 ) );
@ -245,8 +243,8 @@ void create_existent( int lifetime_arg, int id_arg,
PSA_ASSERT( psa_get_key_policy( handle1, &read_policy ) ); PSA_ASSERT( psa_get_key_policy( handle1, &read_policy ) );
TEST_ASSERT( psa_key_policy_equal( &read_policy, &policy1 ) ); TEST_ASSERT( psa_key_policy_equal( &read_policy, &policy1 ) );
PSA_ASSERT( psa_get_key_information( handle1, &read_type, &read_bits ) ); PSA_ASSERT( psa_get_key_information( handle1, &read_type, &read_bits ) );
TEST_ASSERT( read_type == type1 ); TEST_EQUAL( read_type, type1 );
TEST_ASSERT( read_bits == bits1 ); TEST_EQUAL( read_bits, bits1 );
PSA_ASSERT( psa_export_key( handle1, PSA_ASSERT( psa_export_key( handle1,
reexported, sizeof( reexported ), reexported, sizeof( reexported ),
&reexported_length ) ); &reexported_length ) );
@ -270,8 +268,8 @@ void open_fail( int lifetime_arg, int id_arg,
PSA_ASSERT( psa_crypto_init( ) ); PSA_ASSERT( psa_crypto_init( ) );
TEST_ASSERT( psa_open_key( lifetime, id, &handle ) == expected_status ); TEST_EQUAL( psa_open_key( lifetime, id, &handle ), expected_status );
TEST_ASSERT( handle == 0 ); TEST_EQUAL( handle, 0 );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -294,10 +292,9 @@ void create_fail( int lifetime_arg, int id_arg,
PSA_ASSERT( psa_crypto_init( ) ); PSA_ASSERT( psa_crypto_init( ) );
TEST_ASSERT( psa_create_key( lifetime, id, TEST_EQUAL( psa_create_key( lifetime, id, type, max_bits, &handle ),
type, max_bits, expected_status );
&handle ) == expected_status ); TEST_EQUAL( handle, 0 );
TEST_ASSERT( handle == 0 );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -328,17 +325,17 @@ void invalid_handle( )
material, sizeof( material ) ) ); material, sizeof( material ) ) );
/* Attempt to close and destroy some invalid handles. */ /* Attempt to close and destroy some invalid handles. */
TEST_ASSERT( psa_close_key( 0 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_close_key( 0 ), PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_close_key( handle1 - 1 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_close_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_close_key( handle1 + 1 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_close_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_destroy_key( 0 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_destroy_key( 0 ), PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_destroy_key( handle1 - 1 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_destroy_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
TEST_ASSERT( psa_destroy_key( handle1 + 1 ) == PSA_ERROR_INVALID_HANDLE ); TEST_EQUAL( psa_destroy_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
/* After all this, check that the original handle is intact. */ /* After all this, check that the original handle is intact. */
PSA_ASSERT( psa_get_key_information( handle1, &read_type, &read_bits ) ); PSA_ASSERT( psa_get_key_information( handle1, &read_type, &read_bits ) );
TEST_ASSERT( read_type == PSA_KEY_TYPE_RAW_DATA ); TEST_EQUAL( read_type, PSA_KEY_TYPE_RAW_DATA );
TEST_ASSERT( read_bits == PSA_BYTES_TO_BITS( sizeof( material ) ) ); TEST_EQUAL( read_bits, PSA_BYTES_TO_BITS( sizeof( material ) ) );
PSA_ASSERT( psa_close_key( handle1 ) ); PSA_ASSERT( psa_close_key( handle1 ) );
exit: exit:
@ -369,7 +366,7 @@ void many_transient_handles( int max_handles_arg )
&handles[i] ); &handles[i] );
if( status == PSA_ERROR_INSUFFICIENT_MEMORY ) if( status == PSA_ERROR_INSUFFICIENT_MEMORY )
break; break;
TEST_ASSERT( status == PSA_SUCCESS ); PSA_ASSERT( status );
TEST_ASSERT( handles[i] != 0 ); TEST_ASSERT( handles[i] != 0 );
for( j = 0; j < i; j++ ) for( j = 0; j < i; j++ )
TEST_ASSERT( handles[i] != handles[j] ); TEST_ASSERT( handles[i] != handles[j] );

View file

@ -30,18 +30,17 @@ void load_data_from_file( int id_to_load_arg,
file = fopen( slot_location, "wb+" ); file = fopen( slot_location, "wb+" );
TEST_ASSERT( file != NULL ); TEST_ASSERT( file != NULL );
file_size = fwrite( data->x, 1, data->len, file ); file_size = fwrite( data->x, 1, data->len, file );
TEST_ASSERT( file_size == data->len ); TEST_EQUAL( file_size, data->len );
ret = fclose( file ); ret = fclose( file );
TEST_ASSERT( ret == 0 ); TEST_EQUAL( ret, 0 );
} }
/* Read from the file with psa_crypto_storage_load. */ /* Read from the file with psa_crypto_storage_load. */
loaded_data = mbedtls_calloc( 1, capacity ); ASSERT_ALLOC( loaded_data, capacity );
TEST_ASSERT( loaded_data != NULL );
status = psa_crypto_storage_load( id_to_load, loaded_data, file_size ); status = psa_crypto_storage_load( id_to_load, loaded_data, file_size );
/* Check we get the expected status. */ /* Check we get the expected status. */
TEST_ASSERT( status == expected_status ); TEST_EQUAL( status, expected_status );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;
@ -69,7 +68,7 @@ void write_data_to_file( data_t *data, int expected_status )
status = psa_crypto_storage_store( 1, data->x, data->len ); status = psa_crypto_storage_store( 1, data->x, data->len );
/* Check that we got the expected status. */ /* Check that we got the expected status. */
TEST_ASSERT( status == expected_status ); TEST_EQUAL( status, expected_status );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;
@ -79,17 +78,16 @@ void write_data_to_file( data_t *data, int expected_status )
fseek( file, 0, SEEK_END ); fseek( file, 0, SEEK_END );
file_size = (size_t) ftell( file ); file_size = (size_t) ftell( file );
fseek( file, 0, SEEK_SET ); fseek( file, 0, SEEK_SET );
TEST_ASSERT( file_size == data->len ); TEST_EQUAL( file_size, data->len );
/* Check that the file contents are what we expect */ /* Check that the file contents are what we expect */
loaded_data = mbedtls_calloc( 1, data->len ); ASSERT_ALLOC( loaded_data, data->len );
TEST_ASSERT( loaded_data != NULL );
num_read = fread( loaded_data, 1, file_size, file ); num_read = fread( loaded_data, 1, file_size, file );
TEST_ASSERT( num_read == file_size ); TEST_EQUAL( num_read, file_size );
ASSERT_COMPARE( data->x, data->len, loaded_data, file_size ); ASSERT_COMPARE( data->x, data->len, loaded_data, file_size );
ret = fclose( file ); ret = fclose( file );
TEST_ASSERT( ret == 0 ); TEST_EQUAL( ret, 0 );
exit: exit:
mbedtls_free( loaded_data ); mbedtls_free( loaded_data );
@ -97,7 +95,6 @@ exit:
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void get_file_size( data_t *data, int expected_data_length, void get_file_size( data_t *data, int expected_data_length,
int expected_status, int should_make_file ) int expected_status, int should_make_file )
@ -114,16 +111,16 @@ void get_file_size( data_t *data, int expected_data_length,
file = fopen( slot_location, "wb+" ); file = fopen( slot_location, "wb+" );
TEST_ASSERT( file != NULL ); TEST_ASSERT( file != NULL );
file_size = fwrite( data->x, 1, data->len, file ); file_size = fwrite( data->x, 1, data->len, file );
TEST_ASSERT( file_size == data->len ); TEST_EQUAL( file_size, data->len );
ret = fclose( file ); ret = fclose( file );
TEST_ASSERT( ret == 0 ); TEST_EQUAL( ret, 0 );
} }
/* Check get data size is what we expect */ /* Check get data size is what we expect */
status = psa_crypto_storage_get_data_length( 1, &file_size ); status = psa_crypto_storage_get_data_length( 1, &file_size );
TEST_ASSERT( status == expected_status ); TEST_EQUAL( status, expected_status );
if( expected_status == PSA_SUCCESS ) if( expected_status == PSA_SUCCESS )
TEST_ASSERT( file_size == (size_t)expected_data_length ); TEST_EQUAL( file_size, (size_t)expected_data_length );
exit: exit:
remove( slot_location ); remove( slot_location );
@ -143,13 +140,13 @@ void write_data_to_prexisting_file( char *preexist_file_location,
file = fopen( preexist_file_location, "wb" ); file = fopen( preexist_file_location, "wb" );
TEST_ASSERT( file != NULL ); TEST_ASSERT( file != NULL );
ret = fclose( file ); ret = fclose( file );
TEST_ASSERT( ret == 0 ); TEST_EQUAL( ret, 0 );
/* Write data to file. */ /* Write data to file. */
status = psa_crypto_storage_store( 1, data->x, data->len ); status = psa_crypto_storage_store( 1, data->x, data->len );
/* Check that we got the expected status. */ /* Check that we got the expected status. */
TEST_ASSERT( status == expected_status ); TEST_EQUAL( status, expected_status );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;