| 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> | 
|  | 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) | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 278 |  | 
|  | 279 | static psa_status_t psa_load_builtin_key_into_slot( psa_key_slot_t *slot ) | 
|  | 280 | { | 
| Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 281 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 283 | psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE; | 
| Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 284 | psa_drv_slot_number_t slot_number = 0; | 
| Steven Cooreman | ffc7fc9 | 2021-03-18 17:33:46 +0100 | [diff] [blame] | 285 | size_t key_buffer_size = 0; | 
|  | 286 | size_t key_buffer_length = 0; | 
|  | 287 |  | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 288 | if( ! psa_key_id_is_builtin( | 
|  | 289 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id ) ) ) | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 290 | { | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 291 | return( PSA_ERROR_DOES_NOT_EXIST ); | 
|  | 292 | } | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 293 |  | 
|  | 294 | /* Check the platform function to see whether this key actually exists */ | 
| Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 295 | status = mbedtls_psa_platform_get_builtin_key( | 
|  | 296 | slot->attr.id, &lifetime, &slot_number ); | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 297 | if( status != PSA_SUCCESS ) | 
|  | 298 | return( status ); | 
|  | 299 |  | 
| Steven Cooreman | 966db26 | 2021-04-13 13:45:45 +0200 | [diff] [blame] | 300 | /* Set required key attributes to ensure get_builtin_key can retrieve the | 
|  | 301 | * full attributes. */ | 
|  | 302 | psa_set_key_id( &attributes, slot->attr.id ); | 
| Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 303 | psa_set_key_lifetime( &attributes, lifetime ); | 
|  | 304 |  | 
| Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 305 | /* Get the full key attributes from the driver in order to be able to | 
|  | 306 | * calculate the required buffer size. */ | 
|  | 307 | status = psa_driver_wrapper_get_builtin_key( | 
|  | 308 | slot_number, &attributes, | 
|  | 309 | NULL, 0, NULL ); | 
|  | 310 | if( status != PSA_ERROR_BUFFER_TOO_SMALL ) | 
|  | 311 | { | 
|  | 312 | /* Builtin keys cannot be defined by the attributes alone */ | 
|  | 313 | if( status == PSA_SUCCESS ) | 
|  | 314 | status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 966db26 | 2021-04-13 13:45:45 +0200 | [diff] [blame] | 315 | return( status ); | 
| Steven Cooreman | ce48702 | 2021-04-07 18:09:53 +0200 | [diff] [blame] | 316 | } | 
|  | 317 |  | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 318 | /* If the key should exist according to the platform, then ask the driver | 
|  | 319 | * what its expected size is. */ | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 320 | status = psa_driver_wrapper_get_key_buffer_size( &attributes, | 
| Steven Cooreman | 85d554a | 2021-03-18 17:19:30 +0100 | [diff] [blame] | 321 | &key_buffer_size ); | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 322 | if( status != PSA_SUCCESS ) | 
|  | 323 | return( status ); | 
|  | 324 |  | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 325 | /* 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] | 326 | * into the (now properly sized) slot buffer. */ | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 327 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); | 
|  | 328 | if( status != PSA_SUCCESS ) | 
|  | 329 | return( status ); | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 330 |  | 
|  | 331 | status = psa_driver_wrapper_get_builtin_key( | 
|  | 332 | slot_number, &attributes, | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 333 | slot->key.data, slot->key.bytes, &key_buffer_length ); | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 334 | if( status != PSA_SUCCESS ) | 
|  | 335 | goto exit; | 
|  | 336 |  | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 337 | /* Copy actual key length and core attributes into the slot on success */ | 
|  | 338 | slot->key.bytes = key_buffer_length; | 
| Steven Cooreman | 649a8f4 | 2021-03-18 17:34:55 +0100 | [diff] [blame] | 339 | slot->attr = attributes.core; | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 340 |  | 
|  | 341 | exit: | 
| Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 342 | if( status != PSA_SUCCESS ) | 
| Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 343 | psa_remove_key_data_from_memory( slot ); | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 344 | return( status ); | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 345 | } | 
|  | 346 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
|  | 347 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 348 | psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key, | 
|  | 349 | psa_key_slot_t **p_slot ) | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 350 | { | 
| Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 351 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 352 |  | 
|  | 353 | *p_slot = NULL; | 
|  | 354 | if( ! global_data.key_slots_initialized ) | 
|  | 355 | return( PSA_ERROR_BAD_STATE ); | 
|  | 356 |  | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 357 | /* | 
|  | 358 | * 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] | 359 | * thus no need to unlock the key slot here. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 360 | */ | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 361 | status = psa_get_and_lock_key_slot_in_memory( key, p_slot ); | 
| Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 362 | if( status != PSA_ERROR_DOES_NOT_EXIST ) | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 363 | return( status ); | 
|  | 364 |  | 
| Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 365 | /* Loading keys from storage requires support for such a mechanism */ | 
|  | 366 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ | 
|  | 367 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 368 | psa_key_id_t volatile_key_id; | 
|  | 369 |  | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 370 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); | 
|  | 371 | if( status != PSA_SUCCESS ) | 
|  | 372 | return( status ); | 
|  | 373 |  | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 374 | (*p_slot)->attr.id = key; | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 375 | (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT; | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 376 |  | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 377 | status = PSA_ERROR_DOES_NOT_EXIST; | 
|  | 378 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) | 
| Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 379 | /* Load keys in the 'builtin' range through their own interface */ | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 380 | status = psa_load_builtin_key_into_slot( *p_slot ); | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 381 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
|  | 382 |  | 
|  | 383 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Steven Cooreman | e5e30859 | 2021-02-22 14:40:04 +0100 | [diff] [blame] | 384 | if( status == PSA_ERROR_DOES_NOT_EXIST ) | 
|  | 385 | status = psa_load_persistent_key_into_slot( *p_slot ); | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 386 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
|  | 387 |  | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 388 | if( status != PSA_SUCCESS ) | 
| Maulik  Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 389 | { | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 390 | psa_wipe_key_slot( *p_slot ); | 
| Maulik  Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 391 | if( status == PSA_ERROR_DOES_NOT_EXIST ) | 
|  | 392 | status = PSA_ERROR_INVALID_HANDLE; | 
|  | 393 | } | 
| gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 394 | else | 
| gabor-mezei-arm | 95180fe | 2021-06-28 14:59:52 +0200 | [diff] [blame] | 395 | /* Add implicit usage flags. */ | 
| gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 396 | psa_extend_key_usage_flags( &(*p_slot)->attr.policy.usage ); | 
|  | 397 |  | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 398 | return( status ); | 
| Steven Cooreman | 0bb6536 | 2021-04-06 15:09:57 +0200 | [diff] [blame] | 399 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
|  | 400 | return( PSA_ERROR_INVALID_HANDLE ); | 
|  | 401 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 402 | } | 
|  | 403 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 404 | psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot ) | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 405 | { | 
|  | 406 | if( slot == NULL ) | 
|  | 407 | return( PSA_SUCCESS ); | 
|  | 408 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 409 | if( slot->lock_count > 0 ) | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 410 | { | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 411 | slot->lock_count--; | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 412 | return( PSA_SUCCESS ); | 
|  | 413 | } | 
|  | 414 |  | 
| TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 415 | /* | 
|  | 416 | * As the return error code may not be handled in case of multiple errors, | 
| TRodziewicz | c9890e9 | 2021-07-14 10:16:26 +0200 | [diff] [blame] | 417 | * do our best to report if the lock counter is equal to zero. Assert with | 
|  | 418 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is strictly greater | 
|  | 419 | * than zero: if the MBEDTLS_TEST_HOOKS configuration option is enabled and | 
|  | 420 | * the function is called as part of the execution of a test suite, the | 
|  | 421 | * execution of the test suite is stopped in error if the assertion fails. | 
| TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 422 | */ | 
| TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 423 | MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count > 0 ); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 424 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
|  | 425 | } | 
|  | 426 |  | 
| Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 427 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 428 | psa_se_drv_table_entry_t **p_drv ) | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 429 | { | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 430 | if ( psa_key_lifetime_is_external( lifetime ) ) | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 431 | { | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 432 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 433 | /* Check whether a driver is registered against this lifetime */ | 
| Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 434 | 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] | 435 | if( driver != NULL ) | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 436 | { | 
|  | 437 | if (p_drv != NULL) | 
| Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 438 | *p_drv = driver; | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 439 | return( PSA_SUCCESS ); | 
|  | 440 | } | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 441 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 442 | (void) p_drv; | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 443 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 444 |  | 
| Steven Cooreman | 98435dd | 2021-01-08 19:19:40 +0100 | [diff] [blame] | 445 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) | 
|  | 446 | /* Key location for external keys gets checked by the wrapper */ | 
|  | 447 | return( PSA_SUCCESS ); | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 448 | #else /* MBEDTLS_PSA_CRYPTO_DRIVERS */ | 
|  | 449 | /* No support for external lifetimes at all, or dynamic interface | 
|  | 450 | * did not find driver for requested lifetime. */ | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 451 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 452 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 453 | } | 
|  | 454 | else | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 455 | /* Local/internal keys are always valid */ | 
|  | 456 | return( PSA_SUCCESS ); | 
|  | 457 | } | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 458 |  | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 459 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime ) | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 460 | { | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 461 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) | 
|  | 462 | { | 
|  | 463 | /* Volatile keys are always supported */ | 
|  | 464 | return( PSA_SUCCESS ); | 
|  | 465 | } | 
|  | 466 | else | 
|  | 467 | { | 
|  | 468 | /* Persistent keys require storage support */ | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 469 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | 87bc91c | 2021-04-21 21:23:40 +0200 | [diff] [blame] | 470 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( lifetime ) ) | 
|  | 471 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 472 | else | 
|  | 473 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 474 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 475 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 476 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 477 | } | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 478 | } | 
|  | 479 |  | 
| Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 480 | 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] | 481 | { | 
| Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 482 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \ | 
|  | 483 | defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 484 | psa_status_t status; | 
| Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 485 | psa_key_slot_t *slot; | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 486 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 487 | status = psa_get_and_lock_key_slot( key, &slot ); | 
| Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 488 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 489 | { | 
| Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 490 | *handle = PSA_KEY_HANDLE_INIT; | 
| Maulik  Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 491 | if( status == PSA_ERROR_INVALID_HANDLE ) | 
|  | 492 | status = PSA_ERROR_DOES_NOT_EXIST; | 
|  | 493 |  | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 494 | return( status ); | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 495 | } | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 496 |  | 
|  | 497 | *handle = key; | 
|  | 498 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 499 | return( psa_unlock_key_slot( slot ) ); | 
| Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 500 |  | 
| Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 501 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
| Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 502 | (void) key; | 
| Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 503 | *handle = PSA_KEY_HANDLE_INIT; | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 504 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Archana | 0dc86b5 | 2021-07-14 13:59:48 +0530 | [diff] [blame] | 505 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 506 | } | 
|  | 507 |  | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 508 | psa_status_t psa_close_key( psa_key_handle_t handle ) | 
|  | 509 | { | 
| Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 510 | psa_status_t status; | 
|  | 511 | psa_key_slot_t *slot; | 
|  | 512 |  | 
| Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 513 | if( psa_key_handle_is_null( handle ) ) | 
| Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 514 | return( PSA_SUCCESS ); | 
|  | 515 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 516 | status = psa_get_and_lock_key_slot_in_memory( handle, &slot ); | 
| Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 517 | if( status != PSA_SUCCESS ) | 
| Maulik  Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 518 | { | 
|  | 519 | if( status == PSA_ERROR_DOES_NOT_EXIST ) | 
|  | 520 | status = PSA_ERROR_INVALID_HANDLE; | 
| Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 521 |  | 
| Maulik  Patel | c1bfcdd | 2021-03-15 14:48:14 +0000 | [diff] [blame] | 522 | return( status ); | 
|  | 523 | } | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 524 | if( 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 ) ); | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 528 | } | 
|  | 529 |  | 
| Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 530 | psa_status_t psa_purge_key( mbedtls_svc_key_id_t key ) | 
|  | 531 | { | 
|  | 532 | psa_status_t status; | 
|  | 533 | psa_key_slot_t *slot; | 
|  | 534 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 535 | status = psa_get_and_lock_key_slot_in_memory( key, &slot ); | 
| Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 536 | if( status != PSA_SUCCESS ) | 
|  | 537 | return( status ); | 
|  | 538 |  | 
| Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 539 | if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) && | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 540 | ( slot->lock_count <= 1 ) ) | 
| Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 541 | return( psa_wipe_key_slot( slot ) ); | 
|  | 542 | else | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 543 | return( psa_unlock_key_slot( slot ) ); | 
| Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 544 | } | 
|  | 545 |  | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 546 | void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ) | 
|  | 547 | { | 
| Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 548 | size_t slot_idx; | 
|  | 549 |  | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 550 | memset( stats, 0, sizeof( *stats ) ); | 
| Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 551 |  | 
| Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 552 | 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] | 553 | { | 
| Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 554 | const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ]; | 
| Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame] | 555 | if( psa_is_key_slot_locked( slot ) ) | 
| Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 556 | { | 
| Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame] | 557 | ++stats->locked_slots; | 
| Ronald Cron | 0c3752a | 2020-10-30 11:54:03 +0100 | [diff] [blame] | 558 | } | 
| Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 559 | if( ! psa_is_key_slot_occupied( slot ) ) | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 560 | { | 
| Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 561 | ++stats->empty_slots; | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 562 | continue; | 
|  | 563 | } | 
| Gilles Peskine | f5f07c8 | 2021-04-21 20:06:51 +0200 | [diff] [blame] | 564 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 565 | ++stats->volatile_slots; | 
| Gilles Peskine | f5f07c8 | 2021-04-21 20:06:51 +0200 | [diff] [blame] | 566 | else | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 567 | { | 
| Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 568 | 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] | 569 | ++stats->persistent_slots; | 
| Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 570 | if( id > stats->max_open_internal_key_id ) | 
|  | 571 | stats->max_open_internal_key_id = id; | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 572 | } | 
| Gilles Peskine | f5f07c8 | 2021-04-21 20:06:51 +0200 | [diff] [blame] | 573 | if( PSA_KEY_LIFETIME_GET_LOCATION( slot->attr.lifetime ) != | 
|  | 574 | PSA_KEY_LOCATION_LOCAL_STORAGE ) | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 575 | { | 
| Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 576 | 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] | 577 | ++stats->external_slots; | 
| Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 578 | if( id > stats->max_open_external_key_id ) | 
|  | 579 | stats->max_open_external_key_id = id; | 
| Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 580 | } | 
|  | 581 | } | 
|  | 582 | } | 
|  | 583 |  | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 584 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |