blob: f33c4f2ef7b45a63f9311bd38d5a666b3441d83e [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
54/* Access a key slot at the given handle. The handle of a key slot is
55 * the index of the slot in the global slot array, plus one so that handles
56 * start at 1 and not 0. */
57psa_status_t psa_get_key_slot( psa_key_handle_t handle,
58 psa_key_slot_t **p_slot )
59{
60 psa_key_slot_t *slot = NULL;
61
62 if( ! global_data.key_slots_initialized )
63 return( PSA_ERROR_BAD_STATE );
64
65 /* 0 is not a valid handle under any circumstance. This
66 * implementation provides slots number 1 to N where N is the
67 * number of available slots. */
68 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
69 return( PSA_ERROR_INVALID_HANDLE );
70 slot = &global_data.key_slots[handle - 1];
71
Gilles Peskine41e50d22019-07-31 15:01:55 +020072 /* If the slot isn't occupied, the handle is invalid. */
73 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010074 return( PSA_ERROR_INVALID_HANDLE );
75
76 *p_slot = slot;
77 return( PSA_SUCCESS );
78}
79
80psa_status_t psa_initialize_key_slots( void )
81{
82 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
83 * guarantee that the key slots are initialized to all-zero, which
84 * means that all the key slots are in a valid, empty state. */
85 global_data.key_slots_initialized = 1;
86 return( PSA_SUCCESS );
87}
88
89void psa_wipe_all_key_slots( void )
90{
91 psa_key_handle_t key;
92 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
93 {
94 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
95 (void) psa_wipe_key_slot( slot );
96 }
97 global_data.key_slots_initialized = 0;
98}
99
Gilles Peskineedbed562019-08-07 18:19:59 +0200100psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Gilles Peskine267c6562019-05-27 19:01:54 +0200101 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100102{
Gilles Peskine267c6562019-05-27 19:01:54 +0200103 if( ! global_data.key_slots_initialized )
104 return( PSA_ERROR_BAD_STATE );
105
Gilles Peskine66fb1262018-12-10 16:29:04 +0100106 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
107 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200108 *p_slot = &global_data.key_slots[*handle - 1];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200109 if( ! psa_is_key_slot_occupied( *p_slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100110 return( PSA_SUCCESS );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100111 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200112 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100113 return( PSA_ERROR_INSUFFICIENT_MEMORY );
114}
115
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100116#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200117static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100118{
119 psa_status_t status = PSA_SUCCESS;
120 uint8_t *key_data = NULL;
121 size_t key_data_length = 0;
122
Gilles Peskine24318592019-07-30 20:30:51 +0200123 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200124 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100125 if( status != PSA_SUCCESS )
126 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200127
128#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200129 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200130 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200131 psa_se_key_data_storage_t *data;
132 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200133 {
134 status = PSA_ERROR_STORAGE_FAILURE;
135 goto exit;
136 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200137 data = (psa_se_key_data_storage_t *) key_data;
138 memcpy( &slot->data.se.slot_number, &data->slot_number,
139 sizeof( slot->data.se.slot_number ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200140 }
141 else
142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
143 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200144 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
145 if( status != PSA_SUCCESS )
146 goto exit;
147
148 status = psa_detect_bit_size_in_slot( slot );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200149 }
150
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100151exit:
152 psa_free_persistent_key_data( key_data, key_data_length );
153 return( status );
154}
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100155
156/** Check whether a key identifier is acceptable.
157 *
158 * For backward compatibility, key identifiers that were valid in a
159 * past released version must remain valid, unless a migration path
160 * is provided.
161 *
Ronald Cron71016a92020-08-28 19:01:50 +0200162 * \param key The key identifier to check.
163 * \param vendor_ok Nonzero to allow key ids in the vendor range.
164 * 0 to allow only key ids in the application range.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100165 *
Ronald Cron71016a92020-08-28 19:01:50 +0200166 * \return 1 if \p key is acceptable, otherwise 0.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100167 */
Ronald Cron71016a92020-08-28 19:01:50 +0200168static int psa_is_key_id_valid( mbedtls_svc_key_id_t key, int vendor_ok )
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100169{
Ronald Cron71016a92020-08-28 19:01:50 +0200170 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
Gilles Peskinef9fbc382019-05-15 18:42:09 +0200171 if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
172 return( 1 );
173 else if( vendor_ok &&
174 PSA_KEY_ID_VENDOR_MIN <= key_id &&
175 key_id <= PSA_KEY_ID_VENDOR_MAX )
176 return( 1 );
177 else
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100178 return( 0 );
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100179}
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
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200207psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime,
Ronald Cron71016a92020-08-28 19:01:50 +0200208 mbedtls_svc_key_id_t key )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200209{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200210 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
211 {
212 /* Volatile keys are always supported */
213 return( PSA_SUCCESS );
214 }
215 else
216 {
217 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200218#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Cron27238fc2020-07-23 12:30:41 +0200219 if( psa_is_key_id_valid( key,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200220 psa_key_lifetime_is_external( lifetime ) ) )
221 return( PSA_SUCCESS );
222 else
223 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200224#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Ronald Cron27238fc2020-07-23 12:30:41 +0200225 (void) key;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200226 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200227#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200228 }
Gilles Peskined167b942019-04-19 18:19:40 +0200229}
230
Ronald Cron71016a92020-08-28 19:01:50 +0200231psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100232{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200233#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100234 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200235 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100236
237 *handle = 0;
238
Ronald Cron27238fc2020-07-23 12:30:41 +0200239 if( ! psa_is_key_id_valid( key, 1 ) )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100241
Gilles Peskineedbed562019-08-07 18:19:59 +0200242 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100243 if( status != PSA_SUCCESS )
244 return( status );
245
Gilles Peskine8e338702019-07-30 20:06:31 +0200246 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Ronald Cron27238fc2020-07-23 12:30:41 +0200247 slot->attr.id = key;
Gilles Peskine267c6562019-05-27 19:01:54 +0200248
249 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200250 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100251 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200252 psa_wipe_key_slot( slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100253 *handle = 0;
254 }
255 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200256
Gilles Peskine30afafd2019-04-25 13:47:40 +0200257#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Ronald Cron27238fc2020-07-23 12:30:41 +0200258 (void) key;
Gilles Peskine70e085a2019-05-27 19:04:07 +0200259 *handle = 0;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200260 return( PSA_ERROR_NOT_SUPPORTED );
261#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100262}
263
Gilles Peskine961849f2018-11-30 18:54:54 +0100264psa_status_t psa_close_key( psa_key_handle_t handle )
265{
Gilles Peskine267c6562019-05-27 19:01:54 +0200266 psa_status_t status;
267 psa_key_slot_t *slot;
268
Gilles Peskine1841cf42019-10-08 15:48:25 +0200269 if( handle == 0 )
270 return( PSA_SUCCESS );
271
Gilles Peskine267c6562019-05-27 19:01:54 +0200272 status = psa_get_key_slot( handle, &slot );
273 if( status != PSA_SUCCESS )
274 return( status );
275
276 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100277}
278
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200279void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
280{
281 psa_key_handle_t key;
282 memset( stats, 0, sizeof( *stats ) );
283 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
284 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200285 const psa_key_slot_t *slot = &global_data.key_slots[key - 1];
286 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200287 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200288 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200289 continue;
290 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200291 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200292 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200293 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200294 {
Ronald Cron71016a92020-08-28 19:01:50 +0200295 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200296 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100297 if( id > stats->max_open_internal_key_id )
298 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200299 }
300 else
301 {
Ronald Cron71016a92020-08-28 19:01:50 +0200302 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200303 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100304 if( id > stats->max_open_external_key_id )
305 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200306 }
307 }
308}
309
Gilles Peskine961849f2018-11-30 18:54:54 +0100310#endif /* MBEDTLS_PSA_CRYPTO_C */