blob: fb57fc962522639c23056e5e017660d38dfcd9b1 [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 Peskinef989dbe2019-06-26 18:18:12 +0200162
163
164/****************************************************************/
165/* Driver registration */
166/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +0200167
168psa_status_t psa_register_se_driver(
169 psa_key_lifetime_t lifetime,
170 const psa_drv_se_t *methods)
171{
172 size_t i;
Gilles Peskine5243a202019-07-12 23:38:19 +0200173 psa_status_t status;
Gilles Peskinea899a722019-06-24 14:06:43 +0200174
175 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
176 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +0200177 /* Driver table entries are 0-initialized. 0 is not a valid driver
178 * lifetime because it means a volatile key. */
179#if defined(static_assert)
180 static_assert( PSA_KEY_LIFETIME_VOLATILE == 0,
181 "Secure element support requires 0 to mean a volatile key" );
182#endif
Gilles Peskinea899a722019-06-24 14:06:43 +0200183 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
184 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
185 {
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 }
188
189 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
190 {
191 if( driver_table[i].lifetime == 0 )
192 break;
193 /* Check that lifetime isn't already in use up to the first free
194 * entry. Since entries are created in order and never deleted,
195 * there can't be a used entry after the first free entry. */
196 if( driver_table[i].lifetime == lifetime )
197 return( PSA_ERROR_ALREADY_EXISTS );
198 }
199 if( i == PSA_MAX_SE_DRIVERS )
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201
202 driver_table[i].lifetime = lifetime;
203 driver_table[i].methods = methods;
Gilles Peskine5243a202019-07-12 23:38:19 +0200204
205 if( methods->persistent_data_size != 0 )
206 {
207 driver_table[i].internal.persistent_data =
208 mbedtls_calloc( 1, methods->persistent_data_size );
209 if( driver_table[i].internal.persistent_data == NULL )
210 {
211 status = PSA_ERROR_INSUFFICIENT_MEMORY;
212 goto error;
213 }
214 status = psa_load_se_persistent_data( &driver_table[i] );
215 if( status != PSA_SUCCESS )
216 goto error;
217 }
218 driver_table[i].internal.persistent_data_size =
219 methods->persistent_data_size;
220
Gilles Peskinea899a722019-06-24 14:06:43 +0200221 return( PSA_SUCCESS );
Gilles Peskine5243a202019-07-12 23:38:19 +0200222
223error:
224 memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
225 return( status );
Gilles Peskinea899a722019-06-24 14:06:43 +0200226}
227
Gilles Peskined0890212019-06-24 14:34:43 +0200228void psa_unregister_all_se_drivers( void )
229{
Gilles Peskine5243a202019-07-12 23:38:19 +0200230 size_t i;
231 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
232 {
233 if( driver_table[i].internal.persistent_data != NULL )
234 mbedtls_free( driver_table[i].internal.persistent_data );
235 }
Gilles Peskined0890212019-06-24 14:34:43 +0200236 memset( driver_table, 0, sizeof( driver_table ) );
237}
238
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200239
240
241/****************************************************************/
242/* The end */
243/****************************************************************/
244
Gilles Peskinea8ade162019-06-26 11:24:49 +0200245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */