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 | |
| 54 | /* Access a key slot at the given handle. The handle of a key slot is |
| 55 | * the index of the slot in the global slot array, plus one so that handles |
| 56 | * start at 1 and not 0. */ |
| 57 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 58 | psa_key_slot_t **p_slot ) |
| 59 | { |
| 60 | psa_key_slot_t *slot = NULL; |
| 61 | |
| 62 | if( ! global_data.key_slots_initialized ) |
| 63 | return( PSA_ERROR_BAD_STATE ); |
| 64 | |
| 65 | /* 0 is not a valid handle under any circumstance. This |
| 66 | * implementation provides slots number 1 to N where N is the |
| 67 | * number of available slots. */ |
| 68 | if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) ) |
| 69 | return( PSA_ERROR_INVALID_HANDLE ); |
| 70 | slot = &global_data.key_slots[handle - 1]; |
| 71 | |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 72 | /* If the slot isn't occupied, the handle is invalid. */ |
| 73 | if( ! psa_is_key_slot_occupied( slot ) ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 74 | return( PSA_ERROR_INVALID_HANDLE ); |
| 75 | |
| 76 | *p_slot = slot; |
| 77 | return( PSA_SUCCESS ); |
| 78 | } |
| 79 | |
| 80 | psa_status_t psa_initialize_key_slots( void ) |
| 81 | { |
| 82 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
| 83 | * guarantee that the key slots are initialized to all-zero, which |
| 84 | * means that all the key slots are in a valid, empty state. */ |
| 85 | global_data.key_slots_initialized = 1; |
| 86 | return( PSA_SUCCESS ); |
| 87 | } |
| 88 | |
| 89 | void psa_wipe_all_key_slots( void ) |
| 90 | { |
| 91 | psa_key_handle_t key; |
| 92 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
| 93 | { |
| 94 | psa_key_slot_t *slot = &global_data.key_slots[key - 1]; |
| 95 | (void) psa_wipe_key_slot( slot ); |
| 96 | } |
| 97 | global_data.key_slots_initialized = 0; |
| 98 | } |
| 99 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 100 | psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle, |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 101 | psa_key_slot_t **p_slot ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 102 | { |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 103 | if( ! global_data.key_slots_initialized ) |
| 104 | return( PSA_ERROR_BAD_STATE ); |
| 105 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 106 | for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) ) |
| 107 | { |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 108 | *p_slot = &global_data.key_slots[*handle - 1]; |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 109 | if( ! psa_is_key_slot_occupied( *p_slot ) ) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 110 | return( PSA_SUCCESS ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 111 | } |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 112 | *p_slot = NULL; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 113 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 114 | } |
| 115 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 116 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | psa_status_t status = PSA_SUCCESS; |
| 120 | uint8_t *key_data = NULL; |
| 121 | size_t key_data_length = 0; |
| 122 | |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 123 | status = psa_load_persistent_key( &slot->attr, |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 124 | &key_data, &key_data_length ); |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 125 | if( status != PSA_SUCCESS ) |
| 126 | goto exit; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 127 | |
| 128 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 129 | if( psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 130 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 131 | psa_se_key_data_storage_t *data; |
| 132 | if( key_data_length != sizeof( *data ) ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 133 | { |
| 134 | status = PSA_ERROR_STORAGE_FAILURE; |
| 135 | goto exit; |
| 136 | } |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 137 | data = (psa_se_key_data_storage_t *) key_data; |
| 138 | memcpy( &slot->data.se.slot_number, &data->slot_number, |
| 139 | sizeof( slot->data.se.slot_number ) ); |
| 140 | memcpy( &slot->attr.bits, &data->bits, |
| 141 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 145 | { |
Gilles Peskine | 2431859 | 2019-07-30 20:30:51 +0200 | [diff] [blame] | 146 | status = psa_import_key_into_slot( slot, key_data, key_data_length ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 149 | exit: |
| 150 | psa_free_persistent_key_data( key_data, key_data_length ); |
| 151 | return( status ); |
| 152 | } |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 153 | |
| 154 | /** Check whether a key identifier is acceptable. |
| 155 | * |
| 156 | * For backward compatibility, key identifiers that were valid in a |
| 157 | * past released version must remain valid, unless a migration path |
| 158 | * is provided. |
| 159 | * |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 160 | * \param file_id The key identifier to check. |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 161 | * \param vendor_ok Nonzero to allow key ids in the vendor range. |
| 162 | * 0 to allow only key ids in the application range. |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 163 | * |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 164 | * \return 1 if \p file_id is acceptable, otherwise 0. |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 165 | */ |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 166 | static int psa_is_key_id_valid( psa_key_file_id_t file_id, |
| 167 | int vendor_ok ) |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 168 | { |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 169 | psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id ); |
Gilles Peskine | f9fbc38 | 2019-05-15 18:42:09 +0200 | [diff] [blame] | 170 | if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX ) |
| 171 | return( 1 ); |
| 172 | else if( vendor_ok && |
| 173 | PSA_KEY_ID_VENDOR_MIN <= key_id && |
| 174 | key_id <= PSA_KEY_ID_VENDOR_MAX ) |
| 175 | return( 1 ); |
| 176 | else |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 177 | return( 0 ); |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 178 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 179 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 180 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 181 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 182 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 183 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 184 | if ( psa_key_lifetime_is_external( lifetime ) ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 185 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 187 | psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime ); |
| 188 | if( driver == NULL ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 190 | else |
| 191 | { |
| 192 | if (p_drv != NULL) |
Steven Cooreman | 00106a1 | 2020-06-08 18:54:23 +0200 | [diff] [blame] | 193 | *p_drv = driver; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 194 | return( PSA_SUCCESS ); |
| 195 | } |
| 196 | #else |
| 197 | (void) p_drv; |
| 198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 199 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 200 | } |
| 201 | else |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 202 | /* Local/internal keys are always valid */ |
| 203 | return( PSA_SUCCESS ); |
| 204 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 205 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 206 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime, |
| 207 | psa_key_id_t key_id ) |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 208 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 209 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 210 | { |
| 211 | /* Volatile keys are always supported */ |
| 212 | return( PSA_SUCCESS ); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | /* Persistent keys require storage support */ |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 217 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 218 | if( psa_is_key_id_valid( key_id, |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 219 | psa_key_lifetime_is_external( lifetime ) ) ) |
| 220 | return( PSA_SUCCESS ); |
| 221 | else |
| 222 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 223 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | fa68609 | 2020-06-18 14:37:31 +0200 | [diff] [blame] | 224 | (void) key_id; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 225 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 226 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 227 | } |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 230 | psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 231 | { |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 232 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 233 | psa_status_t status; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 234 | psa_key_slot_t *slot; |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 235 | |
| 236 | *handle = 0; |
| 237 | |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 238 | if( ! psa_is_key_id_valid( id, 1 ) ) |
| 239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 240 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 241 | status = psa_get_empty_key_slot( handle, &slot ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 242 | if( status != PSA_SUCCESS ) |
| 243 | return( status ); |
| 244 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 245 | slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
| 246 | slot->attr.id = id; |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 247 | |
| 248 | status = psa_load_persistent_key_into_slot( slot ); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 249 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 250 | { |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 251 | psa_wipe_key_slot( slot ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 252 | *handle = 0; |
| 253 | } |
| 254 | return( status ); |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 255 | |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 256 | #else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 70e085a | 2019-05-27 19:04:07 +0200 | [diff] [blame] | 257 | (void) id; |
| 258 | *handle = 0; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 259 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 260 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 263 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 264 | { |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 265 | psa_status_t status; |
| 266 | psa_key_slot_t *slot; |
| 267 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 268 | if( handle == 0 ) |
| 269 | return( PSA_SUCCESS ); |
| 270 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 271 | status = psa_get_key_slot( handle, &slot ); |
| 272 | if( status != PSA_SUCCESS ) |
| 273 | return( status ); |
| 274 | |
| 275 | return( psa_wipe_key_slot( slot ) ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 278 | void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ) |
| 279 | { |
| 280 | psa_key_handle_t key; |
| 281 | memset( stats, 0, sizeof( *stats ) ); |
| 282 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
| 283 | { |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 284 | const psa_key_slot_t *slot = &global_data.key_slots[key - 1]; |
| 285 | if( ! psa_is_key_slot_occupied( slot ) ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 286 | { |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 287 | ++stats->empty_slots; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 288 | continue; |
| 289 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 290 | if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 291 | ++stats->volatile_slots; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 292 | else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 293 | { |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 294 | psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 295 | ++stats->persistent_slots; |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 296 | if( id > stats->max_open_internal_key_id ) |
| 297 | stats->max_open_internal_key_id = id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 298 | } |
| 299 | else |
| 300 | { |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 301 | psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 302 | ++stats->external_slots; |
Jaeden Amero | 6fa62a5 | 2019-08-20 17:43:48 +0100 | [diff] [blame] | 303 | if( id > stats->max_open_external_key_id ) |
| 304 | stats->max_open_external_key_id = id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 309 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |