blob: 1e521d1748f0559a34f05815f1e8a2586514ecde [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
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 Peskine961849f2018-11-30 18:54:54 +01006 * 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 Peskine961849f2018-11-30 18:54:54 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010026#include "psa/crypto.h"
27
Gilles Peskine66fb1262018-12-10 16:29:04 +010028#include "psa_crypto_core.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010029#include "psa_crypto_slot_management.h"
30#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
32#include "psa_crypto_se.h"
33#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034
35#include <stdlib.h>
36#include <string.h>
37#if defined(MBEDTLS_PLATFORM_C)
38#include "mbedtls/platform.h"
39#else
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#endif
43
44#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
45
Gilles Peskine66fb1262018-12-10 16:29:04 +010046typedef struct
47{
48 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
49 unsigned key_slots_initialized : 1;
50} psa_global_data_t;
51
Gilles Peskine2e14bd32018-12-12 14:05:08 +010052static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010053
Ronald Cronfc9c5562020-10-15 19:24:49 +020054psa_status_t psa_validate_key_id(
55 mbedtls_svc_key_id_t key, int vendor_ok, int volatile_ok )
Ronald Crond2ed4812020-07-17 16:11:30 +020056{
57 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
58
59 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
60 ( key_id <= PSA_KEY_ID_USER_MAX ) )
61 return( PSA_SUCCESS );
62
63 if( vendor_ok &&
64 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
Ronald Cronfc9c5562020-10-15 19:24:49 +020065 ( key_id < PSA_KEY_ID_VOLATILE_MIN ) )
66 return( PSA_SUCCESS );
67
68 if( volatile_ok &&
69 ( PSA_KEY_ID_VOLATILE_MIN <= key_id ) &&
70 ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) )
Ronald Crond2ed4812020-07-17 16:11:30 +020071 return( PSA_SUCCESS );
72
Ronald Cronc4d1b512020-07-31 11:26:37 +020073 return( PSA_ERROR_INVALID_HANDLE );
Ronald Crond2ed4812020-07-17 16:11:30 +020074}
75
Ronald Cronc4d1b512020-07-31 11:26:37 +020076static psa_key_slot_t* psa_get_slot_from_volatile_key_id(
77 mbedtls_svc_key_id_t key )
Gilles Peskine66fb1262018-12-10 16:29:04 +010078{
Ronald Cronc4d1b512020-07-31 11:26:37 +020079 psa_key_slot_t *slot;
80 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Gilles Peskine66fb1262018-12-10 16:29:04 +010081
Ronald Cronc4d1b512020-07-31 11:26:37 +020082 if( ( key_id < PSA_KEY_ID_VOLATILE_MIN ) ||
83 ( key_id > PSA_KEY_ID_VOLATILE_MAX ) )
84 return( NULL );
Gilles Peskine66fb1262018-12-10 16:29:04 +010085
Ronald Cronc4d1b512020-07-31 11:26:37 +020086 slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
Gilles Peskine66fb1262018-12-10 16:29:04 +010087
Ronald Cronc4d1b512020-07-31 11:26:37 +020088 return( mbedtls_svc_key_id_equal( key, slot->attr.id ) ? slot : NULL );
Gilles Peskine66fb1262018-12-10 16:29:04 +010089}
90
Ronald Cronc4d1b512020-07-31 11:26:37 +020091#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
92static psa_key_slot_t* psa_get_slot_from_key_id(
93 mbedtls_svc_key_id_t key )
94{
95 psa_key_slot_t *slot = &global_data.key_slots[ PSA_KEY_SLOT_COUNT ];
96
97 while( slot > &global_data.key_slots[ 0 ] )
98 {
99 slot--;
100 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
101 return( slot );
102 }
103
104 return( NULL );
105}
106#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
107
Gilles Peskine66fb1262018-12-10 16:29:04 +0100108psa_status_t psa_initialize_key_slots( void )
109{
110 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
111 * guarantee that the key slots are initialized to all-zero, which
112 * means that all the key slots are in a valid, empty state. */
113 global_data.key_slots_initialized = 1;
114 return( PSA_SUCCESS );
115}
116
117void psa_wipe_all_key_slots( void )
118{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200119 size_t slot_idx;
120
121 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100122 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200123 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Gilles Peskine66fb1262018-12-10 16:29:04 +0100124 (void) psa_wipe_key_slot( slot );
125 }
126 global_data.key_slots_initialized = 0;
127}
128
Ronald Cronc4d1b512020-07-31 11:26:37 +0200129psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Ronald Cron2a993152020-07-17 14:13:26 +0200130 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100131{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200132 size_t slot_idx;
133
Gilles Peskine267c6562019-05-27 19:01:54 +0200134 if( ! global_data.key_slots_initialized )
135 return( PSA_ERROR_BAD_STATE );
136
Ronald Cron98a54dd2020-07-24 16:33:11 +0200137 for( slot_idx = PSA_KEY_SLOT_COUNT; slot_idx > 0; slot_idx-- )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100138 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200139 *p_slot = &global_data.key_slots[ slot_idx - 1 ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200140 if( ! psa_is_key_slot_occupied( *p_slot ) )
Ronald Cron2a993152020-07-17 14:13:26 +0200141 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200142 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
143 ( (psa_key_id_t)slot_idx ) - 1;
Ronald Cron2a993152020-07-17 14:13:26 +0200144
Gilles Peskine66fb1262018-12-10 16:29:04 +0100145 return( PSA_SUCCESS );
Ronald Cron2a993152020-07-17 14:13:26 +0200146 }
Gilles Peskine66fb1262018-12-10 16:29:04 +0100147 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200148 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100149 return( PSA_ERROR_INSUFFICIENT_MEMORY );
150}
151
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100152#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200153static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100154{
155 psa_status_t status = PSA_SUCCESS;
156 uint8_t *key_data = NULL;
157 size_t key_data_length = 0;
158
Gilles Peskine24318592019-07-30 20:30:51 +0200159 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200160 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100161 if( status != PSA_SUCCESS )
162 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200163
164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200165 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200166 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200167 psa_se_key_data_storage_t *data;
168 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200169 {
170 status = PSA_ERROR_STORAGE_FAILURE;
171 goto exit;
172 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200173 data = (psa_se_key_data_storage_t *) key_data;
174 memcpy( &slot->data.se.slot_number, &data->slot_number,
175 sizeof( slot->data.se.slot_number ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200176 }
177 else
178#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
179 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200180 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
181 if( status != PSA_SUCCESS )
182 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200183 }
184
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100185exit:
186 psa_free_persistent_key_data( key_data, key_data_length );
187 return( status );
188}
Ronald Cronc4d1b512020-07-31 11:26:37 +0200189#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
190
191psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key,
192 psa_key_slot_t **p_slot )
193{
194 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
195
196 *p_slot = NULL;
197 if( ! global_data.key_slots_initialized )
198 return( PSA_ERROR_BAD_STATE );
199
Ronald Cronfc9c5562020-10-15 19:24:49 +0200200 status = psa_validate_key_id( key, 1, 1 );
Ronald Cronc4d1b512020-07-31 11:26:37 +0200201 if( status != PSA_SUCCESS )
202 return( status );
203
204 *p_slot = psa_get_slot_from_volatile_key_id( key );
205 if( *p_slot != NULL )
206 return( PSA_SUCCESS );
207
208#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
209 psa_key_id_t volatile_key_id;
210
211 *p_slot = psa_get_slot_from_key_id( key );
212 if( *p_slot != NULL )
213 return( PSA_SUCCESS );
214
215 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
216 if( status != PSA_SUCCESS )
217 return( status );
218
219 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
220 (*p_slot)->attr.id = key;
221
222 status = psa_load_persistent_key_into_slot( *p_slot );
223 if( status != PSA_SUCCESS )
224 psa_wipe_key_slot( *p_slot );
225
226 return( status );
227#else
228 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200229#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100230
Ronald Cronc4d1b512020-07-31 11:26:37 +0200231}
232
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200233psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200234 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200235{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200236 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200237 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman00106a12020-06-08 18:54:23 +0200239 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
240 if( driver == NULL )
Gilles Peskine011e4282019-06-26 18:34:38 +0200241 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200242 else
243 {
244 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200245 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200246 return( PSA_SUCCESS );
247 }
248#else
249 (void) p_drv;
250 return( PSA_ERROR_INVALID_ARGUMENT );
251#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200252 }
253 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200254 /* Local/internal keys are always valid */
255 return( PSA_SUCCESS );
256}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200257
Ronald Crond2ed4812020-07-17 16:11:30 +0200258psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200259{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200260 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
261 {
262 /* Volatile keys are always supported */
263 return( PSA_SUCCESS );
264 }
265 else
266 {
267 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200268#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond2ed4812020-07-17 16:11:30 +0200269 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200270#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200271 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200272#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200273 }
Gilles Peskined167b942019-04-19 18:19:40 +0200274}
275
Ronald Cron71016a92020-08-28 19:01:50 +0200276psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100277{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200278#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100279 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200280 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100281
Ronald Cronc4d1b512020-07-31 11:26:37 +0200282 status = psa_get_key_slot( key, &slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200283 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100284 {
Ronald Cron91e95152020-07-30 17:48:03 +0200285 *handle = PSA_KEY_HANDLE_INIT;
Ronald Cronc4d1b512020-07-31 11:26:37 +0200286 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100287 }
Ronald Cronc4d1b512020-07-31 11:26:37 +0200288
289 *handle = key;
290
291 return( PSA_SUCCESS );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200292
Gilles Peskine30afafd2019-04-25 13:47:40 +0200293#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200294 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200295 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200296 return( PSA_ERROR_NOT_SUPPORTED );
297#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100298}
299
Gilles Peskine961849f2018-11-30 18:54:54 +0100300psa_status_t psa_close_key( psa_key_handle_t handle )
301{
Gilles Peskine267c6562019-05-27 19:01:54 +0200302 psa_status_t status;
303 psa_key_slot_t *slot;
304
Ronald Cronc26f8d42020-09-01 10:51:51 +0200305 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200306 return( PSA_SUCCESS );
307
Gilles Peskine267c6562019-05-27 19:01:54 +0200308 status = psa_get_key_slot( handle, &slot );
309 if( status != PSA_SUCCESS )
310 return( status );
311
312 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100313}
314
Ronald Cron277a85f2020-08-04 15:49:48 +0200315psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
316{
317 psa_status_t status;
318 psa_key_slot_t *slot;
319
320 status = psa_get_key_slot( key, &slot );
321 if( status != PSA_SUCCESS )
322 return( status );
323
324 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
325 return PSA_SUCCESS;
326
327 return( psa_wipe_key_slot( slot ) );
328}
329
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200330void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
331{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200332 size_t slot_idx;
333
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200334 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200335
336 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200337 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200338 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200339 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200340 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200341 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200342 continue;
343 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200344 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200345 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200346 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200347 {
Ronald Cron71016a92020-08-28 19:01:50 +0200348 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200349 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100350 if( id > stats->max_open_internal_key_id )
351 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200352 }
353 else
354 {
Ronald Cron71016a92020-08-28 19:01:50 +0200355 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200356 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100357 if( id > stats->max_open_external_key_id )
358 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200359 }
360 }
361}
362
Gilles Peskine961849f2018-11-30 18:54:54 +0100363#endif /* MBEDTLS_PSA_CRYPTO_C */