Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up attribute management inside psa_crypto #197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
424f894
SE keys: store the bit size internally (partial implementation)
gilles-peskine-arm Jul 15, 2019
dc5bfe9
SE keys: implement and test psa_get_key_attributes
gilles-peskine-arm Jul 24, 2019
1801740
SE driver: report the bit size on key import
gilles-peskine-arm Jul 24, 2019
e60d1d0
SE keys: save the bit size in storage
gilles-peskine-arm Jul 24, 2019
fc321f1
SE keys: test that the bit size is saved and loaded correctly
gilles-peskine-arm Jul 24, 2019
7e0cff9
Move attribute fields to a substructure
gilles-peskine-arm Jul 30, 2019
c744d99
Limit keys to 65528 bits
gilles-peskine-arm Jul 30, 2019
68cc433
Store key sizes in 16 bits in attributes
gilles-peskine-arm Jul 30, 2019
8e33870
Use psa_core_key_attributes_t in key slots in memory
gilles-peskine-arm Jul 30, 2019
4ed0e6f
Switch storage functions over to psa_core_key_attributes_t
gilles-peskine-arm Jul 30, 2019
2431859
Take advantage of psa_core_key_attributes_t internally: key loading
gilles-peskine-arm Jul 30, 2019
b46bef2
Store the key size in the slot in memory
gilles-peskine-arm Jul 30, 2019
76aa09c
Take advantage of psa_core_key_attributes_t internally #2
gilles-peskine-arm Jul 31, 2019
41e50d2
Remove "allocated" flag from key slots
gilles-peskine-arm Jul 31, 2019
7c227ae
Test key creation with an invalid type (0 and nonzero)
gilles-peskine-arm Jul 31, 2019
6edfa29
Add test function for import with a bad policy
gilles-peskine-arm Jul 31, 2019
3825e14
Fix policy validity check on key creation.
gilles-peskine-arm Jul 31, 2019
1b8594a
More refactoring: consolidate attribute validation
gilles-peskine-arm Jul 31, 2019
8908c5e
Make psa_calculate_key_bits return psa_key_bits_t
gilles-peskine-arm Jul 31, 2019
1b9505c
Correct some comments
gilles-peskine-arm Aug 7, 2019
49232e8
Avoid a lowercase letter in a macro name
gilles-peskine-arm Aug 7, 2019
b1f6c5f
Fix copypasta in test data
gilles-peskine-arm Aug 7, 2019
a6b2f60
Fix double free in psa_generate_key when psa_generate_random fails
gilles-peskine-arm Aug 7, 2019
bdc96fd
Add tests to generate more random than MBEDTLS_CTR_DRBG_MAX_REQUEST
gilles-peskine-arm Aug 7, 2019
f181eca
Fix psa_generate_random for >1024 bytes
gilles-peskine-arm Aug 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 8 additions & 3 deletions include/psa/crypto_se_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,18 @@ typedef psa_status_t (*psa_drv_se_allocate_key_t)(
*
* \param[in,out] drv_context The driver context structure.
* \param[in] key_slot Slot where the key will be stored
* This must be a valid slot for a key of the chosen
* type. It must be unoccupied.
* This must be a valid slot for a key of the
* chosen type. It must be unoccupied.
* \param[in] lifetime The required lifetime of the key storage
* \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
* \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
* \param[in] usage The allowed uses of the key
* \param[in] p_data Buffer containing the key data
* \param[in] data_length Size of the `data` buffer in bytes
* \param[out] bits On success, the key size in bits. The driver
* must determine this value after parsing the
* key according to the key type.
* This value is not used if the function fails.
*
* \retval #PSA_SUCCESS
* Success.
Expand All @@ -852,7 +856,8 @@ typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_contex
psa_algorithm_t algorithm,
psa_key_usage_t usage,
const uint8_t *p_data,
size_t data_length);
size_t data_length,
size_t *bits);

/**
* \brief A function that destroys a secure element key and restore the slot to
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