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