blob: 3c33c1defb93f61e0a47a7f076abea744166fb32 [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 Peskine088b77f2019-02-24 17:00:27 +010053/* Determine a file name (ITS file identifier) for the given key file
54 * identifier. The file name must be distinct from any file that is used
55 * for a purpose other than storing a key. Currently, the only such file
56 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
57 * and whose value is 0xFFFFFF52. */
58static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
59{
60#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
61 defined(PSA_CRYPTO_SECURE)
62 /* Encode the owner in the upper 32 bits. This means that if
63 * owner values are nonzero (as they are on a PSA platform),
64 * no key file will ever have a value less than 0x100000000, so
65 * the whole range 0..0xffffffff is available for non-key files. */
66 uint32_t unsigned_owner = (uint32_t) file_id.owner;
67 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
68#else
69 /* Use the key id directly as a file name.
70 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
71 * is responsible for ensuring that key identifiers do not have a
72 * value that is reserved for non-key files. */
73 return( file_id );
74#endif
75}
76
Gilles Peskine5e80d912019-02-24 17:10:18 +010077/**
78 * \brief Load persistent data for the given key slot number.
79 *
80 * This function reads data from a storage backend and returns the data in a
81 * buffer.
82 *
83 * \param key Persistent identifier of the key to be loaded. This
84 * should be an occupied storage location.
85 * \param[out] data Buffer where the data is to be written.
86 * \param data_size Size of the \c data buffer in bytes.
87 *
88 * \retval PSA_SUCCESS
89 * \retval PSA_ERROR_STORAGE_FAILURE
90 * \retval PSA_ERROR_DOES_NOT_EXIST
91 */
92static psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key,
93 uint8_t *data,
94 size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010095{
96 psa_status_t status;
97 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
98 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010099 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100100
101 status = psa_its_get_info( data_identifier, &data_identifier_info );
102 if( status != PSA_SUCCESS )
103 return( status );
104
Simon D Hughesbda5a212019-07-10 16:34:21 +0100105 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
106 if( data_size != data_length )
107 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100108
109 return( status );
110}
111
112int psa_is_key_present_in_storage( const psa_key_file_id_t key )
113{
114 psa_status_t ret;
115 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
116 struct psa_storage_info_t data_identifier_info;
117
118 ret = psa_its_get_info( data_identifier, &data_identifier_info );
119
120 if( ret == PSA_ERROR_DOES_NOT_EXIST )
121 return( 0 );
122 return( 1 );
123}
124
Gilles Peskine5e80d912019-02-24 17:10:18 +0100125/**
126 * \brief Store persistent data for the given key slot number.
127 *
128 * This function stores the given data buffer to a persistent storage.
129 *
130 * \param key Persistent identifier of the key to be stored. This
131 * should be an unoccupied storage location.
132 * \param[in] data Buffer containing the data to be stored.
133 * \param data_length The number of bytes
134 * that make up the data.
135 *
136 * \retval PSA_SUCCESS
137 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
138 * \retval PSA_ERROR_STORAGE_FAILURE
139 * \retval PSA_ERROR_ALREADY_EXISTS
140 */
141static psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
142 const uint8_t *data,
143 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100144{
145 psa_status_t status;
146 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
147 struct psa_storage_info_t data_identifier_info;
148
149 if( psa_is_key_present_in_storage( key ) == 1 )
150 return( PSA_ERROR_ALREADY_EXISTS );
151
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100152 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100153 if( status != PSA_SUCCESS )
154 {
155 return( PSA_ERROR_STORAGE_FAILURE );
156 }
157
158 status = psa_its_get_info( data_identifier, &data_identifier_info );
159 if( status != PSA_SUCCESS )
160 {
161 goto exit;
162 }
163
164 if( data_identifier_info.size != data_length )
165 {
166 status = PSA_ERROR_STORAGE_FAILURE;
167 goto exit;
168 }
169
170exit:
171 if( status != PSA_SUCCESS )
172 psa_its_remove( data_identifier );
173 return( status );
174}
175
176psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
177{
178 psa_status_t ret;
179 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
180 struct psa_storage_info_t data_identifier_info;
181
182 ret = psa_its_get_info( data_identifier, &data_identifier_info );
183 if( ret == PSA_ERROR_DOES_NOT_EXIST )
184 return( PSA_SUCCESS );
185
186 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
187 return( PSA_ERROR_STORAGE_FAILURE );
188
189 ret = psa_its_get_info( data_identifier, &data_identifier_info );
190 if( ret != PSA_ERROR_DOES_NOT_EXIST )
191 return( PSA_ERROR_STORAGE_FAILURE );
192
193 return( PSA_SUCCESS );
194}
195
Gilles Peskine5e80d912019-02-24 17:10:18 +0100196/**
197 * \brief Get data length for given key slot number.
198 *
199 * \param key Persistent identifier whose stored data length
200 * is to be obtained.
201 * \param[out] data_length The number of bytes that make up the data.
202 *
203 * \retval PSA_SUCCESS
204 * \retval PSA_ERROR_STORAGE_FAILURE
205 */
206static psa_status_t psa_crypto_storage_get_data_length(
207 const psa_key_file_id_t key,
208 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100209{
210 psa_status_t status;
211 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
212 struct psa_storage_info_t data_identifier_info;
213
214 status = psa_its_get_info( data_identifier, &data_identifier_info );
215 if( status != PSA_SUCCESS )
216 return( status );
217
218 *data_length = (size_t) data_identifier_info.size;
219
220 return( PSA_SUCCESS );
221}
222
Darryl Greendb2b8db2018-06-15 13:06:04 +0100223/*
224 * 32-bit integer manipulation macros (little endian)
225 */
226#ifndef GET_UINT32_LE
227#define GET_UINT32_LE(n,b,i) \
228{ \
229 (n) = ( (uint32_t) (b)[(i) ] ) \
230 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
231 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
232 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
233}
234#endif
235
236#ifndef PUT_UINT32_LE
237#define PUT_UINT32_LE(n,b,i) \
238{ \
239 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
240 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
241 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
242 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
243}
244#endif
245
Moran Peker96ebf9e2018-06-28 18:02:17 +0300246/**
247 * Persistent key storage magic header.
248 */
249#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
250#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
251
Darryl Greendb2b8db2018-06-15 13:06:04 +0100252typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300253 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100254 uint8_t version[4];
255 uint8_t type[sizeof( psa_key_type_t )];
256 uint8_t policy[sizeof( psa_key_policy_t )];
257 uint8_t data_len[4];
258 uint8_t key_data[];
259} psa_persistent_key_storage_format;
260
261void psa_format_key_data_for_storage( const uint8_t *data,
262 const size_t data_length,
263 const psa_key_type_t type,
264 const psa_key_policy_t *policy,
265 uint8_t *storage_data )
266{
267 psa_persistent_key_storage_format *storage_format =
268 (psa_persistent_key_storage_format *) storage_data;
269
Moran Peker96ebf9e2018-06-28 18:02:17 +0300270 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100271 PUT_UINT32_LE(0, storage_format->version, 0);
272 PUT_UINT32_LE(type, storage_format->type, 0);
273 PUT_UINT32_LE(policy->usage, storage_format->policy, 0);
274 PUT_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t ));
Gilles Peskine81efb392019-05-13 14:38:16 +0200275 PUT_UINT32_LE(policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100276 PUT_UINT32_LE(data_length, storage_format->data_len, 0);
277 memcpy( storage_format->key_data, data, data_length );
278}
279
Moran Peker96ebf9e2018-06-28 18:02:17 +0300280static psa_status_t check_magic_header( const uint8_t *data )
281{
282 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
283 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
284 return( PSA_ERROR_STORAGE_FAILURE );
285 return( PSA_SUCCESS );
286}
287
Darryl Greendb2b8db2018-06-15 13:06:04 +0100288psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
289 size_t storage_data_length,
290 uint8_t **key_data,
291 size_t *key_data_length,
292 psa_key_type_t *type,
293 psa_key_policy_t *policy )
294{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300295 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100296 const psa_persistent_key_storage_format *storage_format =
297 (const psa_persistent_key_storage_format *)storage_data;
298 uint32_t version;
299
Moran Peker96ebf9e2018-06-28 18:02:17 +0300300 if( storage_data_length < sizeof(*storage_format) )
301 return( PSA_ERROR_STORAGE_FAILURE );
302
303 status = check_magic_header( storage_data );
304 if( status != PSA_SUCCESS )
305 return( status );
306
Darryl Greendb2b8db2018-06-15 13:06:04 +0100307 GET_UINT32_LE(version, storage_format->version, 0);
308 if( version != 0 )
309 return( PSA_ERROR_STORAGE_FAILURE );
310
311 GET_UINT32_LE(*key_data_length, storage_format->data_len, 0);
312 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
313 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
314 return( PSA_ERROR_STORAGE_FAILURE );
315
316 *key_data = mbedtls_calloc( 1, *key_data_length );
317 if( *key_data == NULL )
318 return( PSA_ERROR_INSUFFICIENT_MEMORY );
319
320 GET_UINT32_LE(*type, storage_format->type, 0);
321 GET_UINT32_LE(policy->usage, storage_format->policy, 0);
322 GET_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t ));
Gilles Peskine81efb392019-05-13 14:38:16 +0200323 GET_UINT32_LE(policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ));
Darryl Greendb2b8db2018-06-15 13:06:04 +0100324
325 memcpy( *key_data, storage_format->key_data, *key_data_length );
326
327 return( PSA_SUCCESS );
328}
329
Gilles Peskine5b229a02019-02-19 13:24:37 +0100330psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100331 const psa_key_type_t type,
332 const psa_key_policy_t *policy,
333 const uint8_t *data,
334 const size_t data_length )
335{
336 size_t storage_data_length;
337 uint8_t *storage_data;
338 psa_status_t status;
339
340 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
341 return PSA_ERROR_INSUFFICIENT_STORAGE;
342 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
343
344 storage_data = mbedtls_calloc( 1, storage_data_length );
345 if( storage_data == NULL )
346 return( PSA_ERROR_INSUFFICIENT_MEMORY );
347
348 psa_format_key_data_for_storage( data, data_length, type, policy,
349 storage_data );
350
351 status = psa_crypto_storage_store( key,
352 storage_data, storage_data_length );
353
354 mbedtls_free( storage_data );
355
356 return( status );
357}
358
359void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
360{
361 if( key_data != NULL )
362 {
363 mbedtls_platform_zeroize( key_data, key_data_length );
364 }
365 mbedtls_free( key_data );
366}
367
Gilles Peskine5b229a02019-02-19 13:24:37 +0100368psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100369 psa_key_type_t *type,
370 psa_key_policy_t *policy,
371 uint8_t **data,
372 size_t *data_length )
373{
374 psa_status_t status = PSA_SUCCESS;
375 uint8_t *loaded_data;
376 size_t storage_data_length = 0;
377
378 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
379 if( status != PSA_SUCCESS )
380 return( status );
381
382 loaded_data = mbedtls_calloc( 1, storage_data_length );
383
384 if( loaded_data == NULL )
385 return( PSA_ERROR_INSUFFICIENT_MEMORY );
386
387 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
388 if( status != PSA_SUCCESS )
389 goto exit;
390
391 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
392 data, data_length, type, policy );
393
394exit:
395 mbedtls_free( loaded_data );
396 return( status );
397}
398
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100399#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
400psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
401 size_t seed_size )
402{
403 psa_status_t status;
404 struct psa_storage_info_t p_info;
405
406 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
407
408 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
409 {
410 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
411 }
412 else if( PSA_SUCCESS == status )
413 {
414 /* You should not be here. Seed needs to be injected only once */
415 status = PSA_ERROR_NOT_PERMITTED;
416 }
417 return( status );
418}
419#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
420
Darryl Greendb2b8db2018-06-15 13:06:04 +0100421#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */