psa_crypto_slot_management.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 HAW Hamburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include "clist.h"
28 #include "psa/crypto.h"
30 
34 #define PSA_PROTECTED_KEY_COUNT (CONFIG_PSA_PROTECTED_KEY_COUNT)
35 
39 #define PSA_ASYMMETRIC_KEYPAIR_COUNT (CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT)
40 
44 #define PSA_SINGLE_KEY_COUNT (CONFIG_PSA_SINGLE_KEY_COUNT)
45 
49 #define PSA_KEY_SLOT_COUNT (PSA_PROTECTED_KEY_COUNT + \
50  PSA_ASYMMETRIC_KEYPAIR_COUNT + \
51  PSA_SINGLE_KEY_COUNT)
52 
58 #define PSA_KEY_ID_VOLATILE_MIN (PSA_KEY_ID_VENDOR_MIN)
59 
65 #define PSA_KEY_ID_VOLATILE_MAX (PSA_KEY_ID_VENDOR_MAX)
66 
75 typedef struct {
77  size_t lock_count;
80 #if PSA_SINGLE_KEY_COUNT
81  struct key_data {
82  uint8_t data[PSA_MAX_KEY_DATA_SIZE];
83  size_t data_len;
84  } key;
85 #endif /* PSA_SINGLE_KEY_COUNT */
87 
88 #if PSA_PROTECTED_KEY_COUNT
95 typedef struct {
96  clist_node_t node;
97  size_t lock_count;
99  struct prot_key_data {
100  psa_key_slot_number_t slot_number;
101 #if IS_USED(MODULE_PSA_ASYMMETRIC)
102  uint8_t pubkey_data[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
103  size_t pubkey_data_len;
104 #endif
105  } key;
106 } psa_prot_key_slot_t;
107 #endif /* PSA_PROTECTED_KEY_COUNT */
108 
109 #if PSA_ASYMMETRIC_KEYPAIR_COUNT
116 typedef struct {
117  clist_node_t node;
118  size_t lock_count;
120  struct key_pair_data {
122  uint8_t privkey_data[PSA_BITS_TO_BYTES(PSA_MAX_PRIV_KEY_SIZE)];
124  size_t privkey_data_len;
126  uint8_t pubkey_data[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
128  size_t pubkey_data_len;
129  } key;
130 } psa_key_pair_slot_t;
131 #endif /* PSA_ASYMMETRIC_KEYPAIR_COUNT */
132 
137 
146 static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
147 {
148  return ((key_id >= PSA_KEY_ID_VOLATILE_MIN) &&
149  (key_id <= PSA_KEY_ID_VOLATILE_MAX));
150 }
151 
159 static inline int psa_is_key_slot_locked(psa_key_slot_t *slot)
160 {
161  return (slot->lock_count > 0);
162 }
163 
171 
181 {
183 }
184 
194 
199 
211 
219 
231  const psa_key_attributes_t *attr,
232  psa_key_slot_t **p_slot);
233 
243 
253 
264  psa_se_drv_data_t **driver);
265 
275 
285 int psa_is_valid_key_id(psa_key_id_t id, int vendor);
286 
297  uint8_t **key_data,
298  size_t **key_bytes);
299 
308  uint8_t **pubkey_data,
309  size_t **pubkey_data_len);
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
Circular linked list.
Function declarations for PSA Crypto.
uint64_t psa_key_slot_number_t
Encoding of a key slot number on a secure element.
int psa_is_valid_key_id(psa_key_id_t id, int vendor)
Check if provided key ID is either a valid user ID or vendor ID.
psa_status_t psa_lock_key_slot(psa_key_slot_t *slot)
Increase lock count.
#define PSA_KEY_ID_VOLATILE_MAX
Maximum key id for volatile keys.
void psa_init_key_slots(void)
Initializes the allocated key slots and prepares the internal key slot lists.
static int psa_is_key_slot_locked(psa_key_slot_t *slot)
Check whether a key slot is locked.
static int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
Check whether a key is stored on an external device.
psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Validate key persistence.
psa_status_t psa_get_and_lock_key_slot(psa_key_id_t id, psa_key_slot_t **slot)
Find a key slot in local memory and lock it.
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime, psa_se_drv_data_t **driver)
Check if key location exists.
psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Wipe volatile key slot and its contents.
#define PSA_KEY_ID_VOLATILE_MIN
Minimum key id for volatile keys.
psa_status_t psa_persist_key_slot_in_storage(psa_key_slot_t *slot)
Store a key slot in persistent storage.
psa_status_t psa_allocate_empty_key_slot(psa_key_id_t *id, const psa_key_attributes_t *attr, psa_key_slot_t **p_slot)
Find a currently empty key slot that is appropriate for the key.
psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Decrease lock count.
psa_key_slot_number_t * psa_key_slot_get_slot_number(const psa_key_slot_t *slot)
Get slot number in protected memory.
void psa_get_public_key_data_from_key_slot(const psa_key_slot_t *slot, uint8_t **pubkey_data, size_t **pubkey_data_len)
Get public key data and size from key slot.
void psa_wipe_all_key_slots(void)
Wipe all existing volatile key slots.
static int psa_key_id_is_volatile(psa_key_id_t key_id)
Check whether a key identifier is a volatile key identifier.
size_t psa_get_key_data_from_key_slot(const psa_key_slot_t *slot, uint8_t **key_data, size_t **key_bytes)
Get key data and key size from key slot.
uint32_t psa_key_id_t
Key identifier.
Definition: id.h:36
#define PSA_MAX_PRIV_KEY_SIZE
The maximum size of an asymmetric private key.
Definition: sizes.h:139
#define PSA_MAX_KEY_DATA_SIZE
The maximum size of the used key data.
Definition: sizes.h:267
#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
Sufficient buffer size for exporting any asymmetric public key.
Definition: sizes.h:245
#define PSA_KEY_LOCATION_LOCAL_STORAGE
The local storage area for persistent keys.
Definition: lifetime.h:201
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
Extract the location indicator from a key lifetime.
Definition: lifetime.h:244
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
Definition: lifetime.h:66
int32_t psa_status_t
Status code type used for all PSA Certified APIs.
Definition: error.h:40
PSA Secure Element management function declarations.
#define PSA_BITS_TO_BYTES(bits)
Functions to convert bits to bytes.
Definition: sizes.h:35
List node structure.
Definition: list.h:39
Structure storing key attributes.
Definition: attributes.h:51
Structure of a virtual key slot in local memory.
clist_node_t node
List node to link slot in global list.
psa_key_attributes_t attr
Attributes associated with the stored key.
size_t lock_count
Number of entities accessing the slot.
Structure containing secure element driver data and contexts.