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