2020-07-16 18:28:59 +00:00
|
|
|
/* BEGIN_HEADER */
|
2021-04-30 15:00:34 +00:00
|
|
|
#include "test/drivers/test_driver.h"
|
2020-07-16 18:28:59 +00:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2020-09-07 10:58:16 +00:00
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_DRIVERS:PSA_CRYPTO_DRIVER_TEST
|
2020-07-16 18:28:59 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2021-05-06 09:56:20 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_DETERMINISTIC_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */
|
2021-04-23 10:49:00 +00:00
|
|
|
void ecdsa_sign_hash( int force_status_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *expected_output,
|
|
|
|
int fake_output,
|
|
|
|
int expected_status_arg )
|
2020-07-16 18:28:59 +00:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-16 18:28:59 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 );
|
|
|
|
uint8_t signature[64];
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_status_t actual_status;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-16 18:28:59 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
psa_set_key_type( &attributes,
|
2021-04-13 21:12:42 +00:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) );
|
2020-07-16 18:28:59 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 10:58:16 +00:00
|
|
|
key_input->x, key_input->len,
|
2020-08-04 12:58:35 +00:00
|
|
|
&key );
|
2020-07-16 18:28:59 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_status = force_status;
|
2020-09-07 10:58:16 +00:00
|
|
|
if( fake_output == 1 )
|
2020-07-16 18:28:59 +00:00
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output =
|
|
|
|
expected_output->x;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output_length =
|
|
|
|
expected_output->len;
|
2020-07-16 18:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
actual_status = psa_sign_hash( key, alg,
|
2020-09-07 10:58:16 +00:00
|
|
|
data_input->x, data_input->len,
|
2020-07-16 18:28:59 +00:00
|
|
|
signature, sizeof( signature ),
|
|
|
|
&signature_length );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( signature, signature_length,
|
2020-09-07 10:58:16 +00:00
|
|
|
expected_output->x, expected_output->len );
|
2020-07-16 18:28:59 +00:00
|
|
|
}
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_signature_sign_hooks.hits, 1 );
|
2020-07-16 18:28:59 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-07-16 18:28:59 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-16 18:28:59 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-17 17:46:15 +00:00
|
|
|
|
2021-05-06 09:47:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */
|
2021-04-23 10:49:00 +00:00
|
|
|
void ecdsa_verify_hash( int force_status_arg,
|
|
|
|
int register_public_key,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *signature_input,
|
|
|
|
int expected_status_arg )
|
2020-07-17 17:46:15 +00:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-17 17:46:15 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-05-06 09:47:31 +00:00
|
|
|
psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
|
2020-07-17 17:46:15 +00:00
|
|
|
psa_status_t actual_status;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-17 17:46:15 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
if( register_public_key )
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
2021-04-13 21:12:42 +00:00
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY( PSA_ECC_FAMILY_SECP_R1 ) );
|
2020-07-17 17:46:15 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 10:58:16 +00:00
|
|
|
key_input->x, key_input->len,
|
2020-08-04 12:58:35 +00:00
|
|
|
&key );
|
2020-07-17 17:46:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
2021-04-13 21:12:42 +00:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) );
|
2020-07-17 17:46:15 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 10:58:16 +00:00
|
|
|
key_input->x, key_input->len,
|
2020-08-04 12:58:35 +00:00
|
|
|
&key );
|
2020-07-17 17:46:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_verify_hooks.forced_status = force_status;
|
2020-07-17 17:46:15 +00:00
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
actual_status = psa_verify_hash( key, alg,
|
2020-09-07 10:58:16 +00:00
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature_input->x, signature_input->len );
|
2020-07-17 17:46:15 +00:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_signature_verify_hooks.hits, 1 );
|
2020-07-17 17:46:15 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-07-17 17:46:15 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-17 17:46:15 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-20 13:33:08 +00:00
|
|
|
|
2021-05-06 09:56:20 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_DETERMINISTIC_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */
|
2021-04-23 10:38:33 +00:00
|
|
|
void ecdsa_sign_message( int force_status_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *expected_output,
|
|
|
|
int fake_output,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 );
|
|
|
|
uint8_t signature[64];
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
psa_set_key_type( &attributes,
|
2021-05-13 14:27:46 +00:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) );
|
2021-04-23 10:38:33 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key );
|
|
|
|
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_status = force_status;
|
|
|
|
if( fake_output == 1 )
|
|
|
|
{
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output =
|
|
|
|
expected_output->x;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output_length =
|
|
|
|
expected_output->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual_status = psa_sign_message( key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature, sizeof( signature ),
|
|
|
|
&signature_length );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( signature, signature_length,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
}
|
2021-05-03 14:29:54 +00:00
|
|
|
/* In the builtin algorithm the driver is called twice. */
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_signature_sign_hooks.hits,
|
|
|
|
force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1 );
|
2021-04-23 10:38:33 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-06 09:47:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */
|
2021-04-23 10:38:33 +00:00
|
|
|
void ecdsa_verify_message( int force_status_arg,
|
|
|
|
int register_public_key,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *signature_input,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-05-06 09:47:31 +00:00
|
|
|
psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
|
2021-04-23 10:38:33 +00:00
|
|
|
psa_status_t actual_status;
|
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
if( register_public_key )
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
2021-05-13 14:27:46 +00:00
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY( PSA_ECC_FAMILY_SECP_R1 ) );
|
2021-04-23 10:38:33 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
2021-05-13 14:27:46 +00:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) );
|
2021-04-23 10:38:33 +00:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key );
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_test_driver_signature_verify_hooks.forced_status = force_status;
|
|
|
|
|
|
|
|
actual_status = psa_verify_message( key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature_input->x, signature_input->len );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2021-05-03 14:29:54 +00:00
|
|
|
/* In the builtin algorithm the driver is called twice. */
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_signature_verify_hooks.hits,
|
|
|
|
force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1 );
|
2021-04-23 10:38:33 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-06 09:56:20 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
|
2020-07-20 13:33:08 +00:00
|
|
|
void generate_key( int force_status_arg,
|
2020-09-07 10:58:16 +00:00
|
|
|
data_t *fake_output,
|
2020-07-20 13:33:08 +00:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-20 13:33:08 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
|
2020-09-07 10:58:16 +00:00
|
|
|
const uint8_t *expected_output = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
2020-07-20 13:33:08 +00:00
|
|
|
psa_status_t actual_status;
|
2020-09-07 10:58:16 +00:00
|
|
|
uint8_t actual_output[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(256)] = {0};
|
2020-07-20 13:33:08 +00:00
|
|
|
size_t actual_output_length;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-07-20 13:33:08 +00:00
|
|
|
|
|
|
|
psa_set_key_type( &attributes,
|
2021-04-13 21:12:42 +00:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) );
|
2020-07-20 13:33:08 +00:00
|
|
|
psa_set_key_bits( &attributes, 256 );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
|
2020-09-07 10:58:16 +00:00
|
|
|
if( fake_output->len > 0 )
|
2020-07-20 13:33:08 +00:00
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
expected_output =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output =
|
|
|
|
fake_output->x;
|
|
|
|
|
|
|
|
expected_output_length =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output_length =
|
2020-09-07 10:58:16 +00:00
|
|
|
fake_output->len;
|
2020-07-20 13:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks.hits = 0;
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-07-20 13:33:08 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
actual_status = psa_generate_key( &attributes, &key );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 );
|
2020-07-20 13:33:08 +00:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
|
|
|
|
if( actual_status == PSA_SUCCESS )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_export_key( key, actual_output, sizeof(actual_output), &actual_output_length );
|
2020-07-20 13:33:08 +00:00
|
|
|
|
2020-09-07 10:58:16 +00:00
|
|
|
if( fake_output->len > 0 )
|
2020-07-20 13:33:08 +00:00
|
|
|
{
|
|
|
|
ASSERT_COMPARE( actual_output, actual_output_length,
|
|
|
|
expected_output, expected_output_length );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t zeroes = 0;
|
|
|
|
for( size_t i = 0; i < sizeof(actual_output); i++ )
|
|
|
|
{
|
|
|
|
if( actual_output[i] == 0)
|
|
|
|
zeroes++;
|
|
|
|
}
|
|
|
|
TEST_ASSERT( zeroes != sizeof(actual_output) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-07-20 13:33:08 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-07-20 13:33:08 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2021-05-06 09:56:20 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
|
2020-10-13 15:43:44 +00:00
|
|
|
void validate_key( int force_status_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-10-13 15:43:44 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t actual_status;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-13 15:43:44 +00:00
|
|
|
|
|
|
|
psa_set_key_type( &attributes,
|
|
|
|
key_type );
|
|
|
|
psa_set_key_bits( &attributes, 0 );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-10-13 15:43:44 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
actual_status = psa_import_key( &attributes, key_input->x, key_input->len, &key );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 );
|
2020-10-13 15:43:44 +00:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-10-13 15:43:44 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-13 15:43:44 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-06 09:56:20 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
|
2020-10-14 12:39:20 +00:00
|
|
|
void export_key( int force_status_arg,
|
|
|
|
data_t *fake_output,
|
|
|
|
int key_in_type_arg,
|
|
|
|
data_t *key_in,
|
|
|
|
int key_out_type_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_handle_t handle = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t input_key_type = key_in_type_arg;
|
|
|
|
psa_key_type_t output_key_type = key_out_type_arg;
|
|
|
|
const uint8_t *expected_output_ptr = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
uint8_t actual_output[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256)] = {0};
|
|
|
|
size_t actual_output_length;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-14 12:39:20 +00:00
|
|
|
|
|
|
|
psa_set_key_type( &attributes, input_key_type );
|
|
|
|
psa_set_key_bits( &attributes, 256 );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_in->x, key_in->len, &handle ) );
|
|
|
|
|
|
|
|
if( fake_output->len > 0 )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
expected_output_ptr =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output =
|
|
|
|
fake_output->x;
|
|
|
|
|
|
|
|
expected_output_length =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output_length =
|
2020-10-14 12:39:20 +00:00
|
|
|
fake_output->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expected_output_ptr = expected_output->x;
|
|
|
|
expected_output_length = expected_output->len;
|
|
|
|
}
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks.hits = 0;
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-10-14 12:39:20 +00:00
|
|
|
|
|
|
|
if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( output_key_type ) )
|
|
|
|
actual_status = psa_export_public_key( handle, actual_output, sizeof(actual_output), &actual_output_length );
|
|
|
|
else
|
|
|
|
actual_status = psa_export_key( handle, actual_output, sizeof(actual_output), &actual_output_length );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
|
|
|
|
if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( output_key_type ) &&
|
|
|
|
!PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( input_key_type ) )
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 );
|
2020-10-14 12:39:20 +00:00
|
|
|
|
|
|
|
if( actual_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( actual_output, actual_output_length,
|
|
|
|
expected_output_ptr, expected_output_length );
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( handle );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-14 12:39:20 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-09-03 13:31:04 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_encrypt( int alg_arg, int key_type_arg,
|
2020-08-04 12:58:35 +00:00
|
|
|
data_t *key_data, data_t *iv,
|
2020-09-03 13:31:04 +00:00
|
|
|
data_t *input, data_t *expected_output,
|
2020-09-10 16:07:57 +00:00
|
|
|
int mock_output_arg,
|
2020-09-10 12:32:26 +00:00
|
|
|
int force_status_arg,
|
2020-09-03 13:31:04 +00:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 13:31:04 +00:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-09-10 12:32:26 +00:00
|
|
|
psa_status_t force_status = force_status_arg;
|
2020-09-03 13:31:04 +00:00
|
|
|
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 = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = force_status;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
output_buffer_size = ( (size_t) input->len +
|
2020-12-18 13:23:51 +00:00
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
2020-09-10 16:07:57 +00:00
|
|
|
if( mock_output_arg )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len;
|
2020-09-10 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-09-10 16:07:57 +00:00
|
|
|
if( mock_output_arg )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = NULL;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:31:04 +00:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length );
|
2020-09-09 09:51:45 +00:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 2 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
|
|
|
// driver function should've been called as part of the finish() core routine
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
|
2020-08-04 12:58:35 +00:00
|
|
|
data_t *key_data, data_t *iv,
|
2020-09-03 13:31:04 +00:00
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg, int output2_length_arg,
|
|
|
|
data_t *expected_output )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 13:31:04 +00:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
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 = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
output_buffer_size = ( (size_t) input->len +
|
2020-12-18 13:23:51 +00:00
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
|
|
|
TEST_ASSERT( first_part_size <= input->len );
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( function_output_length == output1_length );
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
TEST_ASSERT( function_output_length == output2_length );
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
2020-09-09 09:51:45 +00:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0 ;
|
2020-09-03 13:31:04 +00:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
|
2020-08-04 12:58:35 +00:00
|
|
|
data_t *key_data, data_t *iv,
|
2020-09-03 13:31:04 +00:00
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg, int output2_length_arg,
|
|
|
|
data_t *expected_output )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 13:31:04 +00:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
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 = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
output_buffer_size = ( (size_t) input->len +
|
2020-12-18 13:23:51 +00:00
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
|
|
|
TEST_ASSERT( first_part_size <= input->len );
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( function_output_length == output1_length );
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( function_output_length == output2_length );
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
2020-09-09 09:51:45 +00:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_decrypt( int alg_arg, int key_type_arg,
|
2020-08-04 12:58:35 +00:00
|
|
|
data_t *key_data, data_t *iv,
|
2020-09-03 13:31:04 +00:00
|
|
|
data_t *input, data_t *expected_output,
|
2020-09-10 16:07:57 +00:00
|
|
|
int mock_output_arg,
|
2020-09-10 12:32:26 +00:00
|
|
|
int force_status_arg,
|
2020-09-03 13:31:04 +00:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 13:31:04 +00:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-09-10 12:32:26 +00:00
|
|
|
psa_status_t force_status = force_status_arg;
|
2020-09-03 13:31:04 +00:00
|
|
|
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 = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = force_status;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
output_buffer_size = ( (size_t) input->len +
|
2020-12-18 13:23:51 +00:00
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
2020-09-10 16:07:57 +00:00
|
|
|
if( mock_output_arg )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len;
|
2020-09-10 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
2020-09-10 16:07:57 +00:00
|
|
|
if( mock_output_arg )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = NULL;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:31:04 +00:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length );
|
2020-09-09 09:51:45 +00:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 2 : 0 ) );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 13:31:04 +00:00
|
|
|
|
|
|
|
total_output_length += function_output_length;
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-03 13:31:04 +00:00
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-09-03 13:31:04 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 13:31:04 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_entry_points( int alg_arg, int key_type_arg,
|
2020-08-04 12:58:35 +00:00
|
|
|
data_t *key_data, data_t *iv,
|
2020-09-10 16:07:57 +00:00
|
|
|
data_t *input )
|
|
|
|
{
|
2020-08-04 12:58:35 +00:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-10 16:07:57 +00:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-10 16:07:57 +00:00
|
|
|
|
2020-09-11 09:44:50 +00:00
|
|
|
ASSERT_ALLOC( output, input->len + 16 );
|
|
|
|
output_buffer_size = input->len + 16;
|
|
|
|
|
2020-09-10 16:07:57 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
2020-08-04 12:58:35 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test setup call, encrypt */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
2020-09-10 16:07:57 +00:00
|
|
|
/* When setup fails, it shouldn't call any further entry points */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test setup call failure, decrypt */
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_decrypt_setup( &operation, key, alg );
|
2020-09-10 16:07:57 +00:00
|
|
|
/* When setup fails, it shouldn't call any further entry points */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test IV setting failure */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2020-09-10 16:07:57 +00:00
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
|
|
|
/* When setting the IV fails, it should call abort too */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-11 09:44:50 +00:00
|
|
|
psa_cipher_abort( &operation );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test IV generation failure */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_generate_iv( &operation, output, 16, &function_output_length );
|
|
|
|
/* When generating the IV fails, it should call abort too */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-11 09:44:50 +00:00
|
|
|
psa_cipher_abort( &operation );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test update failure */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2020-09-10 16:07:57 +00:00
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
/* When the update call fails, it should call abort too */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-11 09:44:50 +00:00
|
|
|
psa_cipher_abort( &operation );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
/* Test finish failure */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2020-08-04 12:58:35 +00:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 16:07:57 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2020-09-10 16:07:57 +00:00
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size - function_output_length,
|
|
|
|
&function_output_length );
|
|
|
|
/* When the finish call fails, it should call abort too */
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
|
2020-09-11 09:44:50 +00:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-11 09:44:50 +00:00
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
|
2020-09-11 09:44:50 +00:00
|
|
|
psa_cipher_abort( &operation );
|
2020-09-10 16:07:57 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 12:58:35 +00:00
|
|
|
psa_destroy_key( key );
|
2020-09-10 16:07:57 +00:00
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-10 16:07:57 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-03-23 08:33:39 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_encrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_result,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-03-19 17:46:15 +00:00
|
|
|
size_t key_bits;
|
2021-03-23 08:33:39 +00:00
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 08:33:39 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2021-03-19 17:46:15 +00:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
|
|
|
|
alg );
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
|
|
|
TEST_EQUAL( output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
|
|
|
|
TEST_ASSERT( output_size <=
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
2021-03-23 08:33:39 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
2021-03-23 08:33:39 +00:00
|
|
|
status = psa_aead_encrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x, additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length );
|
2021-06-24 13:37:53 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_encrypt, 1 );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status );
|
2021-03-23 08:33:39 +00:00
|
|
|
|
|
|
|
TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ?
|
|
|
|
PSA_SUCCESS : forced_status );
|
|
|
|
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( expected_result->x, expected_result->len,
|
|
|
|
output_data, output_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 08:33:39 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_decrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_data,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-03-19 17:46:15 +00:00
|
|
|
size_t key_bits;
|
2021-03-23 08:33:39 +00:00
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 08:33:39 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2021-03-19 17:46:15 +00:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
|
|
|
|
alg );
|
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
2021-03-23 08:33:39 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
2021-03-23 08:33:39 +00:00
|
|
|
status = psa_aead_decrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length );
|
2021-06-24 13:37:53 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_decrypt, 1 );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status );
|
2021-03-23 08:33:39 +00:00
|
|
|
|
|
|
|
TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ?
|
|
|
|
PSA_SUCCESS : forced_status );
|
|
|
|
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( expected_data->x, expected_data->len,
|
|
|
|
output_data, output_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 08:33:39 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-02-22 11:44:15 +00:00
|
|
|
|
2021-04-29 19:10:11 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mac_sign( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t *actual_mac = NULL;
|
|
|
|
size_t mac_buffer_size =
|
|
|
|
PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
|
|
|
|
size_t mac_length = 0;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
|
|
|
|
/* We expect PSA_MAC_LENGTH to be exact. */
|
|
|
|
TEST_ASSERT( expected_mac->len == mac_buffer_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( actual_mac, mac_buffer_size );
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
/* Calculate the MAC. */
|
|
|
|
status = psa_mac_sign_setup( &operation, key, alg );
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( forced_status, status );
|
|
|
|
|
|
|
|
status = psa_mac_update( &operation,
|
|
|
|
input->x, input->len );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
|
|
|
|
|
|
|
|
status = psa_mac_sign_finish( &operation,
|
|
|
|
actual_mac, mac_buffer_size,
|
|
|
|
&mac_length );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
|
|
|
|
actual_mac, mac_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_free( actual_mac );
|
|
|
|
actual_mac = NULL;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_mac_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_free( actual_mac );
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 19:10:11 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mac_verify( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
/* Test the correct MAC. */
|
|
|
|
status = psa_mac_verify_setup( &operation, key, alg );
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( forced_status, status );
|
|
|
|
|
|
|
|
status = psa_mac_update( &operation,
|
|
|
|
input->x, input->len );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
|
|
|
|
|
|
|
|
status = psa_mac_verify_finish( &operation,
|
|
|
|
expected_mac->x,
|
|
|
|
expected_mac->len );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
|
|
|
|
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
|
2021-04-29 19:10:11 +00:00
|
|
|
else
|
2021-05-10 09:18:20 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
|
2021-04-29 19:10:11 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_mac_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
2021-05-10 09:18:20 +00:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 19:10:11 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-06 11:17:36 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2021-02-22 11:44:15 +00:00
|
|
|
void builtin_key_export( int builtin_key_id_arg,
|
|
|
|
int builtin_key_type_arg,
|
|
|
|
int builtin_key_bits_arg,
|
|
|
|
int builtin_key_algorithm_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
|
|
|
|
psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
|
|
|
|
psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
|
|
|
|
size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2021-03-18 16:17:40 +00:00
|
|
|
mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id );
|
2021-02-22 11:44:15 +00:00
|
|
|
uint8_t* output_buffer = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
ASSERT_ALLOC( output_buffer, expected_output->len );
|
|
|
|
|
|
|
|
actual_status = psa_export_key( key, output_buffer, expected_output->len, &output_size );
|
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( actual_status );
|
|
|
|
TEST_EQUAL( output_size, expected_output->len );
|
|
|
|
ASSERT_COMPARE( output_buffer, output_size,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( actual_status != expected_status )
|
2021-03-18 16:17:40 +00:00
|
|
|
fprintf( stderr, "Expected %d but got %d\n", expected_status, actual_status );
|
2021-02-22 11:44:15 +00:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
TEST_EQUAL( output_size, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output_buffer );
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-18 19:25:53 +00:00
|
|
|
/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2021-02-22 11:44:15 +00:00
|
|
|
void builtin_pubkey_export( int builtin_key_id_arg,
|
|
|
|
int builtin_key_type_arg,
|
|
|
|
int builtin_key_bits_arg,
|
|
|
|
int builtin_key_algorithm_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
|
|
|
|
psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
|
|
|
|
psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
|
|
|
|
size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2021-03-18 16:17:40 +00:00
|
|
|
mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id );
|
2021-02-22 11:44:15 +00:00
|
|
|
uint8_t* output_buffer = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
ASSERT_ALLOC( output_buffer, expected_output->len );
|
|
|
|
|
|
|
|
actual_status = psa_export_public_key( key, output_buffer, expected_output->len, &output_size );
|
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( actual_status );
|
|
|
|
TEST_EQUAL( output_size, expected_output->len );
|
|
|
|
ASSERT_COMPARE( output_buffer, output_size,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
TEST_EQUAL( output_size, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output_buffer );
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_compute( int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_length;
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_hash_compute( alg, input->x, input->len,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ), expected_status );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( output, output_length, hash->x, hash->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-05-01 12:38:42 +00:00
|
|
|
void hash_multipart_setup( int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg )
|
2021-04-06 16:20:29 +00:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2021-04-06 16:20:29 +00:00
|
|
|
TEST_EQUAL( psa_hash_setup( &operation, alg ), expected_status );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
|
2021-04-06 16:20:29 +00:00
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 2 );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
|
2021-04-06 16:20:29 +00:00
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 4 );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
ASSERT_COMPARE( output, output_length, hash->x, hash->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_hash_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-01 12:52:54 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_multipart_update( int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/*
|
2021-05-04 14:11:06 +00:00
|
|
|
* Update inactive operation, the driver shouldn't be called.
|
2021-05-01 12:52:54 +00:00
|
|
|
*/
|
|
|
|
TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
|
|
|
TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ),
|
|
|
|
forced_status );
|
|
|
|
/* One or two more calls to the driver interface: update or update + abort */
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
|
|
|
|
forced_status == PSA_SUCCESS ? 2 : 3 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ) );
|
|
|
|
/* Two calls to the driver interface: update + abort */
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( output, output_length, hash->x, hash->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_hash_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-01 13:02:51 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_multipart_finish( int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/*
|
2021-05-04 14:11:06 +00:00
|
|
|
* Finish inactive operation, the driver shouldn't be called.
|
2021-05-01 13:02:51 +00:00
|
|
|
*/
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation, output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ),
|
|
|
|
forced_status );
|
|
|
|
/* Two more calls to the driver interface: finish + abort */
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 4 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
|
|
|
ASSERT_COMPARE( output, output_length, hash->x, hash->len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_hash_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-06 16:20:29 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_clone( int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t source_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t target_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/*
|
2021-05-04 14:11:06 +00:00
|
|
|
* Clone inactive operation, the driver shouldn't be called.
|
2021-04-06 16:20:29 +00:00
|
|
|
*/
|
|
|
|
TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_setup( &source_operation, alg ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2021-04-06 16:20:29 +00:00
|
|
|
TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
|
|
|
|
forced_status );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
|
2021-04-06 16:20:29 +00:00
|
|
|
forced_status == PSA_SUCCESS ? 2 : 3 );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
if( forced_status == PSA_SUCCESS )
|
|
|
|
{
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
PSA_ASSERT( psa_hash_update( &target_operation,
|
|
|
|
input->x, input->len ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_finish( &target_operation,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ) );
|
2021-04-13 10:41:34 +00:00
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 3 );
|
|
|
|
TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
|
2021-04-06 16:20:29 +00:00
|
|
|
|
|
|
|
ASSERT_COMPARE( output, output_length, hash->x, hash->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_hash_abort( &source_operation );
|
|
|
|
psa_hash_abort( &target_operation );
|
|
|
|
mbedtls_free( output );
|
|
|
|
PSA_DONE( );
|
2021-04-13 10:41:34 +00:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 16:20:29 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|