blob: 56678d6a90e5996b3dfc9e882ff7c3fecae1d191 [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"
41#if !defined(MBEDTLS_PLATFORM_C)
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#endif
45
46
47
Gilles Peskinef989dbe2019-06-26 18:18:12 +020048/****************************************************************/
49/* Driver lookup */
50/****************************************************************/
51
Gilles Peskine5243a202019-07-12 23:38:19 +020052/* This structure is identical to psa_drv_se_context_t declared in
53 * `crypto_se_driver.h`, except that some parts are writable here
54 * (non-const, or pointer to non-const). */
55typedef struct
56{
57 void *persistent_data;
58 size_t persistent_data_size;
59 uintptr_t transient_data;
60} psa_drv_se_internal_context_t;
61
Gilles Peskine01fd8752020-04-14 19:31:52 +020062struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020063{
Gilles Peskine2b04f462020-05-10 00:44:04 +020064 psa_key_location_t location;
Gilles Peskinea899a722019-06-24 14:06:43 +020065 const psa_drv_se_t *methods;
Gilles Peskine5243a202019-07-12 23:38:19 +020066 union
67 {
68 psa_drv_se_internal_context_t internal;
69 psa_drv_se_context_t context;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +020070 } u;
Gilles Peskine01fd8752020-04-14 19:31:52 +020071};
Gilles Peskinea899a722019-06-24 14:06:43 +020072
Gilles Peskinef989dbe2019-06-26 18:18:12 +020073static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
74
Gilles Peskine5243a202019-07-12 23:38:19 +020075psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +020076 psa_key_lifetime_t lifetime )
77{
78 size_t i;
Gilles Peskine2b04f462020-05-10 00:44:04 +020079 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime );
80 /* In the driver table, location=0 means an entry that isn't used.
81 * No driver has a location of 0 because it's a reserved value
82 * (which designates transparent keys). Make sure we never return
83 * a driver entry for location 0. */
84 if( location == 0 )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020085 return( NULL );
86 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
87 {
Gilles Peskine2b04f462020-05-10 00:44:04 +020088 if( driver_table[i].location == location )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020089 return( &driver_table[i] );
90 }
91 return( NULL );
92}
93
94const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +020095 const psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020096{
Gilles Peskine5243a202019-07-12 23:38:19 +020097 return( driver->methods );
Gilles Peskinef989dbe2019-06-26 18:18:12 +020098}
99
Gilles Peskine5243a202019-07-12 23:38:19 +0200100psa_drv_se_context_t *psa_get_se_driver_context(
101 psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200102{
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200103 return( &driver->u.context );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200104}
105
Gilles Peskine5243a202019-07-12 23:38:19 +0200106int psa_get_se_driver( psa_key_lifetime_t lifetime,
107 const psa_drv_se_t **p_methods,
108 psa_drv_se_context_t **p_drv_context)
109{
110 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
111 if( p_methods != NULL )
112 *p_methods = ( driver ? driver->methods : NULL );
113 if( p_drv_context != NULL )
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200114 *p_drv_context = ( driver ? &driver->u.context : NULL );
Gilles Peskine5243a202019-07-12 23:38:19 +0200115 return( driver != NULL );
116}
117
118
119
120/****************************************************************/
121/* Persistent data management */
122/****************************************************************/
123
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200124static psa_status_t psa_get_se_driver_its_file_uid(
125 const psa_se_drv_table_entry_t *driver,
126 psa_storage_uid_t *uid )
127{
Gilles Peskine2b04f462020-05-10 00:44:04 +0200128 if( driver->location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200129 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine573bbc12019-07-23 19:59:23 +0200130
131#if SIZE_MAX > UINT32_MAX
132 /* ITS file sizes are limited to 32 bits. */
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200133 if( driver->u.internal.persistent_data_size > UINT32_MAX )
Gilles Peskine573bbc12019-07-23 19:59:23 +0200134 return( PSA_ERROR_NOT_SUPPORTED );
135#endif
136
Gilles Peskine75c126b2019-07-24 15:56:01 +0200137 /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200138 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200139 return( PSA_SUCCESS );
140}
141
Gilles Peskine5243a202019-07-12 23:38:19 +0200142psa_status_t psa_load_se_persistent_data(
143 const psa_se_drv_table_entry_t *driver )
144{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200145 psa_status_t status;
146 psa_storage_uid_t uid;
Gilles Peskine8b663892019-07-31 17:57:57 +0200147 size_t length;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200148
149 status = psa_get_se_driver_its_file_uid( driver, &uid );
150 if( status != PSA_SUCCESS )
151 return( status );
152
Gilles Peskine8b663892019-07-31 17:57:57 +0200153 /* Read the amount of persistent data that the driver requests.
154 * If the data in storage is larger, it is truncated. If the data
155 * in storage is smaller, silently keep what is already at the end
156 * of the output buffer. */
Gilles Peskine75c126b2019-07-24 15:56:01 +0200157 /* psa_get_se_driver_its_file_uid ensures that the size_t
158 * persistent_data_size is in range, but compilers don't know that,
159 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200160 return( psa_its_get( uid, 0,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200161 (uint32_t) driver->u.internal.persistent_data_size,
162 driver->u.internal.persistent_data,
Gilles Peskine8b663892019-07-31 17:57:57 +0200163 &length ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200164}
165
166psa_status_t psa_save_se_persistent_data(
167 const psa_se_drv_table_entry_t *driver )
168{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200169 psa_status_t status;
170 psa_storage_uid_t uid;
171
172 status = psa_get_se_driver_its_file_uid( driver, &uid );
173 if( status != PSA_SUCCESS )
174 return( status );
175
Gilles Peskine75c126b2019-07-24 15:56:01 +0200176 /* psa_get_se_driver_its_file_uid ensures that the size_t
177 * persistent_data_size is in range, but compilers don't know that,
178 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200179 return( psa_its_set( uid,
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200180 (uint32_t) driver->u.internal.persistent_data_size,
181 driver->u.internal.persistent_data,
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200182 0 ) );
183}
184
Gilles Peskine2b04f462020-05-10 00:44:04 +0200185psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200186{
187 psa_storage_uid_t uid;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200188 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200189 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200190 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200191 return( psa_its_remove( uid ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200192}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200193
Gilles Peskinecbaff462019-07-12 23:46:04 +0200194psa_status_t psa_find_se_slot_for_key(
195 const psa_key_attributes_t *attributes,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200196 psa_key_creation_method_t method,
Gilles Peskinecbaff462019-07-12 23:46:04 +0200197 psa_se_drv_table_entry_t *driver,
198 psa_key_slot_number_t *slot_number )
199{
200 psa_status_t status;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200201 psa_key_location_t key_location =
202 PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200203
Gilles Peskine2b04f462020-05-10 00:44:04 +0200204 /* If the location is wrong, it's a bug in the library. */
205 if( driver->location != key_location )
Gilles Peskinecbaff462019-07-12 23:46:04 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
207
208 /* If the driver doesn't support key creation in any way, give up now. */
209 if( driver->methods->key_management == NULL )
210 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200211
Gilles Peskine46d94392019-08-05 14:55:50 +0200212 if( psa_get_key_slot_number( attributes, slot_number ) == PSA_SUCCESS )
213 {
214 /* The application wants to use a specific slot. Allow it if
215 * the driver supports it. On a system with isolation,
216 * the crypto service must check that the application is
217 * permitted to request this slot. */
218 psa_drv_se_validate_slot_number_t p_validate_slot_number =
219 driver->methods->key_management->p_validate_slot_number;
220 if( p_validate_slot_number == NULL )
221 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200222 status = p_validate_slot_number( &driver->u.context,
223 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200224 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200225 *slot_number );
226 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200227 else if( method == PSA_KEY_CREATION_REGISTER )
228 {
229 /* The application didn't specify a slot number. This doesn't
230 * make sense when registering a slot. */
231 return( PSA_ERROR_INVALID_ARGUMENT );
232 }
Gilles Peskine46d94392019-08-05 14:55:50 +0200233 else
234 {
235 /* The application didn't tell us which slot to use. Let the driver
236 * choose. This is the normal case. */
237 psa_drv_se_allocate_key_t p_allocate =
238 driver->methods->key_management->p_allocate;
239 if( p_allocate == NULL )
240 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200241 status = p_allocate( &driver->u.context,
242 driver->u.internal.persistent_data,
Gilles Peskinee88c2c12019-08-05 16:44:14 +0200243 attributes, method,
Gilles Peskine46d94392019-08-05 14:55:50 +0200244 slot_number );
245 }
Gilles Peskinecbaff462019-07-12 23:46:04 +0200246 return( status );
247}
248
Gilles Peskine354f7672019-07-12 23:46:38 +0200249psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
250 psa_key_slot_number_t slot_number )
251{
252 psa_status_t status;
253 psa_status_t storage_status;
Gilles Peskine340b1272019-07-25 14:13:24 +0200254 /* Normally a missing method would mean that the action is not
255 * supported. But psa_destroy_key() is not supposed to return
256 * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
257 * be able to destroy it. The only use case for a driver that
258 * does not have a way to destroy keys at all is if the keys are
259 * locked in a read-only state: we can use the keys but not
260 * destroy them. Hence, if the driver doesn't support destroying
261 * keys, it's really a lack of permission. */
Gilles Peskine354f7672019-07-12 23:46:38 +0200262 if( driver->methods->key_management == NULL ||
263 driver->methods->key_management->p_destroy == NULL )
264 return( PSA_ERROR_NOT_PERMITTED );
265 status = driver->methods->key_management->p_destroy(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200266 &driver->u.context,
267 driver->u.internal.persistent_data,
Gilles Peskine354f7672019-07-12 23:46:38 +0200268 slot_number );
269 storage_status = psa_save_se_persistent_data( driver );
270 return( status == PSA_SUCCESS ? storage_status : status );
271}
272
Gilles Peskined9348f22019-10-01 15:22:29 +0200273psa_status_t psa_init_all_se_drivers( void )
274{
275 size_t i;
276 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
277 {
278 psa_se_drv_table_entry_t *driver = &driver_table[i];
Gilles Peskine2b04f462020-05-10 00:44:04 +0200279 if( driver->location == 0 )
Gilles Peskined9348f22019-10-01 15:22:29 +0200280 continue; /* skipping unused entry */
281 const psa_drv_se_t *methods = psa_get_se_driver_methods( driver );
282 if( methods->p_init != NULL )
283 {
284 psa_status_t status = methods->p_init(
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200285 &driver->u.context,
286 driver->u.internal.persistent_data,
Gilles Peskine2b04f462020-05-10 00:44:04 +0200287 driver->location );
Gilles Peskined9348f22019-10-01 15:22:29 +0200288 if( status != PSA_SUCCESS )
289 return( status );
Gilles Peskinec84c70a2019-10-01 15:41:42 +0200290 status = psa_save_se_persistent_data( driver );
291 if( status != PSA_SUCCESS )
292 return( status );
Gilles Peskined9348f22019-10-01 15:22:29 +0200293 }
294 }
295 return( PSA_SUCCESS );
296}
297
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200298
299
300/****************************************************************/
301/* Driver registration */
302/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200303
304psa_status_t psa_register_se_driver(
Gilles Peskine2b04f462020-05-10 00:44:04 +0200305 psa_key_location_t location,
Gilles Peskinea899a722019-06-24 14:06:43 +0200306 const psa_drv_se_t *methods)
307{
308 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200309 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200310
311 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
312 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200313 /* Driver table entries are 0-initialized. 0 is not a valid driver
Gilles Peskine2b04f462020-05-10 00:44:04 +0200314 * location because it means a transparent key. */
Gilles Peskine9717d102019-06-26 11:50:04 +0200315#if defined(static_assert)
Gilles Peskine2b04f462020-05-10 00:44:04 +0200316 static_assert( PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
317 "Secure element support requires 0 to mean a local key" );
Gilles Peskine9717d102019-06-26 11:50:04 +0200318#endif
Gilles Peskine2b04f462020-05-10 00:44:04 +0200319 if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
Gilles Peskinea899a722019-06-24 14:06:43 +0200320 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2b04f462020-05-10 00:44:04 +0200321 if( location > PSA_MAX_SE_LOCATION )
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200322 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea899a722019-06-24 14:06:43 +0200323
324 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
325 {
Gilles Peskine2b04f462020-05-10 00:44:04 +0200326 if( driver_table[i].location == 0 )
Gilles Peskinea899a722019-06-24 14:06:43 +0200327 break;
Gilles Peskine2b04f462020-05-10 00:44:04 +0200328 /* Check that location isn't already in use up to the first free
Gilles Peskinea899a722019-06-24 14:06:43 +0200329 * entry. Since entries are created in order and never deleted,
330 * there can't be a used entry after the first free entry. */
Gilles Peskine2b04f462020-05-10 00:44:04 +0200331 if( driver_table[i].location == location )
Gilles Peskinea899a722019-06-24 14:06:43 +0200332 return( PSA_ERROR_ALREADY_EXISTS );
333 }
334 if( i == PSA_MAX_SE_DRIVERS )
335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
336
Gilles Peskine2b04f462020-05-10 00:44:04 +0200337 driver_table[i].location = location;
Gilles Peskinea899a722019-06-24 14:06:43 +0200338 driver_table[i].methods = methods;
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200339 driver_table[i].u.internal.persistent_data_size =
Gilles Peskined5536d82019-10-01 16:55:29 +0200340 methods->persistent_data_size;
Gilles Peskine5243a202019-07-12 23:38:19 +0200341
342 if( methods->persistent_data_size != 0 )
343 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200344 driver_table[i].u.internal.persistent_data =
Gilles Peskine5243a202019-07-12 23:38:19 +0200345 mbedtls_calloc( 1, methods->persistent_data_size );
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200346 if( driver_table[i].u.internal.persistent_data == NULL )
Gilles Peskine5243a202019-07-12 23:38:19 +0200347 {
348 status = PSA_ERROR_INSUFFICIENT_MEMORY;
349 goto error;
350 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200351 /* Load the driver's persistent data. On first use, the persistent
352 * data does not exist in storage, and is initialized to
353 * all-bits-zero by the calloc call just above. */
Gilles Peskine5243a202019-07-12 23:38:19 +0200354 status = psa_load_se_persistent_data( &driver_table[i] );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200355 if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
Gilles Peskine5243a202019-07-12 23:38:19 +0200356 goto error;
357 }
Gilles Peskine5243a202019-07-12 23:38:19 +0200358
Gilles Peskinea899a722019-06-24 14:06:43 +0200359 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200360
361error:
362 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
363 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200364}
365
Gilles Peskined0890212019-06-24 14:34:43 +0200366void psa_unregister_all_se_drivers( void )
367{
Gilles Peskine5243a202019-07-12 23:38:19 +0200368 size_t i;
369 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
370 {
Gilles Peskine1a75d0c2020-04-14 19:33:25 +0200371 if( driver_table[i].u.internal.persistent_data != NULL )
372 mbedtls_free( driver_table[i].u.internal.persistent_data );
Gilles Peskine5243a202019-07-12 23:38:19 +0200373 }
Gilles Peskined0890212019-06-24 14:34:43 +0200374 memset( driver_table, 0, sizeof( driver_table ) );
375}
376
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200377
378
379/****************************************************************/
380/* The end */
381/****************************************************************/
382
Gilles Peskinea8ade162019-06-26 11:24:49 +0200383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */