Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto support for secure element drivers |
| 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 | a899a72 | 2019-06-24 14:06:43 +0200 | [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 | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 22 | |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 24 | |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame] | 25 | #include <assert.h> |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 29 | #include "psa/crypto_se_driver.h" |
| 30 | |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 31 | #include "psa_crypto_se.h" |
| 32 | |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_PSA_ITS_FILE_C) |
| 34 | #include "psa_crypto_its.h" |
| 35 | #else /* Native ITS implementation */ |
| 36 | #include "psa/error.h" |
| 37 | #include "psa/internal_trusted_storage.h" |
| 38 | #endif |
| 39 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 40 | #include "mbedtls/platform.h" |
| 41 | #if !defined(MBEDTLS_PLATFORM_C) |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 48 | /****************************************************************/ |
| 49 | /* Driver lookup */ |
| 50 | /****************************************************************/ |
| 51 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 52 | /* This structure is identical to psa_drv_se_context_t declared in |
| 53 | * `crypto_se_driver.h`, except that some parts are writable here |
| 54 | * (non-const, or pointer to non-const). */ |
| 55 | typedef struct |
| 56 | { |
| 57 | void *persistent_data; |
| 58 | size_t persistent_data_size; |
| 59 | uintptr_t transient_data; |
| 60 | } psa_drv_se_internal_context_t; |
| 61 | |
Gilles Peskine | 01fd875 | 2020-04-14 19:31:52 +0200 | [diff] [blame] | 62 | struct psa_se_drv_table_entry_s |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 63 | { |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 64 | psa_key_location_t location; |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 65 | const psa_drv_se_t *methods; |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 66 | union |
| 67 | { |
| 68 | psa_drv_se_internal_context_t internal; |
| 69 | psa_drv_se_context_t context; |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 70 | } u; |
Gilles Peskine | 01fd875 | 2020-04-14 19:31:52 +0200 | [diff] [blame] | 71 | }; |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 72 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 73 | static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS]; |
| 74 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 75 | psa_se_drv_table_entry_t *psa_get_se_driver_entry( |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 76 | psa_key_lifetime_t lifetime ) |
| 77 | { |
| 78 | size_t i; |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 79 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime ); |
| 80 | /* In the driver table, location=0 means an entry that isn't used. |
| 81 | * No driver has a location of 0 because it's a reserved value |
| 82 | * (which designates transparent keys). Make sure we never return |
| 83 | * a driver entry for location 0. */ |
| 84 | if( location == 0 ) |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 85 | return( NULL ); |
| 86 | for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ ) |
| 87 | { |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 88 | if( driver_table[i].location == location ) |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 89 | return( &driver_table[i] ); |
| 90 | } |
| 91 | return( NULL ); |
| 92 | } |
| 93 | |
| 94 | const psa_drv_se_t *psa_get_se_driver_methods( |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 95 | const psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 96 | { |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 97 | return( driver->methods ); |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 100 | psa_drv_se_context_t *psa_get_se_driver_context( |
| 101 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 102 | { |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 103 | return( &driver->u.context ); |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 106 | int psa_get_se_driver( psa_key_lifetime_t lifetime, |
| 107 | const psa_drv_se_t **p_methods, |
| 108 | psa_drv_se_context_t **p_drv_context) |
| 109 | { |
| 110 | psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime ); |
| 111 | if( p_methods != NULL ) |
| 112 | *p_methods = ( driver ? driver->methods : NULL ); |
| 113 | if( p_drv_context != NULL ) |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 114 | *p_drv_context = ( driver ? &driver->u.context : NULL ); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 115 | return( driver != NULL ); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | /****************************************************************/ |
| 121 | /* Persistent data management */ |
| 122 | /****************************************************************/ |
| 123 | |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 124 | static psa_status_t psa_get_se_driver_its_file_uid( |
| 125 | const psa_se_drv_table_entry_t *driver, |
| 126 | psa_storage_uid_t *uid ) |
| 127 | { |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 128 | if( driver->location > PSA_MAX_SE_LOCATION ) |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 129 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 573bbc1 | 2019-07-23 19:59:23 +0200 | [diff] [blame] | 130 | |
| 131 | #if SIZE_MAX > UINT32_MAX |
| 132 | /* ITS file sizes are limited to 32 bits. */ |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 133 | if( driver->u.internal.persistent_data_size > UINT32_MAX ) |
Gilles Peskine | 573bbc1 | 2019-07-23 19:59:23 +0200 | [diff] [blame] | 134 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 135 | #endif |
| 136 | |
Gilles Peskine | 75c126b | 2019-07-24 15:56:01 +0200 | [diff] [blame] | 137 | /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */ |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 138 | *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location; |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 139 | return( PSA_SUCCESS ); |
| 140 | } |
| 141 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 142 | psa_status_t psa_load_se_persistent_data( |
| 143 | const psa_se_drv_table_entry_t *driver ) |
| 144 | { |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 145 | psa_status_t status; |
| 146 | psa_storage_uid_t uid; |
Gilles Peskine | 8b66389 | 2019-07-31 17:57:57 +0200 | [diff] [blame] | 147 | size_t length; |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 148 | |
| 149 | status = psa_get_se_driver_its_file_uid( driver, &uid ); |
| 150 | if( status != PSA_SUCCESS ) |
| 151 | return( status ); |
| 152 | |
Gilles Peskine | 8b66389 | 2019-07-31 17:57:57 +0200 | [diff] [blame] | 153 | /* Read the amount of persistent data that the driver requests. |
| 154 | * If the data in storage is larger, it is truncated. If the data |
| 155 | * in storage is smaller, silently keep what is already at the end |
| 156 | * of the output buffer. */ |
Gilles Peskine | 75c126b | 2019-07-24 15:56:01 +0200 | [diff] [blame] | 157 | /* psa_get_se_driver_its_file_uid ensures that the size_t |
| 158 | * persistent_data_size is in range, but compilers don't know that, |
| 159 | * so cast to reassure them. */ |
Gilles Peskine | 573bbc1 | 2019-07-23 19:59:23 +0200 | [diff] [blame] | 160 | return( psa_its_get( uid, 0, |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 161 | (uint32_t) driver->u.internal.persistent_data_size, |
| 162 | driver->u.internal.persistent_data, |
Gilles Peskine | 8b66389 | 2019-07-31 17:57:57 +0200 | [diff] [blame] | 163 | &length ) ); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | psa_status_t psa_save_se_persistent_data( |
| 167 | const psa_se_drv_table_entry_t *driver ) |
| 168 | { |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 169 | psa_status_t status; |
| 170 | psa_storage_uid_t uid; |
| 171 | |
| 172 | status = psa_get_se_driver_its_file_uid( driver, &uid ); |
| 173 | if( status != PSA_SUCCESS ) |
| 174 | return( status ); |
| 175 | |
Gilles Peskine | 75c126b | 2019-07-24 15:56:01 +0200 | [diff] [blame] | 176 | /* psa_get_se_driver_its_file_uid ensures that the size_t |
| 177 | * persistent_data_size is in range, but compilers don't know that, |
| 178 | * so cast to reassure them. */ |
Gilles Peskine | 573bbc1 | 2019-07-23 19:59:23 +0200 | [diff] [blame] | 179 | return( psa_its_set( uid, |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 180 | (uint32_t) driver->u.internal.persistent_data_size, |
| 181 | driver->u.internal.persistent_data, |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 182 | 0 ) ); |
| 183 | } |
| 184 | |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 185 | psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location ) |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 186 | { |
| 187 | psa_storage_uid_t uid; |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 188 | if( location > PSA_MAX_SE_LOCATION ) |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 189 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 190 | uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location; |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 191 | return( psa_its_remove( uid ) ); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 192 | } |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 193 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 194 | psa_status_t psa_find_se_slot_for_key( |
| 195 | const psa_key_attributes_t *attributes, |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 196 | psa_key_creation_method_t method, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 197 | psa_se_drv_table_entry_t *driver, |
| 198 | psa_key_slot_number_t *slot_number ) |
| 199 | { |
| 200 | psa_status_t status; |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 201 | psa_key_location_t key_location = |
| 202 | PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 203 | |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 204 | /* If the location is wrong, it's a bug in the library. */ |
| 205 | if( driver->location != key_location ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 206 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
| 207 | |
| 208 | /* If the driver doesn't support key creation in any way, give up now. */ |
| 209 | if( driver->methods->key_management == NULL ) |
| 210 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 211 | |
Gilles Peskine | 46d9439 | 2019-08-05 14:55:50 +0200 | [diff] [blame] | 212 | if( psa_get_key_slot_number( attributes, slot_number ) == PSA_SUCCESS ) |
| 213 | { |
| 214 | /* The application wants to use a specific slot. Allow it if |
| 215 | * the driver supports it. On a system with isolation, |
| 216 | * the crypto service must check that the application is |
| 217 | * permitted to request this slot. */ |
| 218 | psa_drv_se_validate_slot_number_t p_validate_slot_number = |
| 219 | driver->methods->key_management->p_validate_slot_number; |
| 220 | if( p_validate_slot_number == NULL ) |
| 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 222 | status = p_validate_slot_number( &driver->u.context, |
| 223 | driver->u.internal.persistent_data, |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 224 | attributes, method, |
Gilles Peskine | 46d9439 | 2019-08-05 14:55:50 +0200 | [diff] [blame] | 225 | *slot_number ); |
| 226 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 227 | else if( method == PSA_KEY_CREATION_REGISTER ) |
| 228 | { |
| 229 | /* The application didn't specify a slot number. This doesn't |
| 230 | * make sense when registering a slot. */ |
| 231 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 232 | } |
Gilles Peskine | 46d9439 | 2019-08-05 14:55:50 +0200 | [diff] [blame] | 233 | else |
| 234 | { |
| 235 | /* The application didn't tell us which slot to use. Let the driver |
| 236 | * choose. This is the normal case. */ |
| 237 | psa_drv_se_allocate_key_t p_allocate = |
| 238 | driver->methods->key_management->p_allocate; |
| 239 | if( p_allocate == NULL ) |
| 240 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 241 | status = p_allocate( &driver->u.context, |
| 242 | driver->u.internal.persistent_data, |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 243 | attributes, method, |
Gilles Peskine | 46d9439 | 2019-08-05 14:55:50 +0200 | [diff] [blame] | 244 | slot_number ); |
| 245 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 246 | return( status ); |
| 247 | } |
| 248 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 249 | psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver, |
| 250 | psa_key_slot_number_t slot_number ) |
| 251 | { |
| 252 | psa_status_t status; |
| 253 | psa_status_t storage_status; |
Gilles Peskine | 340b127 | 2019-07-25 14:13:24 +0200 | [diff] [blame] | 254 | /* Normally a missing method would mean that the action is not |
| 255 | * supported. But psa_destroy_key() is not supposed to return |
| 256 | * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should |
| 257 | * be able to destroy it. The only use case for a driver that |
| 258 | * does not have a way to destroy keys at all is if the keys are |
| 259 | * locked in a read-only state: we can use the keys but not |
| 260 | * destroy them. Hence, if the driver doesn't support destroying |
| 261 | * keys, it's really a lack of permission. */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 262 | if( driver->methods->key_management == NULL || |
| 263 | driver->methods->key_management->p_destroy == NULL ) |
| 264 | return( PSA_ERROR_NOT_PERMITTED ); |
| 265 | status = driver->methods->key_management->p_destroy( |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 266 | &driver->u.context, |
| 267 | driver->u.internal.persistent_data, |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 268 | slot_number ); |
| 269 | storage_status = psa_save_se_persistent_data( driver ); |
| 270 | return( status == PSA_SUCCESS ? storage_status : status ); |
| 271 | } |
| 272 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 273 | psa_status_t psa_init_all_se_drivers( void ) |
| 274 | { |
| 275 | size_t i; |
| 276 | for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ ) |
| 277 | { |
| 278 | psa_se_drv_table_entry_t *driver = &driver_table[i]; |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 279 | if( driver->location == 0 ) |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 280 | continue; /* skipping unused entry */ |
| 281 | const psa_drv_se_t *methods = psa_get_se_driver_methods( driver ); |
| 282 | if( methods->p_init != NULL ) |
| 283 | { |
| 284 | psa_status_t status = methods->p_init( |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 285 | &driver->u.context, |
| 286 | driver->u.internal.persistent_data, |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 287 | driver->location ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 288 | if( status != PSA_SUCCESS ) |
| 289 | return( status ); |
Gilles Peskine | c84c70a | 2019-10-01 15:41:42 +0200 | [diff] [blame] | 290 | status = psa_save_se_persistent_data( driver ); |
| 291 | if( status != PSA_SUCCESS ) |
| 292 | return( status ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | return( PSA_SUCCESS ); |
| 296 | } |
| 297 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 298 | |
| 299 | |
| 300 | /****************************************************************/ |
| 301 | /* Driver registration */ |
| 302 | /****************************************************************/ |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 303 | |
| 304 | psa_status_t psa_register_se_driver( |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 305 | psa_key_location_t location, |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 306 | const psa_drv_se_t *methods) |
| 307 | { |
| 308 | size_t i; |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 309 | psa_status_t status; |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 310 | |
| 311 | if( methods->hal_version != PSA_DRV_SE_HAL_VERSION ) |
| 312 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame] | 313 | /* Driver table entries are 0-initialized. 0 is not a valid driver |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 314 | * location because it means a transparent key. */ |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame] | 315 | #if defined(static_assert) |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 316 | static_assert( PSA_KEY_LOCATION_LOCAL_STORAGE == 0, |
| 317 | "Secure element support requires 0 to mean a local key" ); |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame] | 318 | #endif |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 319 | if( location == PSA_KEY_LOCATION_LOCAL_STORAGE ) |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 320 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 321 | if( location > PSA_MAX_SE_LOCATION ) |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 322 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 323 | |
| 324 | for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ ) |
| 325 | { |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 326 | if( driver_table[i].location == 0 ) |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 327 | break; |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 328 | /* Check that location isn't already in use up to the first free |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 329 | * entry. Since entries are created in order and never deleted, |
| 330 | * there can't be a used entry after the first free entry. */ |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 331 | if( driver_table[i].location == location ) |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 332 | return( PSA_ERROR_ALREADY_EXISTS ); |
| 333 | } |
| 334 | if( i == PSA_MAX_SE_DRIVERS ) |
| 335 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 336 | |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 337 | driver_table[i].location = location; |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 338 | driver_table[i].methods = methods; |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 339 | driver_table[i].u.internal.persistent_data_size = |
Gilles Peskine | d5536d8 | 2019-10-01 16:55:29 +0200 | [diff] [blame] | 340 | methods->persistent_data_size; |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 341 | |
| 342 | if( methods->persistent_data_size != 0 ) |
| 343 | { |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 344 | driver_table[i].u.internal.persistent_data = |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 345 | mbedtls_calloc( 1, methods->persistent_data_size ); |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 346 | if( driver_table[i].u.internal.persistent_data == NULL ) |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 347 | { |
| 348 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 349 | goto error; |
| 350 | } |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 351 | /* Load the driver's persistent data. On first use, the persistent |
| 352 | * data does not exist in storage, and is initialized to |
| 353 | * all-bits-zero by the calloc call just above. */ |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 354 | status = psa_load_se_persistent_data( &driver_table[i] ); |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 355 | if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST ) |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 356 | goto error; |
| 357 | } |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 358 | |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 359 | return( PSA_SUCCESS ); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 360 | |
| 361 | error: |
| 362 | memset( &driver_table[i], 0, sizeof( driver_table[i] ) ); |
| 363 | return( status ); |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 366 | void psa_unregister_all_se_drivers( void ) |
| 367 | { |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 368 | size_t i; |
| 369 | for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ ) |
| 370 | { |
Gilles Peskine | 1a75d0c | 2020-04-14 19:33:25 +0200 | [diff] [blame] | 371 | if( driver_table[i].u.internal.persistent_data != NULL ) |
| 372 | mbedtls_free( driver_table[i].u.internal.persistent_data ); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 373 | } |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 374 | memset( driver_table, 0, sizeof( driver_table ) ); |
| 375 | } |
| 376 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 377 | |
| 378 | |
| 379 | /****************************************************************/ |
| 380 | /* The end */ |
| 381 | /****************************************************************/ |
| 382 | |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 383 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |