blob: 61e6c98d20d991a7adbbf87a7ba580b5abd17a6c [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskinea899a722019-06-24 14:06:43 +020024
Gilles Peskinea8ade162019-06-26 11:24:49 +020025#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinea899a722019-06-24 14:06:43 +020026
Gilles Peskine9717d102019-06-26 11:50:04 +020027#include <assert.h>
Gilles Peskine5243a202019-07-12 23:38:19 +020028#include <stdint.h>
Gilles Peskined0890212019-06-24 14:34:43 +020029#include <string.h>
30
Gilles Peskine5243a202019-07-12 23:38:19 +020031#include "psa/crypto_se_driver.h"
32
Gilles Peskinea899a722019-06-24 14:06:43 +020033#include "psa_crypto_se.h"
34
Gilles Peskine8b96cad2019-07-23 17:38:08 +020035#if defined(MBEDTLS_PSA_ITS_FILE_C)
36#include "psa_crypto_its.h"
37#else /* Native ITS implementation */
38#include "psa/error.h"
39#include "psa/internal_trusted_storage.h"
40#endif
41
Gilles Peskine5243a202019-07-12 23:38:19 +020042#include "mbedtls/platform.h"
43#if !defined(MBEDTLS_PLATFORM_C)
44#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
48
49
Gilles Peskinef989dbe2019-06-26 18:18:12 +020050/****************************************************************/
51/* Driver lookup */
52/****************************************************************/
53
Gilles Peskine5243a202019-07-12 23:38:19 +020054/* This structure is identical to psa_drv_se_context_t declared in
55 * `crypto_se_driver.h`, except that some parts are writable here
56 * (non-const, or pointer to non-const). */
57typedef struct
58{
59 void *persistent_data;
60 size_t persistent_data_size;
61 uintptr_t transient_data;
62} psa_drv_se_internal_context_t;
63
Gilles Peskine01fd8752020-04-14 19:31:52 +020064struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020065{
Gilles Peskine2b04f462020-05-10 00:44:04 +020066 psa_key_location_t location;
Gilles Peskinea899a722019-06-24 14:06:43 +020067 const psa_drv_se_t *methods;
Gilles Peskine5243a202019-07-12 23:38:19 +020068 union
69 {
70 psa_drv_se_internal_context_t internal;
71 psa_drv_se_context_t context;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +020072 } u;
Gilles Peskine01fd8752020-04-14 19:31:52 +020073};
Gilles Peskinea899a722019-06-24 14:06:43 +020074
Gilles Peskinef989dbe2019-06-26 18:18:12 +020075static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
76
Gilles Peskine5243a202019-07-12 23:38:19 +020077psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +020078 psa_key_lifetime_t lifetime )
79{
80 size_t i;
Gilles Peskine2b04f462020-05-10 00:44:04 +020081 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime );
82 /* In the driver table, location=0 means an entry that isn't used.
83 * No driver has a location of 0 because it's a reserved value
84 * (which designates transparent keys). Make sure we never return
85 * a driver entry for location 0. */
86 if( location == 0 )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020087 return( NULL );
88 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
89 {
Gilles Peskine2b04f462020-05-10 00:44:04 +020090 if( driver_table[i].location == location )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020091 return( &driver_table[i] );
92 }
93 return( NULL );
94}
95
96const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +020097 const psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020098{
Gilles Peskine5243a202019-07-12 23:38:19 +020099 return( driver->methods );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200100}
101
Gilles Peskine5243a202019-07-12 23:38:19 +0200102psa_drv_se_context_t *psa_get_se_driver_context(
103 psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200104{
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200105 return( &driver->u.context );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200106}
107
Gilles Peskine5243a202019-07-12 23:38:19 +0200108int psa_get_se_driver( psa_key_lifetime_t lifetime,
109 const psa_drv_se_t **p_methods,
110 psa_drv_se_context_t **p_drv_context)
111{
112 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
113 if( p_methods != NULL )
114 *p_methods = ( driver ? driver->methods : NULL );
115 if( p_drv_context != NULL )
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200116 *p_drv_context = ( driver ? &driver->u.context : NULL );
Gilles Peskine5243a202019-07-12 23:38:19 +0200117 return( driver != NULL );
118}
119
120
121
122/****************************************************************/
123/* Persistent data management */
124/****************************************************************/
125
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200126static psa_status_t psa_get_se_driver_its_file_uid(
127 const psa_se_drv_table_entry_t *driver,
128 psa_storage_uid_t *uid )
129{
Gilles Peskine2b04f462020-05-10 00:44:04 +0200130 if( driver->location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200131 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine573bbc12019-07-23 19:59:23 +0200132
133#if SIZE_MAX > UINT32_MAX
134 /* ITS file sizes are limited to 32 bits. */
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200135 if( driver->u.internal.persistent_data_size > UINT32_MAX )
Gilles Peskine573bbc12019-07-23 19:59:23 +0200136 return( PSA_ERROR_NOT_SUPPORTED );
137#endif
138
Gilles Peskine75c126b2019-07-24 15:56:01 +0200139 /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200140 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200141 return( PSA_SUCCESS );
142}
143
Gilles Peskine5243a202019-07-12 23:38:19 +0200144psa_status_t psa_load_se_persistent_data(
145 const psa_se_drv_table_entry_t *driver )
146{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200147 psa_status_t status;
148 psa_storage_uid_t uid;
Gilles Peskine8b663892019-07-31 17:57:57 +0200149 size_t length;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200150
151 status = psa_get_se_driver_its_file_uid( driver, &uid );
152 if( status != PSA_SUCCESS )
153 return( status );
154
Gilles Peskine8b663892019-07-31 17:57:57 +0200155 /* Read the amount of persistent data that the driver requests.
156 * If the data in storage is larger, it is truncated. If the data
157 * in storage is smaller, silently keep what is already at the end
158 * of the output buffer. */
Gilles Peskine75c126b2019-07-24 15:56:01 +0200159 /* psa_get_se_driver_its_file_uid ensures that the size_t
160 * persistent_data_size is in range, but compilers don't know that,
161 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200162 return( psa_its_get( uid, 0,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200163 (uint32_t) driver->u.internal.persistent_data_size,
164 driver->u.internal.persistent_data,
Gilles Peskine8b663892019-07-31 17:57:57 +0200165 &length ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200166}
167
168psa_status_t psa_save_se_persistent_data(
169 const psa_se_drv_table_entry_t *driver )
170{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200171 psa_status_t status;
172 psa_storage_uid_t uid;
173
174 status = psa_get_se_driver_its_file_uid( driver, &uid );
175 if( status != PSA_SUCCESS )
176 return( status );
177
Gilles Peskine75c126b2019-07-24 15:56:01 +0200178 /* psa_get_se_driver_its_file_uid ensures that the size_t
179 * persistent_data_size is in range, but compilers don't know that,
180 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200181 return( psa_its_set( uid,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200182 (uint32_t) driver->u.internal.persistent_data_size,
183 driver->u.internal.persistent_data,
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200184 0 ) );
185}
186
Gilles Peskine2b04f462020-05-10 00:44:04 +0200187psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200188{
189 psa_storage_uid_t uid;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200190 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200191 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200192 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200193 return( psa_its_remove( uid ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200194}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200195
Gilles Peskinecbaff462019-07-12 23:46:04 +0200196psa_status_t psa_find_se_slot_for_key(
197 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200198 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200199 psa_se_drv_table_entry_t *driver,
200 psa_key_slot_number_t *slot_number )
201{
202 psa_status_t status;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200203 psa_key_location_t key_location =
204 PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200205
Gilles Peskine2b04f462020-05-10 00:44:04 +0200206 /* If the location is wrong, it's a bug in the library. */
207 if( driver->location != key_location )
Gilles Peskinecbaff462019-07-12 23:46:04 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
209
210 /* If the driver doesn't support key creation in any way, give up now. */
211 if( driver->methods->key_management == NULL )
212 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200213
Gilles Peskine46d94392019-08-05 14:55:50 +0200214 if( psa_get_key_slot_number( attributes, slot_number ) == PSA_SUCCESS )
215 {
216 /* The application wants to use a specific slot. Allow it if
217 * the driver supports it. On a system with isolation,
218 * the crypto service must check that the application is
219 * permitted to request this slot. */
220 psa_drv_se_validate_slot_number_t p_validate_slot_number =
221 driver->methods->key_management->p_validate_slot_number;
222 if( p_validate_slot_number == NULL )
223 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200224 status = p_validate_slot_number( &driver->u.context,
225 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200226 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200227 *slot_number );
228 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200229 else if( method == PSA_KEY_CREATION_REGISTER )
230 {
231 /* The application didn't specify a slot number. This doesn't
232 * make sense when registering a slot. */
233 return( PSA_ERROR_INVALID_ARGUMENT );
234 }
Gilles Peskine46d94392019-08-05 14:55:50 +0200235 else
236 {
237 /* The application didn't tell us which slot to use. Let the driver
238 * choose. This is the normal case. */
239 psa_drv_se_allocate_key_t p_allocate =
240 driver->methods->key_management->p_allocate;
241 if( p_allocate == NULL )
242 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200243 status = p_allocate( &driver->u.context,
244 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200245 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200246 slot_number );
247 }
Gilles Peskinecbaff462019-07-12 23:46:04 +0200248 return( status );
249}
250
Gilles Peskine354f7672019-07-12 23:46:38 +0200251psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
252 psa_key_slot_number_t slot_number )
253{
254 psa_status_t status;
255 psa_status_t storage_status;
Gilles Peskine340b1272019-07-25 14:13:24 +0200256 /* Normally a missing method would mean that the action is not
257 * supported. But psa_destroy_key() is not supposed to return
258 * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
259 * be able to destroy it. The only use case for a driver that
260 * does not have a way to destroy keys at all is if the keys are
261 * locked in a read-only state: we can use the keys but not
262 * destroy them. Hence, if the driver doesn't support destroying
263 * keys, it's really a lack of permission. */
Gilles Peskine354f7672019-07-12 23:46:38 +0200264 if( driver->methods->key_management == NULL ||
265 driver->methods->key_management->p_destroy == NULL )
266 return( PSA_ERROR_NOT_PERMITTED );
267 status = driver->methods->key_management->p_destroy(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200268 &driver->u.context,
269 driver->u.internal.persistent_data,
Gilles Peskine354f7672019-07-12 23:46:38 +0200270 slot_number );
271 storage_status = psa_save_se_persistent_data( driver );
272 return( status == PSA_SUCCESS ? storage_status : status );
273}
274
Gilles Peskined9348f22019-10-01 15:22:29 +0200275psa_status_t psa_init_all_se_drivers( void )
276{
277 size_t i;
278 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
279 {
280 psa_se_drv_table_entry_t *driver = &driver_table[i];
Gilles Peskine2b04f462020-05-10 00:44:04 +0200281 if( driver->location == 0 )
Gilles Peskined9348f22019-10-01 15:22:29 +0200282 continue; /* skipping unused entry */
283 const psa_drv_se_t *methods = psa_get_se_driver_methods( driver );
284 if( methods->p_init != NULL )
285 {
286 psa_status_t status = methods->p_init(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200287 &driver->u.context,
288 driver->u.internal.persistent_data,
Gilles Peskine2b04f462020-05-10 00:44:04 +0200289 driver->location );
Gilles Peskined9348f22019-10-01 15:22:29 +0200290 if( status != PSA_SUCCESS )
291 return( status );
Gilles Peskinec84c70a2019-10-01 15:41:42 +0200292 status = psa_save_se_persistent_data( driver );
293 if( status != PSA_SUCCESS )
294 return( status );
Gilles Peskined9348f22019-10-01 15:22:29 +0200295 }
296 }
297 return( PSA_SUCCESS );
298}
299
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200300
301
302/****************************************************************/
303/* Driver registration */
304/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200305
306psa_status_t psa_register_se_driver(
Gilles Peskine2b04f462020-05-10 00:44:04 +0200307 psa_key_location_t location,
Gilles Peskinea899a722019-06-24 14:06:43 +0200308 const psa_drv_se_t *methods)
309{
310 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200311 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200312
313 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
314 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200315 /* Driver table entries are 0-initialized. 0 is not a valid driver
Gilles Peskine2b04f462020-05-10 00:44:04 +0200316 * location because it means a transparent key. */
Gilles Peskine9717d102019-06-26 11:50:04 +0200317#if defined(static_assert)
Gilles Peskine2b04f462020-05-10 00:44:04 +0200318 static_assert( PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
319 "Secure element support requires 0 to mean a local key" );
Gilles Peskine9717d102019-06-26 11:50:04 +0200320#endif
Gilles Peskine2b04f462020-05-10 00:44:04 +0200321 if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
Gilles Peskinea899a722019-06-24 14:06:43 +0200322 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200323 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200324 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea899a722019-06-24 14:06:43 +0200325
326 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
327 {
Gilles Peskine2b04f462020-05-10 00:44:04 +0200328 if( driver_table[i].location == 0 )
Gilles Peskinea899a722019-06-24 14:06:43 +0200329 break;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200330 /* Check that location isn't already in use up to the first free
Gilles Peskinea899a722019-06-24 14:06:43 +0200331 * entry. Since entries are created in order and never deleted,
332 * there can't be a used entry after the first free entry. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200333 if( driver_table[i].location == location )
Gilles Peskinea899a722019-06-24 14:06:43 +0200334 return( PSA_ERROR_ALREADY_EXISTS );
335 }
336 if( i == PSA_MAX_SE_DRIVERS )
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338
Gilles Peskine2b04f462020-05-10 00:44:04 +0200339 driver_table[i].location = location;
Gilles Peskinea899a722019-06-24 14:06:43 +0200340 driver_table[i].methods = methods;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200341 driver_table[i].u.internal.persistent_data_size =
Gilles Peskined5536d82019-10-01 16:55:29 +0200342 methods->persistent_data_size;
Gilles Peskine5243a202019-07-12 23:38:19 +0200343
344 if( methods->persistent_data_size != 0 )
345 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200346 driver_table[i].u.internal.persistent_data =
Gilles Peskine5243a202019-07-12 23:38:19 +0200347 mbedtls_calloc( 1, methods->persistent_data_size );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200348 if( driver_table[i].u.internal.persistent_data == NULL )
Gilles Peskine5243a202019-07-12 23:38:19 +0200349 {
350 status = PSA_ERROR_INSUFFICIENT_MEMORY;
351 goto error;
352 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200353 /* Load the driver's persistent data. On first use, the persistent
354 * data does not exist in storage, and is initialized to
355 * all-bits-zero by the calloc call just above. */
Gilles Peskine5243a202019-07-12 23:38:19 +0200356 status = psa_load_se_persistent_data( &driver_table[i] );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200357 if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
Gilles Peskine5243a202019-07-12 23:38:19 +0200358 goto error;
359 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200360
Gilles Peskinea899a722019-06-24 14:06:43 +0200361 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200362
363error:
364 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
365 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200366}
367
Gilles Peskined0890212019-06-24 14:34:43 +0200368void psa_unregister_all_se_drivers( void )
369{
Gilles Peskine5243a202019-07-12 23:38:19 +0200370 size_t i;
371 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
372 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200373 if( driver_table[i].u.internal.persistent_data != NULL )
374 mbedtls_free( driver_table[i].u.internal.persistent_data );
Gilles Peskine5243a202019-07-12 23:38:19 +0200375 }
Gilles Peskined0890212019-06-24 14:34:43 +0200376 memset( driver_table, 0, sizeof( driver_table ) );
377}
378
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200379
380
381/****************************************************************/
382/* The end */
383/****************************************************************/
384
Gilles Peskinea8ade162019-06-26 11:24:49 +0200385#endif /* MBEDTLS_PSA_CRYPTO_SE_C */