blob: 2cfc4a9e8cbabbb853a386cb740272cdcebd6768 [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"
36
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
74 /* If the slot hasn't been allocated, the handle is invalid. */
Gilles Peskine8e338702019-07-30 20:06:31 +020075 if( ! psa_key_slot_get_flags( slot, PSA_KEY_SLOT_FLAG_ALLOCATED ) )
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 Peskine267c6562019-05-27 19:01:54 +0200102psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle,
103 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 Peskine8e338702019-07-30 20:06:31 +0200111 if( ! psa_key_slot_get_flags( *p_slot, PSA_KEY_SLOT_FLAG_ALLOCATED ) )
Gilles Peskine66fb1262018-12-10 16:29:04 +0100112 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200113 psa_key_slot_set_bits_in_flags( *p_slot,
114 PSA_KEY_SLOT_FLAG_ALLOCATED );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100115 return( PSA_SUCCESS );
116 }
117 }
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)
123static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
124{
125 psa_status_t status = PSA_SUCCESS;
126 uint8_t *key_data = NULL;
127 size_t key_data_length = 0;
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100129
Gilles Peskine8e338702019-07-30 20:06:31 +0200130 psa_set_key_id( &attributes, p_slot->attr.id );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200131 status = psa_load_persistent_key( &attributes.core,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200132 &key_data, &key_data_length );
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100133 if( status != PSA_SUCCESS )
134 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +0200135 p_slot->attr.lifetime = psa_get_key_lifetime( &attributes );
136 p_slot->attr.type = psa_get_key_type( &attributes );
137 p_slot->attr.policy = attributes.core.policy;
Gilles Peskine1df83d42019-07-23 16:13:14 +0200138
139#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200140 if( psa_key_lifetime_is_external( p_slot->attr.lifetime ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200141 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +0200142 if( key_data_length != sizeof( p_slot->data.se ) )
Gilles Peskine1df83d42019-07-23 16:13:14 +0200143 {
144 status = PSA_ERROR_STORAGE_FAILURE;
145 goto exit;
146 }
Gilles Peskinee60d1d02019-07-24 20:27:59 +0200147 memcpy( &p_slot->data.se, key_data, sizeof( p_slot->data.se ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +0200148 }
149 else
150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
151 {
152 status = psa_import_key_into_slot( p_slot,
153 key_data, key_data_length );
154 }
155
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100156exit:
157 psa_free_persistent_key_data( key_data, key_data_length );
158 return( status );
159}
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100160
161/** Check whether a key identifier is acceptable.
162 *
163 * For backward compatibility, key identifiers that were valid in a
164 * past released version must remain valid, unless a migration path
165 * is provided.
166 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100167 * \param file_id The key identifier to check.
Gilles Peskinef9666592019-05-06 18:56:30 +0200168 * \param vendor_ok Nonzero to allow key ids in the vendor range.
169 * 0 to allow only key ids in the application range.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100170 *
Gilles Peskine5b229a02019-02-19 13:24:37 +0100171 * \return 1 if \p file_id is acceptable, otherwise 0.
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100172 */
Gilles Peskinef9666592019-05-06 18:56:30 +0200173static int psa_is_key_id_valid( psa_key_file_id_t file_id,
174 int vendor_ok )
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100175{
Gilles Peskine5b229a02019-02-19 13:24:37 +0100176 psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
Gilles Peskinef9fbc382019-05-15 18:42:09 +0200177 if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
178 return( 1 );
179 else if( vendor_ok &&
180 PSA_KEY_ID_VENDOR_MIN <= key_id &&
181 key_id <= PSA_KEY_ID_VENDOR_MAX )
182 return( 1 );
183 else
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100184 return( 0 );
Gilles Peskinec8569bc2019-02-19 13:04:02 +0100185}
Gilles Peskine30afafd2019-04-25 13:47:40 +0200186#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100187
Gilles Peskined167b942019-04-19 18:19:40 +0200188psa_status_t psa_validate_persistent_key_parameters(
189 psa_key_lifetime_t lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +0200190 psa_key_file_id_t id,
Gilles Peskine8abe6a22019-07-12 23:40:35 +0200191 psa_se_drv_table_entry_t **p_drv,
Gilles Peskinef9666592019-05-06 18:56:30 +0200192 int creating )
Gilles Peskined167b942019-04-19 18:19:40 +0200193{
Gilles Peskine011e4282019-06-26 18:34:38 +0200194 if( p_drv != NULL )
195 *p_drv = NULL;
196#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
197 if( psa_key_lifetime_is_external( lifetime ) )
198 {
199 *p_drv = psa_get_se_driver_entry( lifetime );
200 if( *p_drv == NULL )
201 return( PSA_ERROR_INVALID_ARGUMENT );
202 }
203 else
204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskined167b942019-04-19 18:19:40 +0200205 if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
206 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200207
208#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinef9666592019-05-06 18:56:30 +0200209 if( ! psa_is_key_id_valid( id, ! creating ) )
Gilles Peskined167b942019-04-19 18:19:40 +0200210 return( PSA_ERROR_INVALID_ARGUMENT );
211 return( PSA_SUCCESS );
Gilles Peskine30afafd2019-04-25 13:47:40 +0200212
213#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
214 (void) id;
Gilles Peskinee2f62ba2019-05-16 00:31:48 +0200215 (void) creating;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200216 return( PSA_ERROR_NOT_SUPPORTED );
217#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskined167b942019-04-19 18:19:40 +0200218}
219
Gilles Peskine70e085a2019-05-27 19:04:07 +0200220psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100221{
Gilles Peskine70e085a2019-05-27 19:04:07 +0200222#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100223 psa_status_t status;
Gilles Peskine267c6562019-05-27 19:01:54 +0200224 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100225
226 *handle = 0;
227
Gilles Peskine70e085a2019-05-27 19:04:07 +0200228 status = psa_validate_persistent_key_parameters(
Gilles Peskine011e4282019-06-26 18:34:38 +0200229 PSA_KEY_LIFETIME_PERSISTENT, id, NULL, 0 );
Gilles Peskined167b942019-04-19 18:19:40 +0200230 if( status != PSA_SUCCESS )
231 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100232
Gilles Peskine267c6562019-05-27 19:01:54 +0200233 status = psa_internal_allocate_key_slot( handle, &slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100234 if( status != PSA_SUCCESS )
235 return( status );
236
Gilles Peskine8e338702019-07-30 20:06:31 +0200237 slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
238 slot->attr.id = id;
Gilles Peskine267c6562019-05-27 19:01:54 +0200239
240 status = psa_load_persistent_key_into_slot( slot );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200241 if( status != PSA_SUCCESS )
Gilles Peskine961849f2018-11-30 18:54:54 +0100242 {
Gilles Peskine267c6562019-05-27 19:01:54 +0200243 psa_wipe_key_slot( slot );
Gilles Peskine961849f2018-11-30 18:54:54 +0100244 *handle = 0;
245 }
246 return( status );
Gilles Peskine70e085a2019-05-27 19:04:07 +0200247
Gilles Peskine30afafd2019-04-25 13:47:40 +0200248#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine70e085a2019-05-27 19:04:07 +0200249 (void) id;
250 *handle = 0;
Gilles Peskine30afafd2019-04-25 13:47:40 +0200251 return( PSA_ERROR_NOT_SUPPORTED );
252#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine961849f2018-11-30 18:54:54 +0100253}
254
Gilles Peskine961849f2018-11-30 18:54:54 +0100255psa_status_t psa_close_key( psa_key_handle_t handle )
256{
Gilles Peskine267c6562019-05-27 19:01:54 +0200257 psa_status_t status;
258 psa_key_slot_t *slot;
259
260 status = psa_get_key_slot( handle, &slot );
261 if( status != PSA_SUCCESS )
262 return( status );
263
264 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100265}
266
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200267void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
268{
269 psa_key_handle_t key;
270 memset( stats, 0, sizeof( *stats ) );
271 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
272 {
273 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskine8e338702019-07-30 20:06:31 +0200274 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200275 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200276 if( psa_key_slot_get_flags( slot, PSA_KEY_SLOT_FLAG_ALLOCATED ) )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200277 ++stats->half_filled_slots;
278 else
279 ++stats->empty_slots;
280 continue;
281 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200282 if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200283 ++stats->volatile_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200284 else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200285 {
286 ++stats->persistent_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200287 if( slot->attr.id > stats->max_open_internal_key_id )
288 stats->max_open_internal_key_id = slot->attr.id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200289 }
290 else
291 {
292 ++stats->external_slots;
Gilles Peskine8e338702019-07-30 20:06:31 +0200293 if( slot->attr.id > stats->max_open_external_key_id )
294 stats->max_open_external_key_id = slot->attr.id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200295 }
296 }
297}
298
Gilles Peskine961849f2018-11-30 18:54:54 +0100299#endif /* MBEDTLS_PSA_CRYPTO_C */