blob: 55fd65af98eac09e0d45cbb3afa54e045200cf89 [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];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200258 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100259 uint8_t type[sizeof( psa_key_type_t )];
260 uint8_t policy[sizeof( psa_key_policy_t )];
261 uint8_t data_len[4];
262 uint8_t key_data[];
263} psa_persistent_key_storage_format;
264
265void psa_format_key_data_for_storage( const uint8_t *data,
266 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200267 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100268 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 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200275 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
276 PUT_UINT32_LE( attr->type, storage_format->type, 0 );
277 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
278 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
279 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200280 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100281 memcpy( storage_format->key_data, data, data_length );
282}
283
Moran Peker96ebf9e2018-06-28 18:02:17 +0300284static psa_status_t check_magic_header( const uint8_t *data )
285{
286 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
287 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
288 return( PSA_ERROR_STORAGE_FAILURE );
289 return( PSA_SUCCESS );
290}
291
Darryl Greendb2b8db2018-06-15 13:06:04 +0100292psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
293 size_t storage_data_length,
294 uint8_t **key_data,
295 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200296 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100297{
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 Peskine4ed0e6f2019-07-30 20:22:33 +0200331 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
332 GET_UINT32_LE( attr->type, storage_format->type, 0 );
333 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
334 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
335 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100336
Darryl Greendb2b8db2018-06-15 13:06:04 +0100337 return( PSA_SUCCESS );
338}
339
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200340psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100341 const uint8_t *data,
342 const size_t data_length )
343{
344 size_t storage_data_length;
345 uint8_t *storage_data;
346 psa_status_t status;
347
348 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
349 return PSA_ERROR_INSUFFICIENT_STORAGE;
350 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
351
352 storage_data = mbedtls_calloc( 1, storage_data_length );
353 if( storage_data == NULL )
354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
355
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200356 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100357
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200358 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100359 storage_data, storage_data_length );
360
361 mbedtls_free( storage_data );
362
363 return( status );
364}
365
366void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
367{
368 if( key_data != NULL )
369 {
370 mbedtls_platform_zeroize( key_data, key_data_length );
371 }
372 mbedtls_free( key_data );
373}
374
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200375psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100376 uint8_t **data,
377 size_t *data_length )
378{
379 psa_status_t status = PSA_SUCCESS;
380 uint8_t *loaded_data;
381 size_t storage_data_length = 0;
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200382 psa_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100383
384 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
385 if( status != PSA_SUCCESS )
386 return( status );
387
388 loaded_data = mbedtls_calloc( 1, storage_data_length );
389
390 if( loaded_data == NULL )
391 return( PSA_ERROR_INSUFFICIENT_MEMORY );
392
393 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
394 if( status != PSA_SUCCESS )
395 goto exit;
396
397 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200398 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100399
400exit:
401 mbedtls_free( loaded_data );
402 return( status );
403}
404
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200405
406
407/****************************************************************/
408/* Transactions */
409/****************************************************************/
410
411#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
412
413psa_crypto_transaction_t psa_crypto_transaction;
414
415psa_status_t psa_crypto_save_transaction( void )
416{
417 struct psa_storage_info_t p_info;
418 psa_status_t status;
419 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
420 if( status == PSA_SUCCESS )
421 {
422 /* This shouldn't happen: we're trying to start a transaction while
423 * there is still a transaction that hasn't been replayed. */
424 return( PSA_ERROR_CORRUPTION_DETECTED );
425 }
426 else if( status != PSA_ERROR_DOES_NOT_EXIST )
427 return( status );
428 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
429 sizeof( psa_crypto_transaction ),
430 &psa_crypto_transaction,
431 0 ) );
432}
433
434psa_status_t psa_crypto_load_transaction( void )
435{
436 return( psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
437 sizeof( psa_crypto_transaction ),
438 &psa_crypto_transaction ) );
439}
440
441psa_status_t psa_crypto_stop_transaction( void )
442{
443 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
444 /* Whether or not updating the storage succeeded, the transaction is
445 * finished now. It's too late to go back, so zero out the in-memory
446 * data. */
447 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
448 return( status );
449}
450
451#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
452
453
454
455/****************************************************************/
456/* Random generator state */
457/****************************************************************/
458
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100459#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
460psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
461 size_t seed_size )
462{
463 psa_status_t status;
464 struct psa_storage_info_t p_info;
465
466 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
467
468 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
469 {
470 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
471 }
472 else if( PSA_SUCCESS == status )
473 {
474 /* You should not be here. Seed needs to be injected only once */
475 status = PSA_ERROR_NOT_PERMITTED;
476 }
477 return( status );
478}
479#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
480
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200481
482
483/****************************************************************/
484/* The end */
485/****************************************************************/
486
Darryl Greendb2b8db2018-06-15 13:06:04 +0100487#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */