blob: a35808a61daafe7fadb84fe131ca8bfee1ece7df [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,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200266 const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100267 uint8_t *storage_data )
268{
269 psa_persistent_key_storage_format *storage_format =
270 (psa_persistent_key_storage_format *) storage_data;
271
Moran Peker96ebf9e2018-06-28 18:02:17 +0300272 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200273 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200274 PUT_UINT32_LE( psa_get_key_type( attributes ), storage_format->type, 0 );
275 PUT_UINT32_LE( psa_get_key_usage_flags( attributes ), storage_format->policy, 0 );
276 PUT_UINT32_LE( psa_get_key_algorithm( attributes ), storage_format->policy, sizeof( uint32_t ) );
277 PUT_UINT32_LE( psa_get_key_enrollment_algorithm( attributes ), storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200278 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100279 memcpy( storage_format->key_data, data, data_length );
280}
281
Moran Peker96ebf9e2018-06-28 18:02:17 +0300282static psa_status_t check_magic_header( const uint8_t *data )
283{
284 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
285 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
286 return( PSA_ERROR_STORAGE_FAILURE );
287 return( PSA_SUCCESS );
288}
289
Darryl Greendb2b8db2018-06-15 13:06:04 +0100290psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
291 size_t storage_data_length,
292 uint8_t **key_data,
293 size_t *key_data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200294 psa_key_attributes_t *attributes )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100295{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300296 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100297 const psa_persistent_key_storage_format *storage_format =
298 (const psa_persistent_key_storage_format *)storage_data;
299 uint32_t version;
300
Moran Peker96ebf9e2018-06-28 18:02:17 +0300301 if( storage_data_length < sizeof(*storage_format) )
302 return( PSA_ERROR_STORAGE_FAILURE );
303
304 status = check_magic_header( storage_data );
305 if( status != PSA_SUCCESS )
306 return( status );
307
Gilles Peskine274a2632019-07-23 11:27:38 +0200308 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100309 if( version != 0 )
310 return( PSA_ERROR_STORAGE_FAILURE );
311
Gilles Peskine274a2632019-07-23 11:27:38 +0200312 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100313 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
314 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
315 return( PSA_ERROR_STORAGE_FAILURE );
316
Gilles Peskine3495b582019-04-25 13:47:06 +0200317 if( *key_data_length == 0 )
318 {
319 *key_data = NULL;
320 }
321 else
322 {
323 *key_data = mbedtls_calloc( 1, *key_data_length );
324 if( *key_data == NULL )
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 memcpy( *key_data, storage_format->key_data, *key_data_length );
327 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100328
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200329 GET_UINT32_LE( attributes->type, storage_format->type, 0 );
330 GET_UINT32_LE( attributes->policy.usage, storage_format->policy, 0 );
331 GET_UINT32_LE( attributes->policy.alg, storage_format->policy, sizeof( uint32_t ) );
332 GET_UINT32_LE( attributes->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100333
Darryl Greendb2b8db2018-06-15 13:06:04 +0100334 return( PSA_SUCCESS );
335}
336
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200337psa_status_t psa_save_persistent_key( const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100338 const uint8_t *data,
339 const size_t data_length )
340{
341 size_t storage_data_length;
342 uint8_t *storage_data;
343 psa_status_t status;
344
345 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
346 return PSA_ERROR_INSUFFICIENT_STORAGE;
347 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
348
349 storage_data = mbedtls_calloc( 1, storage_data_length );
350 if( storage_data == NULL )
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200353 psa_format_key_data_for_storage( data, data_length, attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100354 storage_data );
355
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200356 status = psa_crypto_storage_store( psa_get_key_id( attributes ),
Darryl Greendb2b8db2018-06-15 13:06:04 +0100357 storage_data, storage_data_length );
358
359 mbedtls_free( storage_data );
360
361 return( status );
362}
363
364void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
365{
366 if( key_data != NULL )
367 {
368 mbedtls_platform_zeroize( key_data, key_data_length );
369 }
370 mbedtls_free( key_data );
371}
372
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200373psa_status_t psa_load_persistent_key( psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100374 uint8_t **data,
375 size_t *data_length )
376{
377 psa_status_t status = PSA_SUCCESS;
378 uint8_t *loaded_data;
379 size_t storage_data_length = 0;
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200380 psa_key_id_t key = psa_get_key_id( attributes );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100381
382 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
383 if( status != PSA_SUCCESS )
384 return( status );
385
386 loaded_data = mbedtls_calloc( 1, storage_data_length );
387
388 if( loaded_data == NULL )
389 return( PSA_ERROR_INSUFFICIENT_MEMORY );
390
391 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
392 if( status != PSA_SUCCESS )
393 goto exit;
394
395 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200396 data, data_length, attributes );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100397
398exit:
399 mbedtls_free( loaded_data );
400 return( status );
401}
402
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200403
404
405/****************************************************************/
406/* Transactions */
407/****************************************************************/
408
409#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
410
411psa_crypto_transaction_t psa_crypto_transaction;
412
413psa_status_t psa_crypto_save_transaction( void )
414{
415 struct psa_storage_info_t p_info;
416 psa_status_t status;
417 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
418 if( status == PSA_SUCCESS )
419 {
420 /* This shouldn't happen: we're trying to start a transaction while
421 * there is still a transaction that hasn't been replayed. */
422 return( PSA_ERROR_CORRUPTION_DETECTED );
423 }
424 else if( status != PSA_ERROR_DOES_NOT_EXIST )
425 return( status );
426 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
427 sizeof( psa_crypto_transaction ),
428 &psa_crypto_transaction,
429 0 ) );
430}
431
432psa_status_t psa_crypto_load_transaction( void )
433{
434 return( psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
435 sizeof( psa_crypto_transaction ),
436 &psa_crypto_transaction ) );
437}
438
439psa_status_t psa_crypto_stop_transaction( void )
440{
441 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
442 /* Whether or not updating the storage succeeded, the transaction is
443 * finished now. It's too late to go back, so zero out the in-memory
444 * data. */
445 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
446 return( status );
447}
448
449#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
450
451
452
453/****************************************************************/
454/* Random generator state */
455/****************************************************************/
456
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100457#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
458psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
459 size_t seed_size )
460{
461 psa_status_t status;
462 struct psa_storage_info_t p_info;
463
464 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
465
466 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
467 {
468 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
469 }
470 else if( PSA_SUCCESS == status )
471 {
472 /* You should not be here. Seed needs to be injected only once */
473 status = PSA_ERROR_NOT_PERMITTED;
474 }
475 return( status );
476}
477#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
478
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200479
480
481/****************************************************************/
482/* The end */
483/****************************************************************/
484
Darryl Greendb2b8db2018-06-15 13:06:04 +0100485#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */