blob: 5ceac846a0c5d73fc9839859c3d9653b9369572b [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/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010024
25#if defined(MBEDTLS_PSA_CRYPTO_C)
26
itayzafrir7723ab12019-02-14 10:28:02 +020027#include "psa_crypto_service_integration.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010028#include "psa/crypto.h"
29
Gilles Peskine66fb1262018-12-10 16:29:04 +010030#include "psa_crypto_core.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
32#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
34#include "psa_crypto_se.h"
35#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010036
37#include <stdlib.h>
38#include <string.h>
39#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#endif
45
46#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
47
Gilles Peskine66fb1262018-12-10 16:29:04 +010048typedef struct
49{
50 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
51 unsigned key_slots_initialized : 1;
52} psa_global_data_t;
53
Gilles Peskine2e14bd32018-12-12 14:05:08 +010054static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010055
56/* Access a key slot at the given handle. The handle of a key slot is
57 * the index of the slot in the global slot array, plus one so that handles
58 * start at 1 and not 0. */
59psa_status_t psa_get_key_slot( psa_key_handle_t handle,
60 psa_key_slot_t **p_slot )
61{
62 psa_key_slot_t *slot = NULL;
63
64 if( ! global_data.key_slots_initialized )
65 return( PSA_ERROR_BAD_STATE );
66
67 /* 0 is not a valid handle under any circumstance. This
68 * implementation provides slots number 1 to N where N is the
69 * number of available slots. */
70 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
71 return( PSA_ERROR_INVALID_HANDLE );
72 slot = &global_data.key_slots[handle - 1];
73
Gilles Peskine41e50d22019-07-31 15:01:55 +020074 /* If the slot isn't occupied, the handle is invalid. */
75 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010076 return( PSA_ERROR_INVALID_HANDLE );
77
78 *p_slot = slot;
79 return( PSA_SUCCESS );
80}
81
82psa_status_t psa_initialize_key_slots( void )
83{
84 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
85 * guarantee that the key slots are initialized to all-zero, which
86 * means that all the key slots are in a valid, empty state. */
87 global_data.key_slots_initialized = 1;
88 return( PSA_SUCCESS );
89}
90
91void psa_wipe_all_key_slots( void )
92{
93 psa_key_handle_t key;
94 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
95 {
96 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
97 (void) psa_wipe_key_slot( slot );
98 }
99 global_data.key_slots_initialized = 0;
100}
101
Gilles Peskineedbed562019-08-07 18:19:59 +0200102psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Gilles Peskine267c6562019-05-27 19:01:54 +0200103 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100104{
Gilles Peskine267c6562019-05-27 19:01:54 +0200105 if( ! global_data.key_slots_initialized )
106 return( PSA_ERROR_BAD_STATE );
107
Gilles Peskine66fb1262018-12-10 16:29:04 +0100108 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
109 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200110 *p_slot = &global_data.key_slots[*handle - 1];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200111 if( ! psa_is_key_slot_occupied( *p_slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100112 return( PSA_SUCCESS );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100113 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200114 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100115 return( PSA_ERROR_INSUFFICIENT_MEMORY );
116}
117
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100118#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200119static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100120{
121 psa_status_t status = PSA_SUCCESS;
122 uint8_t *key_data = NULL;
123 size_t key_data_length = 0;
124
Gilles Peskine24318592019-07-30 20:30:51 +0200125 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200126 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100127 if( status != PSA_SUCCESS )
128 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200129
130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200131 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200132 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200133 psa_se_key_data_storage_t *data;
134 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200135 {
136 status = PSA_ERROR_STORAGE_FAILURE;
137 goto exit;
138 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200139 data = (psa_se_key_data_storage_t *) key_data;
140 memcpy( &slot->data.se.slot_number, &data->slot_number,
141 sizeof( slot->data.se.slot_number ) );
142 memcpy( &slot->attr.bits, &data->bits,
143 sizeof( slot->attr.bits ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200144 }
145 else
146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
147 {
Gilles Peskine24318592019-07-30 20:30:51 +0200148 status = psa_import_key_into_slot( slot, key_data, key_data_length );
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 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100162 * \param file_id The key identifier to check.
Gilles Peskinef9666592019-05-06 18:56:30 +0200163 * \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 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100166 * \return 1 if \p file_id is acceptable, otherwise 0.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100167 */
Gilles Peskinef9666592019-05-06 18:56:30 +0200168static int psa_is_key_id_valid( psa_key_file_id_t file_id,
169 int vendor_ok )
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100170{
Gilles Peskine5b229a02019-02-19 13:24:37 +0100171 psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
Gilles Peskinef9fbc382019-05-15 18:42:09 +0200172 if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
173 return( 1 );
174 else if( vendor_ok &&
175 PSA_KEY_ID_VENDOR_MIN <= key_id &&
176 key_id <= PSA_KEY_ID_VENDOR_MAX )
177 return( 1 );
178 else
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100179 return( 0 );
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100180}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200181#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100182
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200183psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200184 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200185{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200186 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200187 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200188#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman00106a12020-06-08 18:54:23 +0200189 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
190 if( driver == NULL )
Gilles Peskine011e4282019-06-26 18:34:38 +0200191 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200192 else
193 {
194 if (p_drv != NULL)
Steven Cooreman00106a12020-06-08 18:54:23 +0200195 *p_drv = driver;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200196 return( PSA_SUCCESS );
197 }
198#else
199 (void) p_drv;
200 return( PSA_ERROR_INVALID_ARGUMENT );
201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200202 }
203 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200204 /* Local/internal keys are always valid */
205 return( PSA_SUCCESS );
206}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200207
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200208psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime,
209 psa_key_id_t key_id )
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200210{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200211 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
212 {
213 /* Volatile keys are always supported */
214 return( PSA_SUCCESS );
215 }
216 else
217 {
218 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200219#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200220 if( psa_is_key_id_valid( key_id,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200221 psa_key_lifetime_is_external( lifetime ) ) )
222 return( PSA_SUCCESS );
223 else
224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200225#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooremanfa686092020-06-18 14:37:31 +0200226 (void) key_id;
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200227 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200228#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200229 }
Gilles Peskined167b942019-04-19 18:19:40 +0200230}
231
Gilles Peskine70e085a2019-05-27 19:04:07 +0200232psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100233{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200234#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100235 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200236 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100237
238 *handle = 0;
239
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200240 if( ! psa_is_key_id_valid( id, 1 ) )
241 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100242
Gilles Peskineedbed562019-08-07 18:19:59 +0200243 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100244 if( status != PSA_SUCCESS )
245 return( status );
246
Gilles Peskine8e338702019-07-30 20:06:31 +0200247 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
248 slot->attr.id = id;
Gilles Peskine267c6562019-05-27 19:01:54 +0200249
250 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200251 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100252 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200253 psa_wipe_key_slot( slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100254 *handle = 0;
255 }
256 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200257
Gilles Peskine30afafd2019-04-25 13:47:40 +0200258#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine70e085a2019-05-27 19:04:07 +0200259 (void) id;
260 *handle = 0;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200261 return( PSA_ERROR_NOT_SUPPORTED );
262#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100263}
264
Gilles Peskine961849f2018-11-30 18:54:54 +0100265psa_status_t psa_close_key( psa_key_handle_t handle )
266{
Gilles Peskine267c6562019-05-27 19:01:54 +0200267 psa_status_t status;
268 psa_key_slot_t *slot;
269
Gilles Peskine1841cf42019-10-08 15:48:25 +0200270 if( handle == 0 )
271 return( PSA_SUCCESS );
272
Gilles Peskine267c6562019-05-27 19:01:54 +0200273 status = psa_get_key_slot( handle, &slot );
274 if( status != PSA_SUCCESS )
275 return( status );
276
277 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100278}
279
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200280void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
281{
282 psa_key_handle_t key;
283 memset( stats, 0, sizeof( *stats ) );
284 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
285 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200286 const psa_key_slot_t *slot = &global_data.key_slots[key - 1];
287 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200288 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200289 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200290 continue;
291 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200292 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200293 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200294 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200295 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100296 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200297 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100298 if( id > stats->max_open_internal_key_id )
299 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200300 }
301 else
302 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100303 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200304 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100305 if( id > stats->max_open_external_key_id )
306 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200307 }
308 }
309}
310
Gilles Peskine961849f2018-11-30 18:54:54 +0100311#endif /* MBEDTLS_PSA_CRYPTO_C */