blob: 222d7fb9cb9bc9617b4cde46a5b8fa48b9f92ca5 [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. */
75 if( ! slot->allocated )
76 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
102/** Find a free key slot and mark it as in use.
103 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100104 * \param[out] handle On success, a slot number that is not in use. This
105 * value can be used as a handle to the slot.
Gilles Peskine66fb1262018-12-10 16:29:04 +0100106 *
107 * \retval #PSA_SUCCESS
108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
109 */
110static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
111{
112 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
113 {
114 psa_key_slot_t *slot = &global_data.key_slots[*handle - 1];
115 if( ! slot->allocated )
116 {
117 slot->allocated = 1;
118 return( PSA_SUCCESS );
119 }
120 }
121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
122}
123
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100124/** Wipe a key slot and mark it as available.
125 *
126 * This does not affect persistent storage.
127 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100128 * \param handle The handle to the key slot to release.
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100129 *
130 * \retval #PSA_SUCCESS
131 * \retval #PSA_ERROR_INVALID_ARGUMENT
132 * \retval #PSA_ERROR_TAMPERING_DETECTED
133 */
134static psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
135{
136 psa_key_slot_t *slot;
137 psa_status_t status;
138
139 status = psa_get_key_slot( handle, &slot );
140 if( status != PSA_SUCCESS )
141 return( status );
142
143 return( psa_wipe_key_slot( slot ) );
144}
145
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100146psa_status_t psa_allocate_key( psa_key_handle_t *handle )
Gilles Peskine961849f2018-11-30 18:54:54 +0100147{
Gilles Peskine961849f2018-11-30 18:54:54 +0100148 *handle = 0;
149 return( psa_internal_allocate_key_slot( handle ) );
150}
151
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100152#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
153static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
154{
155 psa_status_t status = PSA_SUCCESS;
156 uint8_t *key_data = NULL;
157 size_t key_data_length = 0;
158
159 status = psa_load_persistent_key( p_slot->persistent_storage_id,
160 &( p_slot )->type,
161 &( p_slot )->policy, &key_data,
162 &key_data_length );
163 if( status != PSA_SUCCESS )
164 goto exit;
165 status = psa_import_key_into_slot( p_slot,
166 key_data, key_data_length );
167exit:
168 psa_free_persistent_key_data( key_data, key_data_length );
169 return( status );
170}
171#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
172
173/** Declare a slot as persistent and load it from storage.
174 *
175 * This function may only be called immediately after a successful call
176 * to psa_internal_allocate_key_slot().
177 *
178 * \param handle A handle to a key slot freshly allocated with
179 * psa_internal_allocate_key_slot().
180 *
181 * \retval #PSA_SUCCESS
182 * The slot content was loaded successfully.
David Saadab4ecc272019-02-14 13:48:10 +0200183 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100184 * There is no content for this slot in persistent storage.
185 * \retval #PSA_ERROR_INVALID_HANDLE
186 * \retval #PSA_ERROR_INVALID_ARGUMENT
187 * \p id is not acceptable.
188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
189 * \retval #PSA_ERROR_STORAGE_FAILURE
190 */
191static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
192 psa_key_id_t id )
193{
194#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
195 psa_key_slot_t *slot;
196 psa_status_t status;
197
198 /* Reject id=0 because by general library conventions, 0 is an invalid
199 * value wherever possible. */
200 if( id == 0 )
201 return( PSA_ERROR_INVALID_ARGUMENT );
202 /* Reject high values because the file names are reserved for the
203 * library's internal use. */
204 if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
205 return( PSA_ERROR_INVALID_ARGUMENT );
206
207 status = psa_get_key_slot( handle, &slot );
208 if( status != PSA_SUCCESS )
209 return( status );
210
211 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
212 slot->persistent_storage_id = id;
213 status = psa_load_persistent_key_into_slot( slot );
214
215 return( status );
216
217#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
218 (void) handle;
219 (void) id;
220 return( PSA_ERROR_NOT_SUPPORTED );
221#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
222}
223
Gilles Peskine961849f2018-11-30 18:54:54 +0100224static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime,
225 psa_key_id_t id,
226 psa_key_handle_t *handle,
227 psa_status_t wanted_load_status )
228{
229 psa_status_t status;
230
231 *handle = 0;
232
233 if( lifetime != PSA_KEY_LIFETIME_PERSISTENT )
234 return( PSA_ERROR_INVALID_ARGUMENT );
235
236 status = psa_internal_allocate_key_slot( handle );
237 if( status != PSA_SUCCESS )
238 return( status );
239
240 status = psa_internal_make_key_persistent( *handle, id );
241 if( status != wanted_load_status )
242 {
243 psa_internal_release_key_slot( *handle );
244 *handle = 0;
245 }
246 return( status );
247}
248
249psa_status_t psa_open_key( psa_key_lifetime_t lifetime,
250 psa_key_id_t id,
251 psa_key_handle_t *handle )
252{
253 return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) );
254}
255
256psa_status_t psa_create_key( psa_key_lifetime_t lifetime,
257 psa_key_id_t id,
Gilles Peskine961849f2018-11-30 18:54:54 +0100258 psa_key_handle_t *handle )
259{
260 psa_status_t status;
261
Gilles Peskine961849f2018-11-30 18:54:54 +0100262 status = persistent_key_setup( lifetime, id, handle,
David Saadab4ecc272019-02-14 13:48:10 +0200263 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine961849f2018-11-30 18:54:54 +0100264 switch( status )
265 {
David Saadab4ecc272019-02-14 13:48:10 +0200266 case PSA_SUCCESS: return( PSA_ERROR_ALREADY_EXISTS );
267 case PSA_ERROR_DOES_NOT_EXIST: return( PSA_SUCCESS );
Gilles Peskine961849f2018-11-30 18:54:54 +0100268 default: return( status );
269 }
270}
271
272psa_status_t psa_close_key( psa_key_handle_t handle )
273{
274 return( psa_internal_release_key_slot( handle ) );
275}
276
277#endif /* MBEDTLS_PSA_CRYPTO_C */