Skip to content

Commit

Permalink
Merge pull request #197 from gilles-peskine-arm/psa-refactor-attribut…
Browse files Browse the repository at this point in the history
…es-and-slots

Tidy up attribute management inside psa_crypto
  • Loading branch information
gilles-peskine-arm authored Aug 8, 2019
2 parents 640804b + f181eca commit 82a5711
Show file tree
Hide file tree
Showing 14 changed files with 676 additions and 289 deletions.
4 changes: 2 additions & 2 deletions include/psa/crypto_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static inline void psa_set_key_enrollment_algorithm(
psa_key_attributes_t *attributes,
psa_algorithm_t alg2)
{
attributes->policy.alg2 = alg2;
attributes->core.policy.alg2 = alg2;
}

/** Retrieve the enrollment algorithm policy from key attributes.
Expand All @@ -101,7 +101,7 @@ static inline void psa_set_key_enrollment_algorithm(
static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
const psa_key_attributes_t *attributes)
{
return( attributes->policy.alg2 );
return( attributes->core.policy.alg2 );
}

/**@}*/
Expand Down
64 changes: 44 additions & 20 deletions include/psa/crypto_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,39 @@ static inline struct psa_key_policy_s psa_key_policy_init( void )
return( v );
}

struct psa_key_attributes_s
/* The type used internally for key sizes.
* Public interfaces use size_t, but internally we use a smaller type. */
typedef uint16_t psa_key_bits_t;
/* The maximum value of the type used to represent bit-sizes.
* This is used to mark an invalid key size. */
#define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
/* The maximum size of a key in bits.
* Currently defined as the maximum that can be represented, rounded down
* to a whole number of bytes.
* This is an uncast value so that it can be used in preprocessor
* conditionals. */
#define PSA_MAX_KEY_BITS 0xfff8

typedef struct
{
psa_key_id_t id;
psa_key_type_t type;
psa_key_lifetime_t lifetime;
psa_key_id_t id;
psa_key_policy_t policy;
psa_key_type_t type;
size_t bits;
psa_key_bits_t bits;
uint16_t flags;
} psa_core_key_attributes_t;

#define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, 0, {0, 0, 0}, 0, 0}

struct psa_key_attributes_s
{
psa_core_key_attributes_t core;
void *domain_parameters;
size_t domain_parameters_size;
};

#define PSA_KEY_ATTRIBUTES_INIT {0, 0, {0, 0, 0}, 0, 0, NULL, 0}
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
static inline struct psa_key_attributes_s psa_key_attributes_init( void )
{
const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
Expand All @@ -330,53 +351,53 @@ static inline struct psa_key_attributes_s psa_key_attributes_init( void )
static inline void psa_set_key_id(psa_key_attributes_t *attributes,
psa_key_id_t id)
{
attributes->id = id;
if( attributes->lifetime == PSA_KEY_LIFETIME_VOLATILE )
attributes->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
attributes->core.id = id;
if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE )
attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
}

static inline psa_key_id_t psa_get_key_id(
const psa_key_attributes_t *attributes)
{
return( attributes->id );
return( attributes->core.id );
}

static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
psa_key_lifetime_t lifetime)
{
attributes->lifetime = lifetime;
attributes->core.lifetime = lifetime;
if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
attributes->id = 0;
attributes->core.id = 0;
}

static inline psa_key_lifetime_t psa_get_key_lifetime(
const psa_key_attributes_t *attributes)
{
return( attributes->lifetime );
return( attributes->core.lifetime );
}

static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
psa_key_usage_t usage_flags)
{
attributes->policy.usage = usage_flags;
attributes->core.policy.usage = usage_flags;
}

static inline psa_key_usage_t psa_get_key_usage_flags(
const psa_key_attributes_t *attributes)
{
return( attributes->policy.usage );
return( attributes->core.policy.usage );
}

static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
psa_algorithm_t alg)
{
attributes->policy.alg = alg;
attributes->core.policy.alg = alg;
}

static inline psa_algorithm_t psa_get_key_algorithm(
const psa_key_attributes_t *attributes)
{
return( attributes->policy.alg );
return( attributes->core.policy.alg );
}

/* This function is declared in crypto_extra.h, which comes after this
Expand All @@ -392,7 +413,7 @@ static inline void psa_set_key_type(psa_key_attributes_t *attributes,
if( attributes->domain_parameters == NULL )
{
/* Common case: quick path */
attributes->type = type;
attributes->core.type = type;
}
else
{
Expand All @@ -407,19 +428,22 @@ static inline void psa_set_key_type(psa_key_attributes_t *attributes,
static inline psa_key_type_t psa_get_key_type(
const psa_key_attributes_t *attributes)
{
return( attributes->type );
return( attributes->core.type );
}

static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
size_t bits)
{
attributes->bits = bits;
if( bits > PSA_MAX_KEY_BITS )
attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
else
attributes->core.bits = (psa_key_bits_t) bits;
}

static inline size_t psa_get_key_bits(
const psa_key_attributes_t *attributes)
{
return( attributes->bits );
return( attributes->core.bits );
}

#endif /* PSA_CRYPTO_STRUCT_H */
Loading

0 comments on commit 82a5711

Please sign in to comment.