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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | typedef struct { |
Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 62 | psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT]; |
Dave Rodgman | 164614a | 2023-08-16 17:56:28 +0100 | [diff] [blame] | 63 | uint8_t key_slots_initialized; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 64 | } psa_global_data_t; |
| 65 | |
Gilles Peskine | 2e14bd3 | 2018-12-12 14:05:08 +0100 | [diff] [blame] | 66 | static psa_global_data_t global_data; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 67 | |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 68 | static uint8_t psa_get_key_slots_initialized(void) |
| 69 | { |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 70 | uint8_t initialized; |
| 71 | |
| 72 | #if defined(MBEDTLS_THREADING_C) |
| 73 | mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex); |
| 74 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 75 | |
| 76 | initialized = global_data.key_slots_initialized; |
| 77 | |
| 78 | #if defined(MBEDTLS_THREADING_C) |
| 79 | mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex); |
| 80 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 81 | |
| 82 | return initialized; |
| 83 | } |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | 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] | 86 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | 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] | 88 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | if ((PSA_KEY_ID_USER_MIN <= key_id) && |
| 90 | (key_id <= PSA_KEY_ID_USER_MAX)) { |
| 91 | return 1; |
| 92 | } |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 93 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | if (vendor_ok && |
| 95 | (PSA_KEY_ID_VENDOR_MIN <= key_id) && |
| 96 | (key_id <= PSA_KEY_ID_VENDOR_MAX)) { |
| 97 | return 1; |
| 98 | } |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | return 0; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 103 | /** 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] | 104 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 105 | * The descriptions of volatile keys and loaded persistent keys are |
| 106 | * stored in key slots. This function returns a pointer to the key slot |
| 107 | * containing the description of a key given its identifier. |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 108 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 109 | * The function searches the key slots containing the description of the key |
| 110 | * with \p key identifier. The function does only read accesses to the key |
| 111 | * slots. The function does not load any persistent key thus does not access |
| 112 | * any storage. |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 113 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 114 | * For volatile key identifiers, only one key slot is queried as a volatile |
| 115 | * key with identifier key_id can only be stored in slot of index |
| 116 | * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ). |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 117 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 118 | * On success, the function locks the key slot. It is the responsibility of |
| 119 | * 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] | 120 | * |
Ryan Everett | 6ad1fd1 | 2024-01-31 13:21:33 +0000 | [diff] [blame] | 121 | * If multi-threading is enabled, the caller must hold the |
| 122 | * global key slot mutex. |
| 123 | * |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 124 | * \param key Key identifier to query. |
| 125 | * \param[out] p_slot On success, `*p_slot` contains a pointer to the |
| 126 | * key slot containing the description of the key |
| 127 | * identified by \p key. |
| 128 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 129 | * \retval #PSA_SUCCESS |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 130 | * The pointer to the key slot containing the description of the key |
| 131 | * identified by \p key was returned. |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 132 | * \retval #PSA_ERROR_INVALID_HANDLE |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 133 | * \p key is not a valid key identifier. |
| 134 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 135 | * There is no key with key identifier \p key in the key slots. |
| 136 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 137 | static psa_status_t psa_get_and_lock_key_slot_in_memory( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 139 | { |
Ronald Cron | f473d8b | 2020-11-12 10:07:21 +0100 | [diff] [blame] | 140 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | 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] | 142 | size_t slot_idx; |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 143 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | if (psa_key_id_is_volatile(key_id)) { |
| 146 | slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN]; |
Ronald Cron | 1d12d87 | 2020-11-18 17:21:22 +0100 | [diff] [blame] | 147 | |
Ryan Everett | 6ad1fd1 | 2024-01-31 13:21:33 +0000 | [diff] [blame] | 148 | /* Check if both the PSA key identifier key_id and the owner |
| 149 | * identifier of key match those of the key slot. */ |
| 150 | if ((slot->state == PSA_SLOT_FULL) && |
| 151 | (mbedtls_svc_key_id_equal(key, slot->attr.id))) { |
| 152 | status = PSA_SUCCESS; |
| 153 | } else { |
| 154 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 155 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | } else { |
| 157 | if (!psa_is_valid_key_id(key, 1)) { |
| 158 | return PSA_ERROR_INVALID_HANDLE; |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 159 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | |
| 161 | for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) { |
| 162 | slot = &global_data.key_slots[slot_idx]; |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 163 | /* Only consider slots which are in a full state. */ |
| 164 | if ((slot->state == PSA_SLOT_FULL) && |
| 165 | (mbedtls_svc_key_id_equal(key, slot->attr.id))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | } |
| 169 | status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ? |
Ronald Cron | f473d8b | 2020-11-12 10:07:21 +0100 | [diff] [blame] | 170 | PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | if (status == PSA_SUCCESS) { |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 174 | status = psa_register_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | if (status == PSA_SUCCESS) { |
Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 176 | *p_slot = slot; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 178 | } |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | return status; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 181 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | psa_status_t psa_initialize_key_slots(void) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 184 | { |
Ryan Everett | 558da2f | 2024-01-19 12:59:28 +0000 | [diff] [blame] | 185 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 186 | * guarantee that the key slots are initialized to all-zero, which |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 187 | * means that all the key slots are in a valid, empty state. The global |
| 188 | * data mutex is already held when calling this function, so no need to |
| 189 | * lock it here, to set the flag. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 190 | global_data.key_slots_initialized = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | return PSA_SUCCESS; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | void psa_wipe_all_key_slots(void) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 195 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 196 | size_t slot_idx; |
| 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) { |
| 199 | psa_key_slot_t *slot = &global_data.key_slots[slot_idx]; |
Ryan Everett | 6cd2b8d | 2024-01-04 12:10:18 +0000 | [diff] [blame] | 200 | slot->registered_readers = 1; |
| 201 | slot->state = PSA_SLOT_PENDING_DELETION; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | (void) psa_wipe_key_slot(slot); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 203 | } |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 204 | /* The global data mutex is already held when calling this function. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 205 | global_data.key_slots_initialized = 0; |
| 206 | } |
| 207 | |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 208 | psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id, |
| 209 | psa_key_slot_t **p_slot) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 210 | { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 211 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 212 | size_t slot_idx; |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 213 | psa_key_slot_t *selected_slot, *unused_persistent_key_slot; |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 214 | |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 215 | if (!psa_get_key_slots_initialized()) { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 216 | status = PSA_ERROR_BAD_STATE; |
| 217 | goto error; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 218 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 219 | |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 220 | selected_slot = unused_persistent_key_slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) { |
| 222 | psa_key_slot_t *slot = &global_data.key_slots[slot_idx]; |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 223 | if (slot->state == PSA_SLOT_EMPTY) { |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 224 | selected_slot = slot; |
| 225 | break; |
| 226 | } |
| 227 | |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 228 | if ((unused_persistent_key_slot == NULL) && |
| 229 | (slot->state == PSA_SLOT_FULL) && |
| 230 | (!psa_key_slot_has_readers(slot)) && |
| 231 | (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) { |
| 232 | unused_persistent_key_slot = slot; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | } |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 237 | * 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] | 238 | * slot containing the description of a persistent key, recycle the first |
| 239 | * such key slot we encountered. If we later need to operate on the |
| 240 | * persistent key we are evicting now, we will reload its description from |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 241 | * storage. |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 242 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | if ((selected_slot == NULL) && |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 244 | (unused_persistent_key_slot != NULL)) { |
| 245 | selected_slot = unused_persistent_key_slot; |
| 246 | psa_register_read(selected_slot); |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 247 | status = psa_wipe_key_slot(selected_slot); |
| 248 | if (status != PSA_SUCCESS) { |
| 249 | goto error; |
| 250 | } |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | if (selected_slot != NULL) { |
Ryan Everett | 2afb516 | 2023-12-22 15:59:45 +0000 | [diff] [blame] | 254 | status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY, |
| 255 | PSA_SLOT_FILLING); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | if (status != PSA_SUCCESS) { |
Ryan Everett | 709120a | 2024-01-15 11:19:03 +0000 | [diff] [blame] | 257 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | } |
Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 259 | |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 260 | *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | ((psa_key_id_t) (selected_slot - global_data.key_slots)); |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 262 | *p_slot = selected_slot; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | return PSA_SUCCESS; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 265 | } |
| 266 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 267 | |
| 268 | error: |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 269 | *p_slot = NULL; |
Ronald Cron | a5b894f | 2020-10-21 09:04:34 +0200 | [diff] [blame] | 270 | *volatile_key_id = 0; |
| 271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | return status; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 275 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | 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] | 277 | { |
| 278 | psa_status_t status = PSA_SUCCESS; |
| 279 | uint8_t *key_data = NULL; |
| 280 | size_t key_data_length = 0; |
| 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | status = psa_load_persistent_key(&slot->attr, |
| 283 | &key_data, &key_data_length); |
| 284 | if (status != PSA_SUCCESS) { |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 285 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | } |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 287 | |
Steven Cooreman | 98435dd | 2021-01-08 19:19:40 +0100 | [diff] [blame] | 288 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 289 | /* Special handling is required for loading keys associated with a |
| 290 | * dynamically registered SE interface. */ |
| 291 | const psa_drv_se_t *drv; |
| 292 | psa_drv_se_context_t *drv_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 294 | psa_se_key_data_storage_t *data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if (key_data_length != sizeof(*data)) { |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 297 | status = PSA_ERROR_DATA_INVALID; |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 298 | goto exit; |
| 299 | } |
Ryan Everett | d69f401 | 2023-11-23 16:20:45 +0000 | [diff] [blame] | 300 | data = (psa_se_key_data_storage_t *) key_data; |
Ryan Everett | 2a0d4e2 | 2023-11-23 16:33:12 +0000 | [diff] [blame] | 301 | status = psa_copy_key_material_into_slot( |
| 302 | slot, data->slot_number, sizeof(data->slot_number)); |
Ryan Everett | 2a0d4e2 | 2023-11-23 16:33:12 +0000 | [diff] [blame] | 303 | goto exit; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 304 | } |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 305 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | 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] | 308 | if (status != PSA_SUCCESS) { |
Ryan Everett | 975d411 | 2023-11-16 13:37:51 +0000 | [diff] [blame] | 309 | goto exit; |
| 310 | } |
| 311 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 312 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | psa_free_persistent_key_data(key_data, key_data_length); |
| 314 | return status; |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 315 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 316 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 317 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 318 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | 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] | 321 | { |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 322 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 324 | psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE; |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 325 | psa_drv_slot_number_t slot_number = 0; |
Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 326 | size_t key_buffer_size = 0; |
| 327 | size_t key_buffer_length = 0; |
| 328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | if (!psa_key_id_is_builtin( |
| 330 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) { |
| 331 | return PSA_ERROR_DOES_NOT_EXIST; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 332 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 333 | |
| 334 | /* Check the platform function to see whether this key actually exists */ |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 335 | status = mbedtls_psa_platform_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | slot->attr.id, &lifetime, &slot_number); |
| 337 | if (status != PSA_SUCCESS) { |
| 338 | return status; |
| 339 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 340 | |
Steven Cooreman | 966db26 | 2021-04-13 13:45:45 +0200 | [diff] [blame] | 341 | /* Set required key attributes to ensure get_builtin_key can retrieve the |
| 342 | * full attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | psa_set_key_id(&attributes, slot->attr.id); |
| 344 | psa_set_key_lifetime(&attributes, lifetime); |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 345 | |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 346 | /* Get the full key attributes from the driver in order to be able to |
| 347 | * calculate the required buffer size. */ |
| 348 | status = psa_driver_wrapper_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | slot_number, &attributes, |
| 350 | NULL, 0, NULL); |
| 351 | if (status != PSA_ERROR_BUFFER_TOO_SMALL) { |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 352 | /* Builtin keys cannot be defined by the attributes alone */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | if (status == PSA_SUCCESS) { |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 354 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | } |
| 356 | return status; |
Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 359 | /* If the key should exist according to the platform, then ask the driver |
| 360 | * what its expected size is. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 362 | &key_buffer_size); |
| 363 | if (status != PSA_SUCCESS) { |
| 364 | return status; |
| 365 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 366 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 367 | /* 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] | 368 | * into the (now properly sized) slot buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 370 | if (status != PSA_SUCCESS) { |
| 371 | return status; |
| 372 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 373 | |
| 374 | status = psa_driver_wrapper_get_builtin_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | slot_number, &attributes, |
| 376 | slot->key.data, slot->key.bytes, &key_buffer_length); |
| 377 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 378 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | } |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 380 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 381 | /* Copy actual key length and core attributes into the slot on success */ |
| 382 | slot->key.bytes = key_buffer_length; |
Gilles Peskine | 7fad3ef | 2024-02-28 01:08:27 +0100 | [diff] [blame] | 383 | slot->attr = attributes; |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 384 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | if (status != PSA_SUCCESS) { |
| 386 | psa_remove_key_data_from_memory(slot); |
| 387 | } |
| 388 | return status; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 389 | } |
| 390 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key, |
| 393 | psa_key_slot_t **p_slot) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 394 | { |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 395 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 396 | |
| 397 | *p_slot = NULL; |
Paul Elliott | 838886d | 2024-03-12 15:26:04 +0000 | [diff] [blame] | 398 | if (!psa_get_key_slots_initialized()) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | return PSA_ERROR_BAD_STATE; |
| 400 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 401 | |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 402 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 91ce792 | 2024-02-12 12:17:28 +0000 | [diff] [blame] | 403 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 404 | * would be returned if the lock fails. */ |
| 405 | status = PSA_SUCCESS; |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 406 | /* If the key is persistent and not loaded, we cannot unlock the mutex |
| 407 | * between checking if the key is loaded and setting the slot as FULL, |
| 408 | * as otherwise another thread may load and then destroy the key |
| 409 | * in the meantime. */ |
| 410 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 411 | &mbedtls_threading_key_slot_mutex)); |
| 412 | #endif |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 413 | /* |
| 414 | * 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] | 415 | * thus no need to unlock the key slot here. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 416 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | status = psa_get_and_lock_key_slot_in_memory(key, p_slot); |
| 418 | if (status != PSA_ERROR_DOES_NOT_EXIST) { |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 419 | #if defined(MBEDTLS_THREADING_C) |
| 420 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 421 | &mbedtls_threading_key_slot_mutex)); |
| 422 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | return status; |
| 424 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 425 | |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 426 | /* Loading keys from storage requires support for such a mechanism */ |
| 427 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ |
| 428 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 429 | psa_key_id_t volatile_key_id; |
| 430 | |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 431 | status = psa_reserve_free_key_slot(&volatile_key_id, p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | if (status != PSA_SUCCESS) { |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 433 | #if defined(MBEDTLS_THREADING_C) |
| 434 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 435 | &mbedtls_threading_key_slot_mutex)); |
| 436 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return status; |
| 438 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 439 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 440 | (*p_slot)->attr.id = key; |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 441 | (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 442 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 443 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 444 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 445 | /* Load keys in the 'builtin' range through their own interface */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | status = psa_load_builtin_key_into_slot(*p_slot); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 447 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 448 | |
| 449 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
| 451 | status = psa_load_persistent_key_into_slot(*p_slot); |
| 452 | } |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 453 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | if (status != PSA_SUCCESS) { |
| 456 | psa_wipe_key_slot(*p_slot); |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 457 | |
Ryan Everett | 1a3573e | 2024-04-29 18:29:48 +0100 | [diff] [blame] | 458 | /* If the key does not exist, we need to return |
| 459 | * PSA_ERROR_INVALID_HANDLE. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 461 | status = PSA_ERROR_INVALID_HANDLE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | } |
| 463 | } else { |
gabor-mezei-arm | 95180fe | 2021-06-28 14:59:52 +0200 | [diff] [blame] | 464 | /* Add implicit usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage); |
Ryan Everett | 098c665 | 2024-01-03 13:03:36 +0000 | [diff] [blame] | 466 | |
| 467 | psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING, |
| 468 | PSA_SLOT_FULL); |
| 469 | status = psa_register_read(*p_slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | } |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 471 | |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 472 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 473 | status = PSA_ERROR_INVALID_HANDLE; |
Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 474 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 475 | |
Ryan Everett | d4ea40d | 2024-04-29 18:24:58 +0100 | [diff] [blame] | 476 | if (status != PSA_SUCCESS) { |
| 477 | *p_slot = NULL; |
| 478 | } |
Ryan Everett | 2f1f172 | 2024-01-31 13:31:00 +0000 | [diff] [blame] | 479 | #if defined(MBEDTLS_THREADING_C) |
| 480 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 481 | &mbedtls_threading_key_slot_mutex)); |
| 482 | #endif |
| 483 | return status; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 486 | psa_status_t psa_unregister_read(psa_key_slot_t *slot) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 487 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if (slot == NULL) { |
| 489 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 490 | } |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 491 | if ((slot->state != PSA_SLOT_FULL) && |
| 492 | (slot->state != PSA_SLOT_PENDING_DELETION)) { |
Ryan Everett | dfe8bf8 | 2024-01-12 17:45:05 +0000 | [diff] [blame] | 493 | return PSA_ERROR_CORRUPTION_DETECTED; |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 494 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 495 | |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 496 | /* If we are the last reader and the slot is marked for deletion, |
| 497 | * we must wipe the slot here. */ |
| 498 | if ((slot->state == PSA_SLOT_PENDING_DELETION) && |
| 499 | (slot->registered_readers == 1)) { |
| 500 | return psa_wipe_key_slot(slot); |
| 501 | } |
| 502 | |
| 503 | if (psa_key_slot_has_readers(slot)) { |
| 504 | slot->registered_readers--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | return PSA_SUCCESS; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * 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] | 510 | * do our best to report if there are no registered readers. Assert with |
| 511 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers: |
| 512 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | * the function is called as part of the execution of a test suite, the |
| 514 | * execution of the test suite is stopped in error if the assertion fails. |
| 515 | */ |
Ryan Everett | 39cc9d7 | 2023-12-21 17:57:14 +0000 | [diff] [blame] | 516 | MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | return PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Ryan Everett | eb1722a | 2024-01-31 13:36:39 +0000 | [diff] [blame] | 520 | psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot) |
| 521 | { |
| 522 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 523 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 91ce792 | 2024-02-12 12:17:28 +0000 | [diff] [blame] | 524 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 525 | * would be returned if the lock fails. */ |
| 526 | status = PSA_SUCCESS; |
Ryan Everett | eb1722a | 2024-01-31 13:36:39 +0000 | [diff] [blame] | 527 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 528 | &mbedtls_threading_key_slot_mutex)); |
| 529 | #endif |
| 530 | status = psa_unregister_read(slot); |
| 531 | #if defined(MBEDTLS_THREADING_C) |
| 532 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 533 | &mbedtls_threading_key_slot_mutex)); |
| 534 | #endif |
| 535 | return status; |
| 536 | } |
| 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime, |
| 539 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 540 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (psa_key_lifetime_is_external(lifetime)) { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 542 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 543 | /* Check whether a driver is registered against this lifetime */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime); |
| 545 | if (driver != NULL) { |
| 546 | if (p_drv != NULL) { |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 547 | *p_drv = driver; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | } |
| 549 | return PSA_SUCCESS; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 550 | } |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 551 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 552 | (void) p_drv; |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 553 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 554 | |
Steven Cooreman | 98435dd | 2021-01-08 19:19:40 +0100 | [diff] [blame] | 555 | /* Key location for external keys gets checked by the wrapper */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | return PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | } else { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 558 | /* Local/internal keys are always valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | return PSA_SUCCESS; |
| 560 | } |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 561 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime) |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 564 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 566 | /* Volatile keys are always supported */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | return PSA_SUCCESS; |
| 568 | } else { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 569 | /* Persistent keys require storage support */ |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 570 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) { |
| 572 | return PSA_ERROR_INVALID_ARGUMENT; |
| 573 | } else { |
| 574 | return PSA_SUCCESS; |
| 575 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 576 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 578 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 579 | } |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | 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] | 583 | { |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 584 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ |
| 585 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 586 | psa_status_t status; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 587 | psa_key_slot_t *slot; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | status = psa_get_and_lock_key_slot(key, &slot); |
| 590 | if (status != PSA_SUCCESS) { |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 591 | *handle = PSA_KEY_HANDLE_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | if (status == PSA_ERROR_INVALID_HANDLE) { |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 593 | status = PSA_ERROR_DOES_NOT_EXIST; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | } |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | return status; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 597 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 598 | |
| 599 | *handle = key; |
| 600 | |
Ryan Everett | e110a4c | 2024-02-22 10:43:03 +0000 | [diff] [blame] | 601 | return psa_unregister_read_under_mutex(slot); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 602 | |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 603 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 604 | (void) key; |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 605 | *handle = PSA_KEY_HANDLE_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | return PSA_ERROR_NOT_SUPPORTED; |
Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 607 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | psa_status_t psa_close_key(psa_key_handle_t handle) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 611 | { |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 613 | psa_key_slot_t *slot; |
| 614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | if (psa_key_handle_is_null(handle)) { |
| 616 | return PSA_SUCCESS; |
Maulik Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 617 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 618 | |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 619 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 9dc076b | 2024-02-09 14:20:09 +0000 | [diff] [blame] | 620 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 621 | * would be returned if the lock fails. */ |
| 622 | status = PSA_SUCCESS; |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 623 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 624 | &mbedtls_threading_key_slot_mutex)); |
| 625 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | status = psa_get_and_lock_key_slot_in_memory(handle, &slot); |
| 627 | if (status != PSA_SUCCESS) { |
| 628 | if (status == PSA_ERROR_DOES_NOT_EXIST) { |
| 629 | status = PSA_ERROR_INVALID_HANDLE; |
| 630 | } |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 631 | #if defined(MBEDTLS_THREADING_C) |
| 632 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 633 | &mbedtls_threading_key_slot_mutex)); |
| 634 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | return status; |
| 636 | } |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 637 | |
Ryan Everett | c70ce57 | 2024-01-03 16:04:33 +0000 | [diff] [blame] | 638 | if (slot->registered_readers == 1) { |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 639 | status = psa_wipe_key_slot(slot); |
Ryan Everett | 4755e6b | 2024-01-12 16:35:59 +0000 | [diff] [blame] | 640 | } else { |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 641 | status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | } |
Ryan Everett | f23336e | 2024-01-24 11:39:21 +0000 | [diff] [blame] | 643 | #if defined(MBEDTLS_THREADING_C) |
| 644 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 645 | &mbedtls_threading_key_slot_mutex)); |
| 646 | #endif |
| 647 | |
| 648 | return status; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 649 | } |
| 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | psa_status_t psa_purge_key(mbedtls_svc_key_id_t key) |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 652 | { |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 653 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 654 | psa_key_slot_t *slot; |
| 655 | |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 656 | #if defined(MBEDTLS_THREADING_C) |
Ryan Everett | 9dc076b | 2024-02-09 14:20:09 +0000 | [diff] [blame] | 657 | /* We need to set status as success, otherwise CORRUPTION_DETECTED |
| 658 | * would be returned if the lock fails. */ |
| 659 | status = PSA_SUCCESS; |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 660 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 661 | &mbedtls_threading_key_slot_mutex)); |
| 662 | #endif |
Ryan Everett | 3af9bc1 | 2024-01-30 17:21:57 +0000 | [diff] [blame] | 663 | status = psa_get_and_lock_key_slot_in_memory(key, &slot); |
| 664 | if (status != PSA_SUCCESS) { |
| 665 | #if defined(MBEDTLS_THREADING_C) |
| 666 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 667 | &mbedtls_threading_key_slot_mutex)); |
| 668 | #endif |
| 669 | return status; |
| 670 | } |
| 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) && |
Ryan Everett | c70ce57 | 2024-01-03 16:04:33 +0000 | [diff] [blame] | 673 | (slot->registered_readers == 1)) { |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 674 | status = psa_wipe_key_slot(slot); |
Ryan Everett | 4755e6b | 2024-01-12 16:35:59 +0000 | [diff] [blame] | 675 | } else { |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 676 | status = psa_unregister_read(slot); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | } |
Ryan Everett | b082195 | 2024-01-24 11:42:32 +0000 | [diff] [blame] | 678 | #if defined(MBEDTLS_THREADING_C) |
| 679 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 680 | &mbedtls_threading_key_slot_mutex)); |
| 681 | #endif |
| 682 | |
| 683 | return status; |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 687 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 688 | size_t slot_idx; |
| 689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | memset(stats, 0, sizeof(*stats)); |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) { |
| 693 | const psa_key_slot_t *slot = &global_data.key_slots[slot_idx]; |
Ryan Everett | 6a9c14b | 2024-01-04 12:13:45 +0000 | [diff] [blame] | 694 | if (psa_key_slot_has_readers(slot)) { |
Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame] | 695 | ++stats->locked_slots; |
Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 696 | } |
Ryan Everett | 6a9c14b | 2024-01-04 12:13:45 +0000 | [diff] [blame] | 697 | if (slot->state == PSA_SLOT_EMPTY) { |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 698 | ++stats->empty_slots; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 699 | continue; |
| 700 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 702 | ++stats->volatile_slots; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | } else { |
| 704 | psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 705 | ++stats->persistent_slots; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | if (id > stats->max_open_internal_key_id) { |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 707 | stats->max_open_internal_key_id = id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | } |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 709 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) != |
| 711 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
| 712 | psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 713 | ++stats->external_slots; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | if (id > stats->max_open_external_key_id) { |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 715 | stats->max_open_external_key_id = id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | } |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 721 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |