blob: 7287ac0d76d53b19385953e09d266bd55426f4b9 [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 Peskine5243a202019-07-12 23:38:19 +020038#include "mbedtls/platform.h"
39#if !defined(MBEDTLS_PLATFORM_C)
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#endif
43
44
45
Gilles Peskinef989dbe2019-06-26 18:18:12 +020046/****************************************************************/
47/* Driver lookup */
48/****************************************************************/
49
Gilles Peskine5243a202019-07-12 23:38:19 +020050/* This structure is identical to psa_drv_se_context_t declared in
51 * `crypto_se_driver.h`, except that some parts are writable here
52 * (non-const, or pointer to non-const). */
53typedef struct
54{
55 void *persistent_data;
56 size_t persistent_data_size;
57 uintptr_t transient_data;
58} psa_drv_se_internal_context_t;
59
Gilles Peskinef989dbe2019-06-26 18:18:12 +020060typedef struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020061{
62 psa_key_lifetime_t lifetime;
63 const psa_drv_se_t *methods;
Gilles Peskine5243a202019-07-12 23:38:19 +020064 union
65 {
66 psa_drv_se_internal_context_t internal;
67 psa_drv_se_context_t context;
68 };
Gilles Peskinef989dbe2019-06-26 18:18:12 +020069} psa_se_drv_table_entry_t;
Gilles Peskinea899a722019-06-24 14:06:43 +020070
Gilles Peskinef989dbe2019-06-26 18:18:12 +020071static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
72
Gilles Peskine5243a202019-07-12 23:38:19 +020073psa_se_drv_table_entry_t *psa_get_se_driver_entry(
Gilles Peskinef989dbe2019-06-26 18:18:12 +020074 psa_key_lifetime_t lifetime )
75{
76 size_t i;
77 if( lifetime == 0 )
78 return( NULL );
79 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
80 {
81 if( driver_table[i].lifetime == lifetime )
82 return( &driver_table[i] );
83 }
84 return( NULL );
85}
86
87const psa_drv_se_t *psa_get_se_driver_methods(
Gilles Peskine5243a202019-07-12 23:38:19 +020088 const psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020089{
Gilles Peskine5243a202019-07-12 23:38:19 +020090 return( driver->methods );
Gilles Peskinef989dbe2019-06-26 18:18:12 +020091}
92
Gilles Peskine5243a202019-07-12 23:38:19 +020093psa_drv_se_context_t *psa_get_se_driver_context(
94 psa_se_drv_table_entry_t *driver )
Gilles Peskinef989dbe2019-06-26 18:18:12 +020095{
Gilles Peskine5243a202019-07-12 23:38:19 +020096 return( &driver->context );
Gilles Peskinef989dbe2019-06-26 18:18:12 +020097}
98
Gilles Peskine5243a202019-07-12 23:38:19 +020099int psa_get_se_driver( psa_key_lifetime_t lifetime,
100 const psa_drv_se_t **p_methods,
101 psa_drv_se_context_t **p_drv_context)
102{
103 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
104 if( p_methods != NULL )
105 *p_methods = ( driver ? driver->methods : NULL );
106 if( p_drv_context != NULL )
107 *p_drv_context = ( driver ? &driver->context : NULL );
108 return( driver != NULL );
109}
110
111
112
113/****************************************************************/
114/* Persistent data management */
115/****************************************************************/
116
117psa_status_t psa_load_se_persistent_data(
118 const psa_se_drv_table_entry_t *driver )
119{
120 /*TODO*/
121 (void) driver;
122 return( PSA_SUCCESS );
123}
124
125psa_status_t psa_save_se_persistent_data(
126 const psa_se_drv_table_entry_t *driver )
127{
128 /*TODO*/
129 (void) driver;
130 return( PSA_SUCCESS );
131}
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200132
Gilles Peskinecbaff462019-07-12 23:46:04 +0200133psa_status_t psa_find_se_slot_for_key(
134 const psa_key_attributes_t *attributes,
135 psa_se_drv_table_entry_t *driver,
136 psa_key_slot_number_t *slot_number )
137{
138 psa_status_t status;
139 psa_drv_se_allocate_key_t p_allocate = NULL;
140
141 /* If the lifetime is wrong, it's a bug in the library. */
142 if( driver->lifetime != attributes->lifetime )
143 return( PSA_ERROR_CORRUPTION_DETECTED );
144
145 /* If the driver doesn't support key creation in any way, give up now. */
146 if( driver->methods->key_management == NULL )
147 return( PSA_ERROR_NOT_SUPPORTED );
148 p_allocate = driver->methods->key_management->p_allocate;
149
150 /* If the driver doesn't tell us how to allocate a slot, that's
151 * not supported for the time being. */
152 if( p_allocate == NULL )
153 return( PSA_ERROR_NOT_SUPPORTED );
154
155 status = ( *p_allocate )( &driver->context,
156 driver->internal.persistent_data,
157 attributes,
158 slot_number );
159 return( status );
160}
161
Gilles Peskine354f7672019-07-12 23:46:38 +0200162psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
163 psa_key_slot_number_t slot_number )
164{
165 psa_status_t status;
166 psa_status_t storage_status;
167 if( driver->methods->key_management == NULL ||
168 driver->methods->key_management->p_destroy == NULL )
169 return( PSA_ERROR_NOT_PERMITTED );
170 status = driver->methods->key_management->p_destroy(
171 &driver->context,
172 driver->internal.persistent_data,
173 slot_number );
174 storage_status = psa_save_se_persistent_data( driver );
175 return( status == PSA_SUCCESS ? storage_status : status );
176}
177
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200178
179
180/****************************************************************/
181/* Driver registration */
182/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200183
184psa_status_t psa_register_se_driver(
185 psa_key_lifetime_t lifetime,
186 const psa_drv_se_t *methods)
187{
188 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200189 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200190
191 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
192 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200193 /* Driver table entries are 0-initialized. 0 is not a valid driver
194 * lifetime because it means a volatile key. */
195#if defined(static_assert)
196 static_assert( PSA_KEY_LIFETIME_VOLATILE == 0,
197 "Secure element support requires 0 to mean a volatile key" );
198#endif
Gilles Peskinea899a722019-06-24 14:06:43 +0200199 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
200 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
201 {
202 return( PSA_ERROR_INVALID_ARGUMENT );
203 }
204
205 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
206 {
207 if( driver_table[i].lifetime == 0 )
208 break;
209 /* Check that lifetime isn't already in use up to the first free
210 * entry. Since entries are created in order and never deleted,
211 * there can't be a used entry after the first free entry. */
212 if( driver_table[i].lifetime == lifetime )
213 return( PSA_ERROR_ALREADY_EXISTS );
214 }
215 if( i == PSA_MAX_SE_DRIVERS )
216 return( PSA_ERROR_INSUFFICIENT_MEMORY );
217
218 driver_table[i].lifetime = lifetime;
219 driver_table[i].methods = methods;
Gilles Peskine5243a202019-07-12 23:38:19 +0200220
221 if( methods->persistent_data_size != 0 )
222 {
223 driver_table[i].internal.persistent_data =
224 mbedtls_calloc( 1, methods->persistent_data_size );
225 if( driver_table[i].internal.persistent_data == NULL )
226 {
227 status = PSA_ERROR_INSUFFICIENT_MEMORY;
228 goto error;
229 }
230 status = psa_load_se_persistent_data( &driver_table[i] );
231 if( status != PSA_SUCCESS )
232 goto error;
233 }
234 driver_table[i].internal.persistent_data_size =
235 methods->persistent_data_size;
236
Gilles Peskinea899a722019-06-24 14:06:43 +0200237 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200238
239error:
240 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
241 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200242}
243
Gilles Peskined0890212019-06-24 14:34:43 +0200244void psa_unregister_all_se_drivers( void )
245{
Gilles Peskine5243a202019-07-12 23:38:19 +0200246 size_t i;
247 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
248 {
249 if( driver_table[i].internal.persistent_data != NULL )
250 mbedtls_free( driver_table[i].internal.persistent_data );
251 }
Gilles Peskined0890212019-06-24 14:34:43 +0200252 memset( driver_table, 0, sizeof( driver_table ) );
253}
254
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200255
256
257/****************************************************************/
258/* The end */
259/****************************************************************/
260
Gilles Peskinea8ade162019-06-26 11:24:49 +0200261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */