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 | { |
| 48 | psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT]; |
| 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 | fc9c556 | 2020-10-15 19:24:49 +0200 | [diff] [blame] | 54 | psa_status_t psa_validate_key_id( |
| 55 | mbedtls_svc_key_id_t key, int vendor_ok, int volatile_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 ) ) |
| 61 | return( PSA_SUCCESS ); |
| 62 | |
| 63 | if( vendor_ok && |
| 64 | ( PSA_KEY_ID_VENDOR_MIN <= key_id ) && |
Ronald Cron | fc9c556 | 2020-10-15 19:24:49 +0200 | [diff] [blame] | 65 | ( key_id < PSA_KEY_ID_VOLATILE_MIN ) ) |
| 66 | return( PSA_SUCCESS ); |
| 67 | |
| 68 | if( volatile_ok && |
| 69 | ( PSA_KEY_ID_VOLATILE_MIN <= key_id ) && |
| 70 | ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) ) |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 71 | return( PSA_SUCCESS ); |
| 72 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 73 | return( PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 76 | /** Search for the description of a key given its identifier. |
| 77 | * |
| 78 | * The descriptions of volatile keys and loaded persistent keys are |
| 79 | * stored in key slots. This function returns a pointer to the key slot |
| 80 | * containing the description of a key given its identifier. |
| 81 | * |
| 82 | * The function searches the key slots containing the description of the key |
| 83 | * with \p key identifier. The function does only read accesses to the key |
| 84 | * slots. The function does not load any persistent key thus does not access |
| 85 | * any storage. |
| 86 | * |
| 87 | * For volatile key identifiers, only one key slot is queried as a volatile |
| 88 | * key with identifier key_id can only be stored in slot of index |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 89 | * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ). |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 90 | * |
| 91 | * \param key Key identifier to query. |
| 92 | * \param[out] p_slot On success, `*p_slot` contains a pointer to the |
| 93 | * key slot containing the description of the key |
| 94 | * identified by \p key. |
| 95 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 96 | * \retval #PSA_SUCCESS |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 97 | * The pointer to the key slot containing the description of the key |
| 98 | * identified by \p key was returned. |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 99 | * \retval #PSA_ERROR_INVALID_HANDLE |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 100 | * \p key is not a valid key identifier. |
| 101 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 102 | * There is no key with key identifier \p key in the key slots. |
| 103 | */ |
| 104 | static psa_status_t psa_search_key_in_slots( |
| 105 | mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 106 | { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 107 | psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ); |
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 | psa_status_t status = psa_validate_key_id( key, 1, 1 ); |
| 111 | if( status != PSA_SUCCESS ) |
| 112 | return( status ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 113 | |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 114 | if( psa_key_id_is_volatile( key_id ) ) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 115 | { |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 116 | slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ]; |
| 117 | |
| 118 | if( ! mbedtls_svc_key_id_equal( key, slot->attr.id ) ) |
| 119 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | status = PSA_ERROR_DOES_NOT_EXIST; |
| 124 | slot = &global_data.key_slots[ PSA_KEY_SLOT_COUNT ]; |
| 125 | |
| 126 | while( slot > &global_data.key_slots[ 0 ] ) |
| 127 | { |
| 128 | slot--; |
| 129 | if( mbedtls_svc_key_id_equal( key, slot->attr.id ) ) |
| 130 | { |
| 131 | status = PSA_SUCCESS; |
| 132 | break; |
| 133 | } |
| 134 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 137 | if( status == PSA_SUCCESS ) |
| 138 | *p_slot = slot; |
| 139 | |
| 140 | return( status ); |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 141 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 142 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 143 | psa_status_t psa_initialize_key_slots( void ) |
| 144 | { |
| 145 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
| 146 | * guarantee that the key slots are initialized to all-zero, which |
| 147 | * means that all the key slots are in a valid, empty state. */ |
| 148 | global_data.key_slots_initialized = 1; |
| 149 | return( PSA_SUCCESS ); |
| 150 | } |
| 151 | |
| 152 | void psa_wipe_all_key_slots( void ) |
| 153 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 154 | size_t slot_idx; |
| 155 | |
| 156 | for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 157 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 158 | psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ]; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 159 | (void) psa_wipe_key_slot( slot ); |
| 160 | } |
| 161 | global_data.key_slots_initialized = 0; |
| 162 | } |
| 163 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 164 | 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] | 165 | psa_key_slot_t **p_slot ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 166 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 167 | size_t slot_idx; |
| 168 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 169 | if( ! global_data.key_slots_initialized ) |
| 170 | return( PSA_ERROR_BAD_STATE ); |
| 171 | |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 172 | for( slot_idx = PSA_KEY_SLOT_COUNT; slot_idx > 0; slot_idx-- ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 173 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 174 | *p_slot = &global_data.key_slots[ slot_idx - 1 ]; |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 175 | if( ! psa_is_key_slot_occupied( *p_slot ) ) |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 176 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 177 | *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + |
| 178 | ( (psa_key_id_t)slot_idx ) - 1; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 180 | return( PSA_SUCCESS ); |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 181 | } |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 182 | } |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 183 | *p_slot = NULL; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 184 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 185 | } |
| 186 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 187 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 188 | 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] | 189 | { |
| 190 | psa_status_t status = PSA_SUCCESS; |
| 191 | uint8_t *key_data = NULL; |
| 192 | size_t key_data_length = 0; |
| 193 | |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 194 | status = psa_load_persistent_key( &slot->attr, |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 195 | &key_data, &key_data_length ); |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 196 | if( status != PSA_SUCCESS ) |
| 197 | goto exit; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 198 | |
| 199 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 200 | if( psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 201 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 202 | psa_se_key_data_storage_t *data; |
| 203 | if( key_data_length != sizeof( *data ) ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 204 | { |
| 205 | status = PSA_ERROR_STORAGE_FAILURE; |
| 206 | goto exit; |
| 207 | } |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 208 | data = (psa_se_key_data_storage_t *) key_data; |
| 209 | memcpy( &slot->data.se.slot_number, &data->slot_number, |
| 210 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 211 | } |
| 212 | else |
| 213 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 214 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 215 | status = psa_copy_key_material_into_slot( slot, key_data, key_data_length ); |
| 216 | if( status != PSA_SUCCESS ) |
| 217 | goto exit; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 220 | exit: |
| 221 | psa_free_persistent_key_data( key_data, key_data_length ); |
| 222 | return( status ); |
| 223 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 224 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 225 | |
| 226 | psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key, |
| 227 | psa_key_slot_t **p_slot ) |
| 228 | { |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 229 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 230 | |
| 231 | *p_slot = NULL; |
| 232 | if( ! global_data.key_slots_initialized ) |
| 233 | return( PSA_ERROR_BAD_STATE ); |
| 234 | |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 235 | status = psa_search_key_in_slots( key, p_slot ); |
| 236 | if( status != PSA_ERROR_DOES_NOT_EXIST ) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 237 | return( status ); |
| 238 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 239 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 240 | psa_key_id_t volatile_key_id; |
| 241 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 242 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
| 243 | if( status != PSA_SUCCESS ) |
| 244 | return( status ); |
| 245 | |
| 246 | (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
| 247 | (*p_slot)->attr.id = key; |
| 248 | |
| 249 | status = psa_load_persistent_key_into_slot( *p_slot ); |
| 250 | if( status != PSA_SUCCESS ) |
| 251 | psa_wipe_key_slot( *p_slot ); |
| 252 | |
| 253 | return( status ); |
| 254 | #else |
| 255 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 256 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 257 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 260 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 261 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 262 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 263 | if ( psa_key_lifetime_is_external( lifetime ) ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 264 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 265 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 266 | psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime ); |
| 267 | if( driver == NULL ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 269 | else |
| 270 | { |
| 271 | if (p_drv != NULL) |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 272 | *p_drv = driver; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 273 | return( PSA_SUCCESS ); |
| 274 | } |
| 275 | #else |
| 276 | (void) p_drv; |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 279 | } |
| 280 | else |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 281 | /* Local/internal keys are always valid */ |
| 282 | return( PSA_SUCCESS ); |
| 283 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 284 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 285 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime ) |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 286 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 287 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 288 | { |
| 289 | /* Volatile keys are always supported */ |
| 290 | return( PSA_SUCCESS ); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | /* Persistent keys require storage support */ |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 295 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 296 | return( PSA_SUCCESS ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 297 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 298 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 299 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 300 | } |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 303 | 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] | 304 | { |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 306 | psa_status_t status; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 307 | psa_key_slot_t *slot; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 308 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 309 | status = psa_get_key_slot( key, &slot ); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 310 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 311 | { |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 312 | *handle = PSA_KEY_HANDLE_INIT; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 313 | return( status ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 314 | } |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 315 | |
| 316 | *handle = key; |
| 317 | |
| 318 | return( PSA_SUCCESS ); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 319 | |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 320 | #else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 321 | (void) key; |
Ronald Cron | 91e9515 | 2020-07-30 17:48:03 +0200 | [diff] [blame] | 322 | *handle = PSA_KEY_HANDLE_INIT; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 323 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 324 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 327 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 328 | { |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 329 | psa_status_t status; |
| 330 | psa_key_slot_t *slot; |
| 331 | |
Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 332 | if( psa_key_handle_is_null( handle ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 333 | return( PSA_SUCCESS ); |
| 334 | |
Ronald Cron | 5134519 | 2020-10-16 16:07:03 +0200 | [diff] [blame] | 335 | status = psa_search_key_in_slots( handle, &slot ); |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 336 | if( status != PSA_SUCCESS ) |
| 337 | return( status ); |
| 338 | |
| 339 | return( psa_wipe_key_slot( slot ) ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 342 | psa_status_t psa_purge_key( mbedtls_svc_key_id_t key ) |
| 343 | { |
| 344 | psa_status_t status; |
| 345 | psa_key_slot_t *slot; |
| 346 | |
Ronald Cron | 5134519 | 2020-10-16 16:07:03 +0200 | [diff] [blame] | 347 | status = psa_search_key_in_slots( key, &slot ); |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 348 | if( status != PSA_SUCCESS ) |
| 349 | return( status ); |
| 350 | |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame^] | 351 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Ronald Cron | 277a85f | 2020-08-04 15:49:48 +0200 | [diff] [blame] | 352 | return PSA_SUCCESS; |
| 353 | |
| 354 | return( psa_wipe_key_slot( slot ) ); |
| 355 | } |
| 356 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 357 | void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ) |
| 358 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 359 | size_t slot_idx; |
| 360 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 361 | memset( stats, 0, sizeof( *stats ) ); |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 362 | |
| 363 | for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 364 | { |
Ronald Cron | 98a54dd | 2020-07-24 16:33:11 +0200 | [diff] [blame] | 365 | const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ]; |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 366 | if( ! psa_is_key_slot_occupied( slot ) ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 367 | { |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 368 | ++stats->empty_slots; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 369 | continue; |
| 370 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 371 | if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 372 | ++stats->volatile_slots; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 373 | else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 374 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 375 | 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] | 376 | ++stats->persistent_slots; |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 377 | if( id > stats->max_open_internal_key_id ) |
| 378 | stats->max_open_internal_key_id = id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 382 | 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] | 383 | ++stats->external_slots; |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 384 | if( id > stats->max_open_external_key_id ) |
| 385 | stats->max_open_external_key_id = id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 390 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |