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