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 | |
| 21 | #ifndef PSA_CRYPTO_SE_H |
| 22 | #define PSA_CRYPTO_SE_H |
| 23 | |
Ronald Cron | 7871cb1 | 2023-10-10 08:51:39 +0200 | [diff] [blame] | 24 | /* |
| 25 | * Include the build-time configuration information header. Here, we do not |
| 26 | * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which |
| 27 | * is basically just an alias to it. This is to ease the maintenance of the |
| 28 | * TF-PSA-Crypto repository which has a different build system and |
| 29 | * configuration. |
| 30 | */ |
| 31 | #include "psa/build_info.h" |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 32 | |
| 33 | #include "psa/crypto.h" |
| 34 | #include "psa/crypto_se_driver.h" |
| 35 | |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 36 | /** The maximum location value that this implementation supports |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 37 | * for a secure element. |
| 38 | * |
| 39 | * This is not a characteristic that each PSA implementation has, but a |
| 40 | * limitation of the current implementation due to the constraints imposed |
| 41 | * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. |
| 42 | * |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 43 | * The minimum location value for a secure element is 1, like on any |
| 44 | * PSA implementation (0 means a transparent key). |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 45 | */ |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 46 | #define PSA_MAX_SE_LOCATION 255 |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 47 | |
| 48 | /** The base of the range of ITS file identifiers for secure element |
| 49 | * driver persistent data. |
| 50 | * |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 51 | * We use a slice of the implementation reserved range 0xffff0000..0xffffffff, |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 52 | * specifically the range 0xfffffe00..0xfffffeff. The length of this range |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 53 | * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is |
| 54 | * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE |
| 55 | * which doesn't have a driver. |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 56 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | #define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ((psa_key_id_t) 0xfffffe00) |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 58 | |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 59 | /** The maximum number of registered secure element driver locations. */ |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 60 | #define PSA_MAX_SE_DRIVERS 4 |
| 61 | |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 62 | /** Unregister all secure element drivers. |
| 63 | * |
| 64 | * \warning Do not call this function while the library is in the initialized |
| 65 | * state. This function is only intended to be called at the end |
| 66 | * of mbedtls_psa_crypto_free(). |
| 67 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | void psa_unregister_all_se_drivers(void); |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 69 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 70 | /** Initialize all secure element drivers. |
| 71 | * |
| 72 | * Called from psa_crypto_init(). |
| 73 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | psa_status_t psa_init_all_se_drivers(void); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 76 | /** A structure that describes a registered secure element driver. |
| 77 | * |
| 78 | * A secure element driver table entry contains a pointer to the |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 79 | * driver's method table as well as the driver context structure. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 80 | */ |
| 81 | typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t; |
| 82 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 83 | /** Return the secure element driver information for a lifetime value. |
| 84 | * |
| 85 | * \param lifetime The lifetime value to query. |
| 86 | * \param[out] p_methods On output, if there is a driver, |
| 87 | * \c *methods points to its method table. |
| 88 | * Otherwise \c *methods is \c NULL. |
| 89 | * \param[out] p_drv_context On output, if there is a driver, |
| 90 | * \c *drv_context points to its context |
| 91 | * structure. |
| 92 | * Otherwise \c *drv_context is \c NULL. |
| 93 | * |
| 94 | * \retval 1 |
| 95 | * \p lifetime corresponds to a registered driver. |
| 96 | * \retval 0 |
| 97 | * \p lifetime does not correspond to a registered driver. |
| 98 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 103 | /** Return the secure element driver table entry for a lifetime value. |
| 104 | * |
| 105 | * \param lifetime The lifetime value to query. |
| 106 | * |
| 107 | * \return The driver table entry for \p lifetime, or |
| 108 | * \p NULL if \p lifetime does not correspond to a registered driver. |
| 109 | */ |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 110 | psa_se_drv_table_entry_t *psa_get_se_driver_entry( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | psa_key_lifetime_t lifetime); |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 112 | |
| 113 | /** Return the method table for a secure element driver. |
| 114 | * |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 115 | * \param[in] driver The driver table entry to access, or \c NULL. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 116 | * |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 117 | * \return The driver's method table. |
| 118 | * \c NULL if \p driver is \c NULL. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 119 | */ |
| 120 | const psa_drv_se_t *psa_get_se_driver_methods( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | const psa_se_drv_table_entry_t *driver); |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 122 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 123 | /** Return the context of a secure element driver. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 124 | * |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 125 | * \param[in] driver The driver table entry to access, or \c NULL. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 126 | * |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 127 | * \return A pointer to the driver context. |
| 128 | * \c NULL if \p driver is \c NULL. |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 129 | */ |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 130 | psa_drv_se_context_t *psa_get_se_driver_context( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | psa_se_drv_table_entry_t *driver); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 132 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 133 | /** Find a free slot for a key that is to be created. |
| 134 | * |
| 135 | * This function calls the relevant method in the driver to find a suitable |
| 136 | * slot for a key with the given attributes. |
| 137 | * |
| 138 | * \param[in] attributes Metadata about the key that is about to be created. |
| 139 | * \param[in] driver The driver table entry to query. |
| 140 | * \param[out] slot_number On success, a slot number that is free in this |
| 141 | * secure element. |
| 142 | */ |
| 143 | psa_status_t psa_find_se_slot_for_key( |
| 144 | const psa_key_attributes_t *attributes, |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 145 | psa_key_creation_method_t method, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 146 | psa_se_drv_table_entry_t *driver, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | psa_key_slot_number_t *slot_number); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 148 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 149 | /** Destroy a key in a secure element. |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 150 | * |
| 151 | * This function calls the relevant driver method to destroy a key |
| 152 | * and updates the driver's persistent data. |
| 153 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver, |
| 155 | psa_key_slot_number_t slot_number); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 156 | |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 157 | /** Load the persistent data of a secure element driver. |
| 158 | * |
| 159 | * \param driver The driver table entry containing the persistent |
| 160 | * data to load from storage. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 161 | * |
| 162 | * \return #PSA_SUCCESS |
| 163 | * \return #PSA_ERROR_NOT_SUPPORTED |
| 164 | * \return #PSA_ERROR_DOES_NOT_EXIST |
| 165 | * \return #PSA_ERROR_STORAGE_FAILURE |
| 166 | * \return #PSA_ERROR_DATA_CORRUPT |
| 167 | * \return #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 168 | */ |
| 169 | psa_status_t psa_load_se_persistent_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | const psa_se_drv_table_entry_t *driver); |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 171 | |
| 172 | /** Save the persistent data of a secure element driver. |
| 173 | * |
| 174 | * \param[in] driver The driver table entry containing the persistent |
| 175 | * data to save to storage. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 176 | * |
| 177 | * \return #PSA_SUCCESS |
| 178 | * \return #PSA_ERROR_NOT_SUPPORTED |
| 179 | * \return #PSA_ERROR_NOT_PERMITTED |
| 180 | * \return #PSA_ERROR_NOT_SUPPORTED |
| 181 | * \return #PSA_ERROR_INSUFFICIENT_STORAGE |
| 182 | * \return #PSA_ERROR_STORAGE_FAILURE |
| 183 | * \return #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 184 | */ |
| 185 | psa_status_t psa_save_se_persistent_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | const psa_se_drv_table_entry_t *driver); |
Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 187 | |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 188 | /** Destroy the persistent data of a secure element driver. |
| 189 | * |
| 190 | * This is currently only used for testing. |
| 191 | * |
Gilles Peskine | 2b04f46 | 2020-05-10 00:44:04 +0200 | [diff] [blame] | 192 | * \param[in] location The location identifier for the driver whose |
| 193 | * persistent data is to be erased. |
Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | 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] | 196 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 197 | |
| 198 | /** The storage representation of a key whose data is in a secure element. |
| 199 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | typedef struct { |
| 201 | uint8_t slot_number[sizeof(psa_key_slot_number_t)]; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 202 | } psa_se_key_data_storage_t; |
| 203 | |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 204 | #endif /* PSA_CRYPTO_SE_H */ |