blob: f623cc988fc1589486968f34b3e398b9e930ea12 [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
53psa_global_data_t global_data;
54
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 *
103 * \param[out] handle On success, a slot number that is not in use.
104 *
105 * \retval #PSA_SUCCESS
106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
107 */
108static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
109{
110 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
111 {
112 psa_key_slot_t *slot = &global_data.key_slots[*handle - 1];
113 if( ! slot->allocated )
114 {
115 slot->allocated = 1;
116 return( PSA_SUCCESS );
117 }
118 }
119 return( PSA_ERROR_INSUFFICIENT_MEMORY );
120}
121
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100122/** Wipe a key slot and mark it as available.
123 *
124 * This does not affect persistent storage.
125 *
126 * \param handle The key slot number to release.
127 *
128 * \retval #PSA_SUCCESS
129 * \retval #PSA_ERROR_INVALID_ARGUMENT
130 * \retval #PSA_ERROR_TAMPERING_DETECTED
131 */
132static psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
133{
134 psa_key_slot_t *slot;
135 psa_status_t status;
136
137 status = psa_get_key_slot( handle, &slot );
138 if( status != PSA_SUCCESS )
139 return( status );
140
141 return( psa_wipe_key_slot( slot ) );
142}
143
Gilles Peskine961849f2018-11-30 18:54:54 +0100144psa_status_t psa_allocate_key( psa_key_type_t type,
145 size_t max_bits,
146 psa_key_handle_t *handle )
147{
148 /* This implementation doesn't reserve memory for the keys. */
149 (void) type;
150 (void) max_bits;
151 *handle = 0;
152 return( psa_internal_allocate_key_slot( handle ) );
153}
154
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100155#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
156static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
157{
158 psa_status_t status = PSA_SUCCESS;
159 uint8_t *key_data = NULL;
160 size_t key_data_length = 0;
161
162 status = psa_load_persistent_key( p_slot->persistent_storage_id,
163 &( p_slot )->type,
164 &( p_slot )->policy, &key_data,
165 &key_data_length );
166 if( status != PSA_SUCCESS )
167 goto exit;
168 status = psa_import_key_into_slot( p_slot,
169 key_data, key_data_length );
170exit:
171 psa_free_persistent_key_data( key_data, key_data_length );
172 return( status );
173}
174#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
175
176/** Declare a slot as persistent and load it from storage.
177 *
178 * This function may only be called immediately after a successful call
179 * to psa_internal_allocate_key_slot().
180 *
181 * \param handle A handle to a key slot freshly allocated with
182 * psa_internal_allocate_key_slot().
183 *
184 * \retval #PSA_SUCCESS
185 * The slot content was loaded successfully.
186 * \retval #PSA_ERROR_EMPTY_SLOT
187 * There is no content for this slot in persistent storage.
188 * \retval #PSA_ERROR_INVALID_HANDLE
189 * \retval #PSA_ERROR_INVALID_ARGUMENT
190 * \p id is not acceptable.
191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
192 * \retval #PSA_ERROR_STORAGE_FAILURE
193 */
194static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
195 psa_key_id_t id )
196{
197#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
198 psa_key_slot_t *slot;
199 psa_status_t status;
200
201 /* Reject id=0 because by general library conventions, 0 is an invalid
202 * value wherever possible. */
203 if( id == 0 )
204 return( PSA_ERROR_INVALID_ARGUMENT );
205 /* Reject high values because the file names are reserved for the
206 * library's internal use. */
207 if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
208 return( PSA_ERROR_INVALID_ARGUMENT );
209
210 status = psa_get_key_slot( handle, &slot );
211 if( status != PSA_SUCCESS )
212 return( status );
213
214 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
215 slot->persistent_storage_id = id;
216 status = psa_load_persistent_key_into_slot( slot );
217
218 return( status );
219
220#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
221 (void) handle;
222 (void) id;
223 return( PSA_ERROR_NOT_SUPPORTED );
224#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
225}
226
Gilles Peskine961849f2018-11-30 18:54:54 +0100227static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
228 psa_key_id_t id,
229 psa_key_handle_t *handle,
230 psa_status_t wanted_load_status )
231{
232 psa_status_t status;
233
234 *handle = 0;
235
236 if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
237 return( PSA_ERROR_INVALID_ARGUMENT );
238
239 status = psa_internal_allocate_key_slot( handle );
240 if( status != PSA_SUCCESS )
241 return( status );
242
243 status = psa_internal_make_key_persistent( *handle, id );
244 if( status != wanted_load_status )
245 {
246 psa_internal_release_key_slot( *handle );
247 *handle = 0;
248 }
249 return( status );
250}
251
252psa_status_t psa_open_key( psa_key_lifetime_t lifetime,
253 psa_key_id_t id,
254 psa_key_handle_t *handle )
255{
256 return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) );
257}
258
259psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
260 psa_key_id_t id,
261 psa_key_type_t type,
262 size_t max_bits,
263 psa_key_handle_t *handle )
264{
265 psa_status_t status;
266
267 /* This implementation doesn't reserve memory for the keys. */
268 (void) type;
269 (void) max_bits;
270
271 status = persistent_key_setup( lifetime, id, handle,
272 PSA_ERROR_EMPTY_SLOT );
273 switch( status )
274 {
275 case PSA_SUCCESS: return( PSA_ERROR_OCCUPIED_SLOT );
276 case PSA_ERROR_EMPTY_SLOT: return( PSA_SUCCESS );
277 default: return( status );
278 }
279}
280
281psa_status_t psa_close_key( psa_key_handle_t handle )
282{
283 return( psa_internal_release_key_slot( handle ) );
284}
285
286#endif /* MBEDTLS_PSA_CRYPTO_C */