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