2020-07-16 20:28:59 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "test/psa_crypto_helpers.h"
|
|
|
|
|
|
|
|
#include "drivers/test_driver.h"
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2020-09-07 12:58:16 +02:00
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_DRIVERS:PSA_CRYPTO_DRIVER_TEST
|
2020-07-16 20:28:59 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
|
|
|
|
void ecdsa_sign( int force_status_arg,
|
2020-09-07 12:58:16 +02:00
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *expected_output,
|
2020-07-16 20:28:59 +02:00
|
|
|
int fake_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_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;
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_sign_hooks = test_driver_signature_hooks_init();
|
2020-07-16 20:28:59 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
psa_set_key_type( &attributes,
|
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP_R1 ) );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 12:58:16 +02:00
|
|
|
key_input->x, key_input->len,
|
2020-07-16 20:28:59 +02:00
|
|
|
&handle );
|
|
|
|
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_sign_hooks.forced_status = force_status;
|
|
|
|
if( fake_output == 1 )
|
2020-07-16 20:28:59 +02:00
|
|
|
{
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_sign_hooks.forced_output = expected_output->x;
|
|
|
|
test_driver_signature_sign_hooks.forced_output_length = expected_output->len;
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
actual_status = psa_sign_hash( handle, alg,
|
2020-09-07 12:58:16 +02:00
|
|
|
data_input->x, data_input->len,
|
2020-07-16 20:28:59 +02: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 12:58:16 +02:00
|
|
|
expected_output->x, expected_output->len );
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
2020-09-07 12:58:16 +02:00
|
|
|
TEST_EQUAL( test_driver_signature_sign_hooks.hits, 1 );
|
2020-07-16 20:28:59 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( handle );
|
|
|
|
PSA_DONE( );
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_sign_hooks = test_driver_signature_hooks_init();
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-17 19:46:15 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
|
|
|
|
void ecdsa_verify( int force_status_arg,
|
|
|
|
int register_public_key,
|
2020-09-07 12:58:16 +02:00
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *signature_input,
|
2020-07-17 19:46:15 +02:00
|
|
|
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_algorithm_t alg = PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 );
|
|
|
|
psa_status_t actual_status;
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_verify_hooks = test_driver_signature_hooks_init();
|
2020-07-17 19:46:15 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
if( register_public_key )
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY( PSA_ECC_CURVE_SECP_R1 ) );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 12:58:16 +02:00
|
|
|
key_input->x, key_input->len,
|
2020-07-17 19:46:15 +02:00
|
|
|
&handle );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes,
|
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP_R1 ) );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_import_key( &attributes,
|
2020-09-07 12:58:16 +02:00
|
|
|
key_input->x, key_input->len,
|
2020-07-17 19:46:15 +02:00
|
|
|
&handle );
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_verify_hooks.forced_status = force_status;
|
2020-07-17 19:46:15 +02:00
|
|
|
|
|
|
|
actual_status = psa_verify_hash( handle, alg,
|
2020-09-07 12:58:16 +02:00
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature_input->x, signature_input->len );
|
2020-07-17 19:46:15 +02:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2020-09-07 12:58:16 +02:00
|
|
|
TEST_EQUAL( test_driver_signature_verify_hooks.hits, 1 );
|
2020-07-17 19:46:15 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( handle );
|
|
|
|
PSA_DONE( );
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_signature_verify_hooks = test_driver_signature_hooks_init();
|
2020-07-17 19:46:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-20 15:33:08 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED */
|
|
|
|
void generate_key( int force_status_arg,
|
2020-09-07 12:58:16 +02:00
|
|
|
data_t *fake_output,
|
2020-07-20 15:33:08 +02:00
|
|
|
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_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 );
|
2020-09-07 12:58:16 +02:00
|
|
|
const uint8_t *expected_output = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
2020-07-20 15:33:08 +02:00
|
|
|
psa_status_t actual_status;
|
2020-09-07 12:58:16 +02:00
|
|
|
uint8_t actual_output[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(256)] = {0};
|
2020-07-20 15:33:08 +02:00
|
|
|
size_t actual_output_length;
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_keygen_hooks = test_driver_keygen_hooks_init();
|
2020-07-20 15:33:08 +02:00
|
|
|
|
|
|
|
psa_set_key_type( &attributes,
|
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP_R1 ) );
|
|
|
|
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 12:58:16 +02:00
|
|
|
if( fake_output->len > 0 )
|
2020-07-20 15:33:08 +02:00
|
|
|
{
|
2020-09-07 12:58:16 +02:00
|
|
|
expected_output = test_driver_keygen_hooks.forced_output = fake_output->x;
|
|
|
|
expected_output_length = test_driver_keygen_hooks.forced_output_length =
|
|
|
|
fake_output->len;
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_keygen_hooks.hits = 0;
|
|
|
|
test_driver_keygen_hooks.forced_status = force_status;
|
2020-07-20 15:33:08 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
actual_status = psa_generate_key( &attributes, &handle );
|
2020-09-07 12:58:16 +02:00
|
|
|
TEST_EQUAL( test_driver_keygen_hooks.hits, 1 );
|
2020-07-20 15:33:08 +02:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
|
|
|
|
if( actual_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
psa_export_key( handle, actual_output, sizeof(actual_output), &actual_output_length );
|
|
|
|
|
2020-09-07 12:58:16 +02:00
|
|
|
if( fake_output->len > 0 )
|
2020-07-20 15:33:08 +02: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 );
|
|
|
|
psa_destroy_key( handle );
|
|
|
|
PSA_DONE( );
|
2020-09-07 12:58:16 +02:00
|
|
|
test_driver_keygen_hooks = test_driver_keygen_hooks_init();
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|