Skip to content

Commit 82a5711

Browse files
Merge pull request #197 from gilles-peskine-arm/psa-refactor-attributes-and-slots
Tidy up attribute management inside psa_crypto
2 parents 640804b + f181eca commit 82a5711

14 files changed

+676
-289
lines changed

include/psa/crypto_extra.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static inline void psa_set_key_enrollment_algorithm(
8989
psa_key_attributes_t *attributes,
9090
psa_algorithm_t alg2)
9191
{
92-
attributes->policy.alg2 = alg2;
92+
attributes->core.policy.alg2 = alg2;
9393
}
9494

9595
/** Retrieve the enrollment algorithm policy from key attributes.
@@ -101,7 +101,7 @@ static inline void psa_set_key_enrollment_algorithm(
101101
static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
102102
const psa_key_attributes_t *attributes)
103103
{
104-
return( attributes->policy.alg2 );
104+
return( attributes->core.policy.alg2 );
105105
}
106106

107107
/**@}*/

include/psa/crypto_struct.h

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,39 @@ static inline struct psa_key_policy_s psa_key_policy_init( void )
309309
return( v );
310310
}
311311

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
313326
{
314-
psa_key_id_t id;
327+
psa_key_type_t type;
315328
psa_key_lifetime_t lifetime;
329+
psa_key_id_t id;
316330
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;
319340
void *domain_parameters;
320341
size_t domain_parameters_size;
321342
};
322343

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}
324345
static inline struct psa_key_attributes_s psa_key_attributes_init( void )
325346
{
326347
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 )
330351
static inline void psa_set_key_id(psa_key_attributes_t *attributes,
331352
psa_key_id_t id)
332353
{
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;
336357
}
337358

338359
static inline psa_key_id_t psa_get_key_id(
339360
const psa_key_attributes_t *attributes)
340361
{
341-
return( attributes->id );
362+
return( attributes->core.id );
342363
}
343364

344365
static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
345366
psa_key_lifetime_t lifetime)
346367
{
347-
attributes->lifetime = lifetime;
368+
attributes->core.lifetime = lifetime;
348369
if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
349-
attributes->id = 0;
370+
attributes->core.id = 0;
350371
}
351372

352373
static inline psa_key_lifetime_t psa_get_key_lifetime(
353374
const psa_key_attributes_t *attributes)
354375
{
355-
return( attributes->lifetime );
376+
return( attributes->core.lifetime );
356377
}
357378

358379
static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
359380
psa_key_usage_t usage_flags)
360381
{
361-
attributes->policy.usage = usage_flags;
382+
attributes->core.policy.usage = usage_flags;
362383
}
363384

364385
static inline psa_key_usage_t psa_get_key_usage_flags(
365386
const psa_key_attributes_t *attributes)
366387
{
367-
return( attributes->policy.usage );
388+
return( attributes->core.policy.usage );
368389
}
369390

370391
static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
371392
psa_algorithm_t alg)
372393
{
373-
attributes->policy.alg = alg;
394+
attributes->core.policy.alg = alg;
374395
}
375396

376397
static inline psa_algorithm_t psa_get_key_algorithm(
377398
const psa_key_attributes_t *attributes)
378399
{
379-
return( attributes->policy.alg );
400+
return( attributes->core.policy.alg );
380401
}
381402

382403
/* 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,
392413
if( attributes->domain_parameters == NULL )
393414
{
394415
/* Common case: quick path */
395-
attributes->type = type;
416+
attributes->core.type = type;
396417
}
397418
else
398419
{
@@ -407,19 +428,22 @@ static inline void psa_set_key_type(psa_key_attributes_t *attributes,
407428
static inline psa_key_type_t psa_get_key_type(
408429
const psa_key_attributes_t *attributes)
409430
{
410-
return( attributes->type );
431+
return( attributes->core.type );
411432
}
412433

413434
static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
414435
size_t bits)
415436
{
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;
417441
}
418442

419443
static inline size_t psa_get_key_bits(
420444
const psa_key_attributes_t *attributes)
421445
{
422-
return( attributes->bits );
446+
return( attributes->core.bits );
423447
}
424448

425449
#endif /* PSA_CRYPTO_STRUCT_H */

0 commit comments

Comments
 (0)