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