blob: 3c29b12892be662d81a41feb1e5b9c4510d33986 [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/*
5 * Copyright (C) 2019, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of Mbed TLS (https://tls.mbed.org)
21 */
22
23#ifndef PSA_CRYPTO_SE_H
24#define PSA_CRYPTO_SE_H
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "mbedtls/config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32#include "psa/crypto.h"
33#include "psa/crypto_se_driver.h"
34
Gilles Peskine2b04f462020-05-10 00:44:04 +020035/** The maximum location value that this implementation supports
Gilles Peskine8b96cad2019-07-23 17:38:08 +020036 * for a secure element.
37 *
38 * This is not a characteristic that each PSA implementation has, but a
39 * limitation of the current implementation due to the constraints imposed
40 * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
41 *
Gilles Peskine2b04f462020-05-10 00:44:04 +020042 * The minimum location value for a secure element is 1, like on any
43 * PSA implementation (0 means a transparent key).
Gilles Peskine8b96cad2019-07-23 17:38:08 +020044 */
Gilles Peskine2b04f462020-05-10 00:44:04 +020045#define PSA_MAX_SE_LOCATION 255
Gilles Peskine8b96cad2019-07-23 17:38:08 +020046
47/** The base of the range of ITS file identifiers for secure element
48 * driver persistent data.
49 *
50 * We use a slice of the implemenation reserved range 0xffff0000..0xffffffff,
51 * specifically the range 0xfffffe00..0xfffffeff. The length of this range
Gilles Peskine2b04f462020-05-10 00:44:04 +020052 * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
53 * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
54 * which doesn't have a driver.
Gilles Peskine8b96cad2019-07-23 17:38:08 +020055 */
56#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ( (psa_key_id_t) 0xfffffe00 )
57
Gilles Peskine2b04f462020-05-10 00:44:04 +020058/** The maximum number of registered secure element driver locations. */
Gilles Peskinea899a722019-06-24 14:06:43 +020059#define PSA_MAX_SE_DRIVERS 4
60
Gilles Peskined0890212019-06-24 14:34:43 +020061/** Unregister all secure element drivers.
62 *
63 * \warning Do not call this function while the library is in the initialized
64 * state. This function is only intended to be called at the end
65 * of mbedtls_psa_crypto_free().
66 */
67void psa_unregister_all_se_drivers( void );
68
Gilles Peskined9348f22019-10-01 15:22:29 +020069/** Initialize all secure element drivers.
70 *
71 * Called from psa_crypto_init().
72 */
73psa_status_t psa_init_all_se_drivers( void );
74
Gilles Peskinef989dbe2019-06-26 18:18:12 +020075/** A structure that describes a registered secure element driver.
76 *
77 * A secure element driver table entry contains a pointer to the
Gilles Peskine5243a202019-07-12 23:38:19 +020078 * driver's method table as well as the driver context structure.
Gilles Peskinef989dbe2019-06-26 18:18:12 +020079 */
80typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
81
Gilles Peskine5243a202019-07-12 23:38:19 +020082/** Return the secure element driver information for a lifetime value.
83 *
84 * \param lifetime The lifetime value to query.
85 * \param[out] p_methods On output, if there is a driver,
86 * \c *methods points to its method table.
87 * Otherwise \c *methods is \c NULL.
88 * \param[out] p_drv_context On output, if there is a driver,
89 * \c *drv_context points to its context
90 * structure.
91 * Otherwise \c *drv_context is \c NULL.
92 *
93 * \retval 1
94 * \p lifetime corresponds to a registered driver.
95 * \retval 0
96 * \p lifetime does not correspond to a registered driver.
97 */
98int psa_get_se_driver( psa_key_lifetime_t lifetime,
99 const psa_drv_se_t **p_methods,
100 psa_drv_se_context_t **p_drv_context);
101
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200102/** Return the secure element driver table entry for a lifetime value.
103 *
104 * \param lifetime The lifetime value to query.
105 *
106 * \return The driver table entry for \p lifetime, or
107 * \p NULL if \p lifetime does not correspond to a registered driver.
108 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200109psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200110 psa_key_lifetime_t lifetime );
111
112/** Return the method table for a secure element driver.
113 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200114 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200115 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200116 * \return The driver's method table.
117 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200118 */
119const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +0200120 const psa_se_drv_table_entry_t *driver );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200121
Gilles Peskine5243a202019-07-12 23:38:19 +0200122/** Return the context of a secure element driver.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200123 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200124 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200125 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200126 * \return A pointer to the driver context.
127 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200128 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200129psa_drv_se_context_t *psa_get_se_driver_context(
130 psa_se_drv_table_entry_t *driver );
131
Gilles Peskinecbaff462019-07-12 23:46:04 +0200132/** Find a free slot for a key that is to be created.
133 *
134 * This function calls the relevant method in the driver to find a suitable
135 * slot for a key with the given attributes.
136 *
137 * \param[in] attributes Metadata about the key that is about to be created.
138 * \param[in] driver The driver table entry to query.
139 * \param[out] slot_number On success, a slot number that is free in this
140 * secure element.
141 */
142psa_status_t psa_find_se_slot_for_key(
143 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200144 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200145 psa_se_drv_table_entry_t *driver,
146 psa_key_slot_number_t *slot_number );
147
Gilles Peskine354f7672019-07-12 23:46:38 +0200148/** Destoy a key in a secure element.
149 *
150 * This function calls the relevant driver method to destroy a key
151 * and updates the driver's persistent data.
152 */
153psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
154 psa_key_slot_number_t slot_number );
155
Gilles Peskine5243a202019-07-12 23:38:19 +0200156/** Load the persistent data of a secure element driver.
157 *
158 * \param driver The driver table entry containing the persistent
159 * data to load from storage.
160 */
161psa_status_t psa_load_se_persistent_data(
162 const psa_se_drv_table_entry_t *driver );
163
164/** Save the persistent data of a secure element driver.
165 *
166 * \param[in] driver The driver table entry containing the persistent
167 * data to save to storage.
168 */
169psa_status_t psa_save_se_persistent_data(
170 const psa_se_drv_table_entry_t *driver );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200171
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200172/** Destroy the persistent data of a secure element driver.
173 *
174 * This is currently only used for testing.
175 *
Gilles Peskine2b04f462020-05-10 00:44:04 +0200176 * \param[in] location The location identifier for the driver whose
177 * persistent data is to be erased.
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200178 */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200179psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200180
Gilles Peskineb46bef22019-07-30 21:32:04 +0200181
182/** The storage representation of a key whose data is in a secure element.
183 */
184typedef struct
185{
186 uint8_t slot_number[sizeof( psa_key_slot_number_t )];
187 uint8_t bits[sizeof( psa_key_bits_t )];
188} psa_se_key_data_storage_t;
189
Gilles Peskinea899a722019-06-24 14:06:43 +0200190#endif /* PSA_CRYPTO_SE_H */