@@ -402,6 +402,13 @@ exit:
402
402
/* Other test helper functions */
403
403
/****************************************************************/
404
404
405
+ typedef enum
406
+ {
407
+ SIGN_IN_SOFTWARE_AND_PARALLEL_CREATION,
408
+ SIGN_IN_DRIVER_AND_PARALLEL_CREATION,
409
+ SIGN_IN_DRIVER_THEN_EXPORT_PUBLIC,
410
+ } sign_verify_method_t;
411
+
405
412
/* Check that the attributes of a key reported by psa_get_key_attributes()
406
413
* are consistent with the attributes used when creating the key. */
407
414
static int check_key_attributes(
@@ -879,7 +886,7 @@ exit:
879
886
/* END_CASE */
880
887
881
888
/* BEGIN_CASE */
882
- void sign_verify( int sign_in_driver ,
889
+ void sign_verify( int flow ,
883
890
int type_arg, int alg_arg,
884
891
int bits_arg, data_t *key_material,
885
892
data_t *input )
@@ -898,75 +905,121 @@ void sign_verify( int sign_in_driver,
898
905
psa_key_id_t id = 1;
899
906
psa_key_handle_t drv_handle = 0; /* key managed by the driver */
900
907
psa_key_handle_t sw_handle = 0; /* transparent key */
901
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
908
+ psa_key_attributes_t sw_attributes = PSA_KEY_ATTRIBUTES_INIT;
909
+ psa_key_attributes_t drv_attributes;
902
910
uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE];
903
911
size_t signature_length;
904
912
905
913
memset( &driver, 0, sizeof( driver ) );
906
914
memset( &key_management, 0, sizeof( key_management ) );
915
+ memset( &asymmetric, 0, sizeof( asymmetric ) );
907
916
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
908
917
driver.key_management = &key_management;
909
918
driver.asymmetric = &asymmetric;
910
- driver.persistent_data_size = sizeof( psa_key_slot_number_t );
911
919
driver.persistent_data_size = sizeof( ram_slot_usage_t );
912
920
key_management.p_allocate = ram_allocate;
913
921
key_management.p_destroy = ram_destroy;
914
922
if( generating )
915
923
key_management.p_generate = ram_fake_generate;
916
924
else
917
925
key_management.p_import = ram_import;
918
- if( sign_in_driver )
919
- asymmetric.p_sign = ram_sign;
926
+ switch( flow )
927
+ {
928
+ case SIGN_IN_SOFTWARE_AND_PARALLEL_CREATION:
929
+ break;
930
+ case SIGN_IN_DRIVER_AND_PARALLEL_CREATION:
931
+ asymmetric.p_sign = ram_sign;
932
+ break;
933
+ case SIGN_IN_DRIVER_THEN_EXPORT_PUBLIC:
934
+ asymmetric.p_sign = ram_sign;
935
+ key_management.p_export_public = ram_export_public;
936
+ break;
937
+ default:
938
+ TEST_ASSERT( ! "unsupported flow (should be SIGN_IN_xxx)" );
939
+ break;
940
+ }
920
941
asymmetric.p_verify = ram_verify;
921
942
922
943
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
923
944
PSA_ASSERT( psa_crypto_init( ) );
924
945
925
- /* Create two keys with the same key material: a transparent key,
926
- * and one that goes through the driver. */
927
- psa_set_key_usage_flags( &attributes ,
946
+ /* Prepare to create two keys with the same key material: a transparent
947
+ * key, and one that goes through the driver. */
948
+ psa_set_key_usage_flags( &sw_attributes ,
928
949
PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
929
- psa_set_key_algorithm( &attributes , alg );
930
- psa_set_key_type( &attributes , type );
931
- PSA_ASSERT( psa_import_key( &attributes,
932
- key_material->x, key_material->len,
933
- &sw_handle ) );
934
- psa_set_key_id( &attributes, id );
935
- psa_set_key_lifetime( &attributes, lifetime );
950
+ psa_set_key_algorithm( &sw_attributes , alg );
951
+ psa_set_key_type( &sw_attributes , type );
952
+ drv_attributes = sw_attributes;
953
+ psa_set_key_id( &drv_attributes, id );
954
+ psa_set_key_lifetime( &drv_attributes, lifetime );
955
+
956
+ /* Create the key in the driver. */
936
957
if( generating )
937
958
{
938
- psa_set_key_bits( &attributes , bits );
939
- PSA_ASSERT( psa_generate_key( &attributes , &drv_handle ) );
959
+ psa_set_key_bits( &drv_attributes , bits );
960
+ PSA_ASSERT( psa_generate_key( &drv_attributes , &drv_handle ) );
940
961
/* Since we called a generate method that does not actually
941
962
* generate material, store the desired result of generation in
942
963
* the mock secure element storage. */
943
- PSA_ASSERT( psa_get_key_attributes( drv_handle, &attributes ) );
964
+ PSA_ASSERT( psa_get_key_attributes( drv_handle, &drv_attributes ) );
944
965
TEST_ASSERT( key_material->len == PSA_BITS_TO_BYTES( bits ) );
945
966
memcpy( ram_slots[ram_min_slot].content, key_material->x,
946
967
key_material->len );
947
968
}
948
969
else
949
970
{
950
- PSA_ASSERT( psa_import_key( &attributes ,
971
+ PSA_ASSERT( psa_import_key( &drv_attributes ,
951
972
key_material->x, key_material->len,
952
973
&drv_handle ) );
953
974
}
954
975
976
+ /* Either import the same key in software, or export the driver's
977
+ * public key and import that. */
978
+ switch( flow )
979
+ {
980
+ case SIGN_IN_SOFTWARE_AND_PARALLEL_CREATION:
981
+ case SIGN_IN_DRIVER_AND_PARALLEL_CREATION:
982
+ PSA_ASSERT( psa_import_key( &sw_attributes,
983
+ key_material->x, key_material->len,
984
+ &sw_handle ) );
985
+ break;
986
+ case SIGN_IN_DRIVER_THEN_EXPORT_PUBLIC:
987
+ {
988
+ uint8_t public_key[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE( PSA_VENDOR_ECC_MAX_CURVE_BITS )];
989
+ size_t public_key_length;
990
+ PSA_ASSERT( psa_export_public_key( drv_handle,
991
+ public_key, sizeof( public_key ),
992
+ &public_key_length ) );
993
+ psa_set_key_type( &sw_attributes,
994
+ PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type ) );
995
+ PSA_ASSERT( psa_import_key( &sw_attributes,
996
+ public_key, public_key_length,
997
+ &sw_handle ) );
998
+ break;
999
+ }
1000
+ }
1001
+
955
1002
/* Sign with the chosen key. */
956
- if( sign_in_driver )
957
- PSA_ASSERT_VIA_DRIVER(
958
- psa_asymmetric_sign( drv_handle,
959
- alg,
960
- input->x, input->len,
961
- signature, sizeof( signature ),
962
- &signature_length ),
963
- PSA_SUCCESS );
964
- else
965
- PSA_ASSERT( psa_asymmetric_sign( sw_handle,
966
- alg,
967
- input->x, input->len,
968
- signature, sizeof( signature ),
969
- &signature_length ) );
1003
+ switch( flow )
1004
+ {
1005
+ case SIGN_IN_DRIVER_AND_PARALLEL_CREATION:
1006
+ case SIGN_IN_DRIVER_THEN_EXPORT_PUBLIC:
1007
+ PSA_ASSERT_VIA_DRIVER(
1008
+ psa_asymmetric_sign( drv_handle,
1009
+ alg,
1010
+ input->x, input->len,
1011
+ signature, sizeof( signature ),
1012
+ &signature_length ),
1013
+ PSA_SUCCESS );
1014
+ break;
1015
+ case SIGN_IN_SOFTWARE_AND_PARALLEL_CREATION:
1016
+ PSA_ASSERT( psa_asymmetric_sign( sw_handle,
1017
+ alg,
1018
+ input->x, input->len,
1019
+ signature, sizeof( signature ),
1020
+ &signature_length ) );
1021
+ break;
1022
+ }
970
1023
971
1024
/* Verify with both keys. */
972
1025
PSA_ASSERT( psa_asymmetric_verify( sw_handle, alg,
0 commit comments