Skip to content

Commit 2c1c33b

Browse files
Support encoding an owner in key file IDs
Differentiate between _key identifiers_, which are always `uint32_t`, and _key file identifiers_, which are platform-dependent. Normally, the two are the same. In `psa/crypto_platform.h`, define `psa_app_key_id_t` (which is always 32 bits, the standard key identifier type) and `psa_key_file_id_t` (which will be different in some service builds). A subsequent commit will introduce a platform where the two are different. It would make sense for the function declarations in `psa/crypto.h` to use `psa_key_file_id_t`. However this file is currently part of the PSA Crypto API specification, so it must stick to the standard type `psa_key_id_t`. Hence, as long as the specification and Mbed Crypto are not separate, use the implementation-specific file `psa/crypto_platform.h` to define `psa_key_id_t` as `psa_key_file_id_t`. In the library, systematically use `psa_key_file_id_t`. perl -i -pe 's/psa_key_id_t/psa_key_file_id_t/g' library/*.[hc]
1 parent 113a3f2 commit 2c1c33b

8 files changed

+51
-27
lines changed

include/psa/crypto_platform.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@
4949
/* Integral type representing a key handle. */
5050
typedef uint16_t psa_key_handle_t;
5151

52+
/* This implementation distinguishes *application key identifiers*, which
53+
* are the key identifiers specified by the application, from
54+
* *key file identifiers*, which are the key identifiers that the library
55+
* sees internally. The two types can be different if there is a remote
56+
* call layer between the application and the library which supports
57+
* multiple client applications that do not have access to each others'
58+
* keys. The point of having different types is that the key file
59+
* identifier may encode not only the key identifier specified by the
60+
* application, but also the the identity of the application.
61+
*
62+
* Note that this is an internal concept of the library and the remote
63+
* call layer. The application itself never sees anything other than
64+
* #psa_app_key_id_t with its standard definition.
65+
*/
66+
67+
/* The application key identifier is always what the application sees as
68+
* #psa_key_id_t. */
69+
typedef uint32_t psa_app_key_id_t;
70+
71+
/* By default, a key file identifier is just the application key identifier. */
72+
typedef psa_app_key_id_t psa_key_file_id_t;
73+
#define PSA_KEY_FILE_GET_KEY_ID( id ) ( id )
74+
5275
#endif /* PSA_CRYPTO_PLATFORM_H */

library/psa_crypto_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct
4141
psa_key_type_t type;
4242
psa_key_policy_t policy;
4343
psa_key_lifetime_t lifetime;
44-
psa_key_id_t persistent_storage_id;
44+
psa_key_file_id_t persistent_storage_id;
4545
unsigned allocated : 1;
4646
union
4747
{

library/psa_crypto_slot_management.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
194194
*
195195
* \return 1 if \p key_id is acceptable, otherwise 0.
196196
*/
197-
static int psa_is_key_id_valid( psa_key_id_t key_id )
197+
static int psa_is_key_id_valid( psa_key_file_id_t file_id )
198198
{
199+
psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
199200
/* Reject id=0 because by general library conventions, 0 is an invalid
200201
* value wherever possible. */
201202
if( key_id == 0 )
@@ -226,7 +227,7 @@ static int psa_is_key_id_valid( psa_key_id_t key_id )
226227
* \retval #PSA_ERROR_STORAGE_FAILURE
227228
*/
228229
static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
229-
psa_key_id_t id )
230+
psa_key_file_id_t id )
230231
{
231232
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
232233
psa_key_slot_t *slot;
@@ -253,7 +254,7 @@ static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
253254
}
254255

255256
static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
256-
psa_key_id_t id,
257+
psa_key_file_id_t id,
257258
psa_key_handle_t *handle,
258259
psa_status_t wanted_load_status )
259260
{
@@ -278,14 +279,14 @@ static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
278279
}
279280

280281
psa_status_t psa_open_key( psa_key_lifetime_t lifetime,
281-
psa_key_id_t id,
282+
psa_key_file_id_t id,
282283
psa_key_handle_t *handle )
283284
{
284285
return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) );
285286
}
286287

287288
psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
288-
psa_key_id_t id,
289+
psa_key_file_id_t id,
289290
psa_key_handle_t *handle )
290291
{
291292
psa_status_t status;

library/psa_crypto_storage.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
148148
return( PSA_SUCCESS );
149149
}
150150

151-
psa_status_t psa_save_persistent_key( const psa_key_id_t key,
151+
psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
152152
const psa_key_type_t type,
153153
const psa_key_policy_t *policy,
154154
const uint8_t *data,
@@ -186,7 +186,7 @@ void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
186186
mbedtls_free( key_data );
187187
}
188188

189-
psa_status_t psa_load_persistent_key( psa_key_id_t key,
189+
psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
190190
psa_key_type_t *type,
191191
psa_key_policy_t *policy,
192192
uint8_t **data,

library/psa_crypto_storage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extern "C" {
8686
* \retval PSA_ERROR_STORAGE_FAILURE
8787
* \retval PSA_ERROR_ALREADY_EXISTS
8888
*/
89-
psa_status_t psa_save_persistent_key( const psa_key_id_t key,
89+
psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
9090
const psa_key_type_t type,
9191
const psa_key_policy_t *policy,
9292
const uint8_t *data,
@@ -117,7 +117,7 @@ psa_status_t psa_save_persistent_key( const psa_key_id_t key,
117117
* \retval PSA_ERROR_STORAGE_FAILURE
118118
* \retval PSA_ERROR_DOES_NOT_EXIST
119119
*/
120-
psa_status_t psa_load_persistent_key( psa_key_id_t key,
120+
psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
121121
psa_key_type_t *type,
122122
psa_key_policy_t *policy,
123123
uint8_t **data,
@@ -134,7 +134,7 @@ psa_status_t psa_load_persistent_key( psa_key_id_t key,
134134
* or the key did not exist.
135135
* \retval PSA_ERROR_STORAGE_FAILURE
136136
*/
137-
psa_status_t psa_destroy_persistent_key( const psa_key_id_t key );
137+
psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key );
138138

139139
/**
140140
* \brief Free the temporary buffer allocated by psa_load_persistent_key().

library/psa_crypto_storage_backend.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern "C" {
5656
* \retval PSA_ERROR_STORAGE_FAILURE
5757
* \retval PSA_ERROR_DOES_NOT_EXIST
5858
*/
59-
psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
59+
psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
6060
size_t data_size );
6161

6262
/**
@@ -75,7 +75,7 @@ psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
7575
* \retval PSA_ERROR_STORAGE_FAILURE
7676
* \retval PSA_ERROR_ALREADY_EXISTS
7777
*/
78-
psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
78+
psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
7979
const uint8_t *data,
8080
size_t data_length );
8181

@@ -92,7 +92,7 @@ psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
9292
* \retval 1
9393
* Persistent data present for slot number
9494
*/
95-
int psa_is_key_present_in_storage( const psa_key_id_t key );
95+
int psa_is_key_present_in_storage( const psa_key_file_id_t key );
9696

9797
/**
9898
* \brief Get data length for given key slot number.
@@ -104,7 +104,7 @@ int psa_is_key_present_in_storage( const psa_key_id_t key );
104104
* \retval PSA_SUCCESS
105105
* \retval PSA_ERROR_STORAGE_FAILURE
106106
*/
107-
psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
107+
psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
108108
size_t *data_length );
109109

110110

library/psa_crypto_storage_file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
enum { MAX_LOCATION_LEN = sizeof(CRYPTO_STORAGE_FILE_LOCATION) + 40 };
5151

52-
static void key_id_to_location( const psa_key_id_t key,
52+
static void key_id_to_location( const psa_key_file_id_t key,
5353
char *location,
5454
size_t location_size )
5555
{
@@ -58,7 +58,7 @@ static void key_id_to_location( const psa_key_id_t key,
5858
(unsigned long) key );
5959
}
6060

61-
psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
61+
psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
6262
size_t data_size )
6363
{
6464
psa_status_t status = PSA_SUCCESS;
@@ -83,7 +83,7 @@ psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
8383
return( status );
8484
}
8585

86-
int psa_is_key_present_in_storage( const psa_key_id_t key )
86+
int psa_is_key_present_in_storage( const psa_key_file_id_t key )
8787
{
8888
char slot_location[MAX_LOCATION_LEN];
8989
FILE *file;
@@ -101,7 +101,7 @@ int psa_is_key_present_in_storage( const psa_key_id_t key )
101101
return( 1 );
102102
}
103103

104-
psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
104+
psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
105105
const uint8_t *data,
106106
size_t data_length )
107107
{
@@ -156,7 +156,7 @@ psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
156156
return( status );
157157
}
158158

159-
psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
159+
psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
160160
{
161161
FILE *file;
162162
char slot_location[MAX_LOCATION_LEN];
@@ -175,7 +175,7 @@ psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
175175
return( PSA_SUCCESS );
176176
}
177177

178-
psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
178+
psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
179179
size_t *data_length )
180180
{
181181
psa_status_t status = PSA_SUCCESS;

library/psa_crypto_storage_its.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
#include "mbedtls/platform.h"
3737
#endif
3838

39-
static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_id_t key )
39+
static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t key )
4040
{
4141
return( key );
4242
}
4343

44-
psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
44+
psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
4545
size_t data_size )
4646
{
4747
psa_status_t status;
@@ -57,7 +57,7 @@ psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
5757
return( status );
5858
}
5959

60-
int psa_is_key_present_in_storage( const psa_key_id_t key )
60+
int psa_is_key_present_in_storage( const psa_key_file_id_t key )
6161
{
6262
psa_status_t ret;
6363
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
@@ -70,7 +70,7 @@ int psa_is_key_present_in_storage( const psa_key_id_t key )
7070
return( 1 );
7171
}
7272

73-
psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
73+
psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
7474
const uint8_t *data,
7575
size_t data_length )
7676
{
@@ -105,7 +105,7 @@ psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
105105
return( status );
106106
}
107107

108-
psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
108+
psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
109109
{
110110
psa_status_t ret;
111111
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
@@ -125,7 +125,7 @@ psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
125125
return( PSA_SUCCESS );
126126
}
127127

128-
psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
128+
psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
129129
size_t *data_length )
130130
{
131131
psa_status_t status;

0 commit comments

Comments
 (0)