blob: e0bd5acfb3abec75cda84591bf9e628f61536579 [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 Rodgman16799db2023-11-02 19:47:20 +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
Ronald Cron7871cb12023-10-10 08:51:39 +020012/*
13 * Include the build-time configuration information header. Here, we do not
14 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
15 * is basically just an alias to it. This is to ease the maintenance of the
16 * TF-PSA-Crypto repository which has a different build system and
17 * configuration.
18 */
19#include "psa/build_info.h"
Gilles Peskinea899a722019-06-24 14:06:43 +020020
21#include "psa/crypto.h"
22#include "psa/crypto_se_driver.h"
23
Gilles Peskine2b04f462020-05-10 00:44:04 +020024/** The maximum location value that this implementation supports
Gilles Peskine8b96cad2019-07-23 17:38:08 +020025 * for a secure element.
26 *
27 * This is not a characteristic that each PSA implementation has, but a
28 * limitation of the current implementation due to the constraints imposed
29 * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
30 *
Gilles Peskine2b04f462020-05-10 00:44:04 +020031 * The minimum location value for a secure element is 1, like on any
32 * PSA implementation (0 means a transparent key).
Gilles Peskine8b96cad2019-07-23 17:38:08 +020033 */
Gilles Peskine2b04f462020-05-10 00:44:04 +020034#define PSA_MAX_SE_LOCATION 255
Gilles Peskine8b96cad2019-07-23 17:38:08 +020035
36/** The base of the range of ITS file identifiers for secure element
37 * driver persistent data.
38 *
Ronald Cron27238fc2020-07-23 12:30:41 +020039 * We use a slice of the implementation reserved range 0xffff0000..0xffffffff,
Gilles Peskine8b96cad2019-07-23 17:38:08 +020040 * specifically the range 0xfffffe00..0xfffffeff. The length of this range
Gilles Peskine2b04f462020-05-10 00:44:04 +020041 * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
42 * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
43 * which doesn't have a driver.
Gilles Peskine8b96cad2019-07-23 17:38:08 +020044 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ((psa_key_id_t) 0xfffffe00)
Gilles Peskine8b96cad2019-07-23 17:38:08 +020046
Gilles Peskine2b04f462020-05-10 00:44:04 +020047/** The maximum number of registered secure element driver locations. */
Gilles Peskinea899a722019-06-24 14:06:43 +020048#define PSA_MAX_SE_DRIVERS 4
49
Gilles Peskined0890212019-06-24 14:34:43 +020050/** Unregister all secure element drivers.
51 *
52 * \warning Do not call this function while the library is in the initialized
53 * state. This function is only intended to be called at the end
54 * of mbedtls_psa_crypto_free().
55 */
Gilles Peskine449bd832023-01-11 14:50:10 +010056void psa_unregister_all_se_drivers(void);
Gilles Peskined0890212019-06-24 14:34:43 +020057
Gilles Peskined9348f22019-10-01 15:22:29 +020058/** Initialize all secure element drivers.
59 *
60 * Called from psa_crypto_init().
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062psa_status_t psa_init_all_se_drivers(void);
Gilles Peskined9348f22019-10-01 15:22:29 +020063
Gilles Peskinef989dbe2019-06-26 18:18:12 +020064/** A structure that describes a registered secure element driver.
65 *
66 * A secure element driver table entry contains a pointer to the
Gilles Peskine5243a202019-07-12 23:38:19 +020067 * driver's method table as well as the driver context structure.
Gilles Peskinef989dbe2019-06-26 18:18:12 +020068 */
69typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
70
Gilles Peskine5243a202019-07-12 23:38:19 +020071/** Return the secure element driver information for a lifetime value.
72 *
73 * \param lifetime The lifetime value to query.
74 * \param[out] p_methods On output, if there is a driver,
75 * \c *methods points to its method table.
76 * Otherwise \c *methods is \c NULL.
77 * \param[out] p_drv_context On output, if there is a driver,
78 * \c *drv_context points to its context
79 * structure.
80 * Otherwise \c *drv_context is \c NULL.
81 *
82 * \retval 1
83 * \p lifetime corresponds to a registered driver.
84 * \retval 0
85 * \p lifetime does not correspond to a registered driver.
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087int psa_get_se_driver(psa_key_lifetime_t lifetime,
88 const psa_drv_se_t **p_methods,
89 psa_drv_se_context_t **p_drv_context);
Gilles Peskine5243a202019-07-12 23:38:19 +020090
Gilles Peskinef989dbe2019-06-26 18:18:12 +020091/** Return the secure element driver table entry for a lifetime value.
92 *
93 * \param lifetime The lifetime value to query.
94 *
95 * \return The driver table entry for \p lifetime, or
96 * \p NULL if \p lifetime does not correspond to a registered driver.
97 */
Gilles Peskine5243a202019-07-12 23:38:19 +020098psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskine449bd832023-01-11 14:50:10 +010099 psa_key_lifetime_t lifetime);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200100
101/** Return the method table for a secure element driver.
102 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200103 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200104 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200105 * \return The driver's method table.
106 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200107 */
108const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200110
Gilles Peskine5243a202019-07-12 23:38:19 +0200111/** Return the context of a secure element driver.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200112 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200113 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200114 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200115 * \return A pointer to the driver context.
116 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200117 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200118psa_drv_se_context_t *psa_get_se_driver_context(
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200120
Gilles Peskinecbaff462019-07-12 23:46:04 +0200121/** Find a free slot for a key that is to be created.
122 *
123 * This function calls the relevant method in the driver to find a suitable
124 * slot for a key with the given attributes.
125 *
126 * \param[in] attributes Metadata about the key that is about to be created.
127 * \param[in] driver The driver table entry to query.
128 * \param[out] slot_number On success, a slot number that is free in this
129 * secure element.
130 */
131psa_status_t psa_find_se_slot_for_key(
132 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200133 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200134 psa_se_drv_table_entry_t *driver,
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 psa_key_slot_number_t *slot_number);
Gilles Peskinecbaff462019-07-12 23:46:04 +0200136
Tom Cosgrove1797b052022-12-04 17:19:59 +0000137/** Destroy a key in a secure element.
Gilles Peskine354f7672019-07-12 23:46:38 +0200138 *
139 * This function calls the relevant driver method to destroy a key
140 * and updates the driver's persistent data.
141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver,
143 psa_key_slot_number_t slot_number);
Gilles Peskine354f7672019-07-12 23:46:38 +0200144
Gilles Peskine5243a202019-07-12 23:38:19 +0200145/** Load the persistent data of a secure element driver.
146 *
147 * \param driver The driver table entry containing the persistent
148 * data to load from storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100149 *
150 * \return #PSA_SUCCESS
151 * \return #PSA_ERROR_NOT_SUPPORTED
152 * \return #PSA_ERROR_DOES_NOT_EXIST
153 * \return #PSA_ERROR_STORAGE_FAILURE
154 * \return #PSA_ERROR_DATA_CORRUPT
155 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200156 */
157psa_status_t psa_load_se_persistent_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 const psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200159
160/** Save the persistent data of a secure element driver.
161 *
162 * \param[in] driver The driver table entry containing the persistent
163 * data to save to storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100164 *
165 * \return #PSA_SUCCESS
166 * \return #PSA_ERROR_NOT_SUPPORTED
167 * \return #PSA_ERROR_NOT_PERMITTED
168 * \return #PSA_ERROR_NOT_SUPPORTED
169 * \return #PSA_ERROR_INSUFFICIENT_STORAGE
170 * \return #PSA_ERROR_STORAGE_FAILURE
171 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200172 */
173psa_status_t psa_save_se_persistent_data(
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200175
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200176/** Destroy the persistent data of a secure element driver.
177 *
178 * This is currently only used for testing.
179 *
Gilles Peskine2b04f462020-05-10 00:44:04 +0200180 * \param[in] location The location identifier for the driver whose
181 * persistent data is to be erased.
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183psa_status_t psa_destroy_se_persistent_data(psa_key_location_t location);
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200184
Gilles Peskineb46bef22019-07-30 21:32:04 +0200185
186/** The storage representation of a key whose data is in a secure element.
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188typedef struct {
189 uint8_t slot_number[sizeof(psa_key_slot_number_t)];
Gilles Peskineb46bef22019-07-30 21:32:04 +0200190} psa_se_key_data_storage_t;
191
Gilles Peskinea899a722019-06-24 14:06:43 +0200192#endif /* PSA_CRYPTO_SE_H */