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