blob: 6303473d9d9d8d11303c58240dc97857f4a1d46d [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 Crond2ed4812020-07-17 16:11:30 +020054psa_status_t psa_validate_key_id( mbedtls_svc_key_id_t key, int vendor_ok )
55{
56 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
57
58 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
59 ( key_id <= PSA_KEY_ID_USER_MAX ) )
60 return( PSA_SUCCESS );
61
62 if( vendor_ok &&
63 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
64 ( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
65 return( PSA_SUCCESS );
66
67 return( PSA_ERROR_INVALID_ARGUMENT );
68}
69
Gilles Peskine66fb1262018-12-10 16:29:04 +010070/* Access a key slot at the given handle. The handle of a key slot is
71 * the index of the slot in the global slot array, plus one so that handles
72 * start at 1 and not 0. */
73psa_status_t psa_get_key_slot( psa_key_handle_t handle,
74 psa_key_slot_t **p_slot )
75{
76 psa_key_slot_t *slot = NULL;
77
78 if( ! global_data.key_slots_initialized )
79 return( PSA_ERROR_BAD_STATE );
80
81 /* 0 is not a valid handle under any circumstance. This
82 * implementation provides slots number 1 to N where N is the
83 * number of available slots. */
Ronald Cronc26f8d42020-09-01 10:51:51 +020084 if( psa_key_handle_is_null( handle ) ||
85 ( handle > ARRAY_LENGTH( global_data.key_slots ) ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010086 return( PSA_ERROR_INVALID_HANDLE );
87 slot = &global_data.key_slots[handle - 1];
88
Gilles Peskine41e50d22019-07-31 15:01:55 +020089 /* If the slot isn't occupied, the handle is invalid. */
90 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010091 return( PSA_ERROR_INVALID_HANDLE );
92
93 *p_slot = slot;
94 return( PSA_SUCCESS );
95}
96
97psa_status_t psa_initialize_key_slots( void )
98{
99 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
100 * guarantee that the key slots are initialized to all-zero, which
101 * means that all the key slots are in a valid, empty state. */
102 global_data.key_slots_initialized = 1;
103 return( PSA_SUCCESS );
104}
105
106void psa_wipe_all_key_slots( void )
107{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200108 size_t slot_idx;
109
110 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100111 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200112 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Gilles Peskine66fb1262018-12-10 16:29:04 +0100113 (void) psa_wipe_key_slot( slot );
114 }
115 global_data.key_slots_initialized = 0;
116}
117
Gilles Peskineedbed562019-08-07 18:19:59 +0200118psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Ronald Cron2a993152020-07-17 14:13:26 +0200119 psa_key_id_t *volatile_key_id,
120 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100121{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200122 size_t slot_idx;
123
Gilles Peskine267c6562019-05-27 19:01:54 +0200124 if( ! global_data.key_slots_initialized )
125 return( PSA_ERROR_BAD_STATE );
126
Ronald Cron98a54dd2020-07-24 16:33:11 +0200127 for( slot_idx = PSA_KEY_SLOT_COUNT; slot_idx > 0; slot_idx-- )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100128 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200129 *p_slot = &global_data.key_slots[ slot_idx - 1 ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200130 if( ! psa_is_key_slot_occupied( *p_slot ) )
Ronald Cron2a993152020-07-17 14:13:26 +0200131 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200132 *handle = (psa_key_handle_t)slot_idx;
133 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
134 ( (psa_key_id_t)slot_idx ) - 1;
Ronald Cron2a993152020-07-17 14:13:26 +0200135
Gilles Peskine66fb1262018-12-10 16:29:04 +0100136 return( PSA_SUCCESS );
Ronald Cron2a993152020-07-17 14:13:26 +0200137 }
Gilles Peskine66fb1262018-12-10 16:29:04 +0100138 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200139 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100140 return( PSA_ERROR_INSUFFICIENT_MEMORY );
141}
142
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100143#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200144static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100145{
146 psa_status_t status = PSA_SUCCESS;
147 uint8_t *key_data = NULL;
148 size_t key_data_length = 0;
149
Gilles Peskine24318592019-07-30 20:30:51 +0200150 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200151 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100152 if( status != PSA_SUCCESS )
153 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200154
155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200156 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200157 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200158 psa_se_key_data_storage_t *data;
159 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200160 {
161 status = PSA_ERROR_STORAGE_FAILURE;
162 goto exit;
163 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200164 data = (psa_se_key_data_storage_t *) key_data;
165 memcpy( &slot->data.se.slot_number, &data->slot_number,
166 sizeof( slot->data.se.slot_number ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200167 }
168 else
169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
170 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200171 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
172 if( status != PSA_SUCCESS )
173 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200174 }
175
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100176exit:
177 psa_free_persistent_key_data( key_data, key_data_length );
178 return( status );
179}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200180#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100181
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200182psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200183 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200184{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200185 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200186 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman00106a12020-06-08 18:54:23 +0200188 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
189 if( driver == NULL )
Gilles Peskine011e4282019-06-26 18:34:38 +0200190 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200191 else
192 {
193 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200194 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200195 return( PSA_SUCCESS );
196 }
197#else
198 (void) p_drv;
199 return( PSA_ERROR_INVALID_ARGUMENT );
200#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200201 }
202 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200203 /* Local/internal keys are always valid */
204 return( PSA_SUCCESS );
205}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200206
Ronald Crond2ed4812020-07-17 16:11:30 +0200207psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200208{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200209 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
210 {
211 /* Volatile keys are always supported */
212 return( PSA_SUCCESS );
213 }
214 else
215 {
216 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200217#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond2ed4812020-07-17 16:11:30 +0200218 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200219#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200220 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200221#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200222 }
Gilles Peskined167b942019-04-19 18:19:40 +0200223}
224
Ronald Cron71016a92020-08-28 19:01:50 +0200225psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100226{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200227#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100228 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +0200229 psa_key_id_t volatile_key_id;
Gilles Peskine267c6562019-05-27 19:01:54 +0200230 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100231
232 *handle = 0;
233
Ronald Crond2ed4812020-07-17 16:11:30 +0200234 status = psa_validate_key_id( key, 1 );
235 if( status != PSA_SUCCESS )
236 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100237
Ronald Cron2a993152020-07-17 14:13:26 +0200238 status = psa_get_empty_key_slot( handle, &volatile_key_id, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100239 if( status != PSA_SUCCESS )
240 return( status );
241
Gilles Peskine8e338702019-07-30 20:06:31 +0200242 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cron27238fc2020-07-23 12:30:41 +0200243 slot->attr.id = key;
Gilles Peskine267c6562019-05-27 19:01:54 +0200244
245 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200246 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100247 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200248 psa_wipe_key_slot( slot );
Ronald Cron91e95152020-07-30 17:48:03 +0200249 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine961849f2018-11-30 18:54:54 +0100250 }
251 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200252
Gilles Peskine30afafd2019-04-25 13:47:40 +0200253#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200254 (void) key;
Ronald Cron91e95152020-07-30 17:48:03 +0200255 *handle = PSA_KEY_HANDLE_INIT;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200256 return( PSA_ERROR_NOT_SUPPORTED );
257#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100258}
259
Gilles Peskine961849f2018-11-30 18:54:54 +0100260psa_status_t psa_close_key( psa_key_handle_t handle )
261{
Gilles Peskine267c6562019-05-27 19:01:54 +0200262 psa_status_t status;
263 psa_key_slot_t *slot;
264
Ronald Cronc26f8d42020-09-01 10:51:51 +0200265 if( psa_key_handle_is_null( handle ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200266 return( PSA_SUCCESS );
267
Gilles Peskine267c6562019-05-27 19:01:54 +0200268 status = psa_get_key_slot( handle, &slot );
269 if( status != PSA_SUCCESS )
270 return( status );
271
272 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100273}
274
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200275void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
276{
Ronald Cron98a54dd2020-07-24 16:33:11 +0200277 size_t slot_idx;
278
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200279 memset( stats, 0, sizeof( *stats ) );
Ronald Cron98a54dd2020-07-24 16:33:11 +0200280
281 for( slot_idx = 0; slot_idx < PSA_KEY_SLOT_COUNT; slot_idx++ )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200282 {
Ronald Cron98a54dd2020-07-24 16:33:11 +0200283 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200284 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200285 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200286 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200287 continue;
288 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200289 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200290 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200291 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200292 {
Ronald Cron71016a92020-08-28 19:01:50 +0200293 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200294 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100295 if( id > stats->max_open_internal_key_id )
296 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200297 }
298 else
299 {
Ronald Cron71016a92020-08-28 19:01:50 +0200300 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200301 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100302 if( id > stats->max_open_external_key_id )
303 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200304 }
305 }
306}
307
Gilles Peskine961849f2018-11-30 18:54:54 +0100308#endif /* MBEDTLS_PSA_CRYPTO_C */