Skip to content

Commit 99e8d26

Browse files
authored
Merge pull request #104 from gilles-peskine-arm/psa-global_key_id
Make key ids global and define their range
2 parents 16ab391 + 280948a commit 99e8d26

11 files changed

+158
-92
lines changed

include/psa/crypto.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ void psa_reset_key_attributes(psa_key_attributes_t *attributes);
512512
*
513513
* Open a handle to a key which was previously created with psa_create_key().
514514
*
515-
* \param lifetime The lifetime of the key. This designates a storage
516-
* area where the key material is stored. This must not
517-
* be #PSA_KEY_LIFETIME_VOLATILE.
515+
* Implementations may provide additional keys that can be opened with
516+
* psa_open_key(). Such keys have a key identifier in the vendor range,
517+
* as documented in the description of #psa_key_id_t.
518+
*
518519
* \param id The persistent identifier of the key.
519520
* \param[out] handle On success, a handle to a key slot which contains
520521
* the data and metadata loaded from the specified
@@ -526,19 +527,16 @@ void psa_reset_key_attributes(psa_key_attributes_t *attributes);
526527
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
527528
* \retval #PSA_ERROR_DOES_NOT_EXIST
528529
* \retval #PSA_ERROR_INVALID_ARGUMENT
529-
* \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
530-
* \retval #PSA_ERROR_INVALID_ARGUMENT
531-
* \p id is invalid for the specified lifetime.
532-
* \retval #PSA_ERROR_NOT_SUPPORTED
533-
* \p lifetime is not supported.
530+
* \p id is invalid.
534531
* \retval #PSA_ERROR_NOT_PERMITTED
535532
* The specified key exists, but the application does not have the
536533
* permission to access it. Note that this specification does not
537534
* define any way to create such a key, but it may be possible
538535
* through implementation-specific means.
536+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
537+
* \retval #PSA_ERROR_STORAGE_FAILURE
539538
*/
540-
psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
541-
psa_key_id_t id,
539+
psa_status_t psa_open_key(psa_key_id_t id,
542540
psa_key_handle_t *handle);
543541

544542
/** Close a key handle.

include/psa/crypto_types.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,30 @@ typedef uint32_t psa_algorithm_t;
8585
*/
8686

8787
/** Encoding of key lifetimes.
88+
*
89+
* The lifetime of a key indicates where it is stored and what system actions
90+
* may create and destroy it.
91+
*
92+
* Keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE are automatically
93+
* destroyed when the application terminates or on a power reset.
94+
*
95+
* Keys with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE are said
96+
* to be _persistent_.
97+
* Persistent keys are preserved if the application or the system restarts.
98+
* Persistent keys have a key identifier of type #psa_key_id_t.
99+
* The application can call psa_open_key() to open a persistent key that
100+
* it created previously.
88101
*/
89102
typedef uint32_t psa_key_lifetime_t;
90103

91104
/** Encoding of identifiers of persistent keys.
105+
*
106+
* - Applications may freely choose key identifiers in the range
107+
* #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX.
108+
* - Implementations may define additional key identifiers in the range
109+
* #PSA_KEY_ID_VENDOR_MIN to #PSA_KEY_ID_VENDOR_MAX.
110+
* - 0 is reserved as an invalid key identifier.
111+
* - Key identifiers outside these ranges are reserved for future use.
92112
*/
93113
/* Implementation-specific quirk: The Mbed Crypto library can be built as
94114
* part of a multi-client service that exposes the PSA Crypto API in each

include/psa/crypto_values.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,19 @@
14881488
*/
14891489
#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
14901490

1491+
/** The minimum value for a key identifier chosen by the application.
1492+
*/
1493+
#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
1494+
/** The maximum value for a key identifier chosen by the application.
1495+
*/
1496+
#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
1497+
/** The minimum value for a key identifier chosen by the implementation.
1498+
*/
1499+
#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
1500+
/** The maximum value for a key identifier chosen by the implementation.
1501+
*/
1502+
#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
1503+
14911504
/**@}*/
14921505

14931506
/** \defgroup policy Key policies

library/psa_crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ static psa_status_t psa_start_key_creation(
14251425
if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
14261426
{
14271427
status = psa_validate_persistent_key_parameters( attributes->lifetime,
1428-
attributes->id );
1428+
attributes->id, 1 );
14291429
if( status != PSA_SUCCESS )
14301430
return( status );
14311431
slot->persistent_storage_id = attributes->id;

library/psa_crypto_slot_management.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,23 @@ static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
176176
* is provided.
177177
*
178178
* \param file_id The key identifier to check.
179+
* \param vendor_ok Nonzero to allow key ids in the vendor range.
180+
* 0 to allow only key ids in the application range.
179181
*
180182
* \return 1 if \p file_id is acceptable, otherwise 0.
181183
*/
182-
static int psa_is_key_id_valid( psa_key_file_id_t file_id )
184+
static int psa_is_key_id_valid( psa_key_file_id_t file_id,
185+
int vendor_ok )
183186
{
184187
psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
185-
/* Reject id=0 because by general library conventions, 0 is an invalid
186-
* value wherever possible. */
187-
if( key_id == 0 )
188+
if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
189+
return( 1 );
190+
else if( vendor_ok &&
191+
PSA_KEY_ID_VENDOR_MIN <= key_id &&
192+
key_id <= PSA_KEY_ID_VENDOR_MAX )
193+
return( 1 );
194+
else
188195
return( 0 );
189-
/* Reject high values because the file names are reserved for the
190-
* library's internal use. */
191-
if( key_id > PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
192-
return( 0 );
193-
return( 1 );
194196
}
195197

196198
/** Declare a slot as persistent and load it from storage.
@@ -231,32 +233,36 @@ static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
231233

232234
psa_status_t psa_validate_persistent_key_parameters(
233235
psa_key_lifetime_t lifetime,
234-
psa_key_file_id_t id )
236+
psa_key_file_id_t id,
237+
int creating )
235238
{
236239
if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
237240
return( PSA_ERROR_INVALID_ARGUMENT );
238241

239242
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
240-
if( ! psa_is_key_id_valid( id ) )
243+
if( ! psa_is_key_id_valid( id, ! creating ) )
241244
return( PSA_ERROR_INVALID_ARGUMENT );
242245
return( PSA_SUCCESS );
243246

244247
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
245248
(void) id;
249+
(void) creating;
246250
return( PSA_ERROR_NOT_SUPPORTED );
247251
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
248252
}
249253

250254
static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
251255
psa_key_file_id_t id,
252256
psa_key_handle_t *handle,
253-
psa_status_t wanted_load_status )
257+
int creating )
254258
{
255259
psa_status_t status;
260+
psa_status_t wanted_load_status =
261+
( creating ? PSA_ERROR_DOES_NOT_EXIST : PSA_SUCCESS );
256262

257263
*handle = 0;
258264

259-
status = psa_validate_persistent_key_parameters( lifetime, id );
265+
status = psa_validate_persistent_key_parameters( lifetime, id, creating );
260266
if( status != PSA_SUCCESS )
261267
return( status );
262268

@@ -278,11 +284,10 @@ static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
278284
#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
279285
}
280286

281-
psa_status_t psa_open_key( psa_key_lifetime_t lifetime,
282-
psa_key_file_id_t id,
283-
psa_key_handle_t *handle )
287+
psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
284288
{
285-
return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) );
289+
return( persistent_key_setup( PSA_KEY_LIFETIME_PERSISTENT,
290+
id, handle, 0 ) );
286291
}
287292

288293
psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
@@ -291,8 +296,7 @@ psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
291296
{
292297
psa_status_t status;
293298

294-
status = persistent_key_setup( lifetime, id, handle,
295-
PSA_ERROR_DOES_NOT_EXIST );
299+
status = persistent_key_setup( lifetime, id, handle, 1 );
296300
switch( status )
297301
{
298302
case PSA_SUCCESS: return( PSA_ERROR_ALREADY_EXISTS );

library/psa_crypto_slot_management.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ void psa_wipe_all_key_slots( void );
6464
*
6565
* \param lifetime The lifetime to test.
6666
* \param id The key id to test.
67+
* \param creating 0 if attempting to open an existing key.
68+
* Nonzero if attempting to create a key.
6769
*
6870
* \retval PSA_SUCCESS
6971
* The given parameters are valid.
@@ -74,7 +76,8 @@ void psa_wipe_all_key_slots( void );
7476
*/
7577
psa_status_t psa_validate_persistent_key_parameters(
7678
psa_key_lifetime_t lifetime,
77-
psa_key_file_id_t id );
79+
psa_key_file_id_t id,
80+
int creating );
7881

7982

8083
#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */

library/psa_crypto_storage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extern "C" {
5959
* This limitation will probably become moot when we implement client
6060
* separation for key storage.
6161
*/
62-
#define PSA_MAX_PERSISTENT_KEY_IDENTIFIER 0xfffeffff
62+
#define PSA_MAX_PERSISTENT_KEY_IDENTIFIER PSA_KEY_ID_VENDOR_MAX
6363

6464
/**
6565
* \brief Checks if persistent data is stored for the given key slot number

tests/suites/test_suite_psa_crypto.function

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,8 +4942,7 @@ void persistent_key_load_key_from_storage( data_t *data,
49424942
PSA_ASSERT( psa_crypto_init() );
49434943

49444944
/* Check key slot still contains key data */
4945-
PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
4946-
&handle ) );
4945+
PSA_ASSERT( psa_open_key( key_id, &handle ) );
49474946
PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
49484947
TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
49494948
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
@@ -4978,7 +4977,7 @@ exit:
49784977
/* In case there was a test failure after creating the persistent key
49794978
* but while it was not open, try to re-open the persistent key
49804979
* to delete it. */
4981-
psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4980+
psa_open_key( key_id, &handle );
49824981
}
49834982
psa_destroy_key( handle );
49844983
mbedtls_psa_crypto_free();

tests/suites/test_suite_psa_crypto_persistent_key.function

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ void persistent_key_destroy( int key_id_arg, int restart,
133133
psa_close_key( handle );
134134
mbedtls_psa_crypto_free();
135135
PSA_ASSERT( psa_crypto_init() );
136-
PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
137-
&handle ) );
136+
PSA_ASSERT( psa_open_key( key_id, &handle ) );
138137
}
139138
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );
140139

@@ -143,8 +142,7 @@ void persistent_key_destroy( int key_id_arg, int restart,
143142

144143
/* Check key slot storage is removed */
145144
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
146-
TEST_EQUAL( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle ),
147-
PSA_ERROR_DOES_NOT_EXIST );
145+
TEST_EQUAL( psa_open_key( key_id, &handle ), PSA_ERROR_DOES_NOT_EXIST );
148146
TEST_EQUAL( handle, 0 );
149147

150148
/* Shutdown and restart */
@@ -190,8 +188,7 @@ void persistent_key_import( int key_id_arg, int type_arg, data_t *data,
190188
psa_close_key( handle );
191189
mbedtls_psa_crypto_free();
192190
PSA_ASSERT( psa_crypto_init() );
193-
PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
194-
&handle ) );
191+
PSA_ASSERT( psa_open_key( key_id, &handle ) );
195192
}
196193

197194
psa_reset_key_attributes( &attributes );
@@ -240,8 +237,7 @@ void import_export_persistent_key( data_t *data, int type_arg,
240237
psa_close_key( handle );
241238
mbedtls_psa_crypto_free();
242239
PSA_ASSERT( psa_crypto_init() );
243-
PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
244-
&handle ) );
240+
PSA_ASSERT( psa_open_key( key_id, &handle ) );
245241
}
246242

247243
/* Test the key information */

tests/suites/test_suite_psa_crypto_slot_management.data

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ transient_slot_lifecycle:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789ab
77
Transient slot, check after restart
88
transient_slot_lifecycle:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_SHUTDOWN
99

10-
Persistent slot, check after closing
11-
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:1:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_CLOSE
10+
Persistent slot, check after closing, id=min
11+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MIN:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_CLOSE
1212

13-
Persistent slot, check after destroying
14-
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:1:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_DESTROY
13+
Persistent slot, check after destroying, id=min
14+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MIN:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_DESTROY
1515

16-
Persistent slot, check after restart
17-
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:1:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_SHUTDOWN
16+
Persistent slot, check after restart, id=min
17+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MIN:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_SHUTDOWN
18+
19+
Persistent slot, check after closing, id=max
20+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MAX:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_CLOSE
21+
22+
Persistent slot, check after destroying, id=max
23+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MAX:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_DESTROY
24+
25+
Persistent slot, check after restart, id=max
26+
persistent_slot_lifecycle:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MAX:0:0:PSA_KEY_TYPE_RAW_DATA:"0123456789abcdef0123456789abcdef":CLOSE_BY_SHUTDOWN
1827

1928
Attempt to overwrite: close before
2029
create_existent:PSA_KEY_LIFETIME_PERSISTENT:1:CLOSE_BEFORE
@@ -27,21 +36,23 @@ create_existent:PSA_KEY_LIFETIME_PERSISTENT:1:KEEP_OPEN
2736

2837
Open failure: invalid identifier (0)
2938
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
30-
open_fail:PSA_KEY_LIFETIME_PERSISTENT:0:PSA_ERROR_INVALID_ARGUMENT
39+
open_fail:0:PSA_ERROR_INVALID_ARGUMENT
3140

3241
Open failure: invalid identifier (random seed UID)
3342
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
34-
open_fail:PSA_KEY_LIFETIME_PERSISTENT:PSA_CRYPTO_ITS_RANDOM_SEED_UID:PSA_ERROR_INVALID_ARGUMENT
43+
open_fail:PSA_CRYPTO_ITS_RANDOM_SEED_UID:PSA_ERROR_INVALID_ARGUMENT
3544

36-
Open failure: non-existent identifier
45+
Open failure: invalid identifier (reserved range)
3746
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
38-
open_fail:PSA_KEY_LIFETIME_PERSISTENT:1:PSA_ERROR_DOES_NOT_EXIST
47+
open_fail:PSA_KEY_ID_VENDOR_MAX + 1:PSA_ERROR_INVALID_ARGUMENT
3948

40-
Open failure: volatile lifetime
41-
open_fail:PSA_KEY_LIFETIME_VOLATILE:1:PSA_ERROR_INVALID_ARGUMENT
49+
Open failure: invalid identifier (implementation range)
50+
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
51+
open_fail:PSA_KEY_ID_USER_MAX + 1:PSA_ERROR_DOES_NOT_EXIST
4252

43-
Open failure: invalid lifetime
44-
open_fail:0x7fffffff:0:PSA_ERROR_INVALID_ARGUMENT
53+
Open failure: non-existent identifier
54+
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
55+
open_fail:1:PSA_ERROR_DOES_NOT_EXIST
4556

4657
Create failure: invalid lifetime
4758
create_fail:0x7fffffff:0:PSA_ERROR_INVALID_ARGUMENT
@@ -54,9 +65,17 @@ Create failure: invalid key id (random seed UID)
5465
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
5566
create_fail:PSA_KEY_LIFETIME_PERSISTENT:PSA_CRYPTO_ITS_RANDOM_SEED_UID:PSA_ERROR_INVALID_ARGUMENT
5667

68+
Create failure: invalid key id (reserved range)
69+
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
70+
create_fail:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_VENDOR_MAX + 1:PSA_ERROR_INVALID_ARGUMENT
71+
72+
Create failure: invalid key id (implementation range)
73+
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
74+
create_fail:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_ID_USER_MAX + 1:PSA_ERROR_INVALID_ARGUMENT
75+
5776
Open not supported
5877
depends_on:!MBEDTLS_PSA_CRYPTO_STORAGE_C
59-
open_fail:PSA_KEY_LIFETIME_PERSISTENT:1:PSA_ERROR_NOT_SUPPORTED
78+
open_fail:1:PSA_ERROR_NOT_SUPPORTED
6079

6180
Create not supported
6281
depends_on:!MBEDTLS_PSA_CRYPTO_STORAGE_C

0 commit comments

Comments
 (0)