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