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