Skip to content

Commit ad48da8

Browse files
Add slot_number attribute
Add a slot_number field to psa_key_attributes_t and getter/setter functions. Since slot numbers can have the value 0, indicate the presence of the field via a separate flag. In psa_get_key_attributes(), report the slot number if the key is in a secure element. When creating a key, for now, applications cannot choose a slot number. A subsequent commit will add this capability in the secure element HAL.
1 parent 91fcba4 commit ad48da8

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

include/psa/crypto_extra.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,67 @@ static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
104104
return( attributes->core.policy.alg2 );
105105
}
106106

107+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
108+
109+
/** Retrieve the slot number where a key is stored.
110+
*
111+
* A slot number is only defined for keys that are stored in a secure
112+
* element.
113+
*
114+
* This information is only useful if the secure element is not entirely
115+
* managed through the PSA Cryptography API. It is up to the secure
116+
* element driver to decide how PSA slot numbers map to any other interface
117+
* that the secure element may have.
118+
*
119+
* \param[in] attributes The key attribute structure to query.
120+
* \param[out] slot_number On success, the slot number containing the key.
121+
*
122+
* \retval #PSA_SUCCESS
123+
* The key is located in a secure element, and \p *slot_number
124+
* indicates the slot number that contains it.
125+
* \retval #PSA_ERROR_NOT_PERMITTED
126+
* The caller is not permitted to query the slot number.
127+
* Mbed Crypto currently does not return this error.
128+
* \retval #PSA_ERROR_INVALID_ARGUMENT
129+
* The key is not located in a secure element.
130+
*/
131+
psa_status_t psa_get_key_slot_number(
132+
const psa_key_attributes_t *attributes,
133+
psa_key_slot_number_t *slot_number );
134+
135+
/** Choose the slot number where a key is stored.
136+
*
137+
* This function declares a slot number in the specified attribute
138+
* structure.
139+
*
140+
* A slot number is only meaningful for keys that are stored in a secure
141+
* element. It is up to the secure element driver to decide how PSA slot
142+
* numbers map to any other interface that the secure element may have.
143+
*
144+
* \note Setting a slot number in key attributes for a key creation can
145+
* cause the following errors when creating the key:
146+
* - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
147+
* not support choosing a specific slot number.
148+
* - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
149+
* choose slot numbers in general or to choose this specific slot.
150+
* - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
151+
* valid in general or not valid for this specific key.
152+
* - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
153+
* selected slot.
154+
*
155+
* \param[out] attributes The attribute structure to write to.
156+
* \param slot_number The slot number to set.
157+
*/
158+
static inline void psa_set_key_slot_number(
159+
psa_key_attributes_t *attributes,
160+
psa_key_slot_number_t slot_number )
161+
{
162+
attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
163+
attributes->slot_number = slot_number;
164+
}
165+
166+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
167+
107168
/**@}*/
108169

109170
/**

include/psa/crypto_se_driver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,17 @@ typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
134134
void *persistent_data,
135135
psa_key_lifetime_t lifetime);
136136

137+
#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
138+
/* Mbed Crypto with secure element support enabled defines this type in
139+
* crypto_types.h because it is also visible to applications through an
140+
* implementation-specific extension.
141+
* For the PSA Cryptography specification, this type is only visible
142+
* via crypto_se_driver.h. */
137143
/** An internal designation of a key slot between the core part of the
138144
* PSA Crypto implementation and the driver. The meaning of this value
139145
* is driver-dependent. */
140146
typedef uint64_t psa_key_slot_number_t;
147+
#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
141148

142149
/**@}*/
143150

include/psa/crypto_struct.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,13 @@ typedef uint16_t psa_key_bits_t;
331331
*/
332332
typedef uint16_t psa_key_attributes_flag_t;
333333

334-
#define MBEDLTS_PSA_KA_FLAG_SLOT_NUMBER ( (psa_key_attributes_flag_t) 0x0001 )
334+
#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
335+
( (psa_key_attributes_flag_t) 0x0001 )
335336

336337
/* A mask of key attribute flags used externally only.
337338
* Only meant for internal checks inside the library. */
338339
#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
340+
MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
339341
0 )
340342

341343
/* A mask of key attribute flags used both internally and externally.
@@ -358,11 +360,19 @@ typedef struct
358360
struct psa_key_attributes_s
359361
{
360362
psa_core_key_attributes_t core;
363+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
364+
psa_key_slot_number_t slot_number;
365+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
361366
void *domain_parameters;
362367
size_t domain_parameters_size;
363368
};
364369

370+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371+
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
372+
#else
365373
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
374+
#endif
375+
366376
static inline struct psa_key_attributes_s psa_key_attributes_init( void )
367377
{
368378
const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;

include/psa/crypto_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ typedef uint32_t psa_key_usage_t;
244244
*/
245245
typedef struct psa_key_attributes_s psa_key_attributes_t;
246246

247+
248+
#ifndef __DOXYGEN_ONLY__
249+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
250+
/* Mbed Crypto defines this type in crypto_types.h because it is also
251+
* visible to applications through an implementation-specific extension.
252+
* For the PSA Cryptography specification, this type is only visible
253+
* via crypto_se_driver.h. */
254+
typedef uint64_t psa_key_slot_number_t;
255+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256+
#endif /* !__DOXYGEN_ONLY__ */
257+
247258
/**@}*/
248259

249260
/** \defgroup derivation Key derivation

library/psa_crypto.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,13 @@ psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
11861186
return( status );
11871187

11881188
attributes->core = slot->attr;
1189+
attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1190+
MBEDTLS_PSA_KA_MASK_DUAL_USE );
1191+
1192+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1193+
if( psa_key_slot_is_external( slot ) )
1194+
psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1195+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
11891196

11901197
switch( slot->attr.type )
11911198
{
@@ -1195,7 +1202,7 @@ psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
11951202
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
11961203
/* TOnogrepDO: reporting the public exponent for opaque keys
11971204
* is not yet implemented. */
1198-
if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
1205+
if( psa_key_slot_is_external( slot ) )
11991206
break;
12001207
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
12011208
status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
@@ -1211,6 +1218,21 @@ psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
12111218
return( status );
12121219
}
12131220

1221+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1222+
psa_status_t psa_get_key_slot_number(
1223+
const psa_key_attributes_t *attributes,
1224+
psa_key_slot_number_t *slot_number )
1225+
{
1226+
if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1227+
{
1228+
*slot_number = attributes->slot_number;
1229+
return( PSA_SUCCESS );
1230+
}
1231+
else
1232+
return( PSA_ERROR_INVALID_ARGUMENT );
1233+
}
1234+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1235+
12141236
#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
12151237
static int pk_write_pubkey_simple( mbedtls_pk_context *key,
12161238
unsigned char *buf, size_t size )
@@ -1556,6 +1578,10 @@ static psa_status_t psa_start_key_creation(
15561578
* we can roll back to a state where the key doesn't exist. */
15571579
if( *p_drv != NULL )
15581580
{
1581+
/* Choosing a slot number is not supported yet. */
1582+
if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1583+
return( PSA_ERROR_NOT_SUPPORTED );
1584+
15591585
status = psa_find_se_slot_for_key( attributes, *p_drv,
15601586
&slot->data.se.slot_number );
15611587
if( status != PSA_SUCCESS )

0 commit comments

Comments
 (0)