@@ -309,18 +309,39 @@ static inline struct psa_key_policy_s psa_key_policy_init( void )
309
309
return ( v );
310
310
}
311
311
312
- struct psa_key_attributes_s
312
+ /* The type used internally for key sizes.
313
+ * Public interfaces use size_t, but internally we use a smaller type. */
314
+ typedef uint16_t psa_key_bits_t ;
315
+ /* The maximum value of the type used to represent bit-sizes.
316
+ * This is used to mark an invalid key size. */
317
+ #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
318
+ /* The maximum size of a key in bits.
319
+ * Currently defined as the maximum that can be represented, rounded down
320
+ * to a whole number of bytes.
321
+ * This is an uncast value so that it can be used in preprocessor
322
+ * conditionals. */
323
+ #define PSA_MAX_KEY_BITS 0xfff8
324
+
325
+ typedef struct
313
326
{
314
- psa_key_id_t id ;
327
+ psa_key_type_t type ;
315
328
psa_key_lifetime_t lifetime ;
329
+ psa_key_id_t id ;
316
330
psa_key_policy_t policy ;
317
- psa_key_type_t type ;
318
- size_t bits ;
331
+ psa_key_bits_t bits ;
332
+ uint16_t flags ;
333
+ } psa_core_key_attributes_t ;
334
+
335
+ #define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, 0, {0, 0, 0}, 0, 0}
336
+
337
+ struct psa_key_attributes_s
338
+ {
339
+ psa_core_key_attributes_t core ;
319
340
void * domain_parameters ;
320
341
size_t domain_parameters_size ;
321
342
};
322
343
323
- #define PSA_KEY_ATTRIBUTES_INIT {0, 0, {0, 0, 0}, 0, 0 , NULL, 0}
344
+ #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT , NULL, 0}
324
345
static inline struct psa_key_attributes_s psa_key_attributes_init ( void )
325
346
{
326
347
const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT ;
@@ -330,53 +351,53 @@ static inline struct psa_key_attributes_s psa_key_attributes_init( void )
330
351
static inline void psa_set_key_id (psa_key_attributes_t * attributes ,
331
352
psa_key_id_t id )
332
353
{
333
- attributes -> id = id ;
334
- if ( attributes -> lifetime == PSA_KEY_LIFETIME_VOLATILE )
335
- attributes -> lifetime = PSA_KEY_LIFETIME_PERSISTENT ;
354
+ attributes -> core . id = id ;
355
+ if ( attributes -> core . lifetime == PSA_KEY_LIFETIME_VOLATILE )
356
+ attributes -> core . lifetime = PSA_KEY_LIFETIME_PERSISTENT ;
336
357
}
337
358
338
359
static inline psa_key_id_t psa_get_key_id (
339
360
const psa_key_attributes_t * attributes )
340
361
{
341
- return ( attributes -> id );
362
+ return ( attributes -> core . id );
342
363
}
343
364
344
365
static inline void psa_set_key_lifetime (psa_key_attributes_t * attributes ,
345
366
psa_key_lifetime_t lifetime )
346
367
{
347
- attributes -> lifetime = lifetime ;
368
+ attributes -> core . lifetime = lifetime ;
348
369
if ( lifetime == PSA_KEY_LIFETIME_VOLATILE )
349
- attributes -> id = 0 ;
370
+ attributes -> core . id = 0 ;
350
371
}
351
372
352
373
static inline psa_key_lifetime_t psa_get_key_lifetime (
353
374
const psa_key_attributes_t * attributes )
354
375
{
355
- return ( attributes -> lifetime );
376
+ return ( attributes -> core . lifetime );
356
377
}
357
378
358
379
static inline void psa_set_key_usage_flags (psa_key_attributes_t * attributes ,
359
380
psa_key_usage_t usage_flags )
360
381
{
361
- attributes -> policy .usage = usage_flags ;
382
+ attributes -> core . policy .usage = usage_flags ;
362
383
}
363
384
364
385
static inline psa_key_usage_t psa_get_key_usage_flags (
365
386
const psa_key_attributes_t * attributes )
366
387
{
367
- return ( attributes -> policy .usage );
388
+ return ( attributes -> core . policy .usage );
368
389
}
369
390
370
391
static inline void psa_set_key_algorithm (psa_key_attributes_t * attributes ,
371
392
psa_algorithm_t alg )
372
393
{
373
- attributes -> policy .alg = alg ;
394
+ attributes -> core . policy .alg = alg ;
374
395
}
375
396
376
397
static inline psa_algorithm_t psa_get_key_algorithm (
377
398
const psa_key_attributes_t * attributes )
378
399
{
379
- return ( attributes -> policy .alg );
400
+ return ( attributes -> core . policy .alg );
380
401
}
381
402
382
403
/* This function is declared in crypto_extra.h, which comes after this
@@ -392,7 +413,7 @@ static inline void psa_set_key_type(psa_key_attributes_t *attributes,
392
413
if ( attributes -> domain_parameters == NULL )
393
414
{
394
415
/* Common case: quick path */
395
- attributes -> type = type ;
416
+ attributes -> core . type = type ;
396
417
}
397
418
else
398
419
{
@@ -407,19 +428,22 @@ static inline void psa_set_key_type(psa_key_attributes_t *attributes,
407
428
static inline psa_key_type_t psa_get_key_type (
408
429
const psa_key_attributes_t * attributes )
409
430
{
410
- return ( attributes -> type );
431
+ return ( attributes -> core . type );
411
432
}
412
433
413
434
static inline void psa_set_key_bits (psa_key_attributes_t * attributes ,
414
435
size_t bits )
415
436
{
416
- attributes -> bits = bits ;
437
+ if ( bits > PSA_MAX_KEY_BITS )
438
+ attributes -> core .bits = PSA_KEY_BITS_TOO_LARGE ;
439
+ else
440
+ attributes -> core .bits = (psa_key_bits_t ) bits ;
417
441
}
418
442
419
443
static inline size_t psa_get_key_bits (
420
444
const psa_key_attributes_t * attributes )
421
445
{
422
- return ( attributes -> bits );
446
+ return ( attributes -> core . bits );
423
447
}
424
448
425
449
#endif /* PSA_CRYPTO_STRUCT_H */
0 commit comments