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