1 /*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #include "common.h"
22
23 #if defined(MBEDTLS_PSA_CRYPTO_C)
24
25 #include "psa/crypto.h"
26
27 #include "psa_crypto_core.h"
28 #include "psa_crypto_driver_wrappers.h"
29 #include "psa_crypto_slot_management.h"
30 #include "psa_crypto_storage.h"
31 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
32 #include "psa_crypto_se.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include "mbedtls/platform.h"
38
39 #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
40
41 typedef struct
42 {
43 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
44 unsigned key_slots_initialized : 1;
45 } psa_global_data_t;
46
47 static psa_global_data_t global_data;
48
psa_is_valid_key_id(mbedtls_svc_key_id_t key,int vendor_ok)49 int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok )
50 {
51 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
52
53 if( ( PSA_KEY_ID_USER_MIN <= key_id ) &&
54 ( key_id <= PSA_KEY_ID_USER_MAX ) )
55 return( 1 );
56
57 if( vendor_ok &&
58 ( PSA_KEY_ID_VENDOR_MIN <= key_id ) &&
59 ( key_id <= PSA_KEY_ID_VENDOR_MAX ) )
60 return( 1 );
61
62 return( 0 );
63 }
64
65 /** Get the description in memory of a key given its identifier and lock it.
66 *
67 * The descriptions of volatile keys and loaded persistent keys are
68 * stored in key slots. This function returns a pointer to the key slot
69 * containing the description of a key given its identifier.
70 *
71 * The function searches the key slots containing the description of the key
72 * with \p key identifier. The function does only read accesses to the key
73 * slots. The function does not load any persistent key thus does not access
74 * any storage.
75 *
76 * For volatile key identifiers, only one key slot is queried as a volatile
77 * key with identifier key_id can only be stored in slot of index
78 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
79 *
80 * On success, the function locks the key slot. It is the responsibility of
81 * the caller to unlock the key slot when it does not access it anymore.
82 *
83 * \param key Key identifier to query.
84 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
85 * key slot containing the description of the key
86 * identified by \p key.
87 *
88 * \retval #PSA_SUCCESS
89 * The pointer to the key slot containing the description of the key
90 * identified by \p key was returned.
91 * \retval #PSA_ERROR_INVALID_HANDLE
92 * \p key is not a valid key identifier.
93 * \retval #PSA_ERROR_DOES_NOT_EXIST
94 * There is no key with key identifier \p key in the key slots.
95 */
psa_get_and_lock_key_slot_in_memory(mbedtls_svc_key_id_t key,psa_key_slot_t ** p_slot)96 static psa_status_t psa_get_and_lock_key_slot_in_memory(
97 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot )
98 {
99 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
100 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key );
101 size_t slot_idx;
102 psa_key_slot_t *slot = NULL;
103
104 if( psa_key_id_is_volatile( key_id ) )
105 {
106 slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
107
108 /*
109 * Check if both the PSA key identifier key_id and the owner
110 * identifier of key match those of the key slot.
111 *
112 * Note that, if the key slot is not occupied, its PSA key identifier
113 * is equal to zero. This is an invalid value for a PSA key identifier
114 * and thus cannot be equal to the valid PSA key identifier key_id.
115 */
116 status = mbedtls_svc_key_id_equal( key, slot->attr.id ) ?
117 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
118 }
119 else
120 {
121 if ( !psa_is_valid_key_id( key, 1 ) )
122 return( PSA_ERROR_INVALID_HANDLE );
123
124 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
125 {
126 slot = &global_data.key_slots[ slot_idx ];
127 if( mbedtls_svc_key_id_equal( key, slot->attr.id ) )
128 break;
129 }
130 status = ( slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT ) ?
131 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
132 }
133
134 if( status == PSA_SUCCESS )
135 {
136 status = psa_lock_key_slot( slot );
137 if( status == PSA_SUCCESS )
138 *p_slot = slot;
139 }
140
141 return( status );
142 }
143
psa_initialize_key_slots(void)144 psa_status_t psa_initialize_key_slots( void )
145 {
146 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
147 * guarantee that the key slots are initialized to all-zero, which
148 * means that all the key slots are in a valid, empty state. */
149 global_data.key_slots_initialized = 1;
150 return( PSA_SUCCESS );
151 }
152
psa_wipe_all_key_slots(void)153 void psa_wipe_all_key_slots( void )
154 {
155 size_t slot_idx;
156
157 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
158 {
159 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
160 slot->lock_count = 1;
161 (void) psa_wipe_key_slot( slot );
162 }
163 global_data.key_slots_initialized = 0;
164 }
165
psa_get_empty_key_slot(psa_key_id_t * volatile_key_id,psa_key_slot_t ** p_slot)166 psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
167 psa_key_slot_t **p_slot )
168 {
169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
170 size_t slot_idx;
171 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
172
173 if( ! global_data.key_slots_initialized )
174 {
175 status = PSA_ERROR_BAD_STATE;
176 goto error;
177 }
178
179 selected_slot = unlocked_persistent_key_slot = NULL;
180 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
181 {
182 psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
183 if( ! psa_is_key_slot_occupied( slot ) )
184 {
185 selected_slot = slot;
186 break;
187 }
188
189 if( ( unlocked_persistent_key_slot == NULL ) &&
190 ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
191 ( ! psa_is_key_slot_locked( slot ) ) )
192 unlocked_persistent_key_slot = slot;
193 }
194
195 /*
196 * If there is no unused key slot and there is at least one unlocked key
197 * slot containing the description of a persistent key, recycle the first
198 * such key slot we encountered. If we later need to operate on the
199 * persistent key we are evicting now, we will reload its description from
200 * storage.
201 */
202 if( ( selected_slot == NULL ) &&
203 ( unlocked_persistent_key_slot != NULL ) )
204 {
205 selected_slot = unlocked_persistent_key_slot;
206 selected_slot->lock_count = 1;
207 psa_wipe_key_slot( selected_slot );
208 }
209
210 if( selected_slot != NULL )
211 {
212 status = psa_lock_key_slot( selected_slot );
213 if( status != PSA_SUCCESS )
214 goto error;
215
216 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
217 ( (psa_key_id_t)( selected_slot - global_data.key_slots ) );
218 *p_slot = selected_slot;
219
220 return( PSA_SUCCESS );
221 }
222 status = PSA_ERROR_INSUFFICIENT_MEMORY;
223
224 error:
225 *p_slot = NULL;
226 *volatile_key_id = 0;
227
228 return( status );
229 }
230
231 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
psa_load_persistent_key_into_slot(psa_key_slot_t * slot)232 static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
233 {
234 psa_status_t status = PSA_SUCCESS;
235 uint8_t *key_data = NULL;
236 size_t key_data_length = 0;
237
238 status = psa_load_persistent_key( &slot->attr,
239 &key_data, &key_data_length );
240 if( status != PSA_SUCCESS )
241 goto exit;
242
243 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
244 /* Special handling is required for loading keys associated with a
245 * dynamically registered SE interface. */
246 const psa_drv_se_t *drv;
247 psa_drv_se_context_t *drv_context;
248 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
249 {
250 psa_se_key_data_storage_t *data;
251
252 if( key_data_length != sizeof( *data ) )
253 {
254 status = PSA_ERROR_DATA_INVALID;
255 goto exit;
256 }
257 data = (psa_se_key_data_storage_t *) key_data;
258 status = psa_copy_key_material_into_slot(
259 slot, data->slot_number, sizeof( data->slot_number ) );
260 goto exit;
261 }
262 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
263
264 status = psa_copy_key_material_into_slot( slot, key_data, key_data_length );
265
266 exit:
267 psa_free_persistent_key_data( key_data, key_data_length );
268 return( status );
269 }
270 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
271
272 #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
273
psa_load_builtin_key_into_slot(psa_key_slot_t * slot)274 static psa_status_t psa_load_builtin_key_into_slot( psa_key_slot_t *slot )
275 {
276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
278 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
279 psa_drv_slot_number_t slot_number = 0;
280 size_t key_buffer_size = 0;
281 size_t key_buffer_length = 0;
282
283 if( ! psa_key_id_is_builtin(
284 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id ) ) )
285 {
286 return( PSA_ERROR_DOES_NOT_EXIST );
287 }
288
289 /* Check the platform function to see whether this key actually exists */
290 status = mbedtls_psa_platform_get_builtin_key(
291 slot->attr.id, &lifetime, &slot_number );
292 if( status != PSA_SUCCESS )
293 return( status );
294
295 /* Set required key attributes to ensure get_builtin_key can retrieve the
296 * full attributes. */
297 psa_set_key_id( &attributes, slot->attr.id );
298 psa_set_key_lifetime( &attributes, lifetime );
299
300 /* Get the full key attributes from the driver in order to be able to
301 * calculate the required buffer size. */
302 status = psa_driver_wrapper_get_builtin_key(
303 slot_number, &attributes,
304 NULL, 0, NULL );
305 if( status != PSA_ERROR_BUFFER_TOO_SMALL )
306 {
307 /* Builtin keys cannot be defined by the attributes alone */
308 if( status == PSA_SUCCESS )
309 status = PSA_ERROR_CORRUPTION_DETECTED;
310 return( status );
311 }
312
313 /* If the key should exist according to the platform, then ask the driver
314 * what its expected size is. */
315 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
316 &key_buffer_size );
317 if( status != PSA_SUCCESS )
318 return( status );
319
320 /* Allocate a buffer of the required size and load the builtin key directly
321 * into the (now properly sized) slot buffer. */
322 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
323 if( status != PSA_SUCCESS )
324 return( status );
325
326 status = psa_driver_wrapper_get_builtin_key(
327 slot_number, &attributes,
328 slot->key.data, slot->key.bytes, &key_buffer_length );
329 if( status != PSA_SUCCESS )
330 goto exit;
331
332 /* Copy actual key length and core attributes into the slot on success */
333 slot->key.bytes = key_buffer_length;
334 slot->attr = attributes.core;
335
336 exit:
337 if( status != PSA_SUCCESS )
338 psa_remove_key_data_from_memory( slot );
339 return( status );
340 }
341 #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
342
psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,psa_key_slot_t ** p_slot)343 psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
344 psa_key_slot_t **p_slot )
345 {
346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
347
348 *p_slot = NULL;
349 if( ! global_data.key_slots_initialized )
350 return( PSA_ERROR_BAD_STATE );
351
352 /*
353 * On success, the pointer to the slot is passed directly to the caller
354 * thus no need to unlock the key slot here.
355 */
356 status = psa_get_and_lock_key_slot_in_memory( key, p_slot );
357 if( status != PSA_ERROR_DOES_NOT_EXIST )
358 return( status );
359
360 /* Loading keys from storage requires support for such a mechanism */
361 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
362 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
363 psa_key_id_t volatile_key_id;
364
365 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
366 if( status != PSA_SUCCESS )
367 return( status );
368
369 (*p_slot)->attr.id = key;
370 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
371
372 status = PSA_ERROR_DOES_NOT_EXIST;
373 #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
374 /* Load keys in the 'builtin' range through their own interface */
375 status = psa_load_builtin_key_into_slot( *p_slot );
376 #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
377
378 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
379 if( status == PSA_ERROR_DOES_NOT_EXIST )
380 status = psa_load_persistent_key_into_slot( *p_slot );
381 #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
382
383 if( status != PSA_SUCCESS )
384 {
385 psa_wipe_key_slot( *p_slot );
386 if( status == PSA_ERROR_DOES_NOT_EXIST )
387 status = PSA_ERROR_INVALID_HANDLE;
388 }
389 else
390 /* Add implicit usage flags. */
391 psa_extend_key_usage_flags( &(*p_slot)->attr.policy.usage );
392
393 return( status );
394 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
395 return( PSA_ERROR_INVALID_HANDLE );
396 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
397 }
398
psa_unlock_key_slot(psa_key_slot_t * slot)399 psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
400 {
401 if( slot == NULL )
402 return( PSA_SUCCESS );
403
404 if( slot->lock_count > 0 )
405 {
406 slot->lock_count--;
407 return( PSA_SUCCESS );
408 }
409
410 /*
411 * As the return error code may not be handled in case of multiple errors,
412 * do our best to report if the lock counter is equal to zero. Assert with
413 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is strictly greater
414 * than zero: if the MBEDTLS_TEST_HOOKS configuration option is enabled and
415 * the function is called as part of the execution of a test suite, the
416 * execution of the test suite is stopped in error if the assertion fails.
417 */
418 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count > 0 );
419 return( PSA_ERROR_CORRUPTION_DETECTED );
420 }
421
psa_validate_key_location(psa_key_lifetime_t lifetime,psa_se_drv_table_entry_t ** p_drv)422 psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
423 psa_se_drv_table_entry_t **p_drv )
424 {
425 if ( psa_key_lifetime_is_external( lifetime ) )
426 {
427 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
428 /* Check whether a driver is registered against this lifetime */
429 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
430 if( driver != NULL )
431 {
432 if (p_drv != NULL)
433 *p_drv = driver;
434 return( PSA_SUCCESS );
435 }
436 #else /* MBEDTLS_PSA_CRYPTO_SE_C */
437 (void) p_drv;
438 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
439
440 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
441 /* Key location for external keys gets checked by the wrapper */
442 return( PSA_SUCCESS );
443 #else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
444 /* No support for external lifetimes at all, or dynamic interface
445 * did not find driver for requested lifetime. */
446 return( PSA_ERROR_INVALID_ARGUMENT );
447 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
448 }
449 else
450 /* Local/internal keys are always valid */
451 return( PSA_SUCCESS );
452 }
453
psa_validate_key_persistence(psa_key_lifetime_t lifetime)454 psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime )
455 {
456 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
457 {
458 /* Volatile keys are always supported */
459 return( PSA_SUCCESS );
460 }
461 else
462 {
463 /* Persistent keys require storage support */
464 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
465 if( PSA_KEY_LIFETIME_IS_READ_ONLY( lifetime ) )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 else
468 return( PSA_SUCCESS );
469 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
470 return( PSA_ERROR_NOT_SUPPORTED );
471 #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
472 }
473 }
474
psa_open_key(mbedtls_svc_key_id_t key,psa_key_handle_t * handle)475 psa_status_t psa_open_key( mbedtls_svc_key_id_t key, psa_key_handle_t *handle )
476 {
477 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
478 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
479 psa_status_t status;
480 psa_key_slot_t *slot;
481
482 status = psa_get_and_lock_key_slot( key, &slot );
483 if( status != PSA_SUCCESS )
484 {
485 *handle = PSA_KEY_HANDLE_INIT;
486 if( status == PSA_ERROR_INVALID_HANDLE )
487 status = PSA_ERROR_DOES_NOT_EXIST;
488
489 return( status );
490 }
491
492 *handle = key;
493
494 return( psa_unlock_key_slot( slot ) );
495
496 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
497 (void) key;
498 *handle = PSA_KEY_HANDLE_INIT;
499 return( PSA_ERROR_NOT_SUPPORTED );
500 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
501 }
502
psa_close_key(psa_key_handle_t handle)503 psa_status_t psa_close_key( psa_key_handle_t handle )
504 {
505 psa_status_t status;
506 psa_key_slot_t *slot;
507
508 if( psa_key_handle_is_null( handle ) )
509 return( PSA_SUCCESS );
510
511 status = psa_get_and_lock_key_slot_in_memory( handle, &slot );
512 if( status != PSA_SUCCESS )
513 {
514 if( status == PSA_ERROR_DOES_NOT_EXIST )
515 status = PSA_ERROR_INVALID_HANDLE;
516
517 return( status );
518 }
519 if( slot->lock_count <= 1 )
520 return( psa_wipe_key_slot( slot ) );
521 else
522 return( psa_unlock_key_slot( slot ) );
523 }
524
psa_purge_key(mbedtls_svc_key_id_t key)525 psa_status_t psa_purge_key( mbedtls_svc_key_id_t key )
526 {
527 psa_status_t status;
528 psa_key_slot_t *slot;
529
530 status = psa_get_and_lock_key_slot_in_memory( key, &slot );
531 if( status != PSA_SUCCESS )
532 return( status );
533
534 if( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) &&
535 ( slot->lock_count <= 1 ) )
536 return( psa_wipe_key_slot( slot ) );
537 else
538 return( psa_unlock_key_slot( slot ) );
539 }
540
mbedtls_psa_get_stats(mbedtls_psa_stats_t * stats)541 void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
542 {
543 size_t slot_idx;
544
545 memset( stats, 0, sizeof( *stats ) );
546
547 for( slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++ )
548 {
549 const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
550 if( psa_is_key_slot_locked( slot ) )
551 {
552 ++stats->locked_slots;
553 }
554 if( ! psa_is_key_slot_occupied( slot ) )
555 {
556 ++stats->empty_slots;
557 continue;
558 }
559 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
560 ++stats->volatile_slots;
561 else
562 {
563 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
564 ++stats->persistent_slots;
565 if( id > stats->max_open_internal_key_id )
566 stats->max_open_internal_key_id = id;
567 }
568 if( PSA_KEY_LIFETIME_GET_LOCATION( slot->attr.lifetime ) !=
569 PSA_KEY_LOCATION_LOCAL_STORAGE )
570 {
571 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( slot->attr.id );
572 ++stats->external_slots;
573 if( id > stats->max_open_external_key_id )
574 stats->max_open_external_key_id = id;
575 }
576 }
577 }
578
579 #endif /* MBEDTLS_PSA_CRYPTO_C */
580