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