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