blob: 14a700056c00c926dc3b8374fe10275bf3efb061 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinea899a722019-06-24 14:06:43 +02007 */
8
9#ifndef PSA_CRYPTO_SE_H
10#define PSA_CRYPTO_SE_H
11
12#if !defined(MBEDTLS_CONFIG_FILE)
13#include "mbedtls/config.h"
14#else
15#include MBEDTLS_CONFIG_FILE
16#endif
17
18#include "psa/crypto.h"
19#include "psa/crypto_se_driver.h"
20
Gilles Peskine2b04f462020-05-10 00:44:04 +020021/** The maximum location value that this implementation supports
Gilles Peskine8b96cad2019-07-23 17:38:08 +020022 * for a secure element.
23 *
24 * This is not a characteristic that each PSA implementation has, but a
25 * limitation of the current implementation due to the constraints imposed
26 * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
27 *
Gilles Peskine2b04f462020-05-10 00:44:04 +020028 * The minimum location value for a secure element is 1, like on any
29 * PSA implementation (0 means a transparent key).
Gilles Peskine8b96cad2019-07-23 17:38:08 +020030 */
Gilles Peskine2b04f462020-05-10 00:44:04 +020031#define PSA_MAX_SE_LOCATION 255
Gilles Peskine8b96cad2019-07-23 17:38:08 +020032
33/** The base of the range of ITS file identifiers for secure element
34 * driver persistent data.
35 *
Ronald Cron27238fc2020-07-23 12:30:41 +020036 * We use a slice of the implementation reserved range 0xffff0000..0xffffffff,
Gilles Peskine8b96cad2019-07-23 17:38:08 +020037 * specifically the range 0xfffffe00..0xfffffeff. The length of this range
Gilles Peskine2b04f462020-05-10 00:44:04 +020038 * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
39 * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
40 * which doesn't have a driver.
Gilles Peskine8b96cad2019-07-23 17:38:08 +020041 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ((psa_key_id_t) 0xfffffe00)
Gilles Peskine8b96cad2019-07-23 17:38:08 +020043
Gilles Peskine2b04f462020-05-10 00:44:04 +020044/** The maximum number of registered secure element driver locations. */
Gilles Peskinea899a722019-06-24 14:06:43 +020045#define PSA_MAX_SE_DRIVERS 4
46
Gilles Peskined0890212019-06-24 14:34:43 +020047/** Unregister all secure element drivers.
48 *
49 * \warning Do not call this function while the library is in the initialized
50 * state. This function is only intended to be called at the end
51 * of mbedtls_psa_crypto_free().
52 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void psa_unregister_all_se_drivers(void);
Gilles Peskined0890212019-06-24 14:34:43 +020054
Gilles Peskined9348f22019-10-01 15:22:29 +020055/** Initialize all secure element drivers.
56 *
57 * Called from psa_crypto_init().
58 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059psa_status_t psa_init_all_se_drivers(void);
Gilles Peskined9348f22019-10-01 15:22:29 +020060
Gilles Peskinef989dbe2019-06-26 18:18:12 +020061/** A structure that describes a registered secure element driver.
62 *
63 * A secure element driver table entry contains a pointer to the
Gilles Peskine5243a202019-07-12 23:38:19 +020064 * driver's method table as well as the driver context structure.
Gilles Peskinef989dbe2019-06-26 18:18:12 +020065 */
66typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
67
Gilles Peskine5243a202019-07-12 23:38:19 +020068/** Return the secure element driver information for a lifetime value.
69 *
70 * \param lifetime The lifetime value to query.
71 * \param[out] p_methods On output, if there is a driver,
72 * \c *methods points to its method table.
73 * Otherwise \c *methods is \c NULL.
74 * \param[out] p_drv_context On output, if there is a driver,
75 * \c *drv_context points to its context
76 * structure.
77 * Otherwise \c *drv_context is \c NULL.
78 *
79 * \retval 1
80 * \p lifetime corresponds to a registered driver.
81 * \retval 0
82 * \p lifetime does not correspond to a registered driver.
83 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084int psa_get_se_driver(psa_key_lifetime_t lifetime,
85 const psa_drv_se_t **p_methods,
86 psa_drv_se_context_t **p_drv_context);
Gilles Peskine5243a202019-07-12 23:38:19 +020087
Gilles Peskinef989dbe2019-06-26 18:18:12 +020088/** Return the secure element driver table entry for a lifetime value.
89 *
90 * \param lifetime The lifetime value to query.
91 *
92 * \return The driver table entry for \p lifetime, or
93 * \p NULL if \p lifetime does not correspond to a registered driver.
94 */
Gilles Peskine5243a202019-07-12 23:38:19 +020095psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 psa_key_lifetime_t lifetime);
Gilles Peskinef989dbe2019-06-26 18:18:12 +020097
98/** Return the method table for a secure element driver.
99 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200100 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200101 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200102 * \return The driver's method table.
103 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200104 */
105const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200107
Gilles Peskine5243a202019-07-12 23:38:19 +0200108/** Return the context of a secure element driver.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200109 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200110 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200111 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200112 * \return A pointer to the driver context.
113 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200114 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200115psa_drv_se_context_t *psa_get_se_driver_context(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200117
Gilles Peskinecbaff462019-07-12 23:46:04 +0200118/** Find a free slot for a key that is to be created.
119 *
120 * This function calls the relevant method in the driver to find a suitable
121 * slot for a key with the given attributes.
122 *
123 * \param[in] attributes Metadata about the key that is about to be created.
124 * \param[in] driver The driver table entry to query.
125 * \param[out] slot_number On success, a slot number that is free in this
126 * secure element.
127 */
128psa_status_t psa_find_se_slot_for_key(
129 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200130 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200131 psa_se_drv_table_entry_t *driver,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 psa_key_slot_number_t *slot_number);
Gilles Peskinecbaff462019-07-12 23:46:04 +0200133
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000134/** Destroy a key in a secure element.
Gilles Peskine354f7672019-07-12 23:46:38 +0200135 *
136 * This function calls the relevant driver method to destroy a key
137 * and updates the driver's persistent data.
138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver,
140 psa_key_slot_number_t slot_number);
Gilles Peskine354f7672019-07-12 23:46:38 +0200141
Gilles Peskine5243a202019-07-12 23:38:19 +0200142/** Load the persistent data of a secure element driver.
143 *
144 * \param driver The driver table entry containing the persistent
145 * data to load from storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100146 *
147 * \return #PSA_SUCCESS
148 * \return #PSA_ERROR_NOT_SUPPORTED
149 * \return #PSA_ERROR_DOES_NOT_EXIST
150 * \return #PSA_ERROR_STORAGE_FAILURE
151 * \return #PSA_ERROR_DATA_CORRUPT
152 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200153 */
154psa_status_t psa_load_se_persistent_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 const psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200156
157/** Save the persistent data of a secure element driver.
158 *
159 * \param[in] driver The driver table entry containing the persistent
160 * data to save to storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100161 *
162 * \return #PSA_SUCCESS
163 * \return #PSA_ERROR_NOT_SUPPORTED
164 * \return #PSA_ERROR_NOT_PERMITTED
165 * \return #PSA_ERROR_NOT_SUPPORTED
166 * \return #PSA_ERROR_INSUFFICIENT_STORAGE
167 * \return #PSA_ERROR_STORAGE_FAILURE
168 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200169 */
170psa_status_t psa_save_se_persistent_data(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200172
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200173/** Destroy the persistent data of a secure element driver.
174 *
175 * This is currently only used for testing.
176 *
Gilles Peskine2b04f462020-05-10 00:44:04 +0200177 * \param[in] location The location identifier for the driver whose
178 * persistent data is to be erased.
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200179 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180psa_status_t psa_destroy_se_persistent_data(psa_key_location_t location);
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200181
Gilles Peskineb46bef22019-07-30 21:32:04 +0200182
183/** The storage representation of a key whose data is in a secure element.
184 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185typedef struct {
186 uint8_t slot_number[sizeof(psa_key_slot_number_t)];
Gilles Peskineb46bef22019-07-30 21:32:04 +0200187} psa_se_key_data_storage_t;
188
Gilles Peskinea899a722019-06-24 14:06:43 +0200189#endif /* PSA_CRYPTO_SE_H */