blob: 70d9f4f0f5880e812807f49e70746a64e83aa581 [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
21#ifndef PSA_CRYPTO_SE_H
22#define PSA_CRYPTO_SE_H
23
Bence Szépkútic662b362021-05-27 11:25:03 +020024#include "mbedtls/build_info.h"
Gilles Peskinea899a722019-06-24 14:06:43 +020025
26#include "psa/crypto.h"
27#include "psa/crypto_se_driver.h"
28
Gilles Peskine2b04f462020-05-10 00:44:04 +020029/** The maximum location value that this implementation supports
Gilles Peskine8b96cad2019-07-23 17:38:08 +020030 * for a secure element.
31 *
32 * This is not a characteristic that each PSA implementation has, but a
33 * limitation of the current implementation due to the constraints imposed
34 * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
35 *
Gilles Peskine2b04f462020-05-10 00:44:04 +020036 * The minimum location value for a secure element is 1, like on any
37 * PSA implementation (0 means a transparent key).
Gilles Peskine8b96cad2019-07-23 17:38:08 +020038 */
Gilles Peskine2b04f462020-05-10 00:44:04 +020039#define PSA_MAX_SE_LOCATION 255
Gilles Peskine8b96cad2019-07-23 17:38:08 +020040
41/** The base of the range of ITS file identifiers for secure element
42 * driver persistent data.
43 *
Ronald Cron27238fc2020-07-23 12:30:41 +020044 * We use a slice of the implementation reserved range 0xffff0000..0xffffffff,
Gilles Peskine8b96cad2019-07-23 17:38:08 +020045 * specifically the range 0xfffffe00..0xfffffeff. The length of this range
Gilles Peskine2b04f462020-05-10 00:44:04 +020046 * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
47 * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
48 * which doesn't have a driver.
Gilles Peskine8b96cad2019-07-23 17:38:08 +020049 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ((psa_key_id_t)0xfffffe00)
Gilles Peskine8b96cad2019-07-23 17:38:08 +020051
Gilles Peskine2b04f462020-05-10 00:44:04 +020052/** The maximum number of registered secure element driver locations. */
Gilles Peskinea899a722019-06-24 14:06:43 +020053#define PSA_MAX_SE_DRIVERS 4
54
Gilles Peskined0890212019-06-24 14:34:43 +020055/** Unregister all secure element drivers.
56 *
57 * \warning Do not call this function while the library is in the initialized
58 * state. This function is only intended to be called at the end
59 * of mbedtls_psa_crypto_free().
60 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061void psa_unregister_all_se_drivers(void);
Gilles Peskined0890212019-06-24 14:34:43 +020062
Gilles Peskined9348f22019-10-01 15:22:29 +020063/** Initialize all secure element drivers.
64 *
65 * Called from psa_crypto_init().
66 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067psa_status_t psa_init_all_se_drivers(void);
Gilles Peskined9348f22019-10-01 15:22:29 +020068
Gilles Peskinef989dbe2019-06-26 18:18:12 +020069/** A structure that describes a registered secure element driver.
70 *
71 * A secure element driver table entry contains a pointer to the
Gilles Peskine5243a202019-07-12 23:38:19 +020072 * driver's method table as well as the driver context structure.
Gilles Peskinef989dbe2019-06-26 18:18:12 +020073 */
74typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
75
Gilles Peskine5243a202019-07-12 23:38:19 +020076/** Return the secure element driver information for a lifetime value.
77 *
78 * \param lifetime The lifetime value to query.
79 * \param[out] p_methods On output, if there is a driver,
80 * \c *methods points to its method table.
81 * Otherwise \c *methods is \c NULL.
82 * \param[out] p_drv_context On output, if there is a driver,
83 * \c *drv_context points to its context
84 * structure.
85 * Otherwise \c *drv_context is \c NULL.
86 *
87 * \retval 1
88 * \p lifetime corresponds to a registered driver.
89 * \retval 0
90 * \p lifetime does not correspond to a registered driver.
91 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092int psa_get_se_driver(psa_key_lifetime_t lifetime,
93 const psa_drv_se_t **p_methods,
94 psa_drv_se_context_t **p_drv_context);
Gilles Peskine5243a202019-07-12 23:38:19 +020095
Gilles Peskinef989dbe2019-06-26 18:18:12 +020096/** Return the secure element driver table entry for a lifetime value.
97 *
98 * \param lifetime The lifetime value to query.
99 *
100 * \return The driver table entry for \p lifetime, or
101 * \p NULL if \p lifetime does not correspond to a registered driver.
102 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103psa_se_drv_table_entry_t *psa_get_se_driver_entry(psa_key_lifetime_t lifetime);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200104
105/** Return the method table for a secure element driver.
106 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200107 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200108 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200109 * \return The driver's method table.
110 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112const psa_drv_se_t *
113psa_get_se_driver_methods(const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200114
Gilles Peskine5243a202019-07-12 23:38:19 +0200115/** Return the context of a secure element driver.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200116 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200117 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200118 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200119 * \return A pointer to the driver context.
120 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200121 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122psa_drv_se_context_t *
123psa_get_se_driver_context(psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200124
Gilles Peskinecbaff462019-07-12 23:46:04 +0200125/** Find a free slot for a key that is to be created.
126 *
127 * This function calls the relevant method in the driver to find a suitable
128 * slot for a key with the given attributes.
129 *
130 * \param[in] attributes Metadata about the key that is about to be created.
131 * \param[in] driver The driver table entry to query.
132 * \param[out] slot_number On success, a slot number that is free in this
133 * secure element.
134 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135psa_status_t psa_find_se_slot_for_key(const psa_key_attributes_t *attributes,
136 psa_key_creation_method_t method,
137 psa_se_drv_table_entry_t *driver,
138 psa_key_slot_number_t *slot_number);
Gilles Peskinecbaff462019-07-12 23:46:04 +0200139
Gilles Peskine354f7672019-07-12 23:46:38 +0200140/** Destoy a key in a secure element.
141 *
142 * This function calls the relevant driver method to destroy a key
143 * and updates the driver's persistent data.
144 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver,
146 psa_key_slot_number_t slot_number);
Gilles Peskine354f7672019-07-12 23:46:38 +0200147
Gilles Peskine5243a202019-07-12 23:38:19 +0200148/** Load the persistent data of a secure element driver.
149 *
150 * \param driver The driver table entry containing the persistent
151 * data to load from storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100152 *
153 * \return #PSA_SUCCESS
154 * \return #PSA_ERROR_NOT_SUPPORTED
155 * \return #PSA_ERROR_DOES_NOT_EXIST
156 * \return #PSA_ERROR_STORAGE_FAILURE
157 * \return #PSA_ERROR_DATA_CORRUPT
158 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160psa_status_t
161psa_load_se_persistent_data(const psa_se_drv_table_entry_t *driver);
Gilles Peskine5243a202019-07-12 23:38:19 +0200162
163/** Save the persistent data of a secure element driver.
164 *
165 * \param[in] driver The driver table entry containing the persistent
166 * data to save to storage.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100167 *
168 * \return #PSA_SUCCESS
169 * \return #PSA_ERROR_NOT_SUPPORTED
170 * \return #PSA_ERROR_NOT_PERMITTED
171 * \return #PSA_ERROR_NOT_SUPPORTED
172 * \return #PSA_ERROR_INSUFFICIENT_STORAGE
173 * \return #PSA_ERROR_STORAGE_FAILURE
174 * \return #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine5243a202019-07-12 23:38:19 +0200175 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176psa_status_t
177psa_save_se_persistent_data(const psa_se_drv_table_entry_t *driver);
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200178
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200179/** Destroy the persistent data of a secure element driver.
180 *
181 * This is currently only used for testing.
182 *
Gilles Peskine2b04f462020-05-10 00:44:04 +0200183 * \param[in] location The location identifier for the driver whose
184 * persistent data is to be erased.
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200185 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186psa_status_t psa_destroy_se_persistent_data(psa_key_location_t location);
Gilles Peskineb46bef22019-07-30 21:32:04 +0200187
188/** The storage representation of a key whose data is in a secure element.
189 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190typedef struct {
191 uint8_t slot_number[sizeof(psa_key_slot_number_t)];
Gilles Peskineb46bef22019-07-30 21:32:04 +0200192} psa_se_key_data_storage_t;
193
Gilles Peskinea899a722019-06-24 14:06:43 +0200194#endif /* PSA_CRYPTO_SE_H */