blob: 97b2481d496e537a10f2076d608115312b6c2e6b [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/*
2 * PSA persistent key storage
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_FILE
24#else
25#include "mbedtls/config.h"
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
29
30#include <stdlib.h>
31#include <string.h>
32
itayzafrir7723ab12019-02-14 10:28:02 +020033#include "psa_crypto_service_integration.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010034#include "psa/crypto.h"
35#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010036#include "mbedtls/platform_util.h"
37
Gilles Peskine5e80d912019-02-24 17:10:18 +010038#if defined(MBEDTLS_PSA_ITS_FILE_C)
39#include "psa_crypto_its.h"
40#else /* Native ITS implementation */
41#include "psa/error.h"
42#include "psa/internal_trusted_storage.h"
43#endif
44
Darryl Greendb2b8db2018-06-15 13:06:04 +010045#if defined(MBEDTLS_PLATFORM_C)
46#include "mbedtls/platform.h"
47#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000048#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010049#define mbedtls_calloc calloc
50#define mbedtls_free free
51#endif
52
Gilles Peskinec8336cb2019-07-22 19:26:12 +020053
54
55/****************************************************************/
56/* Key storage */
57/****************************************************************/
58
Gilles Peskine088b77f2019-02-24 17:00:27 +010059/* Determine a file name (ITS file identifier) for the given key file
60 * identifier. The file name must be distinct from any file that is used
61 * for a purpose other than storing a key. Currently, the only such file
62 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
63 * and whose value is 0xFFFFFF52. */
64static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
65{
66#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
67 defined(PSA_CRYPTO_SECURE)
68 /* Encode the owner in the upper 32 bits. This means that if
69 * owner values are nonzero (as they are on a PSA platform),
70 * no key file will ever have a value less than 0x100000000, so
71 * the whole range 0..0xffffffff is available for non-key files. */
72 uint32_t unsigned_owner = (uint32_t) file_id.owner;
73 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
74#else
75 /* Use the key id directly as a file name.
76 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
77 * is responsible for ensuring that key identifiers do not have a
78 * value that is reserved for non-key files. */
79 return( file_id );
80#endif
81}
82
Gilles Peskine5e80d912019-02-24 17:10:18 +010083/**
84 * \brief Load persistent data for the given key slot number.
85 *
86 * This function reads data from a storage backend and returns the data in a
87 * buffer.
88 *
89 * \param key Persistent identifier of the key to be loaded. This
90 * should be an occupied storage location.
91 * \param[out] data Buffer where the data is to be written.
92 * \param data_size Size of the \c data buffer in bytes.
93 *
94 * \retval PSA_SUCCESS
95 * \retval PSA_ERROR_STORAGE_FAILURE
96 * \retval PSA_ERROR_DOES_NOT_EXIST
97 */
98static psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key,
99 uint8_t *data,
100 size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101{
102 psa_status_t status;
103 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
104 struct psa_storage_info_t data_identifier_info;
105
106 status = psa_its_get_info( data_identifier, &data_identifier_info );
107 if( status != PSA_SUCCESS )
108 return( status );
109
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100110 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100111
112 return( status );
113}
114
115int psa_is_key_present_in_storage( const psa_key_file_id_t key )
116{
117 psa_status_t ret;
118 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
119 struct psa_storage_info_t data_identifier_info;
120
121 ret = psa_its_get_info( data_identifier, &data_identifier_info );
122
123 if( ret == PSA_ERROR_DOES_NOT_EXIST )
124 return( 0 );
125 return( 1 );
126}
127
Gilles Peskine5e80d912019-02-24 17:10:18 +0100128/**
129 * \brief Store persistent data for the given key slot number.
130 *
131 * This function stores the given data buffer to a persistent storage.
132 *
133 * \param key Persistent identifier of the key to be stored. This
134 * should be an unoccupied storage location.
135 * \param[in] data Buffer containing the data to be stored.
136 * \param data_length The number of bytes
137 * that make up the data.
138 *
139 * \retval PSA_SUCCESS
140 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
141 * \retval PSA_ERROR_STORAGE_FAILURE
142 * \retval PSA_ERROR_ALREADY_EXISTS
143 */
144static psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
145 const uint8_t *data,
146 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100147{
148 psa_status_t status;
149 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
150 struct psa_storage_info_t data_identifier_info;
151
152 if( psa_is_key_present_in_storage( key ) == 1 )
153 return( PSA_ERROR_ALREADY_EXISTS );
154
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100155 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100156 if( status != PSA_SUCCESS )
157 {
158 return( PSA_ERROR_STORAGE_FAILURE );
159 }
160
161 status = psa_its_get_info( data_identifier, &data_identifier_info );
162 if( status != PSA_SUCCESS )
163 {
164 goto exit;
165 }
166
167 if( data_identifier_info.size != data_length )
168 {
169 status = PSA_ERROR_STORAGE_FAILURE;
170 goto exit;
171 }
172
173exit:
174 if( status != PSA_SUCCESS )
175 psa_its_remove( data_identifier );
176 return( status );
177}
178
179psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
180{
181 psa_status_t ret;
182 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
183 struct psa_storage_info_t data_identifier_info;
184
185 ret = psa_its_get_info( data_identifier, &data_identifier_info );
186 if( ret == PSA_ERROR_DOES_NOT_EXIST )
187 return( PSA_SUCCESS );
188
189 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
190 return( PSA_ERROR_STORAGE_FAILURE );
191
192 ret = psa_its_get_info( data_identifier, &data_identifier_info );
193 if( ret != PSA_ERROR_DOES_NOT_EXIST )
194 return( PSA_ERROR_STORAGE_FAILURE );
195
196 return( PSA_SUCCESS );
197}
198
Gilles Peskine5e80d912019-02-24 17:10:18 +0100199/**
200 * \brief Get data length for given key slot number.
201 *
202 * \param key Persistent identifier whose stored data length
203 * is to be obtained.
204 * \param[out] data_length The number of bytes that make up the data.
205 *
206 * \retval PSA_SUCCESS
207 * \retval PSA_ERROR_STORAGE_FAILURE
208 */
209static psa_status_t psa_crypto_storage_get_data_length(
210 const psa_key_file_id_t key,
211 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100212{
213 psa_status_t status;
214 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
215 struct psa_storage_info_t data_identifier_info;
216
217 status = psa_its_get_info( data_identifier, &data_identifier_info );
218 if( status != PSA_SUCCESS )
219 return( status );
220
221 *data_length = (size_t) data_identifier_info.size;
222
223 return( PSA_SUCCESS );
224}
225
Darryl Greendb2b8db2018-06-15 13:06:04 +0100226/*
227 * 32-bit integer manipulation macros (little endian)
228 */
229#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200230#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100231{ \
232 (n) = ( (uint32_t) (b)[(i) ] ) \
233 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
234 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
235 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
236}
237#endif
238
239#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200240#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100241{ \
242 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
243 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
244 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
245 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
246}
247#endif
248
Moran Peker96ebf9e2018-06-28 18:02:17 +0300249/**
250 * Persistent key storage magic header.
251 */
252#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
253#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
254
Darryl Greendb2b8db2018-06-15 13:06:04 +0100255typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300256 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100257 uint8_t version[4];
258 uint8_t type[sizeof( psa_key_type_t )];
259 uint8_t policy[sizeof( psa_key_policy_t )];
260 uint8_t data_len[4];
261 uint8_t key_data[];
262} psa_persistent_key_storage_format;
263
264void psa_format_key_data_for_storage( const uint8_t *data,
265 const size_t data_length,
266 const psa_key_type_t type,
267 const psa_key_policy_t *policy,
268 uint8_t *storage_data )
269{
270 psa_persistent_key_storage_format *storage_format =
271 (psa_persistent_key_storage_format *) storage_data;
272
Moran Peker96ebf9e2018-06-28 18:02:17 +0300273 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200274 PUT_UINT32_LE( 0, storage_format->version, 0 );
275 PUT_UINT32_LE( type, storage_format->type, 0 );
276 PUT_UINT32_LE( policy->usage, storage_format->policy, 0 );
277 PUT_UINT32_LE( policy->alg, storage_format->policy, sizeof( uint32_t ) );
278 PUT_UINT32_LE( policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
279 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100280 memcpy( storage_format->key_data, data, data_length );
281}
282
Moran Peker96ebf9e2018-06-28 18:02:17 +0300283static psa_status_t check_magic_header( const uint8_t *data )
284{
285 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
286 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
287 return( PSA_ERROR_STORAGE_FAILURE );
288 return( PSA_SUCCESS );
289}
290
Darryl Greendb2b8db2018-06-15 13:06:04 +0100291psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
292 size_t storage_data_length,
293 uint8_t **key_data,
294 size_t *key_data_length,
295 psa_key_type_t *type,
296 psa_key_policy_t *policy )
297{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300298 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100299 const psa_persistent_key_storage_format *storage_format =
300 (const psa_persistent_key_storage_format *)storage_data;
301 uint32_t version;
302
Moran Peker96ebf9e2018-06-28 18:02:17 +0300303 if( storage_data_length < sizeof(*storage_format) )
304 return( PSA_ERROR_STORAGE_FAILURE );
305
306 status = check_magic_header( storage_data );
307 if( status != PSA_SUCCESS )
308 return( status );
309
Gilles Peskine274a2632019-07-23 11:27:38 +0200310 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100311 if( version != 0 )
312 return( PSA_ERROR_STORAGE_FAILURE );
313
Gilles Peskine274a2632019-07-23 11:27:38 +0200314 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100315 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
316 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
317 return( PSA_ERROR_STORAGE_FAILURE );
318
Gilles Peskine3495b582019-04-25 13:47:06 +0200319 if( *key_data_length == 0 )
320 {
321 *key_data = NULL;
322 }
323 else
324 {
325 *key_data = mbedtls_calloc( 1, *key_data_length );
326 if( *key_data == NULL )
327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
328 memcpy( *key_data, storage_format->key_data, *key_data_length );
329 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100330
Gilles Peskine274a2632019-07-23 11:27:38 +0200331 GET_UINT32_LE( *type, storage_format->type, 0 );
332 GET_UINT32_LE( policy->usage, storage_format->policy, 0 );
333 GET_UINT32_LE( policy->alg, storage_format->policy, sizeof( uint32_t ) );
334 GET_UINT32_LE( policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100335
Darryl Greendb2b8db2018-06-15 13:06:04 +0100336 return( PSA_SUCCESS );
337}
338
Gilles Peskine5b229a02019-02-19 13:24:37 +0100339psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100340 const psa_key_type_t type,
341 const psa_key_policy_t *policy,
342 const uint8_t *data,
343 const size_t data_length )
344{
345 size_t storage_data_length;
346 uint8_t *storage_data;
347 psa_status_t status;
348
349 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
350 return PSA_ERROR_INSUFFICIENT_STORAGE;
351 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
352
353 storage_data = mbedtls_calloc( 1, storage_data_length );
354 if( storage_data == NULL )
355 return( PSA_ERROR_INSUFFICIENT_MEMORY );
356
357 psa_format_key_data_for_storage( data, data_length, type, policy,
358 storage_data );
359
360 status = psa_crypto_storage_store( key,
361 storage_data, storage_data_length );
362
363 mbedtls_free( storage_data );
364
365 return( status );
366}
367
368void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
369{
370 if( key_data != NULL )
371 {
372 mbedtls_platform_zeroize( key_data, key_data_length );
373 }
374 mbedtls_free( key_data );
375}
376
Gilles Peskine5b229a02019-02-19 13:24:37 +0100377psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100378 psa_key_type_t *type,
379 psa_key_policy_t *policy,
380 uint8_t **data,
381 size_t *data_length )
382{
383 psa_status_t status = PSA_SUCCESS;
384 uint8_t *loaded_data;
385 size_t storage_data_length = 0;
386
387 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
388 if( status != PSA_SUCCESS )
389 return( status );
390
391 loaded_data = mbedtls_calloc( 1, storage_data_length );
392
393 if( loaded_data == NULL )
394 return( PSA_ERROR_INSUFFICIENT_MEMORY );
395
396 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
397 if( status != PSA_SUCCESS )
398 goto exit;
399
400 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
401 data, data_length, type, policy );
402
403exit:
404 mbedtls_free( loaded_data );
405 return( status );
406}
407
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200408
409
410/****************************************************************/
411/* Transactions */
412/****************************************************************/
413
414#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
415
416psa_crypto_transaction_t psa_crypto_transaction;
417
418psa_status_t psa_crypto_save_transaction( void )
419{
420 struct psa_storage_info_t p_info;
421 psa_status_t status;
422 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
423 if( status == PSA_SUCCESS )
424 {
425 /* This shouldn't happen: we're trying to start a transaction while
426 * there is still a transaction that hasn't been replayed. */
427 return( PSA_ERROR_CORRUPTION_DETECTED );
428 }
429 else if( status != PSA_ERROR_DOES_NOT_EXIST )
430 return( status );
431 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
432 sizeof( psa_crypto_transaction ),
433 &psa_crypto_transaction,
434 0 ) );
435}
436
437psa_status_t psa_crypto_load_transaction( void )
438{
439 return( psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
440 sizeof( psa_crypto_transaction ),
441 &psa_crypto_transaction ) );
442}
443
444psa_status_t psa_crypto_stop_transaction( void )
445{
446 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
447 /* Whether or not updating the storage succeeded, the transaction is
448 * finished now. It's too late to go back, so zero out the in-memory
449 * data. */
450 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
451 return( status );
452}
453
454#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
455
456
457
458/****************************************************************/
459/* Random generator state */
460/****************************************************************/
461
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100462#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
463psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
464 size_t seed_size )
465{
466 psa_status_t status;
467 struct psa_storage_info_t p_info;
468
469 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
470
471 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
472 {
473 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
474 }
475 else if( PSA_SUCCESS == status )
476 {
477 /* You should not be here. Seed needs to be injected only once */
478 status = PSA_ERROR_NOT_PERMITTED;
479 }
480 return( status );
481}
482#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
483
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200484
485
486/****************************************************************/
487/* The end */
488/****************************************************************/
489
Darryl Greendb2b8db2018-06-15 13:06:04 +0100490#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */