blob: c1450656c7484a869bd3ba47a15dec500c0982b1 [file] [log] [blame]
Gilles Peskinea899a722019-06-24 14:06:43 +02001/*
2 * PSA crypto support for secure element drivers
3 */
4/* Copyright (C) 2019, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of Mbed TLS (https://tls.mbed.org)
20 */
21
22#ifndef PSA_CRYPTO_SE_H
23#define PSA_CRYPTO_SE_H
24
25#if !defined(MBEDTLS_CONFIG_FILE)
26#include "mbedtls/config.h"
27#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
31#include "psa/crypto.h"
32#include "psa/crypto_se_driver.h"
33
Gilles Peskine2b04f462020-05-10 00:44:04 +020034/** The maximum location value that this implementation supports
Gilles Peskine8b96cad2019-07-23 17:38:08 +020035 * for a secure element.
36 *
37 * This is not a characteristic that each PSA implementation has, but a
38 * limitation of the current implementation due to the constraints imposed
39 * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
40 *
Gilles Peskine2b04f462020-05-10 00:44:04 +020041 * The minimum location value for a secure element is 1, like on any
42 * PSA implementation (0 means a transparent key).
Gilles Peskine8b96cad2019-07-23 17:38:08 +020043 */
Gilles Peskine2b04f462020-05-10 00:44:04 +020044#define PSA_MAX_SE_LOCATION 255
Gilles Peskine8b96cad2019-07-23 17:38:08 +020045
46/** The base of the range of ITS file identifiers for secure element
47 * driver persistent data.
48 *
49 * We use a slice of the implemenation reserved range 0xffff0000..0xffffffff,
50 * specifically the range 0xfffffe00..0xfffffeff. The length of this range
Gilles Peskine2b04f462020-05-10 00:44:04 +020051 * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
52 * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
53 * which doesn't have a driver.
Gilles Peskine8b96cad2019-07-23 17:38:08 +020054 */
55#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ( (psa_key_id_t) 0xfffffe00 )
56
Gilles Peskine2b04f462020-05-10 00:44:04 +020057/** The maximum number of registered secure element driver locations. */
Gilles Peskinea899a722019-06-24 14:06:43 +020058#define PSA_MAX_SE_DRIVERS 4
59
Gilles Peskined0890212019-06-24 14:34:43 +020060/** Unregister all secure element drivers.
61 *
62 * \warning Do not call this function while the library is in the initialized
63 * state. This function is only intended to be called at the end
64 * of mbedtls_psa_crypto_free().
65 */
66void psa_unregister_all_se_drivers( void );
67
Gilles Peskined9348f22019-10-01 15:22:29 +020068/** Initialize all secure element drivers.
69 *
70 * Called from psa_crypto_init().
71 */
72psa_status_t psa_init_all_se_drivers( void );
73
Gilles Peskinef989dbe2019-06-26 18:18:12 +020074/** A structure that describes a registered secure element driver.
75 *
76 * A secure element driver table entry contains a pointer to the
Gilles Peskine5243a202019-07-12 23:38:19 +020077 * driver's method table as well as the driver context structure.
Gilles Peskinef989dbe2019-06-26 18:18:12 +020078 */
79typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
80
Gilles Peskine5243a202019-07-12 23:38:19 +020081/** Return the secure element driver information for a lifetime value.
82 *
83 * \param lifetime The lifetime value to query.
84 * \param[out] p_methods On output, if there is a driver,
85 * \c *methods points to its method table.
86 * Otherwise \c *methods is \c NULL.
87 * \param[out] p_drv_context On output, if there is a driver,
88 * \c *drv_context points to its context
89 * structure.
90 * Otherwise \c *drv_context is \c NULL.
91 *
92 * \retval 1
93 * \p lifetime corresponds to a registered driver.
94 * \retval 0
95 * \p lifetime does not correspond to a registered driver.
96 */
97int psa_get_se_driver( psa_key_lifetime_t lifetime,
98 const psa_drv_se_t **p_methods,
99 psa_drv_se_context_t **p_drv_context);
100
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200101/** Return the secure element driver table entry for a lifetime value.
102 *
103 * \param lifetime The lifetime value to query.
104 *
105 * \return The driver table entry for \p lifetime, or
106 * \p NULL if \p lifetime does not correspond to a registered driver.
107 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200108psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200109 psa_key_lifetime_t lifetime );
110
111/** Return the method table for a secure element driver.
112 *
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 The driver's method table.
116 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200117 */
118const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +0200119 const psa_se_drv_table_entry_t *driver );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200120
Gilles Peskine5243a202019-07-12 23:38:19 +0200121/** Return the context of a secure element driver.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200122 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200123 * \param[in] driver The driver table entry to access, or \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200124 *
Gilles Peskine5243a202019-07-12 23:38:19 +0200125 * \return A pointer to the driver context.
126 * \c NULL if \p driver is \c NULL.
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200127 */
Gilles Peskine5243a202019-07-12 23:38:19 +0200128psa_drv_se_context_t *psa_get_se_driver_context(
129 psa_se_drv_table_entry_t *driver );
130
Gilles Peskinecbaff462019-07-12 23:46:04 +0200131/** Find a free slot for a key that is to be created.
132 *
133 * This function calls the relevant method in the driver to find a suitable
134 * slot for a key with the given attributes.
135 *
136 * \param[in] attributes Metadata about the key that is about to be created.
137 * \param[in] driver The driver table entry to query.
138 * \param[out] slot_number On success, a slot number that is free in this
139 * secure element.
140 */
141psa_status_t psa_find_se_slot_for_key(
142 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200143 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200144 psa_se_drv_table_entry_t *driver,
145 psa_key_slot_number_t *slot_number );
146
Gilles Peskine354f7672019-07-12 23:46:38 +0200147/** Destoy a key in a secure element.
148 *
149 * This function calls the relevant driver method to destroy a key
150 * and updates the driver's persistent data.
151 */
152psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
153 psa_key_slot_number_t slot_number );
154
Gilles Peskine5243a202019-07-12 23:38:19 +0200155/** Load the persistent data of a secure element driver.
156 *
157 * \param driver The driver table entry containing the persistent
158 * data to load from storage.
159 */
160psa_status_t psa_load_se_persistent_data(
161 const psa_se_drv_table_entry_t *driver );
162
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.
167 */
168psa_status_t psa_save_se_persistent_data(
169 const psa_se_drv_table_entry_t *driver );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200170
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200171/** Destroy the persistent data of a secure element driver.
172 *
173 * This is currently only used for testing.
174 *
Gilles Peskine2b04f462020-05-10 00:44:04 +0200175 * \param[in] location The location identifier for the driver whose
176 * persistent data is to be erased.
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200177 */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200178psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200179
Gilles Peskineb46bef22019-07-30 21:32:04 +0200180
181/** The storage representation of a key whose data is in a secure element.
182 */
183typedef struct
184{
185 uint8_t slot_number[sizeof( psa_key_slot_number_t )];
186 uint8_t bits[sizeof( psa_key_bits_t )];
187} psa_se_key_data_storage_t;
188
Gilles Peskinea899a722019-06-24 14:06:43 +0200189#endif /* PSA_CRYPTO_SE_H */