2018-01-27 23:32:46 +01:00
|
|
|
/* BEGIN_HEADER */
|
2018-06-12 16:06:52 +02:00
|
|
|
#include <stdint.h>
|
2018-01-27 23:32:46 +01:00
|
|
|
#include "psa/crypto.h"
|
2018-02-08 10:02:12 +01:00
|
|
|
#include "mbedtls/md.h"
|
2018-06-12 16:06:52 +02:00
|
|
|
|
|
|
|
#if(UINT32_MAX > SIZE_MAX)
|
2018-06-18 15:41:12 +02:00
|
|
|
#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
|
2018-06-12 16:06:52 +02:00
|
|
|
#else
|
2018-06-18 15:41:12 +02:00
|
|
|
#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
|
2018-06-12 16:06:52 +02:00
|
|
|
#endif
|
2018-01-27 23:32:46 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-18 15:41:12 +02:00
|
|
|
void init_deinit( )
|
2018-01-27 23:32:46 +01:00
|
|
|
{
|
2018-01-28 13:16:24 +01:00
|
|
|
psa_status_t status;
|
2018-01-27 23:32:46 +01:00
|
|
|
int i;
|
|
|
|
for( i = 0; i <= 1; i++ )
|
|
|
|
{
|
2018-01-28 13:16:24 +01:00
|
|
|
status = psa_crypto_init( );
|
|
|
|
TEST_ASSERT( status == PSA_SUCCESS );
|
|
|
|
status = psa_crypto_init( );
|
|
|
|
TEST_ASSERT( status == PSA_SUCCESS );
|
2018-01-27 23:32:46 +01:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void import( data_t *data, int type, int expected_status )
|
2018-01-28 13:16:24 +01:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
TEST_ASSERT( data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
|
2018-01-28 13:16:24 +01:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
status = psa_import_key( slot, type, data->x, (size_t) data->len );
|
2018-01-28 13:16:24 +01:00
|
|
|
TEST_ASSERT( status == (psa_status_t) expected_status );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void import_export( data_t *data,
|
2018-06-04 17:42:36 +02:00
|
|
|
int type_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int usage_arg,
|
2018-01-28 13:16:24 +01:00
|
|
|
int expected_bits,
|
|
|
|
int export_size_delta,
|
|
|
|
int expected_export_status,
|
|
|
|
int canonical_input )
|
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
int slot2 = slot + 1;
|
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_status_t status;
|
|
|
|
unsigned char *exported = NULL;
|
|
|
|
unsigned char *reexported = NULL;
|
|
|
|
size_t export_size;
|
|
|
|
size_t exported_length;
|
|
|
|
size_t reexported_length;
|
|
|
|
psa_key_type_t got_type;
|
|
|
|
size_t got_bits;
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
|
|
|
|
export_size = (ssize_t) data->len + export_size_delta;
|
2018-01-28 13:16:24 +01:00
|
|
|
exported = mbedtls_calloc( 1, export_size );
|
|
|
|
TEST_ASSERT( exported != NULL );
|
|
|
|
if( ! canonical_input )
|
|
|
|
{
|
|
|
|
reexported = mbedtls_calloc( 1, export_size );
|
|
|
|
TEST_ASSERT( reexported != NULL );
|
|
|
|
}
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
2018-06-04 17:42:36 +02:00
|
|
|
psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
/* Import the key */
|
|
|
|
TEST_ASSERT( psa_import_key( slot, type,
|
2018-06-12 16:06:52 +02:00
|
|
|
data->x, (size_t) data->len ) == PSA_SUCCESS );
|
2018-01-28 13:16:24 +01:00
|
|
|
|
|
|
|
/* Test the key information */
|
|
|
|
TEST_ASSERT( psa_get_key_information( slot,
|
|
|
|
&got_type, &got_bits ) ==
|
|
|
|
PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( got_type == type );
|
|
|
|
TEST_ASSERT( got_bits == (size_t) expected_bits );
|
|
|
|
|
|
|
|
/* Export the key */
|
|
|
|
status = psa_export_key( slot,
|
|
|
|
exported, export_size,
|
|
|
|
&exported_length );
|
|
|
|
TEST_ASSERT( status == (psa_status_t) expected_export_status );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto destroy;
|
|
|
|
|
|
|
|
if( canonical_input )
|
|
|
|
{
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( exported_length == (size_t) data->len );
|
|
|
|
TEST_ASSERT( memcmp( exported, data->x, (size_t) data->len ) == 0 );
|
2018-01-28 13:16:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-28 12:46:26 +02:00
|
|
|
TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-01-28 13:16:24 +01:00
|
|
|
TEST_ASSERT( psa_import_key( slot2, type,
|
|
|
|
exported, export_size ) ==
|
|
|
|
PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_export_key( slot2,
|
|
|
|
reexported, export_size,
|
|
|
|
&reexported_length ) ==
|
|
|
|
PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( reexported_length == exported_length );
|
|
|
|
TEST_ASSERT( memcmp( reexported, exported,
|
|
|
|
exported_length ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy:
|
|
|
|
/* Destroy the key */
|
|
|
|
TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_get_key_information(
|
|
|
|
slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
|
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( exported );
|
|
|
|
mbedtls_free( reexported );
|
2018-01-28 13:16:24 +01:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-06-06 16:26:04 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void import_export_public_key( data_t *data,
|
2018-06-18 15:41:12 +02:00
|
|
|
int type_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int expected_bits,
|
|
|
|
int public_key_expected_length,
|
|
|
|
int expected_export_status )
|
2018-06-06 16:26:04 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_status_t status;
|
|
|
|
unsigned char *exported = NULL;
|
|
|
|
size_t export_size;
|
|
|
|
size_t exported_length;
|
|
|
|
psa_key_type_t got_type;
|
|
|
|
size_t got_bits;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
|
|
|
|
TEST_ASSERT( data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
|
|
|
|
export_size = (ssize_t) data->len;
|
2018-06-06 16:26:04 +02:00
|
|
|
exported = mbedtls_calloc( 1, export_size );
|
|
|
|
TEST_ASSERT( exported != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
|
|
|
|
alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
/* Import the key */
|
|
|
|
TEST_ASSERT( psa_import_key( slot, type,
|
2018-06-12 16:06:52 +02:00
|
|
|
data->x, (size_t) data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-06-06 16:26:04 +02:00
|
|
|
|
|
|
|
/* Test the key information */
|
|
|
|
TEST_ASSERT( psa_get_key_information( slot,
|
2018-06-18 16:04:39 +02:00
|
|
|
&got_type,
|
|
|
|
&got_bits ) == PSA_SUCCESS );
|
2018-06-06 16:26:04 +02:00
|
|
|
TEST_ASSERT( got_type == type );
|
|
|
|
TEST_ASSERT( got_bits == (size_t) expected_bits );
|
|
|
|
|
|
|
|
/* Export the key */
|
|
|
|
status = psa_export_public_key( slot,
|
2018-06-18 15:41:12 +02:00
|
|
|
exported, export_size,
|
|
|
|
&exported_length );
|
2018-06-06 16:26:04 +02:00
|
|
|
TEST_ASSERT( status == (psa_status_t) expected_export_status );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto destroy;
|
|
|
|
|
|
|
|
TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
|
|
|
|
|
|
|
|
destroy:
|
|
|
|
/* Destroy the key */
|
|
|
|
TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_get_key_information(
|
|
|
|
slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
|
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( exported );
|
2018-06-06 16:26:04 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-07 21:05:37 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
|
2018-02-07 21:05:37 +01:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
size_t actual_hash_length;
|
|
|
|
psa_hash_operation_t operation;
|
|
|
|
|
|
|
|
TEST_ASSERT( input != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_hash_update( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x, (size_t) input->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-02-07 21:05:37 +01:00
|
|
|
TEST_ASSERT( psa_hash_finish( &operation,
|
|
|
|
actual_hash, sizeof( actual_hash ),
|
|
|
|
&actual_hash_length ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( actual_hash_length == (size_t) expected_hash->len );
|
|
|
|
TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
|
|
|
|
(size_t) expected_hash->len ) == 0 );
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
|
2018-02-07 21:05:37 +01:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_hash_operation_t operation;
|
|
|
|
|
|
|
|
TEST_ASSERT( input != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_hash_update( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x, (size_t) input->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-02-07 21:05:37 +01:00
|
|
|
TEST_ASSERT( psa_hash_verify( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
expected_hash->x,
|
|
|
|
(size_t) expected_hash->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-02-07 21:05:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-08 10:02:12 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void mac_verify( int key_type_arg, data_t *key,
|
|
|
|
int alg_arg, data_t *iv,
|
|
|
|
data_t *input, data_t *expected_mac )
|
2018-02-08 10:02:12 +01:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation;
|
2018-04-02 17:34:15 +02:00
|
|
|
psa_key_policy_t policy;
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( key != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( iv != NULL );
|
2018-02-08 10:02:12 +01:00
|
|
|
TEST_ASSERT( input != NULL );
|
|
|
|
TEST_ASSERT( expected_mac != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-04-02 17:34:15 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-02-08 10:02:12 +01:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-02-08 10:02:12 +01:00
|
|
|
// 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,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x, (size_t) input->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-02-08 10:02:12 +01:00
|
|
|
TEST_ASSERT( psa_mac_verify( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
expected_mac->x,
|
|
|
|
(size_t) expected_mac->len ) == PSA_SUCCESS );
|
2018-02-08 10:02:12 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-03 23:57:22 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_encrypt( int alg_arg, int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input, data_t *expected_output,
|
2018-06-08 14:28:46 +02:00
|
|
|
int expected_status )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2018-06-06 15:19:24 +02:00
|
|
|
int key_slot = 1;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_status_t status;
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-06 15:19:24 +02:00
|
|
|
unsigned char iv[16] = {0};
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
2018-06-08 14:40:59 +02:00
|
|
|
size_t total_output_length = 0;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_cipher_operation_t operation;
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
|
|
|
TEST_ASSERT( expected_output != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
|
|
|
memset( iv, 0x2a, sizeof( iv ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_setup( &operation,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
output_buffer_size = (size_t) input->len + operation.block_size;
|
2018-06-08 14:20:49 +02:00
|
|
|
output = mbedtls_calloc( 1, output_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output != NULL );
|
2018-02-03 23:57:22 +01:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-08 14:20:49 +02:00
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size,
|
|
|
|
&function_output_length );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( status == (psa_status_t) expected_status );
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( total_output_length == (size_t) expected_output->len );
|
|
|
|
TEST_ASSERT( memcmp( expected_output->x, output,
|
|
|
|
(size_t) expected_output->len ) == 0 );
|
2018-06-06 15:36:50 +02:00
|
|
|
}
|
2018-06-08 14:40:59 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size,
|
|
|
|
data_t *expected_output )
|
2018-06-06 15:36:50 +02:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char iv[16] = {0};
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
2018-06-08 14:40:59 +02:00
|
|
|
size_t total_output_length = 0;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_cipher_operation_t operation;
|
|
|
|
|
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
|
|
|
TEST_ASSERT( expected_output != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
|
|
|
memset( iv, 0x2a, sizeof( iv ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_setup( &operation,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
output_buffer_size = (size_t) input->len + operation.block_size;
|
2018-06-08 14:20:49 +02:00
|
|
|
output = mbedtls_calloc( 1, output_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output != NULL );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len );
|
|
|
|
TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x + first_part_size,
|
|
|
|
(size_t) input->len - first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-08 14:20:49 +02:00
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( total_output_length == (size_t) expected_output->len );
|
|
|
|
TEST_ASSERT( memcmp( expected_output->x, output,
|
2018-06-18 15:41:12 +02:00
|
|
|
(size_t) expected_output->len ) == 0 );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_destroy_key( key_slot );
|
2018-02-03 22:44:14 +01:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size,
|
|
|
|
data_t *expected_output )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2018-06-06 15:19:24 +02:00
|
|
|
int key_slot = 1;
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-06 15:19:24 +02:00
|
|
|
unsigned char iv[16] = {0};
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
2018-06-08 14:40:59 +02:00
|
|
|
size_t total_output_length = 0;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_cipher_operation_t operation;
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
|
|
|
TEST_ASSERT( expected_output != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
|
|
|
memset( iv, 0x2a, sizeof( iv ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_decrypt_setup( &operation,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) == PSA_SUCCESS );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
output_buffer_size = (size_t) input->len + operation.block_size;
|
2018-06-08 14:20:49 +02:00
|
|
|
output = mbedtls_calloc( 1, output_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output != NULL );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len );
|
|
|
|
TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x + first_part_size,
|
|
|
|
(size_t) input->len - first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-08 14:20:49 +02:00
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( total_output_length == (size_t) expected_output->len );
|
2018-06-18 15:41:12 +02:00
|
|
|
TEST_ASSERT( memcmp( expected_output->x, output,
|
|
|
|
(size_t) expected_output->len ) == 0 );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_destroy_key( key_slot );
|
2018-02-03 22:44:14 +01:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_decrypt( int alg_arg, int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input, data_t *expected_output,
|
2018-06-08 14:28:46 +02:00
|
|
|
int expected_status )
|
2018-03-28 00:21:33 +02:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_status_t status;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char iv[16] = {0};
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
2018-06-08 14:40:59 +02:00
|
|
|
size_t total_output_length = 0;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_cipher_operation_t operation;
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
|
|
|
TEST_ASSERT( expected_output != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
memset( iv, 0x2a, sizeof( iv ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_decrypt_setup( &operation,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) == PSA_SUCCESS );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
output_buffer_size = (size_t) input->len + operation.block_size;
|
2018-06-08 14:20:49 +02:00
|
|
|
output = mbedtls_calloc( 1, output_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output != NULL );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len,
|
2018-06-08 14:20:49 +02:00
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-08 14:20:49 +02:00
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size,
|
|
|
|
&function_output_length );
|
2018-06-08 14:40:59 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( status == (psa_status_t) expected_status );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( total_output_length == (size_t) expected_output->len );
|
|
|
|
TEST_ASSERT( memcmp( expected_output->x, output,
|
|
|
|
(size_t) expected_output->len ) == 0 );
|
2018-06-06 15:36:50 +02:00
|
|
|
}
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-03-28 00:21:33 +02:00
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-03-28 00:21:33 +02:00
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-03-28 14:14:59 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_verify_output( int alg_arg, int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input )
|
2018-03-28 14:14:59 +02:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char iv[16] = {0};
|
|
|
|
size_t iv_size = 16;
|
|
|
|
size_t iv_length = 0;
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output1 = NULL;
|
2018-06-06 15:19:24 +02:00
|
|
|
size_t output1_size = 0;
|
|
|
|
size_t output1_length = 0;
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output2 = NULL;
|
2018-06-06 15:19:24 +02:00
|
|
|
size_t output2_size = 0;
|
|
|
|
size_t output2_length = 0;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t function_output_length = 0;
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_cipher_operation_t operation1;
|
|
|
|
psa_cipher_operation_t operation2;
|
2018-03-28 14:14:59 +02:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
|
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:19:24 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_setup( &operation1,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_decrypt_setup( &operation2,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
|
|
|
|
iv, iv_size,
|
|
|
|
&iv_length ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
output1_size = (size_t) input->len + operation1.block_size;
|
2018-06-06 18:44:09 +02:00
|
|
|
output1 = mbedtls_calloc( 1, output1_size );
|
|
|
|
TEST_ASSERT( output1 != NULL );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation1, input->x, (size_t) input->len,
|
2018-06-06 18:44:09 +02:00
|
|
|
output1, output1_size,
|
|
|
|
&output1_length ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation1,
|
|
|
|
output1 + output1_length, output1_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
output1_length += function_output_length;
|
2018-06-06 15:19:24 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
output2_size = output1_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
output2 = mbedtls_calloc( 1, output2_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output2 != NULL );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation2,
|
|
|
|
iv, iv_length ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
|
|
|
|
output2, output2_size,
|
|
|
|
&output2_length ) == PSA_SUCCESS );
|
2018-06-08 14:20:49 +02:00
|
|
|
function_output_length = 0;
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation2,
|
|
|
|
output2 + output2_length,
|
|
|
|
output2_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
output2_length += function_output_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( (size_t) input->len == output2_length );
|
|
|
|
TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output1 );
|
|
|
|
mbedtls_free( output2 );
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-08 14:28:46 +02:00
|
|
|
void cipher_verify_output_multipart( int alg_arg,
|
|
|
|
int key_type_arg,
|
2018-06-12 16:06:52 +02:00
|
|
|
data_t *key,
|
|
|
|
data_t *input,
|
2018-06-08 14:28:46 +02:00
|
|
|
int first_part_size )
|
2018-06-06 15:36:50 +02:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char iv[16] = {0};
|
|
|
|
size_t iv_size = 16;
|
|
|
|
size_t iv_length = 0;
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output1 = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output1_buffer_size = 0;
|
2018-06-06 15:36:50 +02:00
|
|
|
size_t output1_length = 0;
|
2018-06-12 16:06:52 +02:00
|
|
|
unsigned char *output2 = NULL;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t output2_buffer_size = 0;
|
2018-06-06 15:36:50 +02:00
|
|
|
size_t output2_length = 0;
|
2018-06-08 14:20:49 +02:00
|
|
|
size_t function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_cipher_operation_t operation1;
|
|
|
|
psa_cipher_operation_t operation2;
|
|
|
|
|
|
|
|
TEST_ASSERT( key != NULL );
|
|
|
|
TEST_ASSERT( input != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
|
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key->x, (size_t) key->len ) == PSA_SUCCESS );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_setup( &operation1,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
|
|
|
TEST_ASSERT( psa_decrypt_setup( &operation2,
|
|
|
|
key_slot, alg ) == PSA_SUCCESS );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
|
|
|
|
iv, iv_size,
|
|
|
|
&iv_length ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
output1_buffer_size = (size_t) input->len + operation1.block_size;
|
2018-06-08 14:20:49 +02:00
|
|
|
output1 = mbedtls_calloc( 1, output1_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output1 != NULL );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output1, output1_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output1_length += function_output_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_cipher_update( &operation1,
|
2018-06-12 16:06:52 +02:00
|
|
|
input->x + first_part_size,
|
|
|
|
(size_t) input->len - first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output1, output1_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output1_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation1,
|
|
|
|
output1 + output1_length,
|
|
|
|
output1_buffer_size - output1_length,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output1_length += function_output_length;
|
2018-06-06 15:19:24 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
output2_buffer_size = output1_length;
|
|
|
|
output2 = mbedtls_calloc( 1, output2_buffer_size );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( output2 != NULL );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_encrypt_set_iv( &operation2,
|
|
|
|
iv, iv_length ) == PSA_SUCCESS );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output2, output2_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output2_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
TEST_ASSERT( psa_cipher_update( &operation2,
|
|
|
|
output1 + first_part_size,
|
2018-06-06 18:44:09 +02:00
|
|
|
output1_length - first_part_size,
|
2018-06-08 14:20:49 +02:00
|
|
|
output2, output2_buffer_size,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output2_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-06-06 18:44:09 +02:00
|
|
|
TEST_ASSERT( psa_cipher_finish( &operation2,
|
|
|
|
output2 + output2_length,
|
2018-06-08 14:20:49 +02:00
|
|
|
output2_buffer_size - output2_length,
|
|
|
|
&function_output_length ) == PSA_SUCCESS );
|
|
|
|
output2_length += function_output_length;
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( (size_t) input->len == output2_length );
|
|
|
|
TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 );
|
2018-03-28 14:14:59 +02:00
|
|
|
|
|
|
|
exit:
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output1 );
|
|
|
|
mbedtls_free( output2 );
|
2018-03-28 14:14:59 +02:00
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-18 16:04:39 +02:00
|
|
|
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 )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
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;
|
|
|
|
size_t tag_length = 16;
|
|
|
|
psa_status_t expected_result = (psa_status_t) expected_result_arg;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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;
|
2018-06-11 19:33:02 +02:00
|
|
|
output_data = mbedtls_calloc( 1, output_size );
|
|
|
|
TEST_ASSERT( output_data != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
psa_key_policy_set_usage( &policy,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
|
|
|
|
alg );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) == PSA_SUCCESS );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_aead_encrypt( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
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 );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
|
|
|
if( PSA_SUCCESS == expected_result )
|
|
|
|
{
|
|
|
|
output_data2 = mbedtls_calloc( 1, output_length );
|
|
|
|
TEST_ASSERT( output_data2 != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_aead_decrypt( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
nonce->x, (size_t) nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
(size_t) additional_data->len,
|
2018-06-18 16:04:39 +02:00
|
|
|
output_data, output_length,
|
|
|
|
output_data2, output_length,
|
|
|
|
&output_length2 ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
expected_result );
|
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( memcmp( input_data->x, output_data2,
|
2018-06-18 15:41:12 +02:00
|
|
|
(size_t) input_data->len ) == 0 );
|
2018-06-11 19:33:02 +02:00
|
|
|
}
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_free( output_data2 );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void aead_encrypt( int key_type_arg, data_t * key_data,
|
2018-06-18 15:41:12 +02:00
|
|
|
int alg_arg, data_t * input_data,
|
|
|
|
data_t * additional_data, data_t * nonce,
|
|
|
|
data_t * expected_result )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
size_t tag_length = 16;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( additional_data != NULL );
|
2018-06-11 19:33:02 +02:00
|
|
|
TEST_ASSERT( nonce != NULL );
|
|
|
|
TEST_ASSERT( expected_result != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 );
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_aead_encrypt( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
nonce->x, (size_t) nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
(size_t) additional_data->len,
|
|
|
|
input_data->x, (size_t) input_data->len,
|
2018-06-18 16:04:39 +02:00
|
|
|
output_data, output_size,
|
|
|
|
&output_length ) == PSA_SUCCESS );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( memcmp( output_data, expected_result->x,
|
2018-06-18 15:41:12 +02:00
|
|
|
output_length ) == 0 );
|
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void aead_decrypt( int key_type_arg, data_t * key_data,
|
2018-06-18 15:41:12 +02:00
|
|
|
int alg_arg, data_t * input_data,
|
|
|
|
data_t * additional_data, data_t * nonce,
|
|
|
|
data_t * expected_data, int expected_result_arg )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
size_t tag_length = 16;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
psa_status_t expected_result = (psa_status_t) expected_result_arg;
|
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( additional_data != NULL );
|
2018-06-11 19:33:02 +02:00
|
|
|
TEST_ASSERT( nonce != NULL );
|
|
|
|
TEST_ASSERT( expected_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 );
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_aead_decrypt( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
nonce->x, (size_t) nonce->len,
|
2018-06-18 16:04:39 +02:00
|
|
|
additional_data->x,
|
|
|
|
(size_t) additional_data->len,
|
2018-06-18 15:41:12 +02:00
|
|
|
input_data->x, (size_t) input_data->len,
|
|
|
|
output_data,
|
|
|
|
output_size, &output_length ) ==
|
|
|
|
expected_result );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2018-06-18 15:41:12 +02:00
|
|
|
if( expected_result == PSA_SUCCESS )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( memcmp( output_data, expected_data->x,
|
2018-06-18 15:41:12 +02:00
|
|
|
output_length ) == 0 );
|
2018-06-11 19:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-03 23:57:22 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-18 16:04:39 +02:00
|
|
|
void signature_size( int type_arg,
|
|
|
|
int bits,
|
|
|
|
int alg_arg,
|
|
|
|
int expected_size_arg )
|
2018-02-03 23:57:22 +01:00
|
|
|
{
|
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-18 15:41:12 +02:00
|
|
|
size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
|
2018-02-03 23:57:22 +01:00
|
|
|
TEST_ASSERT( actual_size == (size_t) expected_size_arg );
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void sign_deterministic( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data,
|
|
|
|
data_t *output_data )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-02-03 23:57:22 +01:00
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
2018-02-03 23:58:03 +01:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
|
|
|
TEST_ASSERT( output_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) == PSA_SUCCESS );
|
2018-02-03 23:57:22 +01:00
|
|
|
TEST_ASSERT( psa_get_key_information( slot,
|
|
|
|
NULL,
|
|
|
|
&key_bits ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
|
|
|
|
key_bits, alg );
|
2018-02-03 23:57:22 +01:00
|
|
|
TEST_ASSERT( signature_size != 0 );
|
|
|
|
signature = mbedtls_calloc( 1, signature_size );
|
|
|
|
TEST_ASSERT( signature != NULL );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_asymmetric_sign( slot, alg,
|
2018-06-12 16:06:52 +02:00
|
|
|
input_data->x, (size_t) input_data->len,
|
2018-02-03 22:44:14 +01:00
|
|
|
NULL, 0,
|
2018-02-03 23:57:22 +01:00
|
|
|
signature, signature_size,
|
2018-02-03 22:44:14 +01:00
|
|
|
&signature_length ) == PSA_SUCCESS );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( signature_length == (size_t) output_data->len );
|
2018-06-18 15:41:12 +02:00
|
|
|
TEST_ASSERT( memcmp( signature, output_data->x,
|
|
|
|
(size_t) output_data->len ) == 0 );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
2018-02-03 23:57:22 +01:00
|
|
|
mbedtls_free( signature );
|
2018-02-03 22:44:14 +01:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void sign_fail( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data,
|
2018-02-03 22:44:14 +01:00
|
|
|
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;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-03-07 16:43:36 +01:00
|
|
|
unsigned char *signature = NULL;
|
2018-02-03 23:58:03 +01:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
|
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
signature = mbedtls_calloc( 1, signature_size );
|
|
|
|
TEST_ASSERT( signature != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-03-28 12:46:26 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
actual_status = psa_asymmetric_sign( slot, alg,
|
2018-06-12 16:06:52 +02:00
|
|
|
input_data->x,
|
|
|
|
(size_t) input_data->len,
|
2018-02-03 22:44:14 +01:00
|
|
|
NULL, 0,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length );
|
|
|
|
TEST_ASSERT( actual_status == expected_status );
|
2018-02-03 23:58:03 +01:00
|
|
|
TEST_ASSERT( signature_length == 0 );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_free( signature );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_policy( int usage_arg, int alg_arg )
|
|
|
|
{
|
|
|
|
int key_slot = 1;
|
2018-03-28 14:14:59 +02:00
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_AES;
|
2018-03-28 00:21:33 +02:00
|
|
|
unsigned char key[32] = {0};
|
|
|
|
psa_key_policy_t policy_set = {0};
|
|
|
|
psa_key_policy_t policy_get = {0};
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-03-28 00:21:33 +02:00
|
|
|
memset( key, 0x2a, sizeof( key ) );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-03-28 00:21:33 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-18 15:41:12 +02:00
|
|
|
psa_key_policy_init( &policy_set );
|
|
|
|
psa_key_policy_init( &policy_get );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
|
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) ==
|
|
|
|
(psa_key_usage_t) usage_arg );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) ==
|
|
|
|
(psa_algorithm_t) alg_arg );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
|
|
|
key, sizeof( key ) ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( policy_get.usage == policy_set.usage );
|
|
|
|
TEST_ASSERT( policy_get.alg == policy_set.alg );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-03-28 14:14:59 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
|
|
|
|
data_t *keypair )
|
2018-03-28 14:14:59 +02:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
size_t signature_length = 0;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
int actual_status = PSA_SUCCESS;
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-03-28 14:14:59 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-04-02 17:34:15 +02:00
|
|
|
if( usage_arg & PSA_KEY_USAGE_EXPORT )
|
2018-03-28 14:14:59 +02:00
|
|
|
{
|
2018-04-02 17:34:15 +02:00
|
|
|
TEST_ASSERT( keypair != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
|
2018-04-02 17:34:15 +02:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
|
2018-06-18 15:41:12 +02:00
|
|
|
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 );
|
2018-04-02 17:34:15 +02:00
|
|
|
}
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-04-02 17:34:15 +02:00
|
|
|
if( usage_arg & PSA_KEY_USAGE_SIGN )
|
|
|
|
{
|
2018-04-16 10:53:20 +02:00
|
|
|
TEST_ASSERT( keypair != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
|
2018-04-02 17:34:15 +02:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
|
2018-06-18 15:41:12 +02:00
|
|
|
keypair->x, (size_t) keypair->len ) ==
|
|
|
|
PSA_SUCCESS );
|
2018-04-02 17:34:15 +02:00
|
|
|
actual_status = psa_export_key( key_slot, NULL, 0, NULL );
|
2018-03-28 14:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( actual_status == expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-03-20 21:44:08 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_lifetime( int lifetime_arg )
|
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
psa_key_type_t key_type = PSA_ALG_CBC_BASE;
|
|
|
|
unsigned char key[32] = {0};
|
|
|
|
psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
|
|
|
|
psa_key_lifetime_t lifetime_get;
|
2018-06-06 15:18:02 +02:00
|
|
|
|
2018-03-20 21:44:08 +01:00
|
|
|
memset( key, 0x2a, sizeof( key ) );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-03-20 21:44:08 +01:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
2018-03-12 17:51:53 +01:00
|
|
|
|
2018-05-31 13:25:48 +02:00
|
|
|
TEST_ASSERT( psa_set_key_lifetime( key_slot,
|
2018-06-18 15:41:12 +02:00
|
|
|
lifetime_set ) == PSA_SUCCESS );
|
2018-03-12 17:51:53 +01:00
|
|
|
|
2018-03-28 00:29:41 +02:00
|
|
|
TEST_ASSERT( psa_import_key( key_slot, key_type,
|
|
|
|
key, sizeof( key ) ) == PSA_SUCCESS );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_get_key_lifetime( key_slot,
|
2018-06-18 15:41:12 +02:00
|
|
|
&lifetime_get ) == PSA_SUCCESS );
|
2018-06-06 15:18:02 +02:00
|
|
|
|
2018-05-31 13:25:48 +02:00
|
|
|
TEST_ASSERT( lifetime_get == lifetime_set );
|
2018-03-12 17:51:53 +01:00
|
|
|
|
2018-03-20 21:44:08 +01:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-18 16:04:39 +02:00
|
|
|
void key_lifetime_set_fail( int key_slot_arg,
|
|
|
|
int lifetime_arg,
|
|
|
|
int expected_status_arg )
|
2018-03-20 21:44:08 +01:00
|
|
|
{
|
|
|
|
int key_slot = 1;
|
|
|
|
psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
|
|
|
|
|
|
|
|
if( actual_status == PSA_SUCCESS )
|
|
|
|
actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-03-20 21:44:08 +01:00
|
|
|
TEST_ASSERT( expected_status == actual_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key_slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-05-08 10:18:38 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void asymmetric_verify( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *hash_data,
|
|
|
|
data_t *signature_data )
|
2018-05-08 10:18:38 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_policy_t policy = {0};
|
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( hash_data != NULL );
|
|
|
|
TEST_ASSERT( signature_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-05-08 10:18:38 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-05-08 10:18:38 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_asymmetric_verify( slot, alg,
|
2018-06-12 16:06:52 +02:00
|
|
|
hash_data->x, (size_t) hash_data->len,
|
2018-05-08 10:18:38 +02:00
|
|
|
NULL, 0,
|
2018-06-12 16:06:52 +02:00
|
|
|
signature_data->x,
|
|
|
|
(size_t) signature_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-05-08 10:18:38 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
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 )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( hash_data != NULL );
|
|
|
|
TEST_ASSERT( signature_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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 ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
actual_status = psa_asymmetric_verify( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
hash_data->x, (size_t) hash_data->len,
|
|
|
|
NULL, 0,
|
|
|
|
signature_data->x,
|
|
|
|
(size_t) signature_data->len );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( actual_status == expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output_size = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
size_t output_length = 0;
|
2018-05-02 22:56:12 +02:00
|
|
|
unsigned char *output2 = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output2_size = 0;
|
2018-05-02 22:56:12 +02:00
|
|
|
size_t output2_length = 0;
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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;
|
2018-05-02 22:16:26 +02:00
|
|
|
output = mbedtls_calloc( 1, output_size );
|
|
|
|
TEST_ASSERT( output != NULL );
|
2018-05-02 22:56:12 +02:00
|
|
|
output2 = mbedtls_calloc( 1, output2_size );
|
|
|
|
TEST_ASSERT( output2 != NULL );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_init( &policy );
|
2018-06-18 16:04:39 +02:00
|
|
|
psa_key_policy_set_usage( &policy,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
|
|
|
|
alg_arg );
|
2018-06-04 15:45:27 +02:00
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-06-08 18:11:54 +02:00
|
|
|
/* 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. */
|
2018-06-18 15:41:12 +02:00
|
|
|
TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
|
|
|
|
input_data->x,
|
|
|
|
(size_t) input_data->len,
|
|
|
|
NULL, 0,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
&output_length ) == PSA_SUCCESS );
|
|
|
|
|
|
|
|
TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
|
|
|
|
output,
|
|
|
|
output_length,
|
|
|
|
NULL, 0,
|
|
|
|
output2,
|
|
|
|
output2_size,
|
|
|
|
&output2_length ) == PSA_SUCCESS );
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( memcmp( input_data->x, output2,
|
|
|
|
(size_t) input_data->len ) == 0 );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( output2 );
|
2018-05-02 22:16:26 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
|
2018-06-18 15:41:12 +02:00
|
|
|
int alg_arg, data_t *input_data,
|
|
|
|
int expected_status_arg )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output_size = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
size_t output_length = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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;
|
2018-05-02 22:16:26 +02:00
|
|
|
output = mbedtls_calloc( 1, output_size );
|
|
|
|
TEST_ASSERT( output != NULL );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg_arg );
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
|
|
|
|
|
|
|
actual_status = psa_asymmetric_encrypt( slot, alg,
|
|
|
|
input_data->x,
|
|
|
|
(size_t) input_data->len,
|
|
|
|
NULL, 0,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
&output_length );
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( actual_status == expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-05-02 22:16:26 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
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 )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output_size = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
size_t output_length = 0;
|
2018-06-04 15:31:13 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
|
|
|
TEST_ASSERT( expected_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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;
|
2018-05-02 22:16:26 +02:00
|
|
|
output = mbedtls_calloc( 1, output_size );
|
|
|
|
TEST_ASSERT( output != NULL );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg );
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-05-02 22:56:12 +02:00
|
|
|
TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
|
2018-06-18 15:41:12 +02:00
|
|
|
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 );
|
2018-06-18 16:04:39 +02:00
|
|
|
TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2018-05-02 22:16:26 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-12 16:06:52 +02:00
|
|
|
void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
|
2018-06-18 15:41:12 +02:00
|
|
|
int alg_arg, data_t *input_data,
|
|
|
|
int expected_status_arg )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
|
|
|
int slot = 1;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output_size = 0;
|
2018-05-02 22:16:26 +02:00
|
|
|
size_t output_length = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_t policy = {0};
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( key_data != NULL );
|
|
|
|
TEST_ASSERT( input_data != NULL );
|
2018-06-12 16:06:52 +02:00
|
|
|
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;
|
2018-05-02 22:16:26 +02:00
|
|
|
output = mbedtls_calloc( 1, output_size );
|
|
|
|
TEST_ASSERT( output != NULL );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
|
|
|
|
2018-06-04 15:45:27 +02:00
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg );
|
|
|
|
TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( psa_import_key( slot, key_type,
|
2018-06-12 16:06:52 +02:00
|
|
|
key_data->x, (size_t) key_data->len ) ==
|
2018-06-18 15:41:12 +02:00
|
|
|
PSA_SUCCESS );
|
|
|
|
|
|
|
|
actual_status = psa_asymmetric_decrypt( slot, alg,
|
|
|
|
input_data->x,
|
|
|
|
(size_t) input_data->len,
|
|
|
|
NULL, 0,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
&output_length );
|
2018-05-02 22:16:26 +02:00
|
|
|
TEST_ASSERT( actual_status == expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( slot );
|
2018-06-18 15:41:12 +02:00
|
|
|
mbedtls_free( output );
|
2018-05-02 22:16:26 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
}
|
2018-05-31 13:25:48 +02:00
|
|
|
/* END_CASE */
|