From 3e02b3b28092661702cc343000927e0d961b8e6b Mon Sep 17 00:00:00 2001 From: itayzafrir Date: Tue, 12 Jun 2018 17:06:52 +0300 Subject: [PATCH] On target testing tests adaptation Updated all psa crypto tests to use the new test format --- tests/suites/test_suite_psa_crypto.data | 0 tests/suites/test_suite_psa_crypto.function | 778 ++++++++------------ 2 files changed, 327 insertions(+), 451 deletions(-) mode change 100755 => 100644 tests/suites/test_suite_psa_crypto.data diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data old mode 100755 new mode 100644 diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index add059ed2..002f9f7d0 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -1,7 +1,13 @@ /* BEGIN_HEADER */ +#include #include "psa/crypto.h" - #include "mbedtls/md.h" + +#if(UINT32_MAX > SIZE_MAX) +#define PSA_CRYPTO_TEST_SIZE_T_RANGE(x) ((x) <= SIZE_MAX) +#else +#define PSA_CRYPTO_TEST_SIZE_T_RANGE(x) 1 +#endif /* END_HEADER */ /* BEGIN_DEPENDENCIES @@ -26,30 +32,27 @@ void init_deinit() /* END_CASE */ /* BEGIN_CASE */ -void import( char *hex, int type, int expected_status ) +void import( data_t *data, int type, int expected_status ) { int slot = 1; psa_status_t status; - unsigned char *data = NULL; - size_t data_size; - data = unhexify_alloc( hex, &data_size ); TEST_ASSERT( data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - status = psa_import_key( slot, type, data, data_size ); + status = psa_import_key( slot, type, data->x, (size_t) data->len ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( status == PSA_SUCCESS ) TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); exit: - mbedtls_free( data ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void import_export( char *hex, +void import_export( data_t *data, int type_arg, int alg_arg, int usage_arg, @@ -62,10 +65,8 @@ void import_export( char *hex, int slot2 = slot + 1; psa_key_type_t type = type_arg; psa_status_t status; - unsigned char *data = NULL; unsigned char *exported = NULL; unsigned char *reexported = NULL; - size_t data_size; size_t export_size; size_t exported_length; size_t reexported_length; @@ -73,9 +74,9 @@ void import_export( char *hex, size_t got_bits; psa_key_policy_t policy = {0}; - data = unhexify_alloc( hex, &data_size ); TEST_ASSERT( data != NULL ); - export_size = (ssize_t) data_size + export_size_delta; + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); + export_size = (ssize_t) data->len + export_size_delta; exported = mbedtls_calloc( 1, export_size ); TEST_ASSERT( exported != NULL ); if( ! canonical_input ) @@ -93,7 +94,7 @@ void import_export( char *hex, /* Import the key */ TEST_ASSERT( psa_import_key( slot, type, - data, data_size ) == PSA_SUCCESS ); + data->x, (size_t) data->len ) == PSA_SUCCESS ); /* Test the key information */ TEST_ASSERT( psa_get_key_information( slot, @@ -112,8 +113,8 @@ void import_export( char *hex, if( canonical_input ) { - TEST_ASSERT( exported_length == data_size ); - TEST_ASSERT( memcmp( exported, data, data_size ) == 0 ); + TEST_ASSERT( exported_length == (size_t) data->len ); + TEST_ASSERT( memcmp( exported, data->x, (size_t) data->len ) == 0 ); } else { @@ -138,14 +139,15 @@ destroy: slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); exit: - mbedtls_free( data ); + mbedtls_free( exported ); + mbedtls_free( reexported ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void import_export_public_key( char *hex, +void import_export_public_key( data_t *data, int type_arg, int alg_arg, int expected_bits, @@ -155,18 +157,16 @@ void import_export_public_key( char *hex, int slot = 1; psa_key_type_t type = type_arg; psa_status_t status; - unsigned char *data = NULL; unsigned char *exported = NULL; - size_t data_size; size_t export_size; size_t exported_length; psa_key_type_t got_type; size_t got_bits; psa_key_policy_t policy = {0}; - data = unhexify_alloc( hex, &data_size ); TEST_ASSERT( data != NULL ); - export_size = (ssize_t) data_size ; + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); + export_size = (ssize_t) data->len; exported = mbedtls_calloc( 1, export_size ); TEST_ASSERT( exported != NULL ); @@ -181,7 +181,8 @@ void import_export_public_key( char *hex, /* Import the key */ TEST_ASSERT( psa_import_key( slot, type, - data, data_size ) == PSA_SUCCESS ); + data->x, (size_t) data->len ) == + PSA_SUCCESS ); /* Test the key information */ TEST_ASSERT( psa_get_key_information( slot, @@ -206,104 +207,88 @@ destroy: slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); exit: - mbedtls_free( data ); + mbedtls_free( exported ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void hash_finish( int alg_arg, char *input_hex, char *hash_hex ) +void hash_finish( int alg_arg, data_t *input, data_t *expected_hash ) { psa_algorithm_t alg = alg_arg; - unsigned char *input = NULL; - size_t input_size; - unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; - size_t expected_hash_length; unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; size_t actual_hash_length; psa_hash_operation_t operation; - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - expected_hash_length = unhexify( expected_hash, hash_hex ); + TEST_ASSERT( expected_hash != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_hash_update( &operation, - input, input_size ) == PSA_SUCCESS ); + input->x, (size_t) input->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_hash_finish( &operation, actual_hash, sizeof( actual_hash ), &actual_hash_length ) == PSA_SUCCESS ); - TEST_ASSERT( actual_hash_length == expected_hash_length ); - TEST_ASSERT( memcmp( expected_hash, actual_hash, - expected_hash_length ) == 0 ); + TEST_ASSERT( actual_hash_length == (size_t) expected_hash->len ); + TEST_ASSERT( memcmp( expected_hash->x, actual_hash, + (size_t) expected_hash->len ) == 0 ); exit: - mbedtls_free( input ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void hash_verify( int alg_arg, char *input_hex, char *hash_hex ) +void hash_verify( int alg_arg, data_t *input, data_t *expected_hash ) { psa_algorithm_t alg = alg_arg; - unsigned char *input = NULL; - size_t input_size; - unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; - size_t expected_hash_length; psa_hash_operation_t operation; - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - expected_hash_length = unhexify( expected_hash, hash_hex ); + TEST_ASSERT( expected_hash != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_hash_update( &operation, - input, input_size ) == PSA_SUCCESS ); + input->x, (size_t) input->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_hash_verify( &operation, - expected_hash, - expected_hash_length ) == PSA_SUCCESS ); + expected_hash->x, + (size_t) expected_hash->len ) == + PSA_SUCCESS ); exit: - mbedtls_free( input ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void mac_verify( int key_type_arg, char *key_hex, - int alg_arg, char *iv_hex, - char *input_hex, char *mac_hex ) +void mac_verify( int key_type_arg, data_t *key, + int alg_arg, data_t *iv, + data_t *input, data_t *expected_mac ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; - unsigned char *iv = NULL; - size_t iv_size; - unsigned char *input = NULL; - size_t input_size; - unsigned char *expected_mac = NULL; - size_t expected_mac_size; psa_mac_operation_t operation; psa_key_policy_t policy; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - if( iv_hex[0] != 0 ) - { - iv = unhexify_alloc( iv_hex, &iv_size ); - TEST_ASSERT( iv != NULL ); - } - input = unhexify_alloc( input_hex, &input_size ); + TEST_ASSERT( iv != NULL ); TEST_ASSERT( input != NULL ); - expected_mac = unhexify_alloc( mac_hex, &expected_mac_size ); TEST_ASSERT( expected_mac != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( iv->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -314,21 +299,18 @@ void mac_verify( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); // TODO: support IV TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); TEST_ASSERT( psa_mac_update( &operation, - input, input_size ) == PSA_SUCCESS ); + input->x, (size_t) input->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_mac_verify( &operation, - expected_mac, - expected_mac_size ) == PSA_SUCCESS ); + expected_mac->x, + (size_t) expected_mac->len ) == PSA_SUCCESS ); exit: - mbedtls_free( key ); - mbedtls_free( iv ); - mbedtls_free( input ); - mbedtls_free( expected_mac ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -337,52 +319,44 @@ exit: /* BEGIN_CASE */ void cipher_encrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex, + data_t *key, + data_t *input, data_t *expected_output, int expected_status ) { int key_slot = 1; psa_status_t status; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output; - unsigned char *expected_output; - size_t expected_output_size; + unsigned char *output = NULL; size_t output_buffer_size = 0; size_t function_output_length = 0; size_t total_output_length = 0; psa_cipher_operation_t operation; - - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - - expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); memset( iv, 0x2a, sizeof( iv ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; + output_buffer_size = (size_t) input->len + operation.block_size; output = mbedtls_calloc( 1, output_buffer_size ); + TEST_ASSERT( output != NULL ); - TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; @@ -396,14 +370,13 @@ void cipher_encrypt( int alg_arg, int key_type_arg, if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( total_output_length == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, - expected_output_size ) == 0 ); + TEST_ASSERT( total_output_length == (size_t) expected_output->len ); + TEST_ASSERT( memcmp( expected_output->x, output, + (size_t) expected_output->len ) == 0 ); } exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -411,57 +384,51 @@ exit: /* BEGIN_CASE */ void cipher_encrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, char *output_hex ) + data_t *key, + data_t *input, + int first_part_size, + data_t *expected_output ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output; - unsigned char *expected_output; - size_t expected_output_size; + unsigned char *output = NULL; size_t output_buffer_size = 0; size_t function_output_length = 0; size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - - expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); memset( iv, 0x2a, sizeof( iv ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; + output_buffer_size = (size_t) input->len + operation.block_size; output = mbedtls_calloc( 1, output_buffer_size ); + TEST_ASSERT( output != NULL ); - TEST_ASSERT( (unsigned int) first_part_size < input_size ); - TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); + TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, - input + first_part_size, - input_size - first_part_size, + input->x + first_part_size, + (size_t) input->len - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; @@ -472,12 +439,12 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg, total_output_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( total_output_length == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); + TEST_ASSERT( total_output_length == (size_t) expected_output->len ); + TEST_ASSERT( memcmp( expected_output->x, output, + (size_t) expected_output->len ) == 0 ); exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -485,59 +452,53 @@ exit: /* BEGIN_CASE */ void cipher_decrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, char *output_hex ) + data_t *key, + data_t *input, + int first_part_size, + data_t *expected_output ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output; - unsigned char *expected_output; - size_t expected_output_size; + unsigned char *output = NULL; size_t output_buffer_size = 0; size_t function_output_length = 0; size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - - expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); memset( iv, 0x2a, sizeof( iv ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; + output_buffer_size = (size_t) input->len + operation.block_size; output = mbedtls_calloc( 1, output_buffer_size ); + TEST_ASSERT( output != NULL ); - TEST_ASSERT( (unsigned int) first_part_size < input_size ); - TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); + TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, - input + first_part_size, - input_size - first_part_size, + input->x + first_part_size, + (size_t) input->len - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; @@ -548,12 +509,12 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg, total_output_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( total_output_length == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); + TEST_ASSERT( total_output_length == (size_t) expected_output->len ); + TEST_ASSERT( memcmp( expected_output->x, output, + (size_t) expected_output->len ) == 0 ); exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -562,53 +523,45 @@ exit: /* BEGIN_CASE */ void cipher_decrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex, + data_t *key, + data_t *input, data_t *expected_output, int expected_status ) { int key_slot = 1; psa_status_t status; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output; - unsigned char *expected_output; - size_t expected_output_size; + unsigned char *output = NULL; size_t output_buffer_size = 0; size_t function_output_length = 0; size_t total_output_length = 0; psa_cipher_operation_t operation; - - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - - expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); memset( iv, 0x2a, sizeof( iv ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; + output_buffer_size = (size_t) input->len + operation.block_size; output = mbedtls_calloc( 1, output_buffer_size ); + TEST_ASSERT( output != NULL ); - TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); total_output_length += function_output_length; @@ -622,15 +575,14 @@ void cipher_decrypt( int alg_arg, int key_type_arg, if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( total_output_length == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, - expected_output_size ) == 0 ); + TEST_ASSERT( total_output_length == (size_t) expected_output->len ); + TEST_ASSERT( memcmp( expected_output->x, output, + (size_t) expected_output->len ) == 0 ); } exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -639,39 +591,34 @@ exit: /* BEGIN_CASE */ void cipher_verify_output( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex ) + data_t *key, + data_t *input ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; size_t iv_size = 16; size_t iv_length = 0; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output1; + unsigned char *output1 = NULL; size_t output1_size = 0; size_t output1_length = 0; - unsigned char *output2; + unsigned char *output2 = NULL; size_t output2_size = 0; size_t output2_length = 0; size_t function_output_length = 0; psa_cipher_operation_t operation1; psa_cipher_operation_t operation2; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); @@ -679,11 +626,11 @@ void cipher_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length ) == PSA_SUCCESS ); - output1_size = input_size + operation1.block_size; + output1_size = (size_t) input->len + operation1.block_size; output1 = mbedtls_calloc( 1, output1_size ); TEST_ASSERT( output1 != NULL ); - TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, + TEST_ASSERT( psa_cipher_update( &operation1, input->x, (size_t) input->len, output1, output1_size, &output1_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, @@ -696,6 +643,7 @@ void cipher_verify_output( int alg_arg, int key_type_arg, output2_size = output1_length; output2 = mbedtls_calloc( 1, output2_size ); + TEST_ASSERT( output2 != NULL ); TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, iv_length ) == PSA_SUCCESS ); @@ -712,12 +660,12 @@ void cipher_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - TEST_ASSERT( input_size == output2_length ); - TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); + TEST_ASSERT( (size_t) input->len == output2_length ); + TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output1 ); + mbedtls_free( output2 ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -726,40 +674,35 @@ exit: /* BEGIN_CASE */ void cipher_verify_output_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, + data_t *key, + data_t *input, int first_part_size ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key = NULL; - size_t key_size; unsigned char iv[16] = {0}; size_t iv_size = 16; size_t iv_length = 0; - unsigned char *input = NULL; - size_t input_size = 0; - unsigned char *output1; + unsigned char *output1 = NULL; size_t output1_buffer_size = 0; size_t output1_length = 0; - unsigned char *output2; + unsigned char *output2 = NULL; size_t output2_buffer_size = 0; size_t output2_length = 0; size_t function_output_length; psa_cipher_operation_t operation1; psa_cipher_operation_t operation2; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); - - input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, - key, key_size ) == PSA_SUCCESS ); + key->x, (size_t) key->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); @@ -767,19 +710,20 @@ void cipher_verify_output_multipart( int alg_arg, TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length ) == PSA_SUCCESS ); - output1_buffer_size = input_size + operation1.block_size; + output1_buffer_size = (size_t) input->len + operation1.block_size; output1 = mbedtls_calloc( 1, output1_buffer_size ); + TEST_ASSERT( output1 != NULL ); - TEST_ASSERT( (unsigned int) first_part_size < input_size ); + TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); - TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, + TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, output1, output1_buffer_size, &function_output_length ) == PSA_SUCCESS ); output1_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation1, - input + first_part_size, - input_size - first_part_size, + input->x + first_part_size, + (size_t) input->len - first_part_size, output1, output1_buffer_size, &function_output_length ) == PSA_SUCCESS ); output1_length += function_output_length; @@ -794,6 +738,7 @@ void cipher_verify_output_multipart( int alg_arg, output2_buffer_size = output1_length; output2 = mbedtls_calloc( 1, output2_buffer_size ); + TEST_ASSERT( output2 != NULL ); TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, iv_length ) == PSA_SUCCESS ); @@ -818,54 +763,46 @@ void cipher_verify_output_multipart( int alg_arg, TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - TEST_ASSERT( input_size == output2_length ); - TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); + TEST_ASSERT( (size_t) input->len == output2_length ); + TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); exit: - mbedtls_free( key ); - mbedtls_free( input ); + mbedtls_free( output1 ); + mbedtls_free( output2 ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void aead_encrypt_decrypt( int key_type_arg, char * key_hex, - int alg_arg, char * input_hex, char * nonce_hex, - char * add_data, int expected_result_arg ) +void aead_encrypt_decrypt( int key_type_arg, data_t * key_data, + int alg_arg, data_t * input_data, data_t * nonce, + data_t * additional_data, int expected_result_arg ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output_data = NULL; size_t output_size = 0; size_t output_length = 0; unsigned char *output_data2 = NULL; size_t output_length2 = 0; - uint8_t* nonce; - size_t nonce_length = 16; size_t tag_length = 16; - unsigned char *additional_data = NULL; - size_t additional_data_length = 0; psa_status_t expected_result = (psa_status_t) expected_result_arg; psa_key_policy_t policy = {0}; - - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); - additional_data = unhexify_alloc( add_data, &additional_data_length ); - TEST_ASSERT( input_data != NULL ); - output_size = input_size + tag_length; + TEST_ASSERT( nonce != NULL ); + TEST_ASSERT( additional_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); + + output_size = (size_t) input_data->len + tag_length; output_data = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output_data != NULL ); - nonce = unhexify_alloc( nonce_hex, &nonce_length ); - TEST_ASSERT( nonce != NULL ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -876,13 +813,16 @@ void aead_encrypt_decrypt( int key_type_arg, char * key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_aead_encrypt( slot, alg, - nonce, nonce_length, - additional_data, additional_data_length, - input_data, input_size, output_data, - output_size, &output_length ) == expected_result ); + nonce->x, (size_t) nonce->len, + additional_data->x, + (size_t) additional_data->len, + input_data->x, (size_t) input_data->len, + output_data, + output_size, &output_length ) == + expected_result ); if( PSA_SUCCESS == expected_result ) { @@ -890,22 +830,21 @@ void aead_encrypt_decrypt( int key_type_arg, char * key_hex, TEST_ASSERT( output_data2 != NULL ); TEST_ASSERT( psa_aead_decrypt( slot, alg, - nonce, nonce_length, - additional_data, additional_data_length, + nonce->x, (size_t) nonce->len, + additional_data->x, + (size_t) additional_data->len, output_data, output_length, output_data2, - output_length, &output_length2 ) == expected_result ); + output_length, &output_length2 ) == + expected_result ); - TEST_ASSERT( memcmp( input_data, output_data2, - input_size ) == 0 ); + TEST_ASSERT( memcmp( input_data->x, output_data2, + (size_t) input_data->len ) == 0 ); } exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( additional_data ); mbedtls_free( output_data ); mbedtls_free( output_data2 ); mbedtls_psa_crypto_free( ); @@ -913,44 +852,34 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void aead_encrypt( int key_type_arg, char * key_hex, - int alg_arg, char * input_hex, - char * add_data, char * nonce_hex, - char * expected_result_hex ) +void aead_encrypt( int key_type_arg, data_t * key_data, + int alg_arg, data_t * input_data, + data_t * additional_data, data_t * nonce, + data_t * expected_result ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output_data = NULL; size_t output_size = 0; size_t output_length = 0; - unsigned char *expected_result = NULL; - size_t expected_result_length = 0; - uint8_t* nonce = NULL; - size_t nonce_length = 0; size_t tag_length = 16; - unsigned char *additional_data = NULL; - size_t additional_data_length = 0; psa_key_policy_t policy = {0}; - - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); - additional_data = unhexify_alloc( add_data, &additional_data_length ); - TEST_ASSERT( input_data != NULL ); - output_size = input_size + tag_length; + TEST_ASSERT( additional_data != NULL ); + TEST_ASSERT( nonce != NULL ); + TEST_ASSERT( expected_result != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); + + output_size = (size_t) input_data->len + tag_length; output_data = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output_data != NULL ); - nonce = unhexify_alloc( nonce_hex, &nonce_length ); - TEST_ASSERT( nonce != NULL ); - expected_result = unhexify_alloc( expected_result_hex, &expected_result_length ); - TEST_ASSERT( expected_result != NULL ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -961,71 +890,60 @@ void aead_encrypt( int key_type_arg, char * key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_aead_encrypt( slot, alg, - nonce, nonce_length, - additional_data, additional_data_length, - input_data, input_size, output_data, + nonce->x, (size_t) nonce->len, + additional_data->x, + (size_t) additional_data->len, + input_data->x, (size_t) input_data->len, + output_data, output_size, &output_length ) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( output_data, expected_result, + TEST_ASSERT( memcmp( output_data, expected_result->x, output_length ) == 0 ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( additional_data ); mbedtls_free( output_data ); - mbedtls_free( nonce ); - mbedtls_free( expected_result ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void aead_decrypt( int key_type_arg, char * key_hex, - int alg_arg, char * input_hex, - char * add_data, char * nonce_hex, - char * expected_result_hex, int expected_result_arg ) +void aead_decrypt( int key_type_arg, data_t * key_data, + int alg_arg, data_t * input_data, + data_t * additional_data, data_t * nonce, + data_t * expected_data, int expected_result_arg ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output_data = NULL; size_t output_size = 0; size_t output_length = 0; - unsigned char *expected_data = NULL; - size_t expected_result_length = 0; - uint8_t* nonce = NULL; - size_t nonce_length = 0; size_t tag_length = 16; - unsigned char *additional_data = NULL; - size_t additional_data_length = 0; psa_key_policy_t policy = {0}; psa_status_t expected_result = (psa_status_t) expected_result_arg; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); - additional_data = unhexify_alloc( add_data, &additional_data_length ); - TEST_ASSERT( input_data != NULL ); - output_size = input_size + tag_length; + TEST_ASSERT( additional_data != NULL ); + TEST_ASSERT( nonce != NULL ); + TEST_ASSERT( expected_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); + + output_size = (size_t) input_data->len + tag_length; output_data = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output_data != NULL ); - nonce = unhexify_alloc( nonce_hex, &nonce_length ); - TEST_ASSERT( nonce != NULL ); - expected_data = unhexify_alloc( expected_result_hex, &expected_result_length ); - TEST_ASSERT( expected_data != NULL ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -1036,18 +954,21 @@ void aead_decrypt( int key_type_arg, char * key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_aead_decrypt( slot, alg, - nonce, nonce_length, - additional_data, additional_data_length, - input_data, input_size, output_data, - output_size, &output_length ) == expected_result ); + nonce->x, (size_t) nonce->len, + additional_data->x, (size_t) additional_data->len, + input_data->x, (size_t) input_data->len, + output_data, + output_size, &output_length ) == + expected_result ); if ( expected_result == PSA_SUCCESS ) { - TEST_ASSERT( memcmp( output_data, expected_data, + TEST_ASSERT( memcmp( output_data, expected_data->x, output_length ) == 0 ); } @@ -1055,12 +976,7 @@ void aead_decrypt( int key_type_arg, char * key_hex, exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( additional_data ); mbedtls_free( output_data ); - mbedtls_free( nonce ); - mbedtls_free( expected_data ); mbedtls_psa_crypto_free( ); } /* END_CASE */ @@ -1078,30 +994,25 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void sign_deterministic( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, char *output_hex ) +void sign_deterministic( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data, + data_t *output_data ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; size_t key_bits; - unsigned char *input_data = NULL; - size_t input_size; - unsigned char *output_data = NULL; - size_t output_size; unsigned char *signature = NULL; size_t signature_size; size_t signature_length = 0xdeadbeef; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); - output_data = unhexify_alloc( output_hex, &output_size ); TEST_ASSERT( output_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -1112,7 +1023,7 @@ void sign_deterministic( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); TEST_ASSERT( psa_get_key_information( slot, NULL, &key_bits ) == PSA_SUCCESS ); @@ -1123,45 +1034,40 @@ void sign_deterministic( int key_type_arg, char *key_hex, TEST_ASSERT( signature != NULL ); TEST_ASSERT( psa_asymmetric_sign( slot, alg, - input_data, input_size, + input_data->x, (size_t) input_data->len, NULL, 0, signature, signature_size, &signature_length ) == PSA_SUCCESS ); - TEST_ASSERT( signature_length == output_size ); - TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); + TEST_ASSERT( signature_length == (size_t) output_data->len ); + TEST_ASSERT( memcmp( signature, output_data->x, (size_t) output_data->len ) + == 0 ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( output_data ); mbedtls_free( signature ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void sign_fail( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, +void sign_fail( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data, int signature_size, int expected_status_arg ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; psa_status_t actual_status; psa_status_t expected_status = expected_status_arg; unsigned char *signature = NULL; size_t signature_length = 0xdeadbeef; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + signature = mbedtls_calloc( 1, signature_size ); TEST_ASSERT( signature != NULL ); @@ -1174,10 +1080,12 @@ void sign_fail( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); actual_status = psa_asymmetric_sign( slot, alg, - input_data, input_size, + input_data->x, + (size_t) input_data->len, NULL, 0, signature, signature_size, &signature_length ); @@ -1186,8 +1094,6 @@ void sign_fail( int key_type_arg, char *key_hex, exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); mbedtls_free( signature ); mbedtls_psa_crypto_free( ); } @@ -1232,11 +1138,10 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) +void key_policy_fail( int usage_arg, int alg_arg, int expected_status, + data_t *keypair ) { int key_slot = 1; - unsigned char* keypair = NULL; - size_t key_size = 0; size_t signature_length = 0; psa_key_policy_t policy = {0}; int actual_status = PSA_SUCCESS; @@ -1251,21 +1156,23 @@ void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key if( usage_arg & PSA_KEY_USAGE_EXPORT ) { - keypair = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( keypair != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, - keypair, key_size ) == PSA_SUCCESS ); - actual_status = psa_asymmetric_sign( key_slot, - ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, + keypair->x, (size_t) keypair->len ) == + PSA_SUCCESS ); + actual_status = psa_asymmetric_sign( key_slot, + ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, NULL, 0, &signature_length ); } if( usage_arg & PSA_KEY_USAGE_SIGN ) { - keypair = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( keypair != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, - keypair, key_size ) == PSA_SUCCESS ); + keypair->x, (size_t) keypair->len ) == + PSA_SUCCESS ); actual_status = psa_export_key( key_slot, NULL, 0, NULL ); } @@ -1273,7 +1180,6 @@ void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key exit: psa_destroy_key( key_slot ); - mbedtls_free( keypair ); mbedtls_psa_crypto_free( ); } /* END_CASE */ @@ -1333,26 +1239,21 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void asymmetric_verify( int key_type_arg, char *key_hex, - int alg_arg, char *hash_hex, char *signature_hex ) +void asymmetric_verify( int key_type_arg, data_t *key_data, + int alg_arg, data_t *hash_data, + data_t *signature_data ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *hash_data = NULL; - size_t hash_size; - unsigned char *signature_data = NULL; - size_t signature_size; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - hash_data = unhexify_alloc( hash_hex, &hash_size ); TEST_ASSERT( hash_data != NULL ); - signature_data = unhexify_alloc( signature_hex, &signature_size ); TEST_ASSERT( signature_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -1363,47 +1264,41 @@ void asymmetric_verify( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_asymmetric_verify( slot, alg, - hash_data, hash_size, + hash_data->x, (size_t) hash_data->len, NULL, 0, - signature_data, signature_size ) == + signature_data->x, + (size_t) signature_data->len ) == PSA_SUCCESS ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( hash_data ); - mbedtls_free( signature_data ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void asymmetric_verify_fail( int key_type_arg, char *key_hex, - int alg_arg, char *hash_hex, char *signature_hex, - int expected_status_arg ) +void asymmetric_verify_fail( int key_type_arg, data_t *key_data, + int alg_arg, data_t *hash_data, + data_t *signature_data, + int expected_status_arg ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *hash_data = NULL; - size_t hash_size; - unsigned char *signature_data = NULL; - size_t signature_size; psa_status_t actual_status; psa_status_t expected_status = expected_status_arg; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - hash_data = unhexify_alloc( hash_hex, &hash_size ); TEST_ASSERT( hash_data != NULL ); - signature_data = unhexify_alloc( signature_hex, &signature_size ); TEST_ASSERT( signature_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -1414,37 +1309,32 @@ void asymmetric_verify_fail( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); actual_status = psa_asymmetric_verify( slot, alg, - hash_data, hash_size, + hash_data->x, (size_t) hash_data->len, NULL, 0, - signature_data, signature_size ); + signature_data->x, + (size_t) signature_data->len ); TEST_ASSERT( actual_status == expected_status ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( hash_data ); - mbedtls_free( signature_data ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void asymmetric_encrypt_decrypt( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex ) +void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output = NULL; size_t output_size = 0; size_t output_length = 0; @@ -1453,12 +1343,13 @@ void asymmetric_encrypt_decrypt( int key_type_arg, char *key_hex, size_t output2_length = 0; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - output_size = key_size; - output2_size = key_size; - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + + output_size = (size_t) key_data->len; + output2_size = output_size; output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output != NULL ); output2 = mbedtls_calloc( 1, output2_size ); @@ -1471,14 +1362,15 @@ void asymmetric_encrypt_decrypt( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); /* We test encryption by checking that encrypt-then-decrypt gives back * the original plaintext because of the non-optional random * part of encryption process which prevents using fixed vectors. */ TEST_ASSERT( psa_asymmetric_encrypt(slot, alg, - input_data, - input_size, + input_data->x, + (size_t) input_data->len, NULL, 0, output, output_size, @@ -1491,14 +1383,13 @@ void asymmetric_encrypt_decrypt( int key_type_arg, char *key_hex, output2, output2_size, &output2_length) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( input_data, output2, input_size ) == 0 ); + TEST_ASSERT( memcmp( input_data->x, output2, (size_t) input_data->len ) + == 0 ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( output); - mbedtls_free( output2); + mbedtls_free( output ); + mbedtls_free( output2 ); mbedtls_psa_crypto_free( ); } @@ -1506,8 +1397,8 @@ exit: /* BEGIN_CASE */ -void asymmetric_encrypt_fail( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, +void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data, int expected_status_arg ) { @@ -1515,10 +1406,6 @@ void asymmetric_encrypt_fail( int key_type_arg, char *key_hex, int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output = NULL; size_t output_size = 0; size_t output_length = 0; @@ -1526,11 +1413,12 @@ void asymmetric_encrypt_fail( int key_type_arg, char *key_hex, psa_status_t expected_status = expected_status_arg; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - output_size = key_size; - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + + output_size = (size_t) key_data->len; output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output != NULL ); @@ -1541,11 +1429,12 @@ void asymmetric_encrypt_fail( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); actual_status = psa_asymmetric_encrypt(slot, alg, - input_data, - input_size, + input_data->x, + (size_t) input_data->len, NULL, 0, output, output_size, @@ -1554,40 +1443,33 @@ void asymmetric_encrypt_fail( int key_type_arg, char *key_hex, exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( output); + mbedtls_free( output ); mbedtls_psa_crypto_free( ); } /* END_CASE */ /* BEGIN_CASE */ -void asymmetric_decrypt( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, - char *expected_hex, int expected_size ) +void asymmetric_decrypt( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data, + data_t *expected_data, int expected_size ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; - unsigned char *expected_data = NULL; - size_t expected_data_size; unsigned char *output = NULL; size_t output_size = 0; size_t output_length = 0; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - output_size = key_size; - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); - expected_data = unhexify_alloc( expected_hex, &expected_data_size ); TEST_ASSERT( expected_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); + + output_size = (size_t) key_data->len; output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output != NULL ); @@ -1598,24 +1480,22 @@ void asymmetric_decrypt( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, - input_data, - input_size, + input_data->x, + (size_t) input_data->len, NULL, 0, output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( ((size_t)expected_size) == output_length ); - TEST_ASSERT( memcmp( expected_data, output, (output_length) ) == 0 ); + TEST_ASSERT( memcmp( expected_data->x, output, (output_length) ) == 0 ); exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( expected_data ); - mbedtls_free( output); + mbedtls_free( output ); mbedtls_psa_crypto_free( ); @@ -1624,18 +1504,14 @@ exit: /* BEGIN_CASE */ -void asymmetric_decrypt_fail( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, +void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, + int alg_arg, data_t *input_data, int expected_status_arg ) { int slot = 1; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; - unsigned char *key_data = NULL; - size_t key_size; - unsigned char *input_data = NULL; - size_t input_size; unsigned char *output = NULL; size_t output_size = 0; size_t output_length = 0; @@ -1643,11 +1519,12 @@ void asymmetric_decrypt_fail( int key_type_arg, char *key_hex, psa_status_t expected_status = expected_status_arg; psa_key_policy_t policy = {0}; - key_data = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key_data != NULL ); - output_size = key_size; - input_data = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input_data != NULL ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); + TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); + + output_size = (size_t) key_data->len; output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( output != NULL ); @@ -1658,11 +1535,12 @@ void asymmetric_decrypt_fail( int key_type_arg, char *key_hex, TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); + key_data->x, (size_t) key_data->len ) == + PSA_SUCCESS ); actual_status = psa_asymmetric_decrypt(slot, alg, - input_data, - input_size, + input_data->x, + (size_t) input_data->len, NULL, 0, output, output_size, @@ -1671,8 +1549,6 @@ void asymmetric_decrypt_fail( int key_type_arg, char *key_hex, exit: psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); mbedtls_free( output); mbedtls_psa_crypto_free( ); }