Skip to content

Commit 117a99e

Browse files
SE keys: implement and smoke-test p_generate
1 parent bbf7a21 commit 117a99e

File tree

3 files changed

+146
-42
lines changed

3 files changed

+146
-42
lines changed

library/psa_crypto.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5817,20 +5817,37 @@ psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
58175817
psa_status_t status;
58185818
psa_key_slot_t *slot = NULL;
58195819
psa_se_drv_table_entry_t *driver = NULL;
5820+
58205821
status = psa_start_key_creation( attributes, handle, &slot, &driver );
5822+
5823+
if( status != PSA_SUCCESS )
5824+
goto exit;
5825+
58215826
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
58225827
if( driver != NULL )
58235828
{
5824-
/* Generating a key in a secure element is not implemented yet. */
5825-
status = PSA_ERROR_NOT_SUPPORTED;
5829+
const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5830+
size_t pubkey_length = 0; /* We don't support this feature yet */
5831+
if( drv->key_management == NULL ||
5832+
drv->key_management->p_generate == NULL )
5833+
{
5834+
status = PSA_ERROR_NOT_SUPPORTED;
5835+
goto exit;
5836+
}
5837+
status = drv->key_management->p_generate(
5838+
psa_get_se_driver_context( driver ),
5839+
slot->data.se.slot_number, attributes,
5840+
NULL, 0, &pubkey_length );
58265841
}
5842+
else
58275843
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5828-
if( status == PSA_SUCCESS )
58295844
{
58305845
status = psa_generate_key_internal(
58315846
slot, attributes->core.bits,
58325847
attributes->domain_parameters, attributes->domain_parameters_size );
58335848
}
5849+
5850+
exit:
58345851
if( status == PSA_SUCCESS )
58355852
status = psa_finish_key_creation( slot, driver );
58365853
if( status != PSA_SUCCESS )

tests/suites/test_suite_psa_crypto_se_driver_hal.data

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,65 @@ key_creation_import_export:0:1
3939
SE key import-export, check after restart (slot 3)
4040
key_creation_import_export:3:1
4141

42-
Key creation smoke test: AES-CTR
43-
key_creation_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CTR:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
42+
Key import smoke test: AES-CTR
43+
import_key_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CTR:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
4444

45-
Key creation smoke test: AES-CBC
46-
key_creation_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CBC_NO_PADDING:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
45+
Key import smoke test: AES-CBC
46+
import_key_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CBC_NO_PADDING:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
4747

48-
Key creation smoke test: AES-CMAC
49-
key_creation_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
48+
Key import smoke test: AES-CMAC
49+
import_key_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
5050

51-
Key creation smoke test: AES-CCM
52-
key_creation_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
51+
Key import smoke test: AES-CCM
52+
import_key_smoke:PSA_KEY_TYPE_AES:PSA_ALG_CCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
5353

54-
Key creation smoke test: AES-GCM
55-
key_creation_smoke:PSA_KEY_TYPE_AES:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
54+
Key import smoke test: AES-GCM
55+
import_key_smoke:PSA_KEY_TYPE_AES:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
5656

57-
Key creation smoke test: CAMELLIA-CTR
58-
key_creation_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CTR:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
57+
Key import smoke test: CAMELLIA-CTR
58+
import_key_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CTR:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
5959

60-
Key creation smoke test: CAMELLIA-CBC
61-
key_creation_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CBC_NO_PADDING:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
60+
Key import smoke test: CAMELLIA-CBC
61+
import_key_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CBC_NO_PADDING:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6262

63-
Key creation smoke test: CAMELLIA-CMAC
64-
key_creation_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
63+
Key import smoke test: CAMELLIA-CMAC
64+
import_key_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_CMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6565

66-
Key creation smoke test: CAMELLIA-CCM
67-
key_creation_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
66+
Key import smoke test: CAMELLIA-CCM
67+
import_key_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6868

69-
Key creation smoke test: CAMELLIA-CCM
70-
key_creation_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
69+
Key import smoke test: CAMELLIA-CCM
70+
import_key_smoke:PSA_KEY_TYPE_CAMELLIA:PSA_ALG_GCM:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7171

72-
Key creation smoke test: HMAC-SHA-256
73-
key_creation_smoke:PSA_KEY_TYPE_HMAC:PSA_ALG_HMAC( PSA_ALG_SHA_256 ):"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
72+
Key import smoke test: HMAC-SHA-256
73+
import_key_smoke:PSA_KEY_TYPE_HMAC:PSA_ALG_HMAC( PSA_ALG_SHA_256 ):"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7474

75-
Key creation smoke test: HKDF-SHA-256
76-
key_creation_smoke:PSA_KEY_TYPE_DERIVE:PSA_ALG_HKDF( PSA_ALG_SHA_256 ):"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
75+
Key import smoke test: HKDF-SHA-256
76+
import_key_smoke:PSA_KEY_TYPE_DERIVE:PSA_ALG_HKDF( PSA_ALG_SHA_256 ):"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7777

78-
Key creation smoke test: RSA PKCS#1v1.5 signature
79-
key_creation_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
78+
Key import smoke test: RSA PKCS#1v1.5 signature
79+
import_key_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
8080

81-
Key creation smoke test: RSA PKCS#1v1.5 encryption
82-
key_creation_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_PKCS1V15_CRYPT:"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
81+
Key import smoke test: RSA PKCS#1v1.5 encryption
82+
import_key_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_PKCS1V15_CRYPT:"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
8383

84-
Key creation smoke test: RSA OAEP encryption
85-
key_creation_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_OAEP( PSA_ALG_SHA_256 ):"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
84+
Key import smoke test: RSA OAEP encryption
85+
import_key_smoke:PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_OAEP( PSA_ALG_SHA_256 ):"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001"
8686

87-
Key creation smoke test: ECDSA secp256r1
88-
key_creation_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_ECDSA_ANY:"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
87+
Key import smoke test: ECDSA secp256r1
88+
import_key_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_ECDSA_ANY:"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
8989

90-
Key creation smoke test: ECDH secp256r1
91-
key_creation_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_ECDH:"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
90+
Key import smoke test: ECDH secp256r1
91+
import_key_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_ECDH:"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
9292

93-
Key creation smoke test: ECDH secp256r1 with HKDF
94-
key_creation_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_KEY_AGREEMENT( PSA_ALG_ECDH, PSA_ALG_HKDF( PSA_ALG_SHA_256 ) ):"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
93+
Key import smoke test: ECDH secp256r1 with HKDF
94+
import_key_smoke:PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP256R1 ):PSA_ALG_KEY_AGREEMENT( PSA_ALG_ECDH, PSA_ALG_HKDF( PSA_ALG_SHA_256 ) ):"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee"
9595

96-
Generate key: not supported
97-
generate_key_not_supported:PSA_KEY_TYPE_AES:128
96+
Key generation smoke test: AES-128-CTR
97+
generate_key_smoke:PSA_KEY_TYPE_AES:128:PSA_ALG_CTR
98+
99+
Key generation smoke test: AES-256-CTR
100+
generate_key_smoke:PSA_KEY_TYPE_AES:128:PSA_ALG_CTR
101+
102+
Key generation smoke test: HMAC-SHA-256
103+
generate_key_smoke:PSA_KEY_TYPE_HMAC:256:PSA_ALG_HMAC( PSA_ALG_SHA_256 )

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ static psa_status_t null_import( psa_drv_se_context_t *context,
7272
return( PSA_SUCCESS );
7373
}
7474

75+
/* Null generate: do nothing, but pretend it worked. */
76+
static psa_status_t null_generate( psa_drv_se_context_t *context,
77+
psa_key_slot_number_t slot_number,
78+
const psa_key_attributes_t *attributes,
79+
uint8_t *pubkey,
80+
size_t pubkey_size,
81+
size_t *pubkey_length )
82+
{
83+
(void) context;
84+
(void) slot_number;
85+
(void) attributes;
86+
87+
DRIVER_ASSERT( *pubkey_length == 0 );
88+
if( ! PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
89+
{
90+
DRIVER_ASSERT( pubkey == NULL );
91+
DRIVER_ASSERT( pubkey_size == 0 );
92+
}
93+
94+
return( PSA_SUCCESS );
95+
}
96+
7597

7698

7799
/****************************************************************/
@@ -498,7 +520,7 @@ exit:
498520
/* END_CASE */
499521

500522
/* BEGIN_CASE */
501-
void key_creation_smoke( int type_arg, int alg_arg,
523+
void import_key_smoke( int type_arg, int alg_arg,
502524
data_t *key_material )
503525
{
504526
psa_key_type_t type = type_arg;
@@ -574,6 +596,7 @@ void generate_key_not_supported( int type_arg, int bits_arg )
574596
driver.key_management = &key_management;
575597
driver.persistent_data_size = sizeof( psa_key_slot_number_t );
576598
key_management.p_allocate = counter_allocate;
599+
/* No p_generate method */
577600

578601
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
579602
PSA_ASSERT( psa_crypto_init( ) );
@@ -591,3 +614,61 @@ exit:
591614
psa_purge_storage( );
592615
}
593616
/* END_CASE */
617+
618+
/* BEGIN_CASE */
619+
void generate_key_smoke( int type_arg, int bits_arg, int alg_arg )
620+
{
621+
psa_key_type_t type = type_arg;
622+
psa_key_bits_t bits = bits_arg;
623+
psa_algorithm_t alg = alg_arg;
624+
psa_drv_se_t driver;
625+
psa_drv_se_key_management_t key_management;
626+
psa_key_lifetime_t lifetime = 2;
627+
psa_key_id_t id = 1;
628+
psa_key_handle_t handle = 0;
629+
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
630+
631+
memset( &driver, 0, sizeof( driver ) );
632+
memset( &key_management, 0, sizeof( key_management ) );
633+
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
634+
driver.key_management = &key_management;
635+
driver.persistent_data_size = sizeof( psa_key_slot_number_t );
636+
key_management.p_allocate = counter_allocate;
637+
key_management.p_generate = null_generate;
638+
639+
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
640+
PSA_ASSERT( psa_crypto_init( ) );
641+
642+
/* Create a key. */
643+
psa_set_key_id( &attributes, id );
644+
psa_set_key_lifetime( &attributes, lifetime );
645+
psa_set_key_usage_flags( &attributes,
646+
PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY |
647+
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT |
648+
PSA_KEY_USAGE_EXPORT );
649+
psa_set_key_algorithm( &attributes, alg );
650+
psa_set_key_type( &attributes, type );
651+
psa_set_key_bits( &attributes, bits );
652+
PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
653+
654+
/* Do stuff with the key. */
655+
if( ! smoke_test_key( handle ) )
656+
goto exit;
657+
658+
/* Restart and try again. */
659+
mbedtls_psa_crypto_free( );
660+
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
661+
PSA_ASSERT( psa_crypto_init( ) );
662+
PSA_ASSERT( psa_open_key( id, &handle ) );
663+
if( ! smoke_test_key( handle ) )
664+
goto exit;
665+
666+
/* We're done. */
667+
PSA_ASSERT( psa_destroy_key( handle ) );
668+
669+
exit:
670+
PSA_DONE( );
671+
ram_slots_reset( );
672+
psa_purge_storage( );
673+
}
674+
/* END_CASE */

0 commit comments

Comments
 (0)