Skip to content

Commit 2f13648

Browse files
Add infrastructure for key attribute flags
Add infrastructure for internal, external and dual-use flags, with a compile-time check (if static_assert is available) to ensure that the same numerical value doesn't get declared for two different purposes in crypto_struct.h (external or dual-use) and psa_crypto_core.h (internal).
1 parent 8908c5e commit 2f13648

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

include/psa/crypto_struct.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,35 @@ typedef uint16_t psa_key_bits_t;
322322
* conditionals. */
323323
#define PSA_MAX_KEY_BITS 0xfff8
324324

325+
/** A mask of flags that can be stored in key attributes.
326+
*
327+
* This type is also used internally to store flags in slots. Internal
328+
* flags are defined in library/psa_crypto_core.h. Internal flags may have
329+
* the same value as external flags if they are properly handled during
330+
* key creation and in psa_get_key_attributes.
331+
*/
332+
typedef uint16_t psa_key_attributes_flag_t;
333+
334+
#define MBEDLTS_PSA_KA_FLAG_SLOT_NUMBER ( (psa_key_attributes_flag_t) 0x0001 )
335+
336+
/* A mask of key attribute flags used externally only.
337+
* Only meant for internal checks inside the library. */
338+
#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
339+
0 )
340+
341+
/* A mask of key attribute flags used both internally and externally.
342+
* Currently there aren't any. */
343+
#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
344+
0 )
345+
325346
typedef struct
326347
{
327348
psa_key_type_t type;
328349
psa_key_lifetime_t lifetime;
329350
psa_key_id_t id;
330351
psa_key_policy_t policy;
331352
psa_key_bits_t bits;
332-
uint16_t flags;
353+
psa_key_attributes_flag_t flags;
333354
} psa_core_key_attributes_t;
334355

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

library/psa_crypto.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,15 @@ psa_status_t psa_export_public_key( psa_key_handle_t handle,
14071407
data_length, 1 ) );
14081408
}
14091409

1410+
#if defined(static_assert)
1411+
static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1412+
"One or more key attribute flag is listed as both external-only and dual-use" );
1413+
static_assert( ( MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1414+
"One or more key attribute flag is listed as both external-only and dual-use" );
1415+
static_assert( ( MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
1416+
"One or more key attribute flag is listed as both internal-only and external-only" );
1417+
#endif
1418+
14101419
/** Validate that a key policy is internally well-formed.
14111420
*
14121421
* This function only rejects invalid policies. It does not validate the
@@ -1466,6 +1475,11 @@ static psa_status_t psa_validate_key_attributes(
14661475
if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
14671476
return( PSA_ERROR_NOT_SUPPORTED );
14681477

1478+
/* Reject invalid flags. These should not be reachable through the API. */
1479+
if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1480+
MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1481+
return( PSA_ERROR_INVALID_ARGUMENT );
1482+
14691483
return( PSA_SUCCESS );
14701484
}
14711485

@@ -1522,6 +1536,10 @@ static psa_status_t psa_start_key_creation(
15221536

15231537
slot->attr = attributes->core;
15241538

1539+
/* Erase external-only flags from the internal copy. To access
1540+
* external-only flags, query `attributes`. */
1541+
slot->attr.flags |= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
1542+
15251543
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
15261544
/* For a key in a secure element, we need to do three things:
15271545
* create the key file in internal storage, create the

library/psa_crypto_core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ typedef struct
6464
} data;
6565
} psa_key_slot_t;
6666

67+
/* A mask of key attribute flags used only internally.
68+
* Currently there aren't any. */
69+
#define MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY ( \
70+
0 )
71+
6772
/** Test whether a key slot is occupied.
6873
*
6974
* A key slot is occupied iff the key type is nonzero. This works because

0 commit comments

Comments
 (0)