blob: b530ee5a773243fcbaa669d1e4acbbfab821f350 [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
30#include "psa/crypto.h"
31
Gilles Peskine66fb1262018-12-10 16:29:04 +010032#include "psa_crypto_core.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010033#include "psa_crypto_slot_management.h"
34#include "psa_crypto_storage.h"
35
36#include <stdlib.h>
37#include <string.h>
38#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#endif
44
45#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
46
Gilles Peskine66fb1262018-12-10 16:29:04 +010047typedef struct
48{
49 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
50 unsigned key_slots_initialized : 1;
51} psa_global_data_t;
52
Gilles Peskine2e14bd32018-12-12 14:05:08 +010053static psa_global_data_t global_data;
Gilles Peskine66fb1262018-12-10 16:29:04 +010054
55/* Access a key slot at the given handle. The handle of a key slot is
56 * the index of the slot in the global slot array, plus one so that handles
57 * start at 1 and not 0. */
58psa_status_t psa_get_key_slot( psa_key_handle_t handle,
59 psa_key_slot_t **p_slot )
60{
61 psa_key_slot_t *slot = NULL;
62
63 if( ! global_data.key_slots_initialized )
64 return( PSA_ERROR_BAD_STATE );
65
66 /* 0 is not a valid handle under any circumstance. This
67 * implementation provides slots number 1 to N where N is the
68 * number of available slots. */
69 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
70 return( PSA_ERROR_INVALID_HANDLE );
71 slot = &global_data.key_slots[handle - 1];
72
73 /* If the slot hasn't been allocated, the handle is invalid. */
74 if( ! slot->allocated )
75 return( PSA_ERROR_INVALID_HANDLE );
76
77 *p_slot = slot;
78 return( PSA_SUCCESS );
79}
80
81psa_status_t psa_initialize_key_slots( void )
82{
83 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
84 * guarantee that the key slots are initialized to all-zero, which
85 * means that all the key slots are in a valid, empty state. */
86 global_data.key_slots_initialized = 1;
87 return( PSA_SUCCESS );
88}
89
90void psa_wipe_all_key_slots( void )
91{
92 psa_key_handle_t key;
93 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
94 {
95 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
96 (void) psa_wipe_key_slot( slot );
97 }
98 global_data.key_slots_initialized = 0;
99}
100
101/** Find a free key slot and mark it as in use.
102 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100103 * \param[out] handle On success, a slot number that is not in use. This
104 * value can be used as a handle to the slot.
Gilles Peskine66fb1262018-12-10 16:29:04 +0100105 *
106 * \retval #PSA_SUCCESS
107 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
108 */
109static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
110{
111 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
112 {
113 psa_key_slot_t *slot = &global_data.key_slots[*handle - 1];
114 if( ! slot->allocated )
115 {
116 slot->allocated = 1;
117 return( PSA_SUCCESS );
118 }
119 }
120 return( PSA_ERROR_INSUFFICIENT_MEMORY );
121}
122
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100123/** Wipe a key slot and mark it as available.
124 *
125 * This does not affect persistent storage.
126 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100127 * \param handle The handle to the key slot to release.
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100128 *
129 * \retval #PSA_SUCCESS
130 * \retval #PSA_ERROR_INVALID_ARGUMENT
131 * \retval #PSA_ERROR_TAMPERING_DETECTED
132 */
133static psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
134{
135 psa_key_slot_t *slot;
136 psa_status_t status;
137
138 status = psa_get_key_slot( handle, &slot );
139 if( status != PSA_SUCCESS )
140 return( status );
141
142 return( psa_wipe_key_slot( slot ) );
143}
144
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100145psa_status_t psa_allocate_key( psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100146{
Gilles Peskine961849f2018-11-30 18:54:54 +0100147 *handle = 0;
148 return( psa_internal_allocate_key_slot( handle ) );
149}
150
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100151#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
152static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
153{
154 psa_status_t status = PSA_SUCCESS;
155 uint8_t *key_data = NULL;
156 size_t key_data_length = 0;
157
158 status = psa_load_persistent_key( p_slot->persistent_storage_id,
159 &( p_slot )->type,
160 &( p_slot )->policy, &key_data,
161 &key_data_length );
162 if( status != PSA_SUCCESS )
163 goto exit;
164 status = psa_import_key_into_slot( p_slot,
165 key_data, key_data_length );
166exit:
167 psa_free_persistent_key_data( key_data, key_data_length );
168 return( status );
169}
170#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
171
172/** Declare a slot as persistent and load it from storage.
173 *
174 * This function may only be called immediately after a successful call
175 * to psa_internal_allocate_key_slot().
176 *
177 * \param handle A handle to a key slot freshly allocated with
178 * psa_internal_allocate_key_slot().
179 *
180 * \retval #PSA_SUCCESS
181 * The slot content was loaded successfully.
182 * \retval #PSA_ERROR_EMPTY_SLOT
183 * There is no content for this slot in persistent storage.
184 * \retval #PSA_ERROR_INVALID_HANDLE
185 * \retval #PSA_ERROR_INVALID_ARGUMENT
186 * \p id is not acceptable.
187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
188 * \retval #PSA_ERROR_STORAGE_FAILURE
189 */
190static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
191 psa_key_id_t id )
192{
193#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
194 psa_key_slot_t *slot;
195 psa_status_t status;
196
197 /* Reject id=0 because by general library conventions, 0 is an invalid
198 * value wherever possible. */
199 if( id == 0 )
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 /* Reject high values because the file names are reserved for the
202 * library's internal use. */
203 if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
204 return( PSA_ERROR_INVALID_ARGUMENT );
205
206 status = psa_get_key_slot( handle, &slot );
207 if( status != PSA_SUCCESS )
208 return( status );
209
210 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
211 slot->persistent_storage_id = id;
212 status = psa_load_persistent_key_into_slot( slot );
213
214 return( status );
215
216#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
217 (void) handle;
218 (void) id;
219 return( PSA_ERROR_NOT_SUPPORTED );
220#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
221}
222
Gilles Peskine961849f2018-11-30 18:54:54 +0100223static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
224 psa_key_id_t id,
225 psa_key_handle_t *handle,
226 psa_status_t wanted_load_status )
227{
228 psa_status_t status;
229
230 *handle = 0;
231
232 if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
233 return( PSA_ERROR_INVALID_ARGUMENT );
234
235 status = psa_internal_allocate_key_slot( handle );
236 if( status != PSA_SUCCESS )
237 return( status );
238
239 status = psa_internal_make_key_persistent( *handle, id );
240 if( status != wanted_load_status )
241 {
242 psa_internal_release_key_slot( *handle );
243 *handle = 0;
244 }
245 return( status );
246}
247
248psa_status_t psa_open_key( psa_key_lifetime_t lifetime,
249 psa_key_id_t id,
250 psa_key_handle_t *handle )
251{
252 return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) );
253}
254
255psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
256 psa_key_id_t id,
Gilles Peskine961849f2018-11-30 18:54:54 +0100257 psa_key_handle_t *handle )
258{
259 psa_status_t status;
260
Gilles Peskine961849f2018-11-30 18:54:54 +0100261 status = persistent_key_setup( lifetime, id, handle,
262 PSA_ERROR_EMPTY_SLOT );
263 switch( status )
264 {
265 case PSA_SUCCESS: return( PSA_ERROR_OCCUPIED_SLOT );
266 case PSA_ERROR_EMPTY_SLOT: return( PSA_SUCCESS );
267 default: return( status );
268 }
269}
270
271psa_status_t psa_close_key( psa_key_handle_t handle )
272{
273 return( psa_internal_release_key_slot( handle ) );
274}
275
276#endif /* MBEDTLS_PSA_CRYPTO_C */