Skip to content

Commit d35249e

Browse files
authored
Merge pull request #109 from gilles-peskine-arm/psa-key_attributes-set_persistent
Individual setters for persistent key attributes
2 parents 99e8d26 + f1b7694 commit d35249e

File tree

7 files changed

+126
-41
lines changed

7 files changed

+126
-41
lines changed

include/psa/crypto.h

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ psa_status_t psa_crypto_init(void);
132132
* psa_reset_key_attributes() on an attribute structure is optional if
133133
* the structure has only been modified by the following functions
134134
* since it was initialized or last reset with psa_reset_key_attributes():
135-
* - psa_make_key_persistent()
135+
* - psa_set_key_id()
136+
* - psa_set_key_lifetime()
136137
* - psa_set_key_type()
137138
* - psa_set_key_bits()
138139
* - psa_set_key_usage_flags()
@@ -173,7 +174,9 @@ psa_status_t psa_crypto_init(void);
173174
*
174175
* A typical sequence to create a key is as follows:
175176
* -# Create and initialize an attribute structure.
176-
* -# If the key is persistent, call psa_make_key_persistent().
177+
* -# If the key is persistent, call psa_set_key_id().
178+
* Also call psa_set_key_lifetime() to place the key in a non-default
179+
* location.
177180
* -# Set the key policy with psa_set_key_usage_flags() and
178181
* psa_set_key_algorithm().
179182
* -# Set the key type with psa_set_key_type(). If the key type requires
@@ -203,30 +206,56 @@ psa_status_t psa_crypto_init(void);
203206
*/
204207
typedef struct psa_key_attributes_s psa_key_attributes_t;
205208

206-
/** Declare a key as persistent.
209+
/** Declare a key as persistent and set its key identifier.
207210
*
208-
* This function does not access storage, it merely fills the attribute
209-
* structure with given values. The persistent key will be written to
210-
* storage when the attribute structure is passed to a key creation
211-
* function such as psa_import_key(), psa_generate_random_key(),
212-
* psa_generate_derived_key() or psa_copy_key().
211+
* If the attribute structure currently declares the key as volatile (which
212+
* is the default content of an attribute structure), this function sets
213+
* the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
213214
*
214-
* This function overwrites any identifier and lifetime values
215-
* previously set in \p attributes.
215+
* This function does not access storage, it merely stores the given
216+
* value in the structure.
217+
* The persistent key will be written to storage when the attribute
218+
* structure is passed to a key creation function such as
219+
* psa_import_key(), psa_generate_random_key(),
220+
* psa_generate_derived_key() or psa_copy_key().
216221
*
217222
* This function may be declared as `static` (i.e. without external
218223
* linkage). This function may be provided as a function-like macro,
219224
* but in this case it must evaluate each of its arguments exactly once.
220225
*
221226
* \param[out] attributes The attribute structure to write to.
222227
* \param id The persistent identifier for the key.
228+
*/
229+
static void psa_set_key_id(psa_key_attributes_t *attributes,
230+
psa_key_id_t id);
231+
232+
/** Set the location of a persistent key.
233+
*
234+
* To make a key persistent, you must give it a persistent key identifier
235+
* with psa_set_key_id(). By default, a key that has a persistent identifier
236+
* is stored in the default storage area identifier by
237+
* #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
238+
* area, or to explicitly declare the key as volatile.
239+
*
240+
* This function does not access storage, it merely stores the given
241+
* value in the structure.
242+
* The persistent key will be written to storage when the attribute
243+
* structure is passed to a key creation function such as
244+
* psa_import_key(), psa_generate_random_key(),
245+
* psa_generate_derived_key() or psa_copy_key().
246+
*
247+
* This function may be declared as `static` (i.e. without external
248+
* linkage). This function may be provided as a function-like macro,
249+
* but in this case it must evaluate each of its arguments exactly once.
250+
*
251+
* \param[out] attributes The attribute structure to write to.
223252
* \param lifetime The lifetime for the key.
224253
* If this is #PSA_KEY_LIFETIME_VOLATILE, the
225-
* key will be volatile, and \p id is ignored.
254+
* key will be volatile, and the key identifier
255+
* attribute is reset to 0.
226256
*/
227-
static void psa_make_key_persistent(psa_key_attributes_t *attributes,
228-
psa_key_id_t id,
229-
psa_key_lifetime_t lifetime);
257+
static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
258+
psa_key_lifetime_t lifetime);
230259

231260
/** Retrieve the key identifier from key attributes.
232261
*

include/psa/crypto_struct.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ static inline struct psa_key_attributes_s psa_key_attributes_init( void )
279279
return( v );
280280
}
281281

282-
static inline void psa_make_key_persistent(psa_key_attributes_t *attributes,
283-
psa_key_id_t id,
284-
psa_key_lifetime_t lifetime)
282+
static inline void psa_set_key_id(psa_key_attributes_t *attributes,
283+
psa_key_id_t id)
285284
{
286285
attributes->id = id;
287-
attributes->lifetime = lifetime;
286+
if( attributes->lifetime == PSA_KEY_LIFETIME_VOLATILE )
287+
attributes->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
288288
}
289289

290290
static inline psa_key_id_t psa_get_key_id(
@@ -293,6 +293,14 @@ static inline psa_key_id_t psa_get_key_id(
293293
return( attributes->id );
294294
}
295295

296+
static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
297+
psa_key_lifetime_t lifetime)
298+
{
299+
attributes->lifetime = lifetime;
300+
if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
301+
attributes->id = 0;
302+
}
303+
296304
static inline psa_key_lifetime_t psa_get_key_lifetime(
297305
const psa_key_attributes_t *attributes)
298306
{

include/psa/crypto_values.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,8 @@
672672
* Then you may create and use a key as follows:
673673
* - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
674674
* ```
675-
* psa_key_policy_set_usage(&policy,
676-
* PSA_KEY_USAGE_SIGN, //or PSA_KEY_USAGE_VERIFY
677-
* PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
678-
* psa_set_key_policy(handle, &policy);
675+
* psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN); // or VERIFY
676+
* psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
679677
* ```
680678
* - Import or generate key material.
681679
* - Call psa_asymmetric_sign() or psa_asymmetric_verify(), passing

tests/suites/test_suite_psa_crypto.data

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ static_checks:
44
PSA key attributes structure
55
attributes_set_get:0x6963:PSA_KEY_LIFETIME_PERSISTENT:PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT:PSA_ALG_CCM:PSA_KEY_TYPE_AES:128
66

7+
PSA key attributes: id only
8+
persistence_attributes:0x1234:-1:-1:0x1234:PSA_KEY_LIFETIME_PERSISTENT
9+
10+
PSA key attributes: lifetime=3 only
11+
persistence_attributes:-1:3:-1:0:3
12+
13+
PSA key attributes: id then back to volatile
14+
persistence_attributes:0x1234:PSA_KEY_LIFETIME_VOLATILE:-1:0:PSA_KEY_LIFETIME_VOLATILE
15+
16+
PSA key attributes: id then lifetime
17+
persistence_attributes:0x1234:3:-1:0x1234:3
18+
19+
PSA key attributes: lifetime then id
20+
persistence_attributes:0x1234:3:0x1235:0x1235:3
21+
722
PSA import/export raw: 0 bytes
823
import_export:"":PSA_KEY_TYPE_RAW_DATA:0:PSA_KEY_USAGE_EXPORT:0:0:PSA_SUCCESS:1
924

tests/suites/test_suite_psa_crypto.function

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ static int test_operations_on_invalid_handle( psa_key_handle_t handle )
10981098
size_t length;
10991099
int ok = 0;
11001100

1101-
psa_make_key_persistent( &attributes, 0x6964, PSA_KEY_LIFETIME_PERSISTENT );
1101+
psa_set_key_id( &attributes, 0x6964 );
11021102
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
11031103
psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
11041104
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
@@ -1181,7 +1181,8 @@ void attributes_set_get( int id_arg, int lifetime_arg,
11811181
TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
11821182
TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
11831183

1184-
psa_make_key_persistent( &attributes, id, lifetime );
1184+
psa_set_key_id( &attributes, id );
1185+
psa_set_key_lifetime( &attributes, lifetime );
11851186
psa_set_key_usage_flags( &attributes, usage_flags );
11861187
psa_set_key_algorithm( &attributes, alg );
11871188
psa_set_key_type( &attributes, type );
@@ -1205,6 +1206,29 @@ void attributes_set_get( int id_arg, int lifetime_arg,
12051206
}
12061207
/* END_CASE */
12071208

1209+
/* BEGIN_CASE */
1210+
void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1211+
int expected_id_arg, int expected_lifetime_arg )
1212+
{
1213+
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1214+
psa_key_id_t id1 = id1_arg;
1215+
psa_key_lifetime_t lifetime = lifetime_arg;
1216+
psa_key_id_t id2 = id2_arg;
1217+
psa_key_id_t expected_id = expected_id_arg;
1218+
psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1219+
1220+
if( id1_arg != -1 )
1221+
psa_set_key_id( &attributes, id1 );
1222+
if( lifetime_arg != -1 )
1223+
psa_set_key_lifetime( &attributes, lifetime );
1224+
if( id2_arg != -1 )
1225+
psa_set_key_id( &attributes, id2 );
1226+
1227+
TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1228+
TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1229+
}
1230+
/* END_CASE */
1231+
12081232
/* BEGIN_CASE */
12091233
void import( data_t *data, int type_arg,
12101234
int attr_bits_arg,
@@ -4877,7 +4901,7 @@ void persistent_key_load_key_from_storage( data_t *data,
48774901

48784902
PSA_ASSERT( psa_crypto_init() );
48794903

4880-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4904+
psa_set_key_id( &attributes, key_id );
48814905
psa_set_key_usage_flags( &attributes, usage_flags );
48824906
psa_set_key_algorithm( &attributes, alg );
48834907
psa_set_key_type( &attributes, type );

tests/suites/test_suite_psa_crypto_persistent_key.function

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void save_large_persistent_key( int data_too_large, int expected_status )
9696

9797
PSA_ASSERT( psa_crypto_init() );
9898

99-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
99+
psa_set_key_id( &attributes, key_id );
100100
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
101101

102102
TEST_EQUAL( psa_import_key( &attributes, data, data_length, &handle ),
@@ -122,7 +122,7 @@ void persistent_key_destroy( int key_id_arg, int restart,
122122

123123
PSA_ASSERT( psa_crypto_init() );
124124

125-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
125+
psa_set_key_id( &attributes, key_id );
126126
psa_set_key_type( &attributes, first_type );
127127

128128
PSA_ASSERT( psa_import_key( &attributes, first_data->x, first_data->len,
@@ -150,7 +150,7 @@ void persistent_key_destroy( int key_id_arg, int restart,
150150
PSA_ASSERT( psa_crypto_init() );
151151

152152
/* Create another key in the same slot */
153-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
153+
psa_set_key_id( &attributes, key_id );
154154
psa_set_key_type( &attributes, second_type );
155155
PSA_ASSERT( psa_import_key( &attributes, second_data->x, second_data->len,
156156
&handle ) );
@@ -172,7 +172,7 @@ void persistent_key_import( int key_id_arg, int type_arg, data_t *data,
172172

173173
PSA_ASSERT( psa_crypto_init() );
174174

175-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
175+
psa_set_key_id( &attributes, key_id );
176176
psa_set_key_type( &attributes, type );
177177
TEST_EQUAL( psa_import_key( &attributes, data->x, data->len, &handle ),
178178
expected_status );
@@ -224,7 +224,7 @@ void import_export_persistent_key( data_t *data, int type_arg,
224224

225225
PSA_ASSERT( psa_crypto_init( ) );
226226

227-
psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
227+
psa_set_key_id( &attributes, key_id );
228228
psa_set_key_type( &attributes, type );
229229
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
230230

tests/suites/test_suite_psa_crypto_slot_management.function

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void persistent_slot_lifecycle( int lifetime_arg, int id_arg,
143143
PSA_ASSERT( psa_crypto_init( ) );
144144

145145
/* Get a handle and import a key. */
146-
psa_make_key_persistent( &attributes, id, lifetime );
146+
psa_set_key_id( &attributes, id );
147+
psa_set_key_lifetime( &attributes, lifetime );
147148
psa_set_key_type( &attributes, type );
148149
psa_set_key_usage_flags( &attributes, usage_flags );
149150
psa_set_key_algorithm( &attributes, alg );
@@ -221,7 +222,8 @@ void create_existent( int lifetime_arg, int id_arg,
221222
PSA_ASSERT( psa_crypto_init( ) );
222223

223224
/* Create a key. */
224-
psa_make_key_persistent( &attributes, id, lifetime );
225+
psa_set_key_id( &attributes, id );
226+
psa_set_key_lifetime( &attributes, lifetime );
225227
psa_set_key_type( &attributes, type1 );
226228
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
227229
psa_set_key_algorithm( &attributes, 0 );
@@ -298,7 +300,8 @@ void create_fail( int lifetime_arg, int id_arg,
298300

299301
PSA_ASSERT( psa_crypto_init( ) );
300302

301-
psa_make_key_persistent( &attributes, id, lifetime );
303+
psa_set_key_id( &attributes, id );
304+
psa_set_key_lifetime( &attributes, lifetime );
302305
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
303306
TEST_EQUAL( psa_import_key( &attributes, material, sizeof( material ),
304307
&handle ),
@@ -345,8 +348,10 @@ void copy_across_lifetimes( int source_lifetime_arg, int source_id_arg,
345348

346349
/* Populate the source slot. */
347350
if( source_lifetime != PSA_KEY_LIFETIME_VOLATILE )
348-
psa_make_key_persistent( &source_attributes,
349-
source_id, source_lifetime );
351+
{
352+
psa_set_key_id( &source_attributes, source_id );
353+
psa_set_key_lifetime( &source_attributes, source_lifetime );
354+
}
350355
psa_set_key_type( &source_attributes, source_type );
351356
psa_set_key_usage_flags( &source_attributes, source_usage );
352357
psa_set_key_algorithm( &source_attributes, source_alg );
@@ -358,8 +363,10 @@ void copy_across_lifetimes( int source_lifetime_arg, int source_id_arg,
358363

359364
/* Prepare the target slot. */
360365
if( target_lifetime != PSA_KEY_LIFETIME_VOLATILE )
361-
psa_make_key_persistent( &target_attributes,
362-
target_id, target_lifetime );
366+
{
367+
psa_set_key_id( &target_attributes, target_id );
368+
psa_set_key_lifetime( &target_attributes, target_lifetime );
369+
}
363370
psa_set_key_usage_flags( &target_attributes, target_usage );
364371
psa_set_key_algorithm( &target_attributes, target_alg );
365372

@@ -449,8 +456,10 @@ void copy_to_occupied( int source_lifetime_arg, int source_id_arg,
449456

450457
/* Populate the source slot. */
451458
if( source_lifetime != PSA_KEY_LIFETIME_VOLATILE )
452-
psa_make_key_persistent( &attributes,
453-
source_id, source_lifetime );
459+
{
460+
psa_set_key_id( &attributes, source_id );
461+
psa_set_key_lifetime( &attributes, source_lifetime );
462+
}
454463
psa_set_key_type( &attributes, source_type );
455464
psa_set_key_usage_flags( &attributes, source_usage );
456465
psa_set_key_algorithm( &attributes, source_alg );
@@ -465,7 +474,8 @@ void copy_to_occupied( int source_lifetime_arg, int source_id_arg,
465474
}
466475
else
467476
{
468-
psa_make_key_persistent( &attributes1, target_id, target_lifetime );
477+
psa_set_key_id( &attributes1, target_id );
478+
psa_set_key_lifetime( &attributes1, target_lifetime );
469479
psa_set_key_type( &attributes1, target_type );
470480
psa_set_key_usage_flags( &attributes1, target_usage );
471481
psa_set_key_algorithm( &attributes1, target_alg );
@@ -476,7 +486,8 @@ void copy_to_occupied( int source_lifetime_arg, int source_id_arg,
476486
PSA_ASSERT( psa_get_key_attributes( target_handle, &attributes1 ) );
477487

478488
/* Make a copy attempt. */
479-
psa_make_key_persistent( &attributes, target_id, target_lifetime );
489+
psa_set_key_id( &attributes, target_id );
490+
psa_set_key_lifetime( &attributes, target_lifetime );
480491
TEST_EQUAL( psa_copy_key( source_handle,
481492
&attributes, &new_handle ),
482493
PSA_ERROR_ALREADY_EXISTS );

0 commit comments

Comments
 (0)