blob: bae44fa04ba79b5deaf4a0c132accaedca40b31a [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;
84 if( lifetime == 0 )
85 return( NULL );
86 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
87 {
88 if( driver_table[i].lifetime == lifetime )
89 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 Peskine5243a202019-07-12 23:38:19 +0200103 return( &driver->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 )
114 *p_drv_context = ( driver ? &driver->context : NULL );
115 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{
128 if( driver->lifetime > PSA_MAX_SE_LIFETIME )
129 return( PSA_ERROR_NOT_SUPPORTED );
130 *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->lifetime;
131 return( PSA_SUCCESS );
132}
133
Gilles Peskine5243a202019-07-12 23:38:19 +0200134psa_status_t psa_load_se_persistent_data(
135 const psa_se_drv_table_entry_t *driver )
136{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200137 psa_status_t status;
138 psa_storage_uid_t uid;
139
140 status = psa_get_se_driver_its_file_uid( driver, &uid );
141 if( status != PSA_SUCCESS )
142 return( status );
143
144 return( psa_its_get( uid, 0, driver->internal.persistent_data_size,
145 driver->internal.persistent_data ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200146}
147
148psa_status_t psa_save_se_persistent_data(
149 const psa_se_drv_table_entry_t *driver )
150{
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200151 psa_status_t status;
152 psa_storage_uid_t uid;
153
154 status = psa_get_se_driver_its_file_uid( driver, &uid );
155 if( status != PSA_SUCCESS )
156 return( status );
157
158 return( psa_its_set( uid, driver->internal.persistent_data_size,
159 driver->internal.persistent_data,
160 0 ) );
161}
162
163psa_status_t psa_destroy_se_persistent_data( psa_key_lifetime_t lifetime )
164{
165 psa_storage_uid_t uid;
166 if( lifetime > PSA_MAX_SE_LIFETIME )
167 return( PSA_ERROR_NOT_SUPPORTED );
168 uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + lifetime;
169 return( psa_its_remove( uid ) );
Gilles Peskine5243a202019-07-12 23:38:19 +0200170}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200171
Gilles Peskinecbaff462019-07-12 23:46:04 +0200172psa_status_t psa_find_se_slot_for_key(
173 const psa_key_attributes_t *attributes,
174 psa_se_drv_table_entry_t *driver,
175 psa_key_slot_number_t *slot_number )
176{
177 psa_status_t status;
178 psa_drv_se_allocate_key_t p_allocate = NULL;
179
180 /* If the lifetime is wrong, it's a bug in the library. */
181 if( driver->lifetime != attributes->lifetime )
182 return( PSA_ERROR_CORRUPTION_DETECTED );
183
184 /* If the driver doesn't support key creation in any way, give up now. */
185 if( driver->methods->key_management == NULL )
186 return( PSA_ERROR_NOT_SUPPORTED );
187 p_allocate = driver->methods->key_management->p_allocate;
188
189 /* If the driver doesn't tell us how to allocate a slot, that's
190 * not supported for the time being. */
191 if( p_allocate == NULL )
192 return( PSA_ERROR_NOT_SUPPORTED );
193
194 status = ( *p_allocate )( &driver->context,
195 driver->internal.persistent_data,
196 attributes,
197 slot_number );
198 return( status );
199}
200
Gilles Peskine354f7672019-07-12 23:46:38 +0200201psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
202 psa_key_slot_number_t slot_number )
203{
204 psa_status_t status;
205 psa_status_t storage_status;
206 if( driver->methods->key_management == NULL ||
207 driver->methods->key_management->p_destroy == NULL )
208 return( PSA_ERROR_NOT_PERMITTED );
209 status = driver->methods->key_management->p_destroy(
210 &driver->context,
211 driver->internal.persistent_data,
212 slot_number );
213 storage_status = psa_save_se_persistent_data( driver );
214 return( status == PSA_SUCCESS ? storage_status : status );
215}
216
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200217
218
219/****************************************************************/
220/* Driver registration */
221/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200222
223psa_status_t psa_register_se_driver(
224 psa_key_lifetime_t lifetime,
225 const psa_drv_se_t *methods)
226{
227 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200228 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200229
230 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
231 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200232 /* Driver table entries are 0-initialized. 0 is not a valid driver
233 * lifetime because it means a volatile key. */
234#if defined(static_assert)
235 static_assert( PSA_KEY_LIFETIME_VOLATILE == 0,
236 "Secure element support requires 0 to mean a volatile key" );
237#endif
Gilles Peskinea899a722019-06-24 14:06:43 +0200238 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
239 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
240 {
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200243 if( lifetime > PSA_MAX_SE_LIFETIME )
244 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea899a722019-06-24 14:06:43 +0200245
246 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
247 {
248 if( driver_table[i].lifetime == 0 )
249 break;
250 /* Check that lifetime isn't already in use up to the first free
251 * entry. Since entries are created in order and never deleted,
252 * there can't be a used entry after the first free entry. */
253 if( driver_table[i].lifetime == lifetime )
254 return( PSA_ERROR_ALREADY_EXISTS );
255 }
256 if( i == PSA_MAX_SE_DRIVERS )
257 return( PSA_ERROR_INSUFFICIENT_MEMORY );
258
259 driver_table[i].lifetime = lifetime;
260 driver_table[i].methods = methods;
Gilles Peskine5243a202019-07-12 23:38:19 +0200261
262 if( methods->persistent_data_size != 0 )
263 {
264 driver_table[i].internal.persistent_data =
265 mbedtls_calloc( 1, methods->persistent_data_size );
266 if( driver_table[i].internal.persistent_data == NULL )
267 {
268 status = PSA_ERROR_INSUFFICIENT_MEMORY;
269 goto error;
270 }
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200271 /* Load the driver's persistent data. On first use, the persistent
272 * data does not exist in storage, and is initialized to
273 * all-bits-zero by the calloc call just above. */
Gilles Peskine5243a202019-07-12 23:38:19 +0200274 status = psa_load_se_persistent_data( &driver_table[i] );
Gilles Peskine8b96cad2019-07-23 17:38:08 +0200275 if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
Gilles Peskine5243a202019-07-12 23:38:19 +0200276 goto error;
277 }
278 driver_table[i].internal.persistent_data_size =
279 methods->persistent_data_size;
280
Gilles Peskinea899a722019-06-24 14:06:43 +0200281 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200282
283error:
284 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
285 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200286}
287
Gilles Peskined0890212019-06-24 14:34:43 +0200288void psa_unregister_all_se_drivers( void )
289{
Gilles Peskine5243a202019-07-12 23:38:19 +0200290 size_t i;
291 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
292 {
293 if( driver_table[i].internal.persistent_data != NULL )
294 mbedtls_free( driver_table[i].internal.persistent_data );
295 }
Gilles Peskined0890212019-06-24 14:34:43 +0200296 memset( driver_table, 0, sizeof( driver_table ) );
297}
298
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200299
300
301/****************************************************************/
302/* The end */
303/****************************************************************/
304
Gilles Peskinea8ade162019-06-26 11:24:49 +0200305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */