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