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