0.2.0-dev Download ZIP
330 lines ·12.2 KB Raw
| 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef TF_PSA_CRYPTO_PSA_CRYPTO_SLOT_MANAGEMENT_H |
| 10 | #define TF_PSA_CRYPTO_PSA_CRYPTO_SLOT_MANAGEMENT_H |
| 11 | |
| 12 | #include "psa/crypto.h" |
| 13 | #include "psa_crypto_core.h" |
| 14 | |
| 15 | /** Range of volatile key identifiers. |
| 16 | * |
| 17 | * The first #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation |
| 18 | * range of key identifiers are reserved for volatile key identifiers. |
| 19 | * |
| 20 | * If \c id is a a volatile key identifier, #PSA_KEY_ID_VOLATILE_MIN - \c id |
| 21 | * indicates the key slot containing the volatile key definition. See |
| 22 | * psa_crypto_slot_management.c for details. |
| 23 | */ |
| 24 | |
| 25 | /** The minimum value for a volatile key identifier. |
| 26 | */ |
| 27 | #define PSA_KEY_ID_VOLATILE_MIN PSA_KEY_ID_VENDOR_MIN |
| 28 | |
| 29 | /** The maximum value for a volatile key identifier. |
| 30 | */ |
| 31 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 32 | #define PSA_KEY_ID_VOLATILE_MAX (MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1) |
| 33 | #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 34 | #define PSA_KEY_ID_VOLATILE_MAX \ |
| 35 | (PSA_KEY_ID_VOLATILE_MIN + MBEDTLS_PSA_KEY_SLOT_COUNT - 1) |
| 36 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 37 | |
| 38 | /** Test whether a key identifier is a volatile key identifier. |
| 39 | * |
| 40 | * \param key_id Key identifier to test. |
| 41 | * |
| 42 | * \retval 1 |
| 43 | * The key identifier is a volatile key identifier. |
| 44 | * \retval 0 |
| 45 | * The key identifier is not a volatile key identifier. |
| 46 | */ |
| 47 | static inline int psa_key_id_is_volatile(psa_key_id_t key_id) |
| 48 | { |
| 49 | return (key_id >= PSA_KEY_ID_VOLATILE_MIN) && |
| 50 | (key_id <= PSA_KEY_ID_VOLATILE_MAX); |
| 51 | } |
| 52 | |
| 53 | /** Get the description of a key given its identifier and lock it. |
| 54 | * |
| 55 | * The descriptions of volatile keys and loaded persistent keys are stored in |
| 56 | * key slots. This function returns a pointer to the key slot containing the |
| 57 | * description of a key given its identifier. |
| 58 | * |
| 59 | * In case of a persistent key, the function loads the description of the key |
| 60 | * into a key slot if not already done. |
| 61 | * |
| 62 | * On success, the returned key slot has been registered for reading. |
| 63 | * It is the responsibility of the caller to call psa_unregister_read(slot) |
| 64 | * when they have finished reading the contents of the slot. |
| 65 | * |
| 66 | * On failure, `*p_slot` is set to NULL. This ensures that it is always valid |
| 67 | * to call psa_unregister_read on the returned slot. |
| 68 | * |
| 69 | * \param key Key identifier to query. |
| 70 | * \param[out] p_slot On success, `*p_slot` contains a pointer to the |
| 71 | * key slot containing the description of the key |
| 72 | * identified by \p key. |
| 73 | * |
| 74 | * \retval #PSA_SUCCESS |
| 75 | * \p *p_slot contains a pointer to the key slot containing the |
| 76 | * description of the key identified by \p key. |
| 77 | * The key slot counter has been incremented. |
| 78 | * \retval #PSA_ERROR_BAD_STATE |
| 79 | * The library has not been initialized. |
| 80 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 81 | * \p key is not a valid key identifier. |
| 82 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 83 | * \p key is a persistent key identifier. The implementation does not |
| 84 | * have sufficient resources to load the persistent key. This can be |
| 85 | * due to a lack of empty key slot, or available memory. |
| 86 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 87 | * There is no key with key identifier \p key. |
| 88 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 89 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 90 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 91 | */ |
| 92 | psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key, |
| 93 | psa_key_slot_t **p_slot); |
| 94 | |
| 95 | /** Initialize the key slot structures. |
| 96 | * |
| 97 | * \retval #PSA_SUCCESS |
| 98 | * Currently this function always succeeds. |
| 99 | */ |
| 100 | psa_status_t psa_initialize_key_slots(void); |
| 101 | |
| 102 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 103 | /* Allow test code to customize the key slice length. We use this in tests |
| 104 | * that exhaust the key store to reach a full key store in reasonable time |
| 105 | * and memory. |
| 106 | * |
| 107 | * The length of each slice must be between 1 and |
| 108 | * (1 << KEY_ID_SLOT_INDEX_WIDTH) inclusive. |
| 109 | * |
| 110 | * The length for a given slice index must not change while |
| 111 | * the key store is initialized. |
| 112 | */ |
| 113 | extern size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)( |
| 114 | size_t slice_idx); |
| 115 | |
| 116 | /* The number of volatile key slices. */ |
| 117 | size_t psa_key_slot_volatile_slice_count(void); |
| 118 | #endif |
| 119 | |
| 120 | /** Delete all data from key slots in memory. |
| 121 | * This function is not thread safe, it wipes every key slot regardless of |
| 122 | * state and reader count. It should only be called when no slot is in use. |
| 123 | * |
| 124 | * This does not affect persistent storage. */ |
| 125 | void psa_wipe_all_key_slots(void); |
| 126 | |
| 127 | /** Find a free key slot and reserve it to be filled with a key. |
| 128 | * |
| 129 | * This function finds a key slot that is free, |
| 130 | * sets its state to PSA_SLOT_FILLING and then returns the slot. |
| 131 | * |
| 132 | * On success, the key slot's state is PSA_SLOT_FILLING. |
| 133 | * It is the responsibility of the caller to change the slot's state to |
| 134 | * PSA_SLOT_EMPTY/FULL once key creation has finished. |
| 135 | * |
| 136 | * If multi-threading is enabled, the caller must hold the |
| 137 | * global key slot mutex. |
| 138 | * |
| 139 | * \param[out] volatile_key_id - If null, reserve a cache slot for |
| 140 | * a persistent or built-in key. |
| 141 | * - If non-null, allocate a slot for |
| 142 | * a volatile key. On success, |
| 143 | * \p *volatile_key_id is the |
| 144 | * identifier corresponding to the |
| 145 | * returned slot. It is the caller's |
| 146 | * responsibility to set this key identifier |
| 147 | * in the attributes. |
| 148 | * \param[out] p_slot On success, a pointer to the slot. |
| 149 | * |
| 150 | * \retval #PSA_SUCCESS \emptydescription |
| 151 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 152 | * There were no free key slots. |
| 153 | * When #MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, there was not |
| 154 | * enough memory to allocate more slots. |
| 155 | * \retval #PSA_ERROR_BAD_STATE \emptydescription |
| 156 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 157 | * This function attempted to operate on a key slot which was in an |
| 158 | * unexpected state. |
| 159 | */ |
| 160 | psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id, |
| 161 | psa_key_slot_t **p_slot); |
| 162 | |
| 163 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 164 | /** Return a key slot to the free list. |
| 165 | * |
| 166 | * Call this function when a slot obtained from psa_reserve_free_key_slot() |
| 167 | * is no longer in use. |
| 168 | * |
| 169 | * If multi-threading is enabled, the caller must hold the |
| 170 | * global key slot mutex. |
| 171 | * |
| 172 | * \param slice_idx The slice containing the slot. |
| 173 | * This is `slot->slice_index` when the slot |
| 174 | * is obtained from psa_reserve_free_key_slot(). |
| 175 | * \param slot The key slot. |
| 176 | * |
| 177 | * \retval #PSA_SUCCESS \emptydescription |
| 178 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 179 | * This function attempted to operate on a key slot which was in an |
| 180 | * unexpected state. |
| 181 | */ |
| 182 | psa_status_t psa_free_key_slot(size_t slice_idx, |
| 183 | psa_key_slot_t *slot); |
| 184 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 185 | |
| 186 | /** Change the state of a key slot. |
| 187 | * |
| 188 | * This function changes the state of the key slot from expected_state to |
| 189 | * new state. If the state of the slot was not expected_state, the state is |
| 190 | * unchanged. |
| 191 | * |
| 192 | * If multi-threading is enabled, the caller must hold the |
| 193 | * global key slot mutex. |
| 194 | * |
| 195 | * \param[in] slot The key slot. |
| 196 | * \param[in] expected_state The current state of the slot. |
| 197 | * \param[in] new_state The new state of the slot. |
| 198 | * |
| 199 | * \retval #PSA_SUCCESS |
| 200 | The key slot's state variable is new_state. |
| 201 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 202 | * The slot's state was not expected_state. |
| 203 | */ |
| 204 | static inline psa_status_t psa_key_slot_state_transition( |
| 205 | psa_key_slot_t *slot, psa_key_slot_state_t expected_state, |
| 206 | psa_key_slot_state_t new_state) |
| 207 | { |
| 208 | if (slot->state != expected_state) { |
| 209 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 210 | } |
| 211 | slot->state = new_state; |
| 212 | return PSA_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | /** Register as a reader of a key slot. |
| 216 | * |
| 217 | * This function increments the key slot registered reader counter by one. |
| 218 | * If multi-threading is enabled, the caller must hold the |
| 219 | * global key slot mutex. |
| 220 | * |
| 221 | * \param[in] slot The key slot. |
| 222 | * |
| 223 | * \retval #PSA_SUCCESS |
| 224 | The key slot registered reader counter was incremented. |
| 225 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 226 | * The reader counter already reached its maximum value and was not |
| 227 | * increased, or the slot's state was not PSA_SLOT_FULL. |
| 228 | */ |
| 229 | static inline psa_status_t psa_register_read(psa_key_slot_t *slot) |
| 230 | { |
| 231 | if ((slot->state != PSA_SLOT_FULL) || |
| 232 | (slot->var.occupied.registered_readers >= SIZE_MAX)) { |
| 233 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 234 | } |
| 235 | slot->var.occupied.registered_readers++; |
| 236 | |
| 237 | return PSA_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | /** Unregister from reading a key slot. |
| 241 | * |
| 242 | * This function decrements the key slot registered reader counter by one. |
| 243 | * If the state of the slot is PSA_SLOT_PENDING_DELETION, |
| 244 | * and there is only one registered reader (the caller), |
| 245 | * this function will call psa_wipe_key_slot(). |
| 246 | * If multi-threading is enabled, the caller must hold the |
| 247 | * global key slot mutex. |
| 248 | * |
| 249 | * \note To ease the handling of errors in retrieving a key slot |
| 250 | * a NULL input pointer is valid, and the function returns |
| 251 | * successfully without doing anything in that case. |
| 252 | * |
| 253 | * \param[in] slot The key slot. |
| 254 | * \retval #PSA_SUCCESS |
| 255 | * \p slot is NULL or the key slot reader counter has been |
| 256 | * decremented (and potentially wiped) successfully. |
| 257 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 258 | * The slot's state was neither PSA_SLOT_FULL nor |
| 259 | * PSA_SLOT_PENDING_DELETION. |
| 260 | * Or a wipe was attempted and the slot's state was not |
| 261 | * PSA_SLOT_PENDING_DELETION. |
| 262 | * Or registered_readers was equal to 0. |
| 263 | */ |
| 264 | psa_status_t psa_unregister_read(psa_key_slot_t *slot); |
| 265 | |
| 266 | /** Wrap a call to psa_unregister_read in the global key slot mutex. |
| 267 | * |
| 268 | * If threading is disabled, this simply calls psa_unregister_read. |
| 269 | * |
| 270 | * \note To ease the handling of errors in retrieving a key slot |
| 271 | * a NULL input pointer is valid, and the function returns |
| 272 | * successfully without doing anything in that case. |
| 273 | * |
| 274 | * \param[in] slot The key slot. |
| 275 | * \retval #PSA_SUCCESS |
| 276 | * \p slot is NULL or the key slot reader counter has been |
| 277 | * decremented (and potentially wiped) successfully. |
| 278 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 279 | * The slot's state was neither PSA_SLOT_FULL nor |
| 280 | * PSA_SLOT_PENDING_DELETION. |
| 281 | * Or a wipe was attempted and the slot's state was not |
| 282 | * PSA_SLOT_PENDING_DELETION. |
| 283 | * Or registered_readers was equal to 0. |
| 284 | */ |
| 285 | psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot); |
| 286 | |
| 287 | /** Test whether a lifetime designates a key in an external cryptoprocessor. |
| 288 | * |
| 289 | * \param lifetime The lifetime to test. |
| 290 | * |
| 291 | * \retval 1 |
| 292 | * The lifetime designates an external key. There should be a |
| 293 | * registered driver for this lifetime, otherwise the key cannot |
| 294 | * be created or manipulated. |
| 295 | * \retval 0 |
| 296 | * The lifetime designates a key that is volatile or in internal |
| 297 | * storage. |
| 298 | */ |
| 299 | static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime) |
| 300 | { |
| 301 | return PSA_KEY_LIFETIME_GET_LOCATION(lifetime) |
| 302 | != PSA_KEY_LOCATION_LOCAL_STORAGE; |
| 303 | } |
| 304 | |
| 305 | /** Validate the persistence of a key. |
| 306 | * |
| 307 | * \param[in] lifetime The key lifetime attribute. |
| 308 | * |
| 309 | * \retval #PSA_SUCCESS \emptydescription |
| 310 | * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys |
| 311 | * are not supported. |
| 312 | */ |
| 313 | psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime); |
| 314 | |
| 315 | /** Test whether a key identifier belongs to the user key range. |
| 316 | * |
| 317 | * \param key_id Key identifier to test. |
| 318 | * |
| 319 | * \retval 1 |
| 320 | * The key identifier is a user key identifier. |
| 321 | * \retval 0 |
| 322 | * The key identifier is not a user key identifier. |
| 323 | */ |
| 324 | static inline int psa_key_id_is_user(psa_key_id_t key_id) |
| 325 | { |
| 326 | return (key_id >= PSA_KEY_ID_USER_MIN) && |
| 327 | (key_id <= PSA_KEY_ID_USER_MAX); |
| 328 | } |
| 329 | |
| 330 | #endif /* TF_PSA_CRYPTO_PSA_CRYPTO_SLOT_MANAGEMENT_H */ |