blob: 01fd048162acfb3fa82dc9b99a772e5217f93570 [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, 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
28#if defined(MBEDTLS_PSA_CRYPTO_C)
29
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa/crypto.h"
32
Gilles Peskine66fb1262018-12-10 16:29:04 +010033#include "psa_crypto_core.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
35#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
37#include "psa_crypto_se.h"
38#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010039
40#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
49#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
50
Gilles Peskine66fb1262018-12-10 16:29:04 +010051typedef struct
52{
53 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
54 unsigned key_slots_initialized : 1;
55} psa_global_data_t;
56
Gilles Peskine2e14bd32018-12-12 14:05:08 +010057static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010058
59/* Access a key slot at the given handle. The handle of a key slot is
60 * the index of the slot in the global slot array, plus one so that handles
61 * start at 1 and not 0. */
62psa_status_t psa_get_key_slot( psa_key_handle_t handle,
63 psa_key_slot_t **p_slot )
64{
65 psa_key_slot_t *slot = NULL;
66
67 if( ! global_data.key_slots_initialized )
68 return( PSA_ERROR_BAD_STATE );
69
70 /* 0 is not a valid handle under any circumstance. This
71 * implementation provides slots number 1 to N where N is the
72 * number of available slots. */
73 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
74 return( PSA_ERROR_INVALID_HANDLE );
75 slot = &global_data.key_slots[handle - 1];
76
Gilles Peskine41e50d22019-07-31 15:01:55 +020077 /* If the slot isn't occupied, the handle is invalid. */
78 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010079 return( PSA_ERROR_INVALID_HANDLE );
80
81 *p_slot = slot;
82 return( PSA_SUCCESS );
83}
84
85psa_status_t psa_initialize_key_slots( void )
86{
87 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
88 * guarantee that the key slots are initialized to all-zero, which
89 * means that all the key slots are in a valid, empty state. */
90 global_data.key_slots_initialized = 1;
91 return( PSA_SUCCESS );
92}
93
94void psa_wipe_all_key_slots( void )
95{
96 psa_key_handle_t key;
97 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
98 {
99 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
100 (void) psa_wipe_key_slot( slot );
101 }
102 global_data.key_slots_initialized = 0;
103}
104
Gilles Peskineedbed562019-08-07 18:19:59 +0200105psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Gilles Peskine267c6562019-05-27 19:01:54 +0200106 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100107{
Gilles Peskine267c6562019-05-27 19:01:54 +0200108 if( ! global_data.key_slots_initialized )
109 return( PSA_ERROR_BAD_STATE );
110
Gilles Peskine66fb1262018-12-10 16:29:04 +0100111 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
112 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200113 *p_slot = &global_data.key_slots[*handle - 1];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200114 if( ! psa_is_key_slot_occupied( *p_slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100115 return( PSA_SUCCESS );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100116 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200117 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100118 return( PSA_ERROR_INSUFFICIENT_MEMORY );
119}
120
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100121#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200122static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100123{
124 psa_status_t status = PSA_SUCCESS;
125 uint8_t *key_data = NULL;
126 size_t key_data_length = 0;
127
Gilles Peskine24318592019-07-30 20:30:51 +0200128 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200129 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100130 if( status != PSA_SUCCESS )
131 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200132
133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200134 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200135 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200136 psa_se_key_data_storage_t *data;
137 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200138 {
139 status = PSA_ERROR_STORAGE_FAILURE;
140 goto exit;
141 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200142 data = (psa_se_key_data_storage_t *) key_data;
143 memcpy( &slot->data.se.slot_number, &data->slot_number,
144 sizeof( slot->data.se.slot_number ) );
145 memcpy( &slot->attr.bits, &data->bits,
146 sizeof( slot->attr.bits ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200147 }
148 else
149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
150 {
Gilles Peskine24318592019-07-30 20:30:51 +0200151 status = psa_import_key_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200152 }
153
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100154exit:
155 psa_free_persistent_key_data( key_data, key_data_length );
156 return( status );
157}
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100158
159/** Check whether a key identifier is acceptable.
160 *
161 * For backward compatibility, key identifiers that were valid in a
162 * past released version must remain valid, unless a migration path
163 * is provided.
164 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100165 * \param file_id The key identifier to check.
Gilles Peskinef9666592019-05-06 18:56:30 +0200166 * \param vendor_ok Nonzero to allow key ids in the vendor range.
167 * 0 to allow only key ids in the application range.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100168 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100169 * \return 1 if \p file_id is acceptable, otherwise 0.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100170 */
Gilles Peskinef9666592019-05-06 18:56:30 +0200171static int psa_is_key_id_valid( psa_key_file_id_t file_id,
172 int vendor_ok )
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100173{
Gilles Peskine5b229a02019-02-19 13:24:37 +0100174 psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
Gilles Peskinef9fbc382019-05-15 18:42:09 +0200175 if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
176 return( 1 );
177 else if( vendor_ok &&
178 PSA_KEY_ID_VENDOR_MIN <= key_id &&
179 key_id <= PSA_KEY_ID_VENDOR_MAX )
180 return( 1 );
181 else
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100182 return( 0 );
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100183}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200184#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100185
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200186psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
187 psa_se_drv_table_entry_t **p_drv )
Gilles Peskined167b942019-04-19 18:19:40 +0200188{
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200189 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
190 if ( psa_key_lifetime_is_external( lifetime ) )
Gilles Peskine011e4282019-06-26 18:34:38 +0200191 {
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200192#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
193 psa_se_drv_table_entry_t *p_drv_e = psa_get_se_driver_entry( lifetime );
194 if( p_drv_e == NULL )
Gilles Peskine011e4282019-06-26 18:34:38 +0200195 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200196 else
197 {
198 if (p_drv != NULL)
199 *p_drv = p_drv_e;
200 return( PSA_SUCCESS );
201 }
202#else
203 (void) p_drv;
204 return( PSA_ERROR_INVALID_ARGUMENT );
205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine011e4282019-06-26 18:34:38 +0200206 }
207 else
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200208 /* Local/internal keys are always valid */
209 return( PSA_SUCCESS );
210}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200211
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200212psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes )
213{
214 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
215
216 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
217 {
218 /* Volatile keys are always supported */
219 return( PSA_SUCCESS );
220 }
221 else
222 {
223 /* Persistent keys require storage support */
Gilles Peskine30afafd2019-04-25 13:47:40 +0200224#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200225 if( psa_is_key_id_valid( psa_get_key_id( attributes ),
226 psa_key_lifetime_is_external( lifetime ) ) )
227 return( PSA_SUCCESS );
228 else
229 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200230#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200231 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200232#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200233 }
Gilles Peskined167b942019-04-19 18:19:40 +0200234}
235
Gilles Peskine70e085a2019-05-27 19:04:07 +0200236psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100237{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200238#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100239 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200240 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100241
242 *handle = 0;
243
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200244 if( ! psa_is_key_id_valid( id, 1 ) )
245 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100246
Gilles Peskineedbed562019-08-07 18:19:59 +0200247 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100248 if( status != PSA_SUCCESS )
249 return( status );
250
Gilles Peskine8e338702019-07-30 20:06:31 +0200251 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
252 slot->attr.id = id;
Gilles Peskine267c6562019-05-27 19:01:54 +0200253
254 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200255 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100256 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200257 psa_wipe_key_slot( slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100258 *handle = 0;
259 }
260 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200261
Gilles Peskine30afafd2019-04-25 13:47:40 +0200262#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine70e085a2019-05-27 19:04:07 +0200263 (void) id;
264 *handle = 0;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200265 return( PSA_ERROR_NOT_SUPPORTED );
266#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100267}
268
Gilles Peskine961849f2018-11-30 18:54:54 +0100269psa_status_t psa_close_key( psa_key_handle_t handle )
270{
Gilles Peskine267c6562019-05-27 19:01:54 +0200271 psa_status_t status;
272 psa_key_slot_t *slot;
273
Gilles Peskine1841cf42019-10-08 15:48:25 +0200274 if( handle == 0 )
275 return( PSA_SUCCESS );
276
Gilles Peskine267c6562019-05-27 19:01:54 +0200277 status = psa_get_key_slot( handle, &slot );
278 if( status != PSA_SUCCESS )
279 return( status );
280
281 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100282}
283
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200284void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
285{
286 psa_key_handle_t key;
287 memset( stats, 0, sizeof( *stats ) );
288 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
289 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200290 const psa_key_slot_t *slot = &global_data.key_slots[key - 1];
291 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200292 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200293 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200294 continue;
295 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200296 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200297 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200298 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200299 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100300 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200301 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100302 if( id > stats->max_open_internal_key_id )
303 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200304 }
305 else
306 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100307 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200308 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100309 if( id > stats->max_open_external_key_id )
310 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200311 }
312 }
313}
314
Gilles Peskine961849f2018-11-30 18:54:54 +0100315#endif /* MBEDTLS_PSA_CRYPTO_C */