blob: bc73251808d20a7b07b8cb819bea432c90315b7d [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#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinea899a722019-06-24 14:06:43 +020029
Gilles Peskine9717d102019-06-26 11:50:04 +020030#include <assert.h>
Gilles Peskine5243a202019-07-12 23:38:19 +020031#include <stdint.h>
Gilles Peskined0890212019-06-24 14:34:43 +020032#include <string.h>
33
Gilles Peskine5243a202019-07-12 23:38:19 +020034#include "psa/crypto_se_driver.h"
35
Gilles Peskinea899a722019-06-24 14:06:43 +020036#include "psa_crypto_se.h"
37
Gilles Peskine8b96cad2019-07-23 17:38:08 +020038#if defined(MBEDTLS_PSA_ITS_FILE_C)
39#include "psa_crypto_its.h"
40#else /* Native ITS implementation */
41#include "psa/error.h"
42#include "psa/internal_trusted_storage.h"
43#endif
44
Gilles Peskine5243a202019-07-12 23:38:19 +020045#include "mbedtls/platform.h"
46#if !defined(MBEDTLS_PLATFORM_C)
47#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
51
52
Gilles Peskinef989dbe2019-06-26 18:18:12 +020053/****************************************************************/
54/* Driver lookup */
55/****************************************************************/
56
Gilles Peskine5243a202019-07-12 23:38:19 +020057/* This structure is identical to psa_drv_se_context_t declared in
58 * `crypto_se_driver.h`, except that some parts are writable here
59 * (non-const, or pointer to non-const). */
60typedef struct
61{
62 void *persistent_data;
63 size_t persistent_data_size;
64 uintptr_t transient_data;
65} psa_drv_se_internal_context_t;
66
Gilles Peskinef989dbe2019-06-26 18:18:12 +020067typedef struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020068{
69 psa_key_lifetime_t lifetime;
70 const psa_drv_se_t *methods;
Gilles Peskine5243a202019-07-12 23:38:19 +020071 union
72 {
73 psa_drv_se_internal_context_t internal;
74 psa_drv_se_context_t context;
75 };
Gilles Peskinef989dbe2019-06-26 18:18:12 +020076} psa_se_drv_table_entry_t;
Gilles Peskinea899a722019-06-24 14:06:43 +020077
Gilles Peskinef989dbe2019-06-26 18:18:12 +020078static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
79
Gilles Peskine5243a202019-07-12 23:38:19 +020080psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +020081 psa_key_lifetime_t lifetime )
82{
83 size_t i;
Gilles Peskine75c126b2019-07-24 15:56:01 +020084 /* In the driver table, lifetime=0 means an entry that isn't used.
85 * No driver has a lifetime of 0 because it's a reserved value
86 * (which designates volatile keys). Make sure we never return
87 * a driver entry for lifetime 0. */
Gilles Peskinef989dbe2019-06-26 18:18:12 +020088 if( lifetime == 0 )
89 return( NULL );
90 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
91 {
92 if( driver_table[i].lifetime == lifetime )
93 return( &driver_table[i] );
94 }
95 return( NULL );
96}
97
98const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +020099 const psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200100{
Gilles Peskine5243a202019-07-12 23:38:19 +0200101 return( driver->methods );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200102}
103
Gilles Peskine5243a202019-07-12 23:38:19 +0200104psa_drv_se_context_t *psa_get_se_driver_context(
105 psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200106{
Gilles Peskine5243a202019-07-12 23:38:19 +0200107 return( &driver->context );
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200108}
109
Gilles Peskine5243a202019-07-12 23:38:19 +0200110int psa_get_se_driver( psa_key_lifetime_t lifetime,
111 const psa_drv_se_t **p_methods,
112 psa_drv_se_context_t **p_drv_context)
113{
114 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
115 if( p_methods != NULL )
116 *p_methods = ( driver ? driver->methods : NULL );
117 if( p_drv_context != NULL )
118 *p_drv_context = ( driver ? &driver->context : NULL );
119 return( driver != NULL );
120}
121
122
123
124/****************************************************************/
125/* Persistent data management */
126/****************************************************************/
127
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200128static psa_status_t psa_get_se_driver_its_file_uid(
129 const psa_se_drv_table_entry_t *driver,
130 psa_storage_uid_t *uid )
131{
132 if( driver->lifetime > PSA_MAX_SE_LIFETIME )
133 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine573bbc12019-07-23 19:59:23 +0200134
135#if SIZE_MAX > UINT32_MAX
136 /* ITS file sizes are limited to 32 bits. */
137 if( driver->internal.persistent_data_size > UINT32_MAX )
138 return( PSA_ERROR_NOT_SUPPORTED );
139#endif
140
Gilles Peskine75c126b2019-07-24 15:56:01 +0200141 /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200142 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->lifetime;
143 return( PSA_SUCCESS );
144}
145
Gilles Peskine5243a202019-07-12 23:38:19 +0200146psa_status_t psa_load_se_persistent_data(
147 const psa_se_drv_table_entry_t *driver )
148{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200149 psa_status_t status;
150 psa_storage_uid_t uid;
Gilles Peskine8b663892019-07-31 17:57:57 +0200151 size_t length;
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200152
153 status = psa_get_se_driver_its_file_uid( driver, &uid );
154 if( status != PSA_SUCCESS )
155 return( status );
156
Gilles Peskine8b663892019-07-31 17:57:57 +0200157 /* Read the amount of persistent data that the driver requests.
158 * If the data in storage is larger, it is truncated. If the data
159 * in storage is smaller, silently keep what is already at the end
160 * of the output buffer. */
Gilles Peskine75c126b2019-07-24 15:56:01 +0200161 /* psa_get_se_driver_its_file_uid ensures that the size_t
162 * persistent_data_size is in range, but compilers don't know that,
163 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200164 return( psa_its_get( uid, 0,
165 (uint32_t) driver->internal.persistent_data_size,
Gilles Peskine8b663892019-07-31 17:57:57 +0200166 driver->internal.persistent_data,
167 &length ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200168}
169
170psa_status_t psa_save_se_persistent_data(
171 const psa_se_drv_table_entry_t *driver )
172{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200173 psa_status_t status;
174 psa_storage_uid_t uid;
175
176 status = psa_get_se_driver_its_file_uid( driver, &uid );
177 if( status != PSA_SUCCESS )
178 return( status );
179
Gilles Peskine75c126b2019-07-24 15:56:01 +0200180 /* psa_get_se_driver_its_file_uid ensures that the size_t
181 * persistent_data_size is in range, but compilers don't know that,
182 * so cast to reassure them. */
Gilles Peskine573bbc12019-07-23 19:59:23 +0200183 return( psa_its_set( uid,
184 (uint32_t) driver->internal.persistent_data_size,
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200185 driver->internal.persistent_data,
186 0 ) );
187}
188
189psa_status_t psa_destroy_se_persistent_data( psa_key_lifetime_t lifetime )
190{
191 psa_storage_uid_t uid;
192 if( lifetime > PSA_MAX_SE_LIFETIME )
193 return( PSA_ERROR_NOT_SUPPORTED );
194 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + lifetime;
195 return( psa_its_remove( uid ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200196}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200197
Gilles Peskinecbaff462019-07-12 23:46:04 +0200198psa_status_t psa_find_se_slot_for_key(
199 const psa_key_attributes_t *attributes,
200 psa_se_drv_table_entry_t *driver,
201 psa_key_slot_number_t *slot_number )
202{
203 psa_status_t status;
204 psa_drv_se_allocate_key_t p_allocate = NULL;
205
206 /* If the lifetime is wrong, it's a bug in the library. */
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200207 if( driver->lifetime != psa_get_key_lifetime( attributes ) )
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 );
213 p_allocate = driver->methods->key_management->p_allocate;
214
215 /* If the driver doesn't tell us how to allocate a slot, that's
216 * not supported for the time being. */
217 if( p_allocate == NULL )
218 return( PSA_ERROR_NOT_SUPPORTED );
219
Gilles Peskine2e0f3882019-07-25 11:34:33 +0200220 status = p_allocate( &driver->context,
221 driver->internal.persistent_data,
222 attributes,
223 slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +0200224 return( status );
225}
226
Gilles Peskine354f7672019-07-12 23:46:38 +0200227psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
228 psa_key_slot_number_t slot_number )
229{
230 psa_status_t status;
231 psa_status_t storage_status;
Gilles Peskine340b1272019-07-25 14:13:24 +0200232 /* Normally a missing method would mean that the action is not
233 * supported. But psa_destroy_key() is not supposed to return
234 * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
235 * be able to destroy it. The only use case for a driver that
236 * does not have a way to destroy keys at all is if the keys are
237 * locked in a read-only state: we can use the keys but not
238 * destroy them. Hence, if the driver doesn't support destroying
239 * keys, it's really a lack of permission. */
Gilles Peskine354f7672019-07-12 23:46:38 +0200240 if( driver->methods->key_management == NULL ||
241 driver->methods->key_management->p_destroy == NULL )
242 return( PSA_ERROR_NOT_PERMITTED );
243 status = driver->methods->key_management->p_destroy(
244 &driver->context,
245 driver->internal.persistent_data,
246 slot_number );
247 storage_status = psa_save_se_persistent_data( driver );
248 return( status == PSA_SUCCESS ? storage_status : status );
249}
250
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200251
252
253/****************************************************************/
254/* Driver registration */
255/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200256
257psa_status_t psa_register_se_driver(
258 psa_key_lifetime_t lifetime,
259 const psa_drv_se_t *methods)
260{
261 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200262 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200263
264 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
265 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200266 /* Driver table entries are 0-initialized. 0 is not a valid driver
267 * lifetime because it means a volatile key. */
268#if defined(static_assert)
269 static_assert( PSA_KEY_LIFETIME_VOLATILE == 0,
270 "Secure element support requires 0 to mean a volatile key" );
271#endif
Gilles Peskinea899a722019-06-24 14:06:43 +0200272 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
273 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
274 {
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200277 if( lifetime > PSA_MAX_SE_LIFETIME )
278 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea899a722019-06-24 14:06:43 +0200279
280 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
281 {
282 if( driver_table[i].lifetime == 0 )
283 break;
284 /* Check that lifetime isn't already in use up to the first free
285 * entry. Since entries are created in order and never deleted,
286 * there can't be a used entry after the first free entry. */
287 if( driver_table[i].lifetime == lifetime )
288 return( PSA_ERROR_ALREADY_EXISTS );
289 }
290 if( i == PSA_MAX_SE_DRIVERS )
291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
292
293 driver_table[i].lifetime = lifetime;
294 driver_table[i].methods = methods;
Gilles Peskine5243a202019-07-12 23:38:19 +0200295
296 if( methods->persistent_data_size != 0 )
297 {
298 driver_table[i].internal.persistent_data =
299 mbedtls_calloc( 1, methods->persistent_data_size );
300 if( driver_table[i].internal.persistent_data == NULL )
301 {
302 status = PSA_ERROR_INSUFFICIENT_MEMORY;
303 goto error;
304 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200305 /* Load the driver's persistent data. On first use, the persistent
306 * data does not exist in storage, and is initialized to
307 * all-bits-zero by the calloc call just above. */
Gilles Peskine5243a202019-07-12 23:38:19 +0200308 status = psa_load_se_persistent_data( &driver_table[i] );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200309 if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
Gilles Peskine5243a202019-07-12 23:38:19 +0200310 goto error;
311 }
312 driver_table[i].internal.persistent_data_size =
313 methods->persistent_data_size;
314
Gilles Peskinea899a722019-06-24 14:06:43 +0200315 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200316
317error:
318 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
319 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200320}
321
Gilles Peskined0890212019-06-24 14:34:43 +0200322void psa_unregister_all_se_drivers( void )
323{
Gilles Peskine5243a202019-07-12 23:38:19 +0200324 size_t i;
325 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
326 {
327 if( driver_table[i].internal.persistent_data != NULL )
328 mbedtls_free( driver_table[i].internal.persistent_data );
329 }
Gilles Peskined0890212019-06-24 14:34:43 +0200330 memset( driver_table, 0, sizeof( driver_table ) );
331}
332
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200333
334
335/****************************************************************/
336/* The end */
337/****************************************************************/
338
Gilles Peskinea8ade162019-06-26 11:24:49 +0200339#endif /* MBEDTLS_PSA_CRYPTO_SE_C */