blob: 87d2634e7382f9aaf8e226d794899b5fa714b7da [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinea899a722019-06-24 14:06:43 +020022
Gilles Peskinea8ade162019-06-26 11:24:49 +020023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinea899a722019-06-24 14:06:43 +020024
Gilles Peskine9717d102019-06-26 11:50:04 +020025#include <assert.h>
Gilles Peskine5243a202019-07-12 23:38:19 +020026#include <stdint.h>
Gilles Peskined0890212019-06-24 14:34:43 +020027#include <string.h>
28
Gilles Peskine5243a202019-07-12 23:38:19 +020029#include "psa/crypto_se_driver.h"
30
Gilles Peskinea899a722019-06-24 14:06:43 +020031#include "psa_crypto_se.h"
32
Gilles Peskine8b96cad2019-07-23 17:38:08 +020033#if defined(MBEDTLS_PSA_ITS_FILE_C)
34#include "psa_crypto_its.h"
35#else /* Native ITS implementation */
36#include "psa/error.h"
37#include "psa/internal_trusted_storage.h"
38#endif
39
Gilles Peskine5243a202019-07-12 23:38:19 +020040#include "mbedtls/platform.h"
Gilles Peskine5243a202019-07-12 23:38:19 +020041
42
43
Gilles Peskinef989dbe2019-06-26 18:18:12 +020044/****************************************************************/
45/* Driver lookup */
46/****************************************************************/
47
Gilles Peskine5243a202019-07-12 23:38:19 +020048/* This structure is identical to psa_drv_se_context_t declared in
49 * `crypto_se_driver.h`, except that some parts are writable here
50 * (non-const, or pointer to non-const). */
51typedef struct
52{
53 void *persistent_data;
54 size_t persistent_data_size;
55 uintptr_t transient_data;
56} psa_drv_se_internal_context_t;
57
Gilles Peskine01fd8752020-04-14 19:31:52 +020058struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020059{
Gilles Peskine2b04f462020-05-10 00:44:04 +020060 psa_key_location_t location;
Gilles Peskinea899a722019-06-24 14:06:43 +020061 const psa_drv_se_t *methods;
Gilles Peskine5243a202019-07-12 23:38:19 +020062 union
63 {
64 psa_drv_se_internal_context_t internal;
65 psa_drv_se_context_t context;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +020066 } u;
Gilles Peskine01fd8752020-04-14 19:31:52 +020067};
Gilles Peskinea899a722019-06-24 14:06:43 +020068
Gilles Peskinef989dbe2019-06-26 18:18:12 +020069static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
70
Gilles Peskine5243a202019-07-12 23:38:19 +020071psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +020072 psa_key_lifetime_t lifetime )
73{
74 size_t i;
Gilles Peskine2b04f462020-05-10 00:44:04 +020075 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime );
76 /* In the driver table, location=0 means an entry that isn't used.
77 * No driver has a location of 0 because it's a reserved value
78 * (which designates transparent keys). Make sure we never return
79 * a driver entry for location 0. */
80 if( location == 0 )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020081 return( NULL );
82 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
83 {
Gilles Peskine2b04f462020-05-10 00:44:04 +020084 if( driver_table[i].location == location )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020085 return( &driver_table[i] );
86 }
87 return( NULL );
88}
89
90const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +020091 const psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020092{
Gilles Peskine5243a202019-07-12 23:38:19 +020093 return( driver->methods );
Gilles Peskinef989dbe2019-06-26 18:18:12 +020094}
95
Gilles Peskine5243a202019-07-12 23:38:19 +020096psa_drv_se_context_t *psa_get_se_driver_context(
97 psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020098{
Gilles Peskine1a75d0c2020-04-14 19:33:25 +020099 return( &driver->u.context );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200100}
101
Gilles Peskine5243a202019-07-12 23:38:19 +0200102int psa_get_se_driver( psa_key_lifetime_t lifetime,
103 const psa_drv_se_t **p_methods,
104 psa_drv_se_context_t **p_drv_context)
105{
106 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
107 if( p_methods != NULL )
108 *p_methods = ( driver ? driver->methods : NULL );
109 if( p_drv_context != NULL )
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200110 *p_drv_context = ( driver ? &driver->u.context : NULL );
Gilles Peskine5243a202019-07-12 23:38:19 +0200111 return( driver != NULL );
112}
113
114
115
116/****************************************************************/
117/* Persistent data management */
118/****************************************************************/
119
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200120static psa_status_t psa_get_se_driver_its_file_uid(
121 const psa_se_drv_table_entry_t *driver,
122 psa_storage_uid_t *uid )
123{
Gilles Peskine2b04f462020-05-10 00:44:04 +0200124 if( driver->location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200125 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine573bbc12019-07-23 19:59:23 +0200126
127#if SIZE_MAX > UINT32_MAX
128 /* ITS file sizes are limited to 32 bits. */
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200129 if( driver->u.internal.persistent_data_size > UINT32_MAX )
Gilles Peskine573bbc12019-07-23 19:59:23 +0200130 return( PSA_ERROR_NOT_SUPPORTED );
131#endif
132
Gilles Peskine75c126b2019-07-24 15:56:01 +0200133 /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200134 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200135 return( PSA_SUCCESS );
136}
137
Gilles Peskine5243a202019-07-12 23:38:19 +0200138psa_status_t psa_load_se_persistent_data(
139 const psa_se_drv_table_entry_t *driver )
140{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200141 psa_status_t status;
142 psa_storage_uid_t uid;
Gilles Peskine8b663892019-07-31 17:57:57 +0200143 size_t length;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200144
145 status = psa_get_se_driver_its_file_uid( driver, &uid );
146 if( status != PSA_SUCCESS )
147 return( status );
148
Gilles Peskine8b663892019-07-31 17:57:57 +0200149 /* Read the amount of persistent data that the driver requests.
150 * If the data in storage is larger, it is truncated. If the data
151 * in storage is smaller, silently keep what is already at the end
152 * of the output buffer. */
Gilles Peskine75c126b2019-07-24 15:56:01 +0200153 /* psa_get_se_driver_its_file_uid ensures that the size_t
154 * persistent_data_size is in range, but compilers don't know that,
155 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200156 return( psa_its_get( uid, 0,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200157 (uint32_t) driver->u.internal.persistent_data_size,
158 driver->u.internal.persistent_data,
Gilles Peskine8b663892019-07-31 17:57:57 +0200159 &length ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200160}
161
162psa_status_t psa_save_se_persistent_data(
163 const psa_se_drv_table_entry_t *driver )
164{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200165 psa_status_t status;
166 psa_storage_uid_t uid;
167
168 status = psa_get_se_driver_its_file_uid( driver, &uid );
169 if( status != PSA_SUCCESS )
170 return( status );
171
Gilles Peskine75c126b2019-07-24 15:56:01 +0200172 /* psa_get_se_driver_its_file_uid ensures that the size_t
173 * persistent_data_size is in range, but compilers don't know that,
174 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200175 return( psa_its_set( uid,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200176 (uint32_t) driver->u.internal.persistent_data_size,
177 driver->u.internal.persistent_data,
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200178 0 ) );
179}
180
Gilles Peskine2b04f462020-05-10 00:44:04 +0200181psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200182{
183 psa_storage_uid_t uid;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200184 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200185 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200186 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200187 return( psa_its_remove( uid ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200188}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200189
Gilles Peskinecbaff462019-07-12 23:46:04 +0200190psa_status_t psa_find_se_slot_for_key(
191 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200192 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200193 psa_se_drv_table_entry_t *driver,
194 psa_key_slot_number_t *slot_number )
195{
196 psa_status_t status;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200197 psa_key_location_t key_location =
198 PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200199
Gilles Peskine2b04f462020-05-10 00:44:04 +0200200 /* If the location is wrong, it's a bug in the library. */
201 if( driver->location != key_location )
Gilles Peskinecbaff462019-07-12 23:46:04 +0200202 return( PSA_ERROR_CORRUPTION_DETECTED );
203
204 /* If the driver doesn't support key creation in any way, give up now. */
205 if( driver->methods->key_management == NULL )
206 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200207
Gilles Peskine46d94392019-08-05 14:55:50 +0200208 if( psa_get_key_slot_number( attributes, slot_number ) == PSA_SUCCESS )
209 {
210 /* The application wants to use a specific slot. Allow it if
211 * the driver supports it. On a system with isolation,
212 * the crypto service must check that the application is
213 * permitted to request this slot. */
214 psa_drv_se_validate_slot_number_t p_validate_slot_number =
215 driver->methods->key_management->p_validate_slot_number;
216 if( p_validate_slot_number == NULL )
217 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200218 status = p_validate_slot_number( &driver->u.context,
219 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200220 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200221 *slot_number );
222 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200223 else if( method == PSA_KEY_CREATION_REGISTER )
224 {
225 /* The application didn't specify a slot number. This doesn't
226 * make sense when registering a slot. */
227 return( PSA_ERROR_INVALID_ARGUMENT );
228 }
Gilles Peskine46d94392019-08-05 14:55:50 +0200229 else
230 {
231 /* The application didn't tell us which slot to use. Let the driver
232 * choose. This is the normal case. */
233 psa_drv_se_allocate_key_t p_allocate =
234 driver->methods->key_management->p_allocate;
235 if( p_allocate == NULL )
236 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200237 status = p_allocate( &driver->u.context,
238 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200239 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200240 slot_number );
241 }
Gilles Peskinecbaff462019-07-12 23:46:04 +0200242 return( status );
243}
244
Gilles Peskine354f7672019-07-12 23:46:38 +0200245psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
246 psa_key_slot_number_t slot_number )
247{
248 psa_status_t status;
249 psa_status_t storage_status;
Gilles Peskine340b1272019-07-25 14:13:24 +0200250 /* Normally a missing method would mean that the action is not
251 * supported. But psa_destroy_key() is not supposed to return
252 * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
253 * be able to destroy it. The only use case for a driver that
254 * does not have a way to destroy keys at all is if the keys are
255 * locked in a read-only state: we can use the keys but not
256 * destroy them. Hence, if the driver doesn't support destroying
257 * keys, it's really a lack of permission. */
Gilles Peskine354f7672019-07-12 23:46:38 +0200258 if( driver->methods->key_management == NULL ||
259 driver->methods->key_management->p_destroy == NULL )
260 return( PSA_ERROR_NOT_PERMITTED );
261 status = driver->methods->key_management->p_destroy(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200262 &driver->u.context,
263 driver->u.internal.persistent_data,
Gilles Peskine354f7672019-07-12 23:46:38 +0200264 slot_number );
265 storage_status = psa_save_se_persistent_data( driver );
266 return( status == PSA_SUCCESS ? storage_status : status );
267}
268
Gilles Peskined9348f22019-10-01 15:22:29 +0200269psa_status_t psa_init_all_se_drivers( void )
270{
271 size_t i;
272 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
273 {
274 psa_se_drv_table_entry_t *driver = &driver_table[i];
Gilles Peskine2b04f462020-05-10 00:44:04 +0200275 if( driver->location == 0 )
Gilles Peskined9348f22019-10-01 15:22:29 +0200276 continue; /* skipping unused entry */
277 const psa_drv_se_t *methods = psa_get_se_driver_methods( driver );
278 if( methods->p_init != NULL )
279 {
280 psa_status_t status = methods->p_init(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200281 &driver->u.context,
282 driver->u.internal.persistent_data,
Gilles Peskine2b04f462020-05-10 00:44:04 +0200283 driver->location );
Gilles Peskined9348f22019-10-01 15:22:29 +0200284 if( status != PSA_SUCCESS )
285 return( status );
Gilles Peskinec84c70a2019-10-01 15:41:42 +0200286 status = psa_save_se_persistent_data( driver );
287 if( status != PSA_SUCCESS )
288 return( status );
Gilles Peskined9348f22019-10-01 15:22:29 +0200289 }
290 }
291 return( PSA_SUCCESS );
292}
293
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200294
295
296/****************************************************************/
297/* Driver registration */
298/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200299
300psa_status_t psa_register_se_driver(
Gilles Peskine2b04f462020-05-10 00:44:04 +0200301 psa_key_location_t location,
Gilles Peskinea899a722019-06-24 14:06:43 +0200302 const psa_drv_se_t *methods)
303{
304 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200305 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200306
307 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
308 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200309 /* Driver table entries are 0-initialized. 0 is not a valid driver
Gilles Peskine2b04f462020-05-10 00:44:04 +0200310 * location because it means a transparent key. */
Gilles Peskine9717d102019-06-26 11:50:04 +0200311#if defined(static_assert)
Gilles Peskine2b04f462020-05-10 00:44:04 +0200312 static_assert( PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
313 "Secure element support requires 0 to mean a local key" );
Gilles Peskine9717d102019-06-26 11:50:04 +0200314#endif
Gilles Peskine2b04f462020-05-10 00:44:04 +0200315 if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
Gilles Peskinea899a722019-06-24 14:06:43 +0200316 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200317 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200318 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea899a722019-06-24 14:06:43 +0200319
320 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
321 {
Gilles Peskine2b04f462020-05-10 00:44:04 +0200322 if( driver_table[i].location == 0 )
Gilles Peskinea899a722019-06-24 14:06:43 +0200323 break;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200324 /* Check that location isn't already in use up to the first free
Gilles Peskinea899a722019-06-24 14:06:43 +0200325 * entry. Since entries are created in order and never deleted,
326 * there can't be a used entry after the first free entry. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200327 if( driver_table[i].location == location )
Gilles Peskinea899a722019-06-24 14:06:43 +0200328 return( PSA_ERROR_ALREADY_EXISTS );
329 }
330 if( i == PSA_MAX_SE_DRIVERS )
331 return( PSA_ERROR_INSUFFICIENT_MEMORY );
332
Gilles Peskine2b04f462020-05-10 00:44:04 +0200333 driver_table[i].location = location;
Gilles Peskinea899a722019-06-24 14:06:43 +0200334 driver_table[i].methods = methods;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200335 driver_table[i].u.internal.persistent_data_size =
Gilles Peskined5536d82019-10-01 16:55:29 +0200336 methods->persistent_data_size;
Gilles Peskine5243a202019-07-12 23:38:19 +0200337
338 if( methods->persistent_data_size != 0 )
339 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200340 driver_table[i].u.internal.persistent_data =
Gilles Peskine5243a202019-07-12 23:38:19 +0200341 mbedtls_calloc( 1, methods->persistent_data_size );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200342 if( driver_table[i].u.internal.persistent_data == NULL )
Gilles Peskine5243a202019-07-12 23:38:19 +0200343 {
344 status = PSA_ERROR_INSUFFICIENT_MEMORY;
345 goto error;
346 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200347 /* Load the driver's persistent data. On first use, the persistent
348 * data does not exist in storage, and is initialized to
349 * all-bits-zero by the calloc call just above. */
Gilles Peskine5243a202019-07-12 23:38:19 +0200350 status = psa_load_se_persistent_data( &driver_table[i] );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200351 if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
Gilles Peskine5243a202019-07-12 23:38:19 +0200352 goto error;
353 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200354
Gilles Peskinea899a722019-06-24 14:06:43 +0200355 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200356
357error:
358 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
359 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200360}
361
Gilles Peskined0890212019-06-24 14:34:43 +0200362void psa_unregister_all_se_drivers( void )
363{
Gilles Peskine5243a202019-07-12 23:38:19 +0200364 size_t i;
365 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
366 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200367 if( driver_table[i].u.internal.persistent_data != NULL )
368 mbedtls_free( driver_table[i].u.internal.persistent_data );
Gilles Peskine5243a202019-07-12 23:38:19 +0200369 }
Gilles Peskined0890212019-06-24 14:34:43 +0200370 memset( driver_table, 0, sizeof( driver_table ) );
371}
372
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200373
374
375/****************************************************************/
376/* The end */
377/****************************************************************/
378
Gilles Peskinea8ade162019-06-26 11:24:49 +0200379#endif /* MBEDTLS_PSA_CRYPTO_SE_C */