blob: babc5bb95ac11d8f255ec612321926933543c26e [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;
99
100 status = psa_its_get_info( data_identifier, &data_identifier_info );
101 if( status != PSA_SUCCESS )
102 return( status );
103
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100104 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100105
106 return( status );
107}
108
109int psa_is_key_present_in_storage( const psa_key_file_id_t key )
110{
111 psa_status_t ret;
112 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
113 struct psa_storage_info_t data_identifier_info;
114
115 ret = psa_its_get_info( data_identifier, &data_identifier_info );
116
117 if( ret == PSA_ERROR_DOES_NOT_EXIST )
118 return( 0 );
119 return( 1 );
120}
121
Gilles Peskine5e80d912019-02-24 17:10:18 +0100122/**
123 * \brief Store persistent data for the given key slot number.
124 *
125 * This function stores the given data buffer to a persistent storage.
126 *
127 * \param key Persistent identifier of the key to be stored. This
128 * should be an unoccupied storage location.
129 * \param[in] data Buffer containing the data to be stored.
130 * \param data_length The number of bytes
131 * that make up the data.
132 *
133 * \retval PSA_SUCCESS
134 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
135 * \retval PSA_ERROR_STORAGE_FAILURE
136 * \retval PSA_ERROR_ALREADY_EXISTS
137 */
138static psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
139 const uint8_t *data,
140 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100141{
142 psa_status_t status;
143 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
144 struct psa_storage_info_t data_identifier_info;
145
146 if( psa_is_key_present_in_storage( key ) == 1 )
147 return( PSA_ERROR_ALREADY_EXISTS );
148
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100149 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100150 if( status != PSA_SUCCESS )
151 {
152 return( PSA_ERROR_STORAGE_FAILURE );
153 }
154
155 status = psa_its_get_info( data_identifier, &data_identifier_info );
156 if( status != PSA_SUCCESS )
157 {
158 goto exit;
159 }
160
161 if( data_identifier_info.size != data_length )
162 {
163 status = PSA_ERROR_STORAGE_FAILURE;
164 goto exit;
165 }
166
167exit:
168 if( status != PSA_SUCCESS )
169 psa_its_remove( data_identifier );
170 return( status );
171}
172
173psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
174{
175 psa_status_t ret;
176 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
177 struct psa_storage_info_t data_identifier_info;
178
179 ret = psa_its_get_info( data_identifier, &data_identifier_info );
180 if( ret == PSA_ERROR_DOES_NOT_EXIST )
181 return( PSA_SUCCESS );
182
183 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
184 return( PSA_ERROR_STORAGE_FAILURE );
185
186 ret = psa_its_get_info( data_identifier, &data_identifier_info );
187 if( ret != PSA_ERROR_DOES_NOT_EXIST )
188 return( PSA_ERROR_STORAGE_FAILURE );
189
190 return( PSA_SUCCESS );
191}
192
Gilles Peskine5e80d912019-02-24 17:10:18 +0100193/**
194 * \brief Get data length for given key slot number.
195 *
196 * \param key Persistent identifier whose stored data length
197 * is to be obtained.
198 * \param[out] data_length The number of bytes that make up the data.
199 *
200 * \retval PSA_SUCCESS
201 * \retval PSA_ERROR_STORAGE_FAILURE
202 */
203static psa_status_t psa_crypto_storage_get_data_length(
204 const psa_key_file_id_t key,
205 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100206{
207 psa_status_t status;
208 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
209 struct psa_storage_info_t data_identifier_info;
210
211 status = psa_its_get_info( data_identifier, &data_identifier_info );
212 if( status != PSA_SUCCESS )
213 return( status );
214
215 *data_length = (size_t) data_identifier_info.size;
216
217 return( PSA_SUCCESS );
218}
219
Darryl Greendb2b8db2018-06-15 13:06:04 +0100220/*
221 * 32-bit integer manipulation macros (little endian)
222 */
223#ifndef GET_UINT32_LE
224#define GET_UINT32_LE(n,b,i) \
225{ \
226 (n) = ( (uint32_t) (b)[(i) ] ) \
227 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
228 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
229 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
230}
231#endif
232
233#ifndef PUT_UINT32_LE
234#define PUT_UINT32_LE(n,b,i) \
235{ \
236 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
237 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
238 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
239 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
240}
241#endif
242
Moran Peker96ebf9e2018-06-28 18:02:17 +0300243/**
244 * Persistent key storage magic header.
245 */
246#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
247#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
248
Darryl Greendb2b8db2018-06-15 13:06:04 +0100249typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300250 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100251 uint8_t version[4];
252 uint8_t type[sizeof( psa_key_type_t )];
253 uint8_t policy[sizeof( psa_key_policy_t )];
254 uint8_t data_len[4];
255 uint8_t key_data[];
256} psa_persistent_key_storage_format;
257
258void psa_format_key_data_for_storage( const uint8_t *data,
259 const size_t data_length,
260 const psa_key_type_t type,
261 const psa_key_policy_t *policy,
262 uint8_t *storage_data )
263{
264 psa_persistent_key_storage_format *storage_format =
265 (psa_persistent_key_storage_format *) storage_data;
266
Moran Peker96ebf9e2018-06-28 18:02:17 +0300267 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100268 PUT_UINT32_LE(0, storage_format->version, 0);
269 PUT_UINT32_LE(type, storage_format->type, 0);
270 PUT_UINT32_LE(policy->usage, storage_format->policy, 0);
271 PUT_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t ));
Gilles Peskine81efb392019-05-13 14:38:16 +0200272 PUT_UINT32_LE(policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100273 PUT_UINT32_LE(data_length, storage_format->data_len, 0);
274 memcpy( storage_format->key_data, data, data_length );
275}
276
Moran Peker96ebf9e2018-06-28 18:02:17 +0300277static psa_status_t check_magic_header( const uint8_t *data )
278{
279 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
280 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
281 return( PSA_ERROR_STORAGE_FAILURE );
282 return( PSA_SUCCESS );
283}
284
Darryl Greendb2b8db2018-06-15 13:06:04 +0100285psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
286 size_t storage_data_length,
287 uint8_t **key_data,
288 size_t *key_data_length,
289 psa_key_type_t *type,
290 psa_key_policy_t *policy )
291{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300292 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100293 const psa_persistent_key_storage_format *storage_format =
294 (const psa_persistent_key_storage_format *)storage_data;
295 uint32_t version;
296
Moran Peker96ebf9e2018-06-28 18:02:17 +0300297 if( storage_data_length < sizeof(*storage_format) )
298 return( PSA_ERROR_STORAGE_FAILURE );
299
300 status = check_magic_header( storage_data );
301 if( status != PSA_SUCCESS )
302 return( status );
303
Darryl Greendb2b8db2018-06-15 13:06:04 +0100304 GET_UINT32_LE(version, storage_format->version, 0);
305 if( version != 0 )
306 return( PSA_ERROR_STORAGE_FAILURE );
307
308 GET_UINT32_LE(*key_data_length, storage_format->data_len, 0);
309 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
310 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
311 return( PSA_ERROR_STORAGE_FAILURE );
312
313 *key_data = mbedtls_calloc( 1, *key_data_length );
314 if( *key_data == NULL )
315 return( PSA_ERROR_INSUFFICIENT_MEMORY );
316
317 GET_UINT32_LE(*type, storage_format->type, 0);
318 GET_UINT32_LE(policy->usage, storage_format->policy, 0);
319 GET_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t ));
Gilles Peskine81efb392019-05-13 14:38:16 +0200320 GET_UINT32_LE(policy->alg2, storage_format->policy, 2 * sizeof( uint32_t ));
Darryl Greendb2b8db2018-06-15 13:06:04 +0100321
322 memcpy( *key_data, storage_format->key_data, *key_data_length );
323
324 return( PSA_SUCCESS );
325}
326
Gilles Peskine5b229a02019-02-19 13:24:37 +0100327psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100328 const psa_key_type_t type,
329 const psa_key_policy_t *policy,
330 const uint8_t *data,
331 const size_t data_length )
332{
333 size_t storage_data_length;
334 uint8_t *storage_data;
335 psa_status_t status;
336
337 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
338 return PSA_ERROR_INSUFFICIENT_STORAGE;
339 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
340
341 storage_data = mbedtls_calloc( 1, storage_data_length );
342 if( storage_data == NULL )
343 return( PSA_ERROR_INSUFFICIENT_MEMORY );
344
345 psa_format_key_data_for_storage( data, data_length, type, policy,
346 storage_data );
347
348 status = psa_crypto_storage_store( key,
349 storage_data, storage_data_length );
350
351 mbedtls_free( storage_data );
352
353 return( status );
354}
355
356void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
357{
358 if( key_data != NULL )
359 {
360 mbedtls_platform_zeroize( key_data, key_data_length );
361 }
362 mbedtls_free( key_data );
363}
364
Gilles Peskine5b229a02019-02-19 13:24:37 +0100365psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100366 psa_key_type_t *type,
367 psa_key_policy_t *policy,
368 uint8_t **data,
369 size_t *data_length )
370{
371 psa_status_t status = PSA_SUCCESS;
372 uint8_t *loaded_data;
373 size_t storage_data_length = 0;
374
375 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
376 if( status != PSA_SUCCESS )
377 return( status );
378
379 loaded_data = mbedtls_calloc( 1, storage_data_length );
380
381 if( loaded_data == NULL )
382 return( PSA_ERROR_INSUFFICIENT_MEMORY );
383
384 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
385 if( status != PSA_SUCCESS )
386 goto exit;
387
388 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
389 data, data_length, type, policy );
390
391exit:
392 mbedtls_free( loaded_data );
393 return( status );
394}
395
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100396#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
397psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
398 size_t seed_size )
399{
400 psa_status_t status;
401 struct psa_storage_info_t p_info;
402
403 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
404
405 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
406 {
407 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
408 }
409 else if( PSA_SUCCESS == status )
410 {
411 /* You should not be here. Seed needs to be injected only once */
412 status = PSA_ERROR_NOT_PERMITTED;
413 }
414 return( status );
415}
416#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
417
Darryl Greendb2b8db2018-06-15 13:06:04 +0100418#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */