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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 9 | #include "common.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include "psa/crypto.h" |
| 14 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 15 | #include "psa_crypto_core.h" |
Xiaokang Qian | fe9666b | 2023-09-11 10:36:20 +0000 | [diff] [blame] | 16 | #include "psa_crypto_driver_wrappers_no_static.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 17 | #include "psa_crypto_slot_management.h" |
| 18 | #include "psa_crypto_storage.h" |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 20 | #include "psa_crypto_se.h" |
| 21 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 25 | #include "mbedtls/platform.h" |
Ryan Everett | 491f7e5 | 2024-01-08 11:04:21 +0000 | [diff] [blame] | 26 | #if defined(MBEDTLS_THREADING_C) |
| 27 | #include "mbedtls/threading.h" |
| 28 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 29 | |
Gilles Peskine | b6bf370 | 2024-06-20 22:15:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* Make sure we have distinct ranges of key identifiers for distinct |
| 33 | * purposes. */ |
| 34 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MIN < PSA_KEY_ID_USER_MAX, |
| 35 | "Empty user key ID range"); |
| 36 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN < PSA_KEY_ID_VENDOR_MAX, |
| 37 | "Empty vendor key ID range"); |
| 38 | MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_KEY_ID_BUILTIN_MIN < MBEDTLS_PSA_KEY_ID_BUILTIN_MAX, |
| 39 | "Empty builtin key ID range"); |
| 40 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MIN < PSA_KEY_ID_VOLATILE_MAX, |
| 41 | "Empty volatile key ID range"); |
| 42 | |
| 43 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MAX < PSA_KEY_ID_VENDOR_MIN || |
| 44 | PSA_KEY_ID_VENDOR_MAX < PSA_KEY_ID_USER_MIN, |
| 45 | "Overlap between user key IDs and vendor key IDs"); |
| 46 | |
| 47 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN && |
| 48 | MBEDTLS_PSA_KEY_ID_BUILTIN_MAX <= PSA_KEY_ID_VENDOR_MAX, |
| 49 | "Builtin key identifiers are not in the vendor range"); |
| 50 | |
| 51 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= PSA_KEY_ID_VOLATILE_MIN && |
| 52 | PSA_KEY_ID_VOLATILE_MAX <= PSA_KEY_ID_VENDOR_MAX, |
| 53 | "Volatile key identifiers are not in the vendor range"); |
| 54 | |
| 55 | MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MAX < MBEDTLS_PSA_KEY_ID_BUILTIN_MIN || |
| 56 | MBEDTLS_PSA_KEY_ID_BUILTIN_MAX < PSA_KEY_ID_VOLATILE_MIN, |
| 57 | "Overlap between builtin key IDs and volatile key IDs"); |
| 58 | |
| 59 | |
| 60 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 62 | |
| 63 | /* Dynamic key store. |
| 64 | * |
| 65 | * The key store consists of multiple slices. |
| 66 | * |
| 67 | * The volatile keys are stored in variable-sized tables called slices. |
| 68 | * Slices are allocated on demand and deallocated when possible. |
| 69 | * The size of slices increases exponentially, so the average overhead |
| 70 | * (number of slots that are allocated but not used) is roughly |
| 71 | * proportional to the number of keys (with a factor that grows |
| 72 | * when the key store is fragmented). |
| 73 | * |
| 74 | * One slice is dedicated to the cache of persistent and built-in keys. |
| 75 | * For simplicity, they are separated from volatile keys. This cache |
| 76 | * slice has a fixed size and has the slice index KEY_SLOT_CACHE_SLICE_INDEX, |
| 77 | * located after the slices for volatile keys. |
| 78 | */ |
| 79 | |
| 80 | /* Size of slice 0 containing the cache of persistent and built-in keys. */ |
| 81 | #define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT |
| 82 | |
| 83 | /* Volatile keys are stored in slices 1 through KEY_SLICE_COUNT inclusive. |
| 84 | * Each slice is twice the size of the previous slice. |
| 85 | * Volatile key identifiers encode the slice number as follows: |
| 86 | * bits 30..31: 0b10 (mandated by the PSA Crypto specification). |
| 87 | * bits 25..29: slice index (0...KEY_SLOT_VOLATILE_SLICE_COUNT-1) |
| 88 | * bits 0..24: slot index in slice |
| 89 | */ |
| 90 | #define KEY_ID_SLOT_INDEX_WIDTH 25u |
| 91 | #define KEY_ID_SLICE_INDEX_WIDTH 5u |
| 92 | |
| 93 | #define KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH 16u |
| 94 | #define KEY_SLOT_VOLATILE_SLICE_COUNT 22u |
| 95 | #define KEY_SLICE_COUNT (KEY_SLOT_VOLATILE_SLICE_COUNT + 1u) |
| 96 | #define KEY_SLOT_CACHE_SLICE_INDEX KEY_SLOT_VOLATILE_SLICE_COUNT |
| 97 | |
| 98 | #if KEY_ID_SLICE_INDEX_WIDTH + KEY_ID_SLOT_INDEX_WIDTH > 30 |
| 99 | #error "Not enough room in volatile key IDs for slice index and slot index" |
| 100 | #endif |
| 101 | #if KEY_SLICE_COUNT >= (1 << KEY_ID_SLICE_INDEX_WIDTH) - 1 |
| 102 | #error "Too many slices to fit the slice index in a volatile key ID" |
| 103 | #endif |
| 104 | #define KEY_SLICE_LENGTH_MAX \ |
| 105 | (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << (KEY_SLOT_VOLATILE_SLICE_COUNT - 1)) |
| 106 | #if KEY_SLICE_LENGTH_MAX > 1 << KEY_ID_SLOT_INDEX_WIDTH |
| 107 | #error "Not enough room in volatile key IDs for a slot index in the largest slice" |
| 108 | #endif |
| 109 | #if KEY_ID_SLICE_INDEX_WIDTH > 8 |
| 110 | #error "Slice index does not fit in uint8_t for psa_key_slot_t::slice_index" |
| 111 | #endif |
| 112 | |
| 113 | |
| 114 | /* Calculate the volatile key id to use for a given slot. |
| 115 | * This function assumes valid parameter values. */ |
| 116 | static psa_key_id_t volatile_key_id_of_index(size_t slice_idx, |
| 117 | size_t slot_idx) |
| 118 | { |
Gilles Peskine | ac43de0 | 2024-06-13 20:36:50 +0200 | [diff] [blame^] | 119 | /* We assert above that the slice and slot indexes fit in separate |
| 120 | * bit-fields inside psa_key_id_t, which is a 32-bit type per the |
| 121 | * PSA Cryptography specification. */ |
| 122 | return (psa_key_id_t) (0x40000000u | |
| 123 | (slice_idx << KEY_ID_SLOT_INDEX_WIDTH) | |
| 124 | slot_idx); |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* Calculate the slice containing the given volatile key. |
| 128 | * This function assumes valid parameter values. */ |
| 129 | static size_t slice_index_of_volatile_key_id(psa_key_id_t key_id) |
| 130 | { |
| 131 | size_t mask = (1LU << KEY_ID_SLICE_INDEX_WIDTH) - 1; |
| 132 | return (key_id >> KEY_ID_SLOT_INDEX_WIDTH) & mask; |
| 133 | } |
| 134 | |
| 135 | /* Calculate the index of the slot containing the given volatile key. |
| 136 | * This function assumes valid parameter values. */ |
| 137 | static size_t slot_index_of_volatile_key_id(psa_key_id_t key_id) |
| 138 | { |
| 139 | return key_id & ((1LU << KEY_ID_SLOT_INDEX_WIDTH) - 1); |
| 140 | } |
| 141 | |
| 142 | /* In global_data.first_free_slot_index, use this special value to |
| 143 | * indicate that the slice is full. */ |
| 144 | #define FREE_SLOT_INDEX_NONE ((size_t) -1) |
| 145 | |
Gilles Peskine | 3bc9d2b | 2024-06-21 00:09:07 +0200 | [diff] [blame] | 146 | #if defined(MBEDTLS_TEST_HOOKS) |
| 147 | size_t psa_key_slot_volatile_slice_count(void) |
| 148 | { |
| 149 | return KEY_SLOT_VOLATILE_SLICE_COUNT; |
| 150 | } |
| 151 | #endif |
| 152 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 153 | #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 154 | |
| 155 | /* Static key store. |
| 156 | * |
| 157 | * All the keys (volatile or persistent) are in a single slice. |
| 158 | * We only use slices as a concept to allow some differences between |
| 159 | * static and dynamic key store management to be buried in auxiliary |
| 160 | * functions. |
| 161 | */ |
| 162 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 163 | #define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT |
| 164 | #define KEY_SLICE_COUNT 1u |
| 165 | #define KEY_SLOT_CACHE_SLICE_INDEX 0 |
| 166 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 167 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 168 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | typedef struct { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 171 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 172 | psa_key_slot_t *key_slices[KEY_SLICE_COUNT]; |
| 173 | size_t first_free_slot_index[KEY_SLOT_VOLATILE_SLICE_COUNT]; |
| 174 | #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 175 | psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT]; |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 176 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Dave Rodgman | 164614a | 2023-08-16 17:56:28 +0100 | [diff] [blame] | 177 | uint8_t key_slots_initialized; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 178 | } psa_global_data_t; |
| 179 | |
Gilles Peskine | 2e14bd3 | 2018-12-12 14:05:08 +0100 | [diff] [blame] | 180 | static psa_global_data_t global_data; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 181 | |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 182 | static uint8_t psa_get_key_slots_initialized(void) |
| 183 | { |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 184 | uint8_t initialized; |
| 185 | |
| 186 | #if defined(MBEDTLS_THREADING_C) |
| 187 | mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex); |
| 188 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 189 | |
| 190 | initialized = global_data.key_slots_initialized; |
| 191 | |
| 192 | #if defined(MBEDTLS_THREADING_C) |
| 193 | mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex); |
| 194 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 195 | |
| 196 | return initialized; |
| 197 | } |
| 198 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 199 | |
| 200 | |
| 201 | /** The length of the given slice in the key slot table. |
| 202 | * |
| 203 | * \param slice_idx The slice number. It must satisfy |
| 204 | * 0 <= slice_idx < KEY_SLICE_COUNT. |
| 205 | * |
| 206 | * \return The number of elements in the given slice. |
| 207 | */ |
| 208 | static inline size_t key_slice_length(size_t slice_idx); |
| 209 | |
| 210 | /** Get a pointer to the slot where the given volatile key is located. |
| 211 | * |
| 212 | * \param key_id The key identifier. It must be a valid volatile key |
| 213 | * identifier. |
| 214 | * \return A pointer to the only slot that the given key |
| 215 | * can be in. Note that the slot may be empty or |
| 216 | * contain a different key. |
| 217 | */ |
| 218 | static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id); |
| 219 | |
| 220 | /** Get a pointer to an entry in the persistent key cache. |
| 221 | * |
| 222 | * \param slot_idx The index in the table. It must satisfy |
| 223 | * 0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT. |
| 224 | * \return A pointer to the slot containing the given |
| 225 | * persistent key cache entry. |
| 226 | */ |
| 227 | static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx); |
| 228 | |
| 229 | /** Get a pointer to a slot given by slice and index. |
| 230 | * |
| 231 | * \param slice_idx The slice number. It must satisfy |
| 232 | * 0 <= slice_idx < KEY_SLICE_COUNT. |
| 233 | * \param slot_idx An index in the given slice. It must satisfy |
| 234 | * 0 <= slot_idx < key_slice_length(slice_idx). |
| 235 | * |
| 236 | * \return A pointer to the given slot. |
| 237 | */ |
| 238 | static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx); |
| 239 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 241 | |
Gilles Peskine | 3bc9d2b | 2024-06-21 00:09:07 +0200 | [diff] [blame] | 242 | #if defined(MBEDTLS_TEST_HOOKS) |
| 243 | size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(size_t slice_idx) = NULL; |
| 244 | #endif |
| 245 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 246 | static inline size_t key_slice_length(size_t slice_idx) |
| 247 | { |
| 248 | if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) { |
| 249 | return PERSISTENT_KEY_CACHE_COUNT; |
| 250 | } else { |
Gilles Peskine | 3bc9d2b | 2024-06-21 00:09:07 +0200 | [diff] [blame] | 251 | #if defined(MBEDTLS_TEST_HOOKS) |
| 252 | if (mbedtls_test_hook_psa_volatile_key_slice_length != NULL) { |
| 253 | return mbedtls_test_hook_psa_volatile_key_slice_length(slice_idx); |
| 254 | } |
| 255 | #endif |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 256 | return KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << slice_idx; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id) |
| 261 | { |
| 262 | size_t slice_idx = slice_index_of_volatile_key_id(key_id); |
| 263 | if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) { |
| 264 | return NULL; |
| 265 | } |
| 266 | size_t slot_idx = slot_index_of_volatile_key_id(key_id); |
| 267 | if (slot_idx >= key_slice_length(slice_idx)) { |
| 268 | return NULL; |
| 269 | } |
| 270 | psa_key_slot_t *slice = global_data.key_slices[slice_idx]; |
| 271 | if (slice == NULL) { |
| 272 | return NULL; |
| 273 | } |
| 274 | return &slice[slot_idx]; |
| 275 | } |
| 276 | |
| 277 | static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx) |
| 278 | { |
| 279 | return &global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX][slot_idx]; |
| 280 | } |
| 281 | |
| 282 | static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx) |
| 283 | { |
| 284 | return &global_data.key_slices[slice_idx][slot_idx]; |
| 285 | } |
| 286 | |
| 287 | #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 288 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 289 | static inline size_t key_slice_length(size_t slice_idx) |
| 290 | { |
| 291 | (void) slice_idx; |
| 292 | return ARRAY_LENGTH(global_data.key_slots); |
| 293 | } |
| 294 | |
| 295 | static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id) |
| 296 | { |
| 297 | MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <= |
| 298 | PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1, |
| 299 | "The key slot array is larger than the volatile key ID range"); |
| 300 | return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN]; |
| 301 | } |
| 302 | |
| 303 | static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx) |
| 304 | { |
| 305 | return &global_data.key_slots[slot_idx]; |
| 306 | } |
| 307 | |
| 308 | static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx) |
| 309 | { |
| 310 | (void) slice_idx; |
| 311 | return &global_data.key_slots[slot_idx]; |
| 312 | } |
| 313 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 314 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 315 | |
| 316 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok) |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 319 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key); |
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 | if ((PSA_KEY_ID_USER_MIN <= key_id) && |
| 323 | (key_id <= PSA_KEY_ID_USER_MAX)) { |
| 324 | return 1; |
| 325 | } |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | if (vendor_ok && |
| 328 | (PSA_KEY_ID_VENDOR_MIN <= key_id) && |
| 329 | (key_id <= PSA_KEY_ID_VENDOR_MAX)) { |
| 330 | return 1; |
| 331 | } |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | return 0; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 336 | /** Get the description in memory of a key given its identifier and lock it. |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 337 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 338 | * The descriptions of volatile keys and loaded persistent keys are |
| 339 | * stored in key slots. This function returns a pointer to the key slot |
| 340 | * containing the description of a key given its identifier. |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 341 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 342 | * The function searches the key slots containing the description of the key |
| 343 | * with \p key identifier. The function does only read accesses to the key |
| 344 | * slots. The function does not load any persistent key thus does not access |
| 345 | * any storage. |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 346 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 347 | * For volatile key identifiers, only one key slot is queried as a volatile |
| 348 | * key with identifier key_id can only be stored in slot of index |
| 349 | * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ). |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 350 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 351 | * On success, the function locks the key slot. It is the responsibility of |
| 352 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 353 | * |
Ryan Everett | 6ad1fd1 | 2024-01-31 13:21:33 +0000 | [diff] [blame] | 354 | * If multi-threading is enabled, the caller must hold the |
| 355 | * global key slot mutex. |
| 356 | * |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 357 | * \param key Key identifier to query. |
| 358 | * \param[out] p_slot On success, `*p_slot` contains a pointer to the |
| 359 | * key slot containing the description of the key |
| 360 | * identified by \p key. |
| 361 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 362 | * \retval #PSA_SUCCESS |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 363 | * The pointer to the key slot containing the description of the key |
| 364 | * identified by \p key was returned. |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 365 | * \retval #PSA_ERROR_INVALID_HANDLE |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 366 | * \p key is not a valid key identifier. |
| 367 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 368 | * There is no key with key identifier \p key in the key slots. |
| 369 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 370 | static psa_status_t psa_get_and_lock_key_slot_in_memory( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 372 | { |
Ronald Cron | f473d8b | 2020-11-12 10:07:21 +0100 | [diff] [blame] | 373 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key); |
Ronald Cron | f473d8b | 2020-11-12 10:07:21 +0100 | [diff] [blame] | 375 | size_t slot_idx; |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 376 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | if (psa_key_id_is_volatile(key_id)) { |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 379 | slot = get_volatile_key_slot(key_id); |
Ronald Cron | 1d12d87 | 2020-11-18 17:21:22 +0100 | [diff] [blame] | 380 | |
Ryan Everett | 6ad1fd1 | 2024-01-31 13:21:33 +0000 | [diff] [blame] | 381 | /* Check if both the PSA key identifier key_id and the owner |
| 382 | * identifier of key match those of the key slot. */ |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 383 | if (slot != NULL && |
| 384 | slot->state == PSA_SLOT_FULL && |
| 385 | mbedtls_svc_key_id_equal(key, slot->attr.id)) { |
Ryan Everett | 6ad1fd1 | 2024-01-31 13:21:33 +0000 | [diff] [blame] | 386 | status = PSA_SUCCESS; |
| 387 | } else { |
| 388 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 389 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | } else { |
| 391 | if (!psa_is_valid_key_id(key, 1)) { |
| 392 | return PSA_ERROR_INVALID_HANDLE; |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 393 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 395 | for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) { |
| 396 | slot = get_persistent_key_slot(slot_idx); |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 397 | /* Only consider slots which are in a full state. */ |
| 398 | if ((slot->state == PSA_SLOT_FULL) && |
| 399 | (mbedtls_svc_key_id_equal(key, slot->attr.id))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | break; |
| 401 | } |
| 402 | } |
| 403 | status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ? |
Ronald Cron | f473d8b | 2020-11-12 10:07:21 +0100 | [diff] [blame] | 404 | PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | if (status == PSA_SUCCESS) { |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 408 | status = psa_register_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | if (status == PSA_SUCCESS) { |
Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 410 | *p_slot = slot; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 412 | } |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | return status; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 415 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | psa_status_t psa_initialize_key_slots(void) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 418 | { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 419 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 420 | global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] = |
| 421 | mbedtls_calloc(PERSISTENT_KEY_CACHE_COUNT, |
| 422 | sizeof(*global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX])); |
| 423 | if (global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] == NULL) { |
| 424 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 425 | } |
| 426 | #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Ryan Everett | 558da2f | 2024-01-19 12:59:28 +0000 | [diff] [blame] | 427 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 428 | * guarantee that the key slots are initialized to all-zero, which |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 429 | * means that all the key slots are in a valid, empty state. The global |
| 430 | * data mutex is already held when calling this function, so no need to |
| 431 | * lock it here, to set the flag. */ |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 432 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 433 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 434 | global_data.key_slots_initialized = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | return PSA_SUCCESS; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | void psa_wipe_all_key_slots(void) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 439 | { |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 440 | for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 441 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 442 | if (global_data.key_slices[slice_idx] == NULL) { |
| 443 | continue; |
| 444 | } |
| 445 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 446 | for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) { |
| 447 | psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx); |
Gilles Peskine | a81282c | 2024-06-10 15:41:38 +0200 | [diff] [blame] | 448 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 449 | /* When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled, calling |
| 450 | * psa_wipe_key_slot() on an unused slot is useless, but it |
| 451 | * happens to work (because we flip the state to PENDING_DELETION). |
| 452 | * |
| 453 | * When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, |
| 454 | * psa_wipe_key_slot() needs to have a valid slice_index |
| 455 | * field, but that value might not be correct in a |
| 456 | * free slot, so we must not call it. |
| 457 | * |
| 458 | * Bypass the call to psa_wipe_key_slot() if the slot is empty, |
| 459 | * but only if MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, to save |
| 460 | * a few bytes of code size otherwise. |
| 461 | */ |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 462 | if (slot->state == PSA_SLOT_EMPTY) { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 463 | continue; |
| 464 | } |
Gilles Peskine | a81282c | 2024-06-10 15:41:38 +0200 | [diff] [blame] | 465 | #endif |
Gilles Peskine | 47ad2f7 | 2024-06-10 11:42:41 +0200 | [diff] [blame] | 466 | slot->var.occupied.registered_readers = 1; |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 467 | slot->state = PSA_SLOT_PENDING_DELETION; |
| 468 | (void) psa_wipe_key_slot(slot); |
| 469 | } |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 470 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 471 | mbedtls_free(global_data.key_slices[slice_idx]); |
| 472 | global_data.key_slices[slice_idx] = NULL; |
| 473 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 474 | } |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 475 | |
| 476 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 477 | for (size_t slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) { |
| 478 | global_data.first_free_slot_index[slice_idx] = 0; |
| 479 | } |
| 480 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 481 | |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 482 | /* The global data mutex is already held when calling this function. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 483 | global_data.key_slots_initialized = 0; |
| 484 | } |
| 485 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 486 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 487 | |
| 488 | static psa_status_t psa_allocate_volatile_key_slot(psa_key_id_t *key_id, |
| 489 | psa_key_slot_t **p_slot) |
| 490 | { |
| 491 | size_t slice_idx; |
| 492 | for (slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) { |
| 493 | if (global_data.first_free_slot_index[slice_idx] != FREE_SLOT_INDEX_NONE) { |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (slice_idx == KEY_SLOT_VOLATILE_SLICE_COUNT) { |
| 498 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 499 | } |
| 500 | |
| 501 | if (global_data.key_slices[slice_idx] == NULL) { |
| 502 | global_data.key_slices[slice_idx] = |
| 503 | mbedtls_calloc(key_slice_length(slice_idx), |
| 504 | sizeof(psa_key_slot_t)); |
| 505 | if (global_data.key_slices[slice_idx] == NULL) { |
| 506 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 507 | } |
| 508 | } |
| 509 | psa_key_slot_t *slice = global_data.key_slices[slice_idx]; |
| 510 | |
| 511 | size_t slot_idx = global_data.first_free_slot_index[slice_idx]; |
| 512 | *key_id = volatile_key_id_of_index(slice_idx, slot_idx); |
| 513 | |
| 514 | psa_key_slot_t *slot = &slice[slot_idx]; |
| 515 | size_t next_free = slot_idx + 1 + slot->var.free.next_free_relative_to_next; |
| 516 | if (next_free >= key_slice_length(slice_idx)) { |
| 517 | next_free = FREE_SLOT_INDEX_NONE; |
| 518 | } |
| 519 | global_data.first_free_slot_index[slice_idx] = next_free; |
| 520 | /* The .next_free field is not meaningful when the slot is not free, |
| 521 | * so give it the same content as freshly initialized memory. */ |
| 522 | slot->var.free.next_free_relative_to_next = 0; |
| 523 | |
| 524 | psa_status_t status = psa_key_slot_state_transition(slot, |
| 525 | PSA_SLOT_EMPTY, |
| 526 | PSA_SLOT_FILLING); |
| 527 | if (status != PSA_SUCCESS) { |
| 528 | /* The only reason for failure is if the slot state was not empty. |
| 529 | * This indicates that something has gone horribly wrong. |
| 530 | * In this case, we leave the slot out of the free list, and stop |
| 531 | * modifying it. This minimizes any further corruption. The slot |
| 532 | * is a memory leak, but that's a lesser evil. */ |
| 533 | return status; |
| 534 | } |
| 535 | |
| 536 | *p_slot = slot; |
Gilles Peskine | ac43de0 | 2024-06-13 20:36:50 +0200 | [diff] [blame^] | 537 | /* We assert at compile time that the slice index fits in uint8_t. */ |
| 538 | slot->slice_index = (uint8_t) slice_idx; |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 539 | return PSA_SUCCESS; |
| 540 | } |
| 541 | |
| 542 | psa_status_t psa_free_key_slot(size_t slice_idx, |
| 543 | psa_key_slot_t *slot) |
| 544 | { |
| 545 | |
| 546 | if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) { |
| 547 | /* This is a cache entry. We don't maintain a free list, so |
| 548 | * there's nothing to do. */ |
| 549 | return PSA_SUCCESS; |
| 550 | } |
| 551 | if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) { |
| 552 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 553 | } |
| 554 | |
| 555 | psa_key_slot_t *slice = global_data.key_slices[slice_idx]; |
| 556 | psa_key_slot_t *slice_end = slice + key_slice_length(slice_idx); |
| 557 | if (slot < slice || slot >= slice_end) { |
| 558 | /* The slot isn't actually in the slice! We can't detect that |
| 559 | * condition for sure, because the pointer comparison itself is |
| 560 | * undefined behavior in that case. That same condition makes the |
| 561 | * subtraction to calculate the slot index also UB. |
| 562 | * Give up now to avoid causing further corruption. |
| 563 | */ |
| 564 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 565 | } |
| 566 | size_t slot_idx = slot - slice; |
| 567 | |
| 568 | size_t next_free = global_data.first_free_slot_index[slice_idx]; |
| 569 | if (next_free >= key_slice_length(slice_idx)) { |
| 570 | /* The slot was full. The newly freed slot thus becomes the |
| 571 | * end of the free list. */ |
| 572 | next_free = key_slice_length(slice_idx); |
| 573 | } |
| 574 | global_data.first_free_slot_index[slice_idx] = slot_idx; |
Gilles Peskine | ac43de0 | 2024-06-13 20:36:50 +0200 | [diff] [blame^] | 575 | slot->var.free.next_free_relative_to_next = |
| 576 | (int32_t) next_free - (int32_t) slot_idx - 1; |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 577 | |
| 578 | return PSA_SUCCESS; |
| 579 | } |
| 580 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 581 | |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 582 | psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id, |
| 583 | psa_key_slot_t **p_slot) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 584 | { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 585 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 586 | size_t slot_idx; |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 587 | psa_key_slot_t *selected_slot, *unused_persistent_key_slot; |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 588 | |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 589 | if (!psa_get_key_slots_initialized()) { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 590 | status = PSA_ERROR_BAD_STATE; |
| 591 | goto error; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 592 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 593 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 594 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
Gilles Peskine | a81282c | 2024-06-10 15:41:38 +0200 | [diff] [blame] | 595 | if (volatile_key_id != NULL) { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 596 | return psa_allocate_volatile_key_slot(volatile_key_id, p_slot); |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 597 | } |
Gilles Peskine | a81282c | 2024-06-10 15:41:38 +0200 | [diff] [blame] | 598 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 599 | |
| 600 | /* With a dynamic key store, allocate an entry in the cache slice, |
| 601 | * applicable only to non-volatile keys that get cached in RAM. |
| 602 | * With a static key store, allocate an entry in the sole slice, |
| 603 | * applicable to all keys. */ |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 604 | selected_slot = unused_persistent_key_slot = NULL; |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 605 | for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) { |
| 606 | psa_key_slot_t *slot = get_key_slot(KEY_SLOT_CACHE_SLICE_INDEX, slot_idx); |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 607 | if (slot->state == PSA_SLOT_EMPTY) { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 608 | selected_slot = slot; |
| 609 | break; |
| 610 | } |
| 611 | |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 612 | if ((unused_persistent_key_slot == NULL) && |
| 613 | (slot->state == PSA_SLOT_FULL) && |
| 614 | (!psa_key_slot_has_readers(slot)) && |
| 615 | (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) { |
| 616 | unused_persistent_key_slot = slot; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | } |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 621 | * If there is no unused key slot and there is at least one unlocked key |
Ronald Cron | 1d12d87 | 2020-11-18 17:21:22 +0100 | [diff] [blame] | 622 | * slot containing the description of a persistent key, recycle the first |
| 623 | * such key slot we encountered. If we later need to operate on the |
| 624 | * persistent key we are evicting now, we will reload its description from |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 625 | * storage. |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 626 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if ((selected_slot == NULL) && |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 628 | (unused_persistent_key_slot != NULL)) { |
| 629 | selected_slot = unused_persistent_key_slot; |
| 630 | psa_register_read(selected_slot); |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 631 | status = psa_wipe_key_slot(selected_slot); |
| 632 | if (status != PSA_SUCCESS) { |
| 633 | goto error; |
| 634 | } |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | if (selected_slot != NULL) { |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 638 | status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY, |
| 639 | PSA_SLOT_FILLING); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | if (status != PSA_SUCCESS) { |
Ryan Everett | 709120a | 2024-01-15 11:19:03 +0000 | [diff] [blame] | 641 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | } |
Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 643 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 644 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 645 | selected_slot->slice_index = KEY_SLOT_CACHE_SLICE_INDEX; |
| 646 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 647 | |
| 648 | #if !defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 649 | if (volatile_key_id != NULL) { |
| 650 | /* Refresh slot_idx, for when the slot is not the original |
| 651 | * selected_slot but rather unused_persistent_key_slot. */ |
| 652 | slot_idx = selected_slot - global_data.key_slots; |
| 653 | *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + slot_idx; |
| 654 | } |
| 655 | #endif |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 656 | *p_slot = selected_slot; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | return PSA_SUCCESS; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 659 | } |
| 660 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 661 | |
| 662 | error: |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 663 | *p_slot = NULL; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | return status; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 668 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot) |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 670 | { |
| 671 | psa_status_t status = PSA_SUCCESS; |
| 672 | uint8_t *key_data = NULL; |
| 673 | size_t key_data_length = 0; |
| 674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | status = psa_load_persistent_key(&slot->attr, |
| 676 | &key_data, &key_data_length); |
| 677 | if (status != PSA_SUCCESS) { |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 678 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | } |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 680 | |
Steven Cooreman | 98435dd | 2021-01-08 19:19:40 +0100 | [diff] [blame] | 681 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 682 | /* Special handling is required for loading keys associated with a |
| 683 | * dynamically registered SE interface. */ |
| 684 | const psa_drv_se_t *drv; |
| 685 | psa_drv_se_context_t *drv_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 687 | psa_se_key_data_storage_t *data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | if (key_data_length != sizeof(*data)) { |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 690 | status = PSA_ERROR_DATA_INVALID; |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 691 | goto exit; |
| 692 | } |
Ryan Everett | d69f401 | 2023-11-23 16:20:45 +0000 | [diff] [blame] | 693 | data = (psa_se_key_data_storage_t *) key_data; |
Ryan Everett | 2a0d4e2 | 2023-11-23 16:33:12 +0000 | [diff] [blame] | 694 | status = psa_copy_key_material_into_slot( |
| 695 | slot, data->slot_number, sizeof(data->slot_number)); |
Ryan Everett | 2a0d4e2 | 2023-11-23 16:33:12 +0000 | [diff] [blame] | 696 | goto exit; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 697 | } |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 698 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | status = psa_copy_key_material_into_slot(slot, key_data, key_data_length); |
Ryan Everett | 9f176a2 | 2023-11-21 11:49:57 +0000 | [diff] [blame] | 701 | if (status != PSA_SUCCESS) { |
Ryan Everett | 975d411 | 2023-11-16 13:37:51 +0000 | [diff] [blame] | 702 | goto exit; |
| 703 | } |
| 704 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 705 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | psa_free_persistent_key_data(key_data, key_data_length); |
| 707 | return status; |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 708 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 709 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 710 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 711 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 714 | { |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 715 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 717 | psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE; |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 718 | psa_drv_slot_number_t slot_number = 0; |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 719 | size_t key_buffer_size = 0; |
| 720 | size_t key_buffer_length = 0; |
| 721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | if (!psa_key_id_is_builtin( |
| 723 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) { |
| 724 | return PSA_ERROR_DOES_NOT_EXIST; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 725 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 726 | |
| 727 | /* Check the platform function to see whether this key actually exists */ |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 728 | status = mbedtls_psa_platform_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 729 | slot->attr.id, &lifetime, &slot_number); |
| 730 | if (status != PSA_SUCCESS) { |
| 731 | return status; |
| 732 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 733 | |
Steven Cooreman | 966db26 | 2021-04-13 13:45:45 +0200 | [diff] [blame] | 734 | /* Set required key attributes to ensure get_builtin_key can retrieve the |
| 735 | * full attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | psa_set_key_id(&attributes, slot->attr.id); |
| 737 | psa_set_key_lifetime(&attributes, lifetime); |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 738 | |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 739 | /* Get the full key attributes from the driver in order to be able to |
| 740 | * calculate the required buffer size. */ |
| 741 | status = psa_driver_wrapper_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | slot_number, &attributes, |
| 743 | NULL, 0, NULL); |
| 744 | if (status != PSA_ERROR_BUFFER_TOO_SMALL) { |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 745 | /* Builtin keys cannot be defined by the attributes alone */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 746 | if (status == PSA_SUCCESS) { |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 747 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | } |
| 749 | return status; |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 752 | /* If the key should exist according to the platform, then ask the driver |
| 753 | * what its expected size is. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 755 | &key_buffer_size); |
| 756 | if (status != PSA_SUCCESS) { |
| 757 | return status; |
| 758 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 759 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 760 | /* Allocate a buffer of the required size and load the builtin key directly |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 761 | * into the (now properly sized) slot buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 763 | if (status != PSA_SUCCESS) { |
| 764 | return status; |
| 765 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 766 | |
| 767 | status = psa_driver_wrapper_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | slot_number, &attributes, |
| 769 | slot->key.data, slot->key.bytes, &key_buffer_length); |
| 770 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 771 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 773 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 774 | /* Copy actual key length and core attributes into the slot on success */ |
| 775 | slot->key.bytes = key_buffer_length; |
Gilles Peskine | 7fad3ef | 2024-02-28 01:08:27 +0100 | [diff] [blame] | 776 | slot->attr = attributes; |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 777 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | if (status != PSA_SUCCESS) { |
| 779 | psa_remove_key_data_from_memory(slot); |
| 780 | } |
| 781 | return status; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 782 | } |
| 783 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key, |
| 786 | psa_key_slot_t **p_slot) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 787 | { |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 788 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 789 | |
| 790 | *p_slot = NULL; |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 791 | if (!psa_get_key_slots_initialized()) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | return PSA_ERROR_BAD_STATE; |
| 793 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 794 | |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 795 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 91ce792 | 2024-02-12 12:17:28 +0000 | [diff] [blame] | 796 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 797 | * would be returned if the lock fails. */ |
| 798 | status = PSA_SUCCESS; |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 799 | /* If the key is persistent and not loaded, we cannot unlock the mutex |
| 800 | * between checking if the key is loaded and setting the slot as FULL, |
| 801 | * as otherwise another thread may load and then destroy the key |
| 802 | * in the meantime. */ |
| 803 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 804 | &mbedtls_threading_key_slot_mutex)); |
| 805 | #endif |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 806 | /* |
| 807 | * On success, the pointer to the slot is passed directly to the caller |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 808 | * thus no need to unlock the key slot here. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 809 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | status = psa_get_and_lock_key_slot_in_memory(key, p_slot); |
| 811 | if (status != PSA_ERROR_DOES_NOT_EXIST) { |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 812 | #if defined(MBEDTLS_THREADING_C) |
| 813 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 814 | &mbedtls_threading_key_slot_mutex)); |
| 815 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | return status; |
| 817 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 818 | |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 819 | /* Loading keys from storage requires support for such a mechanism */ |
| 820 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ |
| 821 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 822 | |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 823 | status = psa_reserve_free_key_slot(NULL, p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | if (status != PSA_SUCCESS) { |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 825 | #if defined(MBEDTLS_THREADING_C) |
| 826 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 827 | &mbedtls_threading_key_slot_mutex)); |
| 828 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | return status; |
| 830 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 831 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 832 | (*p_slot)->attr.id = key; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 833 | (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 834 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 835 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 836 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 837 | /* Load keys in the 'builtin' range through their own interface */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | status = psa_load_builtin_key_into_slot(*p_slot); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 839 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 840 | |
| 841 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
| 843 | status = psa_load_persistent_key_into_slot(*p_slot); |
| 844 | } |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 845 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | if (status != PSA_SUCCESS) { |
| 848 | psa_wipe_key_slot(*p_slot); |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 849 | |
Ryan Everett | 1a3573e | 2024-04-29 18:29:48 +0100 | [diff] [blame] | 850 | /* If the key does not exist, we need to return |
| 851 | * PSA_ERROR_INVALID_HANDLE. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 852 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 853 | status = PSA_ERROR_INVALID_HANDLE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | } |
| 855 | } else { |
gabor-mezei-arm | 95180fe | 2021-06-28 14:59:52 +0200 | [diff] [blame] | 856 | /* Add implicit usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage); |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 858 | |
| 859 | psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING, |
| 860 | PSA_SLOT_FULL); |
| 861 | status = psa_register_read(*p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | } |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 863 | |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 864 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 865 | status = PSA_ERROR_INVALID_HANDLE; |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 866 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 867 | |
Ryan Everett | d4ea40d | 2024-04-29 18:24:58 +0100 | [diff] [blame] | 868 | if (status != PSA_SUCCESS) { |
| 869 | *p_slot = NULL; |
| 870 | } |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 871 | #if defined(MBEDTLS_THREADING_C) |
| 872 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 873 | &mbedtls_threading_key_slot_mutex)); |
| 874 | #endif |
| 875 | return status; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 878 | psa_status_t psa_unregister_read(psa_key_slot_t *slot) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 879 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | if (slot == NULL) { |
| 881 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 882 | } |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 883 | if ((slot->state != PSA_SLOT_FULL) && |
| 884 | (slot->state != PSA_SLOT_PENDING_DELETION)) { |
Ryan Everett | dfe8bf8 | 2024-01-12 17:45:05 +0000 | [diff] [blame] | 885 | return PSA_ERROR_CORRUPTION_DETECTED; |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 886 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 887 | |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 888 | /* If we are the last reader and the slot is marked for deletion, |
| 889 | * we must wipe the slot here. */ |
| 890 | if ((slot->state == PSA_SLOT_PENDING_DELETION) && |
Gilles Peskine | 47ad2f7 | 2024-06-10 11:42:41 +0200 | [diff] [blame] | 891 | (slot->var.occupied.registered_readers == 1)) { |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 892 | return psa_wipe_key_slot(slot); |
| 893 | } |
| 894 | |
| 895 | if (psa_key_slot_has_readers(slot)) { |
Gilles Peskine | 47ad2f7 | 2024-06-10 11:42:41 +0200 | [diff] [blame] | 896 | slot->var.occupied.registered_readers--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | return PSA_SUCCESS; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * As the return error code may not be handled in case of multiple errors, |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 902 | * do our best to report if there are no registered readers. Assert with |
| 903 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers: |
| 904 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | * the function is called as part of the execution of a test suite, the |
| 906 | * execution of the test suite is stopped in error if the assertion fails. |
| 907 | */ |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 908 | MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | return PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Ryan Everett | eb1722a | 2024-01-31 13:36:39 +0000 | [diff] [blame] | 912 | psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot) |
| 913 | { |
| 914 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 915 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 91ce792 | 2024-02-12 12:17:28 +0000 | [diff] [blame] | 916 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 917 | * would be returned if the lock fails. */ |
| 918 | status = PSA_SUCCESS; |
Ryan Everett | eb1722a | 2024-01-31 13:36:39 +0000 | [diff] [blame] | 919 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 920 | &mbedtls_threading_key_slot_mutex)); |
| 921 | #endif |
| 922 | status = psa_unregister_read(slot); |
| 923 | #if defined(MBEDTLS_THREADING_C) |
| 924 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 925 | &mbedtls_threading_key_slot_mutex)); |
| 926 | #endif |
| 927 | return status; |
| 928 | } |
| 929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime, |
| 931 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 932 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | if (psa_key_lifetime_is_external(lifetime)) { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 934 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 935 | /* Check whether a driver is registered against this lifetime */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime); |
| 937 | if (driver != NULL) { |
| 938 | if (p_drv != NULL) { |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 939 | *p_drv = driver; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | } |
| 941 | return PSA_SUCCESS; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 942 | } |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 943 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 944 | (void) p_drv; |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 945 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 946 | |
Steven Cooreman | 98435dd | 2021-01-08 19:19:40 +0100 | [diff] [blame] | 947 | /* Key location for external keys gets checked by the wrapper */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 948 | return PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | } else { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 950 | /* Local/internal keys are always valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | return PSA_SUCCESS; |
| 952 | } |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 953 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime) |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 956 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 958 | /* Volatile keys are always supported */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 959 | return PSA_SUCCESS; |
| 960 | } else { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 961 | /* Persistent keys require storage support */ |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 962 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) { |
| 964 | return PSA_ERROR_INVALID_ARGUMENT; |
| 965 | } else { |
| 966 | return PSA_SUCCESS; |
| 967 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 968 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 970 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 971 | } |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 972 | } |
| 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 975 | { |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 976 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ |
| 977 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 978 | psa_status_t status; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 979 | psa_key_slot_t *slot; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 981 | status = psa_get_and_lock_key_slot(key, &slot); |
| 982 | if (status != PSA_SUCCESS) { |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 983 | *handle = PSA_KEY_HANDLE_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | if (status == PSA_ERROR_INVALID_HANDLE) { |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 985 | status = PSA_ERROR_DOES_NOT_EXIST; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 986 | } |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | return status; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 989 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 990 | |
| 991 | *handle = key; |
| 992 | |
Ryan Everett | e110a4c | 2024-02-22 10:43:03 +0000 | [diff] [blame] | 993 | return psa_unregister_read_under_mutex(slot); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 994 | |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 995 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 996 | (void) key; |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 997 | *handle = PSA_KEY_HANDLE_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | return PSA_ERROR_NOT_SUPPORTED; |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 999 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | psa_status_t psa_close_key(psa_key_handle_t handle) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1003 | { |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1004 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 1005 | psa_key_slot_t *slot; |
| 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | if (psa_key_handle_is_null(handle)) { |
| 1008 | return PSA_SUCCESS; |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 1009 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1011 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 9dc076b | 2024-02-09 14:20:09 +0000 | [diff] [blame] | 1012 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 1013 | * would be returned if the lock fails. */ |
| 1014 | status = PSA_SUCCESS; |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1015 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 1016 | &mbedtls_threading_key_slot_mutex)); |
| 1017 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1018 | status = psa_get_and_lock_key_slot_in_memory(handle, &slot); |
| 1019 | if (status != PSA_SUCCESS) { |
| 1020 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
| 1021 | status = PSA_ERROR_INVALID_HANDLE; |
| 1022 | } |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1023 | #if defined(MBEDTLS_THREADING_C) |
| 1024 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1025 | &mbedtls_threading_key_slot_mutex)); |
| 1026 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | return status; |
| 1028 | } |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 1029 | |
Gilles Peskine | 47ad2f7 | 2024-06-10 11:42:41 +0200 | [diff] [blame] | 1030 | if (slot->var.occupied.registered_readers == 1) { |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 1031 | status = psa_wipe_key_slot(slot); |
Ryan Everett | 4755e6b | 2024-01-12 16:35:59 +0000 | [diff] [blame] | 1032 | } else { |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 1033 | status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | } |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 1035 | #if defined(MBEDTLS_THREADING_C) |
| 1036 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1037 | &mbedtls_threading_key_slot_mutex)); |
| 1038 | #endif |
| 1039 | |
| 1040 | return status; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | psa_status_t psa_purge_key(mbedtls_svc_key_id_t key) |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 1044 | { |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1045 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 1046 | psa_key_slot_t *slot; |
| 1047 | |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 1048 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 9dc076b | 2024-02-09 14:20:09 +0000 | [diff] [blame] | 1049 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 1050 | * would be returned if the lock fails. */ |
| 1051 | status = PSA_SUCCESS; |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 1052 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 1053 | &mbedtls_threading_key_slot_mutex)); |
| 1054 | #endif |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 1055 | status = psa_get_and_lock_key_slot_in_memory(key, &slot); |
| 1056 | if (status != PSA_SUCCESS) { |
| 1057 | #if defined(MBEDTLS_THREADING_C) |
| 1058 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1059 | &mbedtls_threading_key_slot_mutex)); |
| 1060 | #endif |
| 1061 | return status; |
| 1062 | } |
| 1063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) && |
Gilles Peskine | 47ad2f7 | 2024-06-10 11:42:41 +0200 | [diff] [blame] | 1065 | (slot->var.occupied.registered_readers == 1)) { |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 1066 | status = psa_wipe_key_slot(slot); |
Ryan Everett | 4755e6b | 2024-01-12 16:35:59 +0000 | [diff] [blame] | 1067 | } else { |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 1068 | status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | } |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 1070 | #if defined(MBEDTLS_THREADING_C) |
| 1071 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1072 | &mbedtls_threading_key_slot_mutex)); |
| 1073 | #endif |
| 1074 | |
| 1075 | return status; |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 1079 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | memset(stats, 0, sizeof(*stats)); |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 1081 | |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 1082 | for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) { |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 1083 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 1084 | if (global_data.key_slices[slice_idx] == NULL) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 1088 | for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) { |
| 1089 | const psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx); |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 1090 | if (slot->state == PSA_SLOT_EMPTY) { |
| 1091 | ++stats->empty_slots; |
| 1092 | continue; |
| 1093 | } |
Gilles Peskine | e8199f5 | 2024-06-10 11:53:33 +0200 | [diff] [blame] | 1094 | if (psa_key_slot_has_readers(slot)) { |
| 1095 | ++stats->locked_slots; |
| 1096 | } |
Gilles Peskine | 5064af6 | 2024-06-07 13:53:28 +0200 | [diff] [blame] | 1097 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1098 | ++stats->volatile_slots; |
| 1099 | } else { |
| 1100 | psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id); |
| 1101 | ++stats->persistent_slots; |
| 1102 | if (id > stats->max_open_internal_key_id) { |
| 1103 | stats->max_open_internal_key_id = id; |
| 1104 | } |
| 1105 | } |
| 1106 | if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) != |
| 1107 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
| 1108 | psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id); |
| 1109 | ++stats->external_slots; |
| 1110 | if (id > stats->max_open_external_key_id) { |
| 1111 | stats->max_open_external_key_id = id; |
| 1112 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | } |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1118 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |