blob: b660393640d593a8b7f7b5dc7ec42f45c57d2a16 [file] [log] [blame]
Gilles Peskinea899a722019-06-24 14:06:43 +02001/*
2 * PSA crypto support for secure element drivers
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinea899a722019-06-24 14:06:43 +02006 * 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 Peskinea899a722019-06-24 14:06:43 +020019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinea899a722019-06-24 14:06:43 +020022
Gilles Peskinea8ade162019-06-26 11:24:49 +020023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinea899a722019-06-24 14:06:43 +020024
Gilles Peskine9717d102019-06-26 11:50:04 +020025#include <assert.h>
Gilles Peskine5243a202019-07-12 23:38:19 +020026#include <stdint.h>
Gilles Peskined0890212019-06-24 14:34:43 +020027#include <string.h>
28
Gilles Peskine5243a202019-07-12 23:38:19 +020029#include "psa/crypto_se_driver.h"
30
Gilles Peskinea899a722019-06-24 14:06:43 +020031#include "psa_crypto_se.h"
32
Gilles Peskine8b96cad2019-07-23 17:38:08 +020033#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 Peskine5243a202019-07-12 23:38:19 +020040#include "mbedtls/platform.h"
Gilles Peskine5243a202019-07-12 23:38:19 +020041
42
43
Gilles Peskinef989dbe2019-06-26 18:18:12 +020044/****************************************************************/
45/* Driver lookup */
46/****************************************************************/
47
Gilles Peskine5243a202019-07-12 23:38:19 +020048/* 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). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051typedef struct {
Gilles Peskine5243a202019-07-12 23:38:19 +020052 void *persistent_data;
53 size_t persistent_data_size;
54 uintptr_t transient_data;
55} psa_drv_se_internal_context_t;
56
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057struct psa_se_drv_table_entry_s {
Gilles Peskine2b04f462020-05-10 00:44:04 +020058 psa_key_location_t location;
Gilles Peskinea899a722019-06-24 14:06:43 +020059 const psa_drv_se_t *methods;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 union {
Gilles Peskine5243a202019-07-12 23:38:19 +020061 psa_drv_se_internal_context_t internal;
62 psa_drv_se_context_t context;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +020063 } u;
Gilles Peskine01fd8752020-04-14 19:31:52 +020064};
Gilles Peskinea899a722019-06-24 14:06:43 +020065
Gilles Peskinef989dbe2019-06-26 18:18:12 +020066static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
67
Gilles Peskine5243a202019-07-12 23:38:19 +020068psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 psa_key_lifetime_t lifetime)
Gilles Peskinef989dbe2019-06-26 18:18:12 +020070{
71 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(lifetime);
Gilles Peskine2b04f462020-05-10 00:44:04 +020073 /* In the driver table, location=0 means an entry that isn't used.
74 * No driver has a location of 0 because it's a reserved value
75 * (which designates transparent keys). Make sure we never return
76 * a driver entry for location 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 if (location == 0) {
78 return NULL;
Gilles Peskinef989dbe2019-06-26 18:18:12 +020079 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
81 if (driver_table[i].location == location) {
82 return &driver_table[i];
83 }
84 }
85 return NULL;
Gilles Peskinef989dbe2019-06-26 18:18:12 +020086}
87
88const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 const psa_se_drv_table_entry_t *driver)
Gilles Peskinef989dbe2019-06-26 18:18:12 +020090{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 return driver->methods;
Gilles Peskinef989dbe2019-06-26 18:18:12 +020092}
93
Gilles Peskine5243a202019-07-12 23:38:19 +020094psa_drv_se_context_t *psa_get_se_driver_context(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 psa_se_drv_table_entry_t *driver)
Gilles Peskinef989dbe2019-06-26 18:18:12 +020096{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 return &driver->u.context;
Gilles Peskinef989dbe2019-06-26 18:18:12 +020098}
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100int psa_get_se_driver(psa_key_lifetime_t lifetime,
101 const psa_drv_se_t **p_methods,
102 psa_drv_se_context_t **p_drv_context)
Gilles Peskine5243a202019-07-12 23:38:19 +0200103{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
105 if (p_methods != NULL) {
106 *p_methods = (driver ? driver->methods : NULL);
107 }
108 if (p_drv_context != NULL) {
109 *p_drv_context = (driver ? &driver->u.context : NULL);
110 }
111 return driver != NULL;
Gilles Peskine5243a202019-07-12 23:38:19 +0200112}
113
114
115
116/****************************************************************/
117/* Persistent data management */
118/****************************************************************/
119
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200120static psa_status_t psa_get_se_driver_its_file_uid(
121 const psa_se_drv_table_entry_t *driver,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 psa_storage_uid_t *uid)
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200123{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if (driver->location > PSA_MAX_SE_LOCATION) {
125 return PSA_ERROR_NOT_SUPPORTED;
126 }
Gilles Peskine573bbc12019-07-23 19:59:23 +0200127
128#if SIZE_MAX > UINT32_MAX
129 /* ITS file sizes are limited to 32 bits. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 if (driver->u.internal.persistent_data_size > UINT32_MAX) {
131 return PSA_ERROR_NOT_SUPPORTED;
132 }
Gilles Peskine573bbc12019-07-23 19:59:23 +0200133#endif
134
Gilles Peskine75c126b2019-07-24 15:56:01 +0200135 /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200136 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 return PSA_SUCCESS;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200138}
139
Gilles Peskine5243a202019-07-12 23:38:19 +0200140psa_status_t psa_load_se_persistent_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 const psa_se_drv_table_entry_t *driver)
Gilles Peskine5243a202019-07-12 23:38:19 +0200142{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200143 psa_status_t status;
144 psa_storage_uid_t uid;
Gilles Peskine8b663892019-07-31 17:57:57 +0200145 size_t length;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 status = psa_get_se_driver_its_file_uid(driver, &uid);
148 if (status != PSA_SUCCESS) {
149 return status;
150 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200151
Gilles Peskine8b663892019-07-31 17:57:57 +0200152 /* Read the amount of persistent data that the driver requests.
153 * If the data in storage is larger, it is truncated. If the data
154 * in storage is smaller, silently keep what is already at the end
155 * of the output buffer. */
Gilles Peskine75c126b2019-07-24 15:56:01 +0200156 /* psa_get_se_driver_its_file_uid ensures that the size_t
157 * persistent_data_size is in range, but compilers don't know that,
158 * so cast to reassure them. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 return psa_its_get(uid, 0,
160 (uint32_t) driver->u.internal.persistent_data_size,
161 driver->u.internal.persistent_data,
162 &length);
Gilles Peskine5243a202019-07-12 23:38:19 +0200163}
164
165psa_status_t psa_save_se_persistent_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 const psa_se_drv_table_entry_t *driver)
Gilles Peskine5243a202019-07-12 23:38:19 +0200167{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200168 psa_status_t status;
169 psa_storage_uid_t uid;
170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 status = psa_get_se_driver_its_file_uid(driver, &uid);
172 if (status != PSA_SUCCESS) {
173 return status;
174 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200175
Gilles Peskine75c126b2019-07-24 15:56:01 +0200176 /* 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 Peskine1b6c09a2023-01-11 14:52:35 +0100179 return psa_its_set(uid,
180 (uint32_t) driver->u.internal.persistent_data_size,
181 driver->u.internal.persistent_data,
182 0);
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200183}
184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185psa_status_t psa_destroy_se_persistent_data(psa_key_location_t location)
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200186{
187 psa_storage_uid_t uid;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 if (location > PSA_MAX_SE_LOCATION) {
189 return PSA_ERROR_NOT_SUPPORTED;
190 }
Gilles Peskine2b04f462020-05-10 00:44:04 +0200191 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return psa_its_remove(uid);
Gilles Peskine5243a202019-07-12 23:38:19 +0200193}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200194
Gilles Peskinecbaff462019-07-12 23:46:04 +0200195psa_status_t psa_find_se_slot_for_key(
196 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200197 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200198 psa_se_drv_table_entry_t *driver,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 psa_key_slot_number_t *slot_number)
Gilles Peskinecbaff462019-07-12 23:46:04 +0200200{
201 psa_status_t status;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200202 psa_key_location_t key_location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(attributes));
Gilles Peskinecbaff462019-07-12 23:46:04 +0200204
Gilles Peskine2b04f462020-05-10 00:44:04 +0200205 /* If the location is wrong, it's a bug in the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 if (driver->location != key_location) {
207 return PSA_ERROR_CORRUPTION_DETECTED;
208 }
Gilles Peskinecbaff462019-07-12 23:46:04 +0200209
210 /* If the driver doesn't support key creation in any way, give up now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 if (driver->methods->key_management == NULL) {
212 return PSA_ERROR_NOT_SUPPORTED;
213 }
Gilles Peskinecbaff462019-07-12 23:46:04 +0200214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 if (psa_get_key_slot_number(attributes, slot_number) == PSA_SUCCESS) {
Gilles Peskine46d94392019-08-05 14:55:50 +0200216 /* The application wants to use a specific slot. Allow it if
217 * the driver supports it. On a system with isolation,
218 * the crypto service must check that the application is
219 * permitted to request this slot. */
220 psa_drv_se_validate_slot_number_t p_validate_slot_number =
221 driver->methods->key_management->p_validate_slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (p_validate_slot_number == NULL) {
223 return PSA_ERROR_NOT_SUPPORTED;
224 }
225 status = p_validate_slot_number(&driver->u.context,
226 driver->u.internal.persistent_data,
227 attributes, method,
228 *slot_number);
229 } else if (method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200230 /* The application didn't specify a slot number. This doesn't
231 * make sense when registering a slot. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 return PSA_ERROR_INVALID_ARGUMENT;
233 } else {
Gilles Peskine46d94392019-08-05 14:55:50 +0200234 /* The application didn't tell us which slot to use. Let the driver
235 * choose. This is the normal case. */
236 psa_drv_se_allocate_key_t p_allocate =
237 driver->methods->key_management->p_allocate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if (p_allocate == NULL) {
239 return PSA_ERROR_NOT_SUPPORTED;
240 }
241 status = p_allocate(&driver->u.context,
242 driver->u.internal.persistent_data,
243 attributes, method,
244 slot_number);
Gilles Peskine46d94392019-08-05 14:55:50 +0200245 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +0200247}
248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver,
250 psa_key_slot_number_t slot_number)
Gilles Peskine354f7672019-07-12 23:46:38 +0200251{
252 psa_status_t status;
253 psa_status_t storage_status;
Gilles Peskine340b1272019-07-25 14:13:24 +0200254 /* 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 Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (driver->methods->key_management == NULL ||
263 driver->methods->key_management->p_destroy == NULL) {
264 return PSA_ERROR_NOT_PERMITTED;
265 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200266 status = driver->methods->key_management->p_destroy(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200267 &driver->u.context,
268 driver->u.internal.persistent_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 slot_number);
270 storage_status = psa_save_se_persistent_data(driver);
271 return status == PSA_SUCCESS ? storage_status : status;
Gilles Peskine354f7672019-07-12 23:46:38 +0200272}
273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274psa_status_t psa_init_all_se_drivers(void)
Gilles Peskined9348f22019-10-01 15:22:29 +0200275{
276 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
Gilles Peskined9348f22019-10-01 15:22:29 +0200278 psa_se_drv_table_entry_t *driver = &driver_table[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 if (driver->location == 0) {
Gilles Peskined9348f22019-10-01 15:22:29 +0200280 continue; /* skipping unused entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 }
282 const psa_drv_se_t *methods = psa_get_se_driver_methods(driver);
283 if (methods->p_init != NULL) {
Gilles Peskined9348f22019-10-01 15:22:29 +0200284 psa_status_t status = methods->p_init(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200285 &driver->u.context,
286 driver->u.internal.persistent_data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 driver->location);
288 if (status != PSA_SUCCESS) {
289 return status;
290 }
291 status = psa_save_se_persistent_data(driver);
292 if (status != PSA_SUCCESS) {
293 return status;
294 }
Gilles Peskined9348f22019-10-01 15:22:29 +0200295 }
296 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_SUCCESS;
Gilles Peskined9348f22019-10-01 15:22:29 +0200298}
299
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200300
301
302/****************************************************************/
303/* Driver registration */
304/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200305
306psa_status_t psa_register_se_driver(
Gilles Peskine2b04f462020-05-10 00:44:04 +0200307 psa_key_location_t location,
Gilles Peskinea899a722019-06-24 14:06:43 +0200308 const psa_drv_se_t *methods)
309{
310 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200311 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (methods->hal_version != PSA_DRV_SE_HAL_VERSION) {
314 return PSA_ERROR_NOT_SUPPORTED;
315 }
Gilles Peskine9717d102019-06-26 11:50:04 +0200316 /* Driver table entries are 0-initialized. 0 is not a valid driver
Gilles Peskine2b04f462020-05-10 00:44:04 +0200317 * location because it means a transparent key. */
Gilles Peskine9717d102019-06-26 11:50:04 +0200318#if defined(static_assert)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 static_assert(PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
320 "Secure element support requires 0 to mean a local key");
Gilles Peskine9717d102019-06-26 11:50:04 +0200321#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 if (location == PSA_KEY_LOCATION_LOCAL_STORAGE) {
323 return PSA_ERROR_INVALID_ARGUMENT;
324 }
325 if (location > PSA_MAX_SE_LOCATION) {
326 return PSA_ERROR_NOT_SUPPORTED;
327 }
Gilles Peskinea899a722019-06-24 14:06:43 +0200328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
330 if (driver_table[i].location == 0) {
Gilles Peskinea899a722019-06-24 14:06:43 +0200331 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 }
Gilles Peskine2b04f462020-05-10 00:44:04 +0200333 /* Check that location isn't already in use up to the first free
Gilles Peskinea899a722019-06-24 14:06:43 +0200334 * entry. Since entries are created in order and never deleted,
335 * there can't be a used entry after the first free entry. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (driver_table[i].location == location) {
337 return PSA_ERROR_ALREADY_EXISTS;
338 }
Gilles Peskinea899a722019-06-24 14:06:43 +0200339 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (i == PSA_MAX_SE_DRIVERS) {
341 return PSA_ERROR_INSUFFICIENT_MEMORY;
342 }
Gilles Peskinea899a722019-06-24 14:06:43 +0200343
Gilles Peskine2b04f462020-05-10 00:44:04 +0200344 driver_table[i].location = location;
Gilles Peskinea899a722019-06-24 14:06:43 +0200345 driver_table[i].methods = methods;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200346 driver_table[i].u.internal.persistent_data_size =
Gilles Peskined5536d82019-10-01 16:55:29 +0200347 methods->persistent_data_size;
Gilles Peskine5243a202019-07-12 23:38:19 +0200348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (methods->persistent_data_size != 0) {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200350 driver_table[i].u.internal.persistent_data =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 mbedtls_calloc(1, methods->persistent_data_size);
352 if (driver_table[i].u.internal.persistent_data == NULL) {
Gilles Peskine5243a202019-07-12 23:38:19 +0200353 status = PSA_ERROR_INSUFFICIENT_MEMORY;
354 goto error;
355 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200356 /* 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 Peskine1b6c09a2023-01-11 14:52:35 +0100359 status = psa_load_se_persistent_data(&driver_table[i]);
360 if (status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskine5243a202019-07-12 23:38:19 +0200361 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200363 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_SUCCESS;
Gilles Peskine5243a202019-07-12 23:38:19 +0200366
367error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 memset(&driver_table[i], 0, sizeof(driver_table[i]));
369 return status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200370}
371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372void psa_unregister_all_se_drivers(void)
Gilles Peskined0890212019-06-24 14:34:43 +0200373{
Gilles Peskine5243a202019-07-12 23:38:19 +0200374 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
376 if (driver_table[i].u.internal.persistent_data != NULL) {
377 mbedtls_free(driver_table[i].u.internal.persistent_data);
378 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200379 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 memset(driver_table, 0, sizeof(driver_table));
Gilles Peskined0890212019-06-24 14:34:43 +0200381}
382
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200383
384
385/****************************************************************/
386/* The end */
387/****************************************************************/
388
Gilles Peskinea8ade162019-06-26 11:24:49 +0200389#endif /* MBEDTLS_PSA_CRYPTO_SE_C */