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