blob: 8ffb5a0e1f9b4cfb3cb5f70b4f14c28346bf65d8 [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
23#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
29#if defined(MBEDTLS_PSA_CRYPTO_C)
30
itayzafrir7723ab12019-02-14 10:28:02 +020031#include "psa_crypto_service_integration.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010032#include "psa/crypto.h"
33
Gilles Peskine66fb1262018-12-10 16:29:04 +010034#include "psa_crypto_core.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
36#include "psa_crypto_storage.h"
Gilles Peskineb46bef22019-07-30 21:32:04 +020037#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
38#include "psa_crypto_se.h"
39#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010040
41#include <stdlib.h>
42#include <string.h>
43#if defined(MBEDTLS_PLATFORM_C)
44#include "mbedtls/platform.h"
45#else
46#define mbedtls_calloc calloc
47#define mbedtls_free free
48#endif
49
50#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
51
Gilles Peskine66fb1262018-12-10 16:29:04 +010052typedef struct
53{
54 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
55 unsigned key_slots_initialized : 1;
56} psa_global_data_t;
57
Gilles Peskine2e14bd32018-12-12 14:05:08 +010058static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010059
60/* Access a key slot at the given handle. The handle of a key slot is
61 * the index of the slot in the global slot array, plus one so that handles
62 * start at 1 and not 0. */
63psa_status_t psa_get_key_slot( psa_key_handle_t handle,
64 psa_key_slot_t **p_slot )
65{
66 psa_key_slot_t *slot = NULL;
67
68 if( ! global_data.key_slots_initialized )
69 return( PSA_ERROR_BAD_STATE );
70
71 /* 0 is not a valid handle under any circumstance. This
72 * implementation provides slots number 1 to N where N is the
73 * number of available slots. */
74 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
75 return( PSA_ERROR_INVALID_HANDLE );
76 slot = &global_data.key_slots[handle - 1];
77
Gilles Peskine41e50d22019-07-31 15:01:55 +020078 /* If the slot isn't occupied, the handle is invalid. */
79 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +010080 return( PSA_ERROR_INVALID_HANDLE );
81
82 *p_slot = slot;
83 return( PSA_SUCCESS );
84}
85
86psa_status_t psa_initialize_key_slots( void )
87{
88 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
89 * guarantee that the key slots are initialized to all-zero, which
90 * means that all the key slots are in a valid, empty state. */
91 global_data.key_slots_initialized = 1;
92 return( PSA_SUCCESS );
93}
94
95void psa_wipe_all_key_slots( void )
96{
97 psa_key_handle_t key;
98 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
99 {
100 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
101 (void) psa_wipe_key_slot( slot );
102 }
103 global_data.key_slots_initialized = 0;
104}
105
Gilles Peskineedbed562019-08-07 18:19:59 +0200106psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Gilles Peskine267c6562019-05-27 19:01:54 +0200107 psa_key_slot_t **p_slot )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100108{
Gilles Peskine267c6562019-05-27 19:01:54 +0200109 if( ! global_data.key_slots_initialized )
110 return( PSA_ERROR_BAD_STATE );
111
Gilles Peskine66fb1262018-12-10 16:29:04 +0100112 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
113 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200114 *p_slot = &global_data.key_slots[*handle - 1];
Gilles Peskine41e50d22019-07-31 15:01:55 +0200115 if( ! psa_is_key_slot_occupied( *p_slot ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100116 return( PSA_SUCCESS );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100117 }
Gilles Peskine267c6562019-05-27 19:01:54 +0200118 *p_slot = NULL;
Gilles Peskine66fb1262018-12-10 16:29:04 +0100119 return( PSA_ERROR_INSUFFICIENT_MEMORY );
120}
121
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100122#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200123static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100124{
125 psa_status_t status = PSA_SUCCESS;
126 uint8_t *key_data = NULL;
127 size_t key_data_length = 0;
128
Gilles Peskine24318592019-07-30 20:30:51 +0200129 status = psa_load_persistent_key( &slot->attr,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200130 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100131 if( status != PSA_SUCCESS )
132 goto exit;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200133
134#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine24318592019-07-30 20:30:51 +0200135 if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200136 {
Gilles Peskineb46bef22019-07-30 21:32:04 +0200137 psa_se_key_data_storage_t *data;
138 if( key_data_length != sizeof( *data ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200139 {
140 status = PSA_ERROR_STORAGE_FAILURE;
141 goto exit;
142 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200143 data = (psa_se_key_data_storage_t *) key_data;
144 memcpy( &slot->data.se.slot_number, &data->slot_number,
145 sizeof( slot->data.se.slot_number ) );
146 memcpy( &slot->attr.bits, &data->bits,
147 sizeof( slot->attr.bits ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200148 }
149 else
150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
151 {
Gilles Peskine24318592019-07-30 20:30:51 +0200152 status = psa_import_key_into_slot( slot, key_data, key_data_length );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200153 }
154
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100155exit:
156 psa_free_persistent_key_data( key_data, key_data_length );
157 return( status );
158}
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100159
160/** Check whether a key identifier is acceptable.
161 *
162 * For backward compatibility, key identifiers that were valid in a
163 * past released version must remain valid, unless a migration path
164 * is provided.
165 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100166 * \param file_id The key identifier to check.
Gilles Peskinef9666592019-05-06 18:56:30 +0200167 * \param vendor_ok Nonzero to allow key ids in the vendor range.
168 * 0 to allow only key ids in the application range.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100169 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100170 * \return 1 if \p file_id is acceptable, otherwise 0.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100171 */
Gilles Peskinef9666592019-05-06 18:56:30 +0200172static int psa_is_key_id_valid( psa_key_file_id_t file_id,
173 int vendor_ok )
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100174{
Gilles Peskine5b229a02019-02-19 13:24:37 +0100175 psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
Gilles Peskinef9fbc382019-05-15 18:42:09 +0200176 if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
177 return( 1 );
178 else if( vendor_ok &&
179 PSA_KEY_ID_VENDOR_MIN <= key_id &&
180 key_id <= PSA_KEY_ID_VENDOR_MAX )
181 return( 1 );
182 else
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100183 return( 0 );
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100184}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200185#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100186
Gilles Peskined167b942019-04-19 18:19:40 +0200187psa_status_t psa_validate_persistent_key_parameters(
188 psa_key_lifetime_t lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +0200189 psa_key_file_id_t id,
Gilles Peskine8abe6a22019-07-12 23:40:35 +0200190 psa_se_drv_table_entry_t **p_drv,
Gilles Peskinef9666592019-05-06 18:56:30 +0200191 int creating )
Gilles Peskined167b942019-04-19 18:19:40 +0200192{
Gilles Peskine011e4282019-06-26 18:34:38 +0200193 if( p_drv != NULL )
194 *p_drv = NULL;
195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
196 if( psa_key_lifetime_is_external( lifetime ) )
197 {
198 *p_drv = psa_get_se_driver_entry( lifetime );
199 if( *p_drv == NULL )
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 }
202 else
203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskined167b942019-04-19 18:19:40 +0200204 if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
205 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200206
207#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinef9666592019-05-06 18:56:30 +0200208 if( ! psa_is_key_id_valid( id, ! creating ) )
Gilles Peskined167b942019-04-19 18:19:40 +0200209 return( PSA_ERROR_INVALID_ARGUMENT );
210 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200211
212#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
213 (void) id;
Gilles Peskinee2f62ba2019-05-16 00:31:48 +0200214 (void) creating;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200215 return( PSA_ERROR_NOT_SUPPORTED );
216#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskined167b942019-04-19 18:19:40 +0200217}
218
Gilles Peskine70e085a2019-05-27 19:04:07 +0200219psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100220{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200221#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100222 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200223 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100224
225 *handle = 0;
226
Gilles Peskine70e085a2019-05-27 19:04:07 +0200227 status = psa_validate_persistent_key_parameters(
Gilles Peskine011e4282019-06-26 18:34:38 +0200228 PSA_KEY_LIFETIME_PERSISTENT, id, NULL, 0 );
Gilles Peskined167b942019-04-19 18:19:40 +0200229 if( status != PSA_SUCCESS )
230 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100231
Gilles Peskineedbed562019-08-07 18:19:59 +0200232 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100233 if( status != PSA_SUCCESS )
234 return( status );
235
Gilles Peskine8e338702019-07-30 20:06:31 +0200236 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
237 slot->attr.id = id;
Gilles Peskine267c6562019-05-27 19:01:54 +0200238
239 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200240 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100241 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200242 psa_wipe_key_slot( slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100243 *handle = 0;
244 }
245 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200246
Gilles Peskine30afafd2019-04-25 13:47:40 +0200247#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine70e085a2019-05-27 19:04:07 +0200248 (void) id;
249 *handle = 0;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200250 return( PSA_ERROR_NOT_SUPPORTED );
251#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100252}
253
Gilles Peskine961849f2018-11-30 18:54:54 +0100254psa_status_t psa_close_key( psa_key_handle_t handle )
255{
Gilles Peskine267c6562019-05-27 19:01:54 +0200256 psa_status_t status;
257 psa_key_slot_t *slot;
258
Gilles Peskine1841cf42019-10-08 15:48:25 +0200259 if( handle == 0 )
260 return( PSA_SUCCESS );
261
Gilles Peskine267c6562019-05-27 19:01:54 +0200262 status = psa_get_key_slot( handle, &slot );
263 if( status != PSA_SUCCESS )
264 return( status );
265
266 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100267}
268
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200269void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
270{
271 psa_key_handle_t key;
272 memset( stats, 0, sizeof( *stats ) );
273 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
274 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200275 const psa_key_slot_t *slot = &global_data.key_slots[key - 1];
276 if( ! psa_is_key_slot_occupied( slot ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200277 {
Gilles Peskine41e50d22019-07-31 15:01:55 +0200278 ++stats->empty_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200279 continue;
280 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200281 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200282 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200283 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200284 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100285 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200286 ++stats->persistent_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100287 if( id > stats->max_open_internal_key_id )
288 stats->max_open_internal_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200289 }
290 else
291 {
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100292 psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200293 ++stats->external_slots;
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100294 if( id > stats->max_open_external_key_id )
295 stats->max_open_external_key_id = id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200296 }
297 }
298}
299
Gilles Peskine961849f2018-11-30 18:54:54 +0100300#endif /* MBEDTLS_PSA_CRYPTO_C */