blob: 2ebfc26a8f6c5259566c73e68a74ea40b2dedfbc [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/*
2 * PSA persistent key storage
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Darryl Greendb2b8db2018-06-15 13:06:04 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Darryl Greendb2b8db2018-06-15 13:06:04 +010019 */
20
21#if defined(MBEDTLS_CONFIG_FILE)
22#include MBEDTLS_CONFIG_FILE
23#else
24#include "mbedtls/config.h"
25#endif
26
27#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
28
29#include <stdlib.h>
30#include <string.h>
31
32#include "psa/crypto.h"
33#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010034#include "mbedtls/platform_util.h"
35
Gilles Peskine5e80d912019-02-24 17:10:18 +010036#if defined(MBEDTLS_PSA_ITS_FILE_C)
37#include "psa_crypto_its.h"
38#else /* Native ITS implementation */
39#include "psa/error.h"
40#include "psa/internal_trusted_storage.h"
41#endif
42
Darryl Greendb2b8db2018-06-15 13:06:04 +010043#if defined(MBEDTLS_PLATFORM_C)
44#include "mbedtls/platform.h"
45#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000046#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinec8336cb2019-07-22 19:26:12 +020051
52
53/****************************************************************/
54/* Key storage */
55/****************************************************************/
56
Ronald Cron71016a92020-08-28 19:01:50 +020057/* Determine a file name (ITS file identifier) for the given key identifier.
58 * The file name must be distinct from any file that is used for a purpose
59 * other than storing a key. Currently, the only such file is the random seed
60 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
61 * 0xFFFFFF52. */
62static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010063{
Ronald Cronecfb2372020-07-23 17:13:42 +020064#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010065 /* Encode the owner in the upper 32 bits. This means that if
66 * owner values are nonzero (as they are on a PSA platform),
67 * no key file will ever have a value less than 0x100000000, so
68 * the whole range 0..0xffffffff is available for non-key files. */
Ronald Cron79ca4272020-08-25 09:53:53 +020069 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
70 return( ( (uint64_t) unsigned_owner_id << 32 ) |
71 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010072#else
73 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020074 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010075 * is responsible for ensuring that key identifiers do not have a
76 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020077 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010078#endif
79}
80
Gilles Peskine5e80d912019-02-24 17:10:18 +010081/**
82 * \brief Load persistent data for the given key slot number.
83 *
84 * This function reads data from a storage backend and returns the data in a
85 * buffer.
86 *
87 * \param key Persistent identifier of the key to be loaded. This
88 * should be an occupied storage location.
89 * \param[out] data Buffer where the data is to be written.
90 * \param data_size Size of the \c data buffer in bytes.
91 *
Ronald Cron96783552020-10-19 12:06:30 +020092 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010093 * \retval #PSA_ERROR_DATA_INVALID
94 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020095 * \retval #PSA_ERROR_STORAGE_FAILURE
96 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010097 */
Ronald Cron71016a92020-08-28 19:01:50 +020098static psa_status_t psa_crypto_storage_load(
99 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100100{
101 psa_status_t status;
102 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
103 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100104 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100105
106 status = psa_its_get_info( data_identifier, &data_identifier_info );
107 if( status != PSA_SUCCESS )
108 return( status );
109
Simon D Hughesbda5a212019-07-10 16:34:21 +0100110 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
111 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100112 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100113
114 return( status );
115}
116
Ronald Cron71016a92020-08-28 19:01:50 +0200117int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100118{
119 psa_status_t ret;
120 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
121 struct psa_storage_info_t data_identifier_info;
122
123 ret = psa_its_get_info( data_identifier, &data_identifier_info );
124
125 if( ret == PSA_ERROR_DOES_NOT_EXIST )
126 return( 0 );
127 return( 1 );
128}
129
Gilles Peskine5e80d912019-02-24 17:10:18 +0100130/**
131 * \brief Store persistent data for the given key slot number.
132 *
133 * This function stores the given data buffer to a persistent storage.
134 *
135 * \param key Persistent identifier of the key to be stored. This
136 * should be an unoccupied storage location.
137 * \param[in] data Buffer containing the data to be stored.
138 * \param data_length The number of bytes
139 * that make up the data.
140 *
Ronald Cron96783552020-10-19 12:06:30 +0200141 * \retval #PSA_SUCCESS
142 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200143 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100144 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100145 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100146 */
Ronald Cron71016a92020-08-28 19:01:50 +0200147static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100148 const uint8_t *data,
149 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100150{
151 psa_status_t status;
152 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
153 struct psa_storage_info_t data_identifier_info;
154
155 if( psa_is_key_present_in_storage( key ) == 1 )
156 return( PSA_ERROR_ALREADY_EXISTS );
157
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100158 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100159 if( status != PSA_SUCCESS )
160 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100161 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100162 }
163
164 status = psa_its_get_info( data_identifier, &data_identifier_info );
165 if( status != PSA_SUCCESS )
166 {
167 goto exit;
168 }
169
170 if( data_identifier_info.size != data_length )
171 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100172 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100173 goto exit;
174 }
175
176exit:
177 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200178 {
179 /* Remove the file in case we managed to create it but something
180 * went wrong. It's ok if the file doesn't exist. If the file exists
181 * but the removal fails, we're already reporting an error so there's
182 * nothing else we can do. */
183 (void) psa_its_remove( data_identifier );
184 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100185 return( status );
186}
187
Ronald Cron71016a92020-08-28 19:01:50 +0200188psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100189{
190 psa_status_t ret;
191 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
192 struct psa_storage_info_t data_identifier_info;
193
194 ret = psa_its_get_info( data_identifier, &data_identifier_info );
195 if( ret == PSA_ERROR_DOES_NOT_EXIST )
196 return( PSA_SUCCESS );
197
198 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100199 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100200
201 ret = psa_its_get_info( data_identifier, &data_identifier_info );
202 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100203 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100204
205 return( PSA_SUCCESS );
206}
207
Gilles Peskine5e80d912019-02-24 17:10:18 +0100208/**
209 * \brief Get data length for given key slot number.
210 *
211 * \param key Persistent identifier whose stored data length
212 * is to be obtained.
213 * \param[out] data_length The number of bytes that make up the data.
214 *
Ronald Cron96783552020-10-19 12:06:30 +0200215 * \retval #PSA_SUCCESS
216 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100217 * \retval #PSA_ERROR_DOES_NOT_EXIST
218 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100219 */
220static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200221 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100222 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100223{
224 psa_status_t status;
225 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
226 struct psa_storage_info_t data_identifier_info;
227
228 status = psa_its_get_info( data_identifier, &data_identifier_info );
229 if( status != PSA_SUCCESS )
230 return( status );
231
232 *data_length = (size_t) data_identifier_info.size;
233
234 return( PSA_SUCCESS );
235}
236
Darryl Greendb2b8db2018-06-15 13:06:04 +0100237/*
238 * 32-bit integer manipulation macros (little endian)
239 */
240#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200241#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100242{ \
243 (n) = ( (uint32_t) (b)[(i) ] ) \
244 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
245 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
246 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
247}
248#endif
249
250#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200251#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100252{ \
253 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
254 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
255 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
256 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
257}
258#endif
259
Torstein Nesse162a1102020-10-07 10:50:15 +0200260/*
261 * 16-bit integer manipulation macros (little endian)
262 */
263#ifndef GET_UINT16_LE
264#define GET_UINT16_LE( n, b, i ) \
265{ \
266 (n) = ( (uint16_t) (b)[(i) ] ) \
267 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
268}
269#endif
270
271#ifndef PUT_UINT16_LE
272#define PUT_UINT16_LE( n, b, i ) \
273{ \
274 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
275 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
276}
277#endif
278
Moran Peker96ebf9e2018-06-28 18:02:17 +0300279/**
280 * Persistent key storage magic header.
281 */
282#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
283#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
284
Darryl Greendb2b8db2018-06-15 13:06:04 +0100285typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300286 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100287 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200288 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200289 uint8_t type[2];
290 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100291 uint8_t policy[sizeof( psa_key_policy_t )];
292 uint8_t data_len[4];
293 uint8_t key_data[];
294} psa_persistent_key_storage_format;
295
296void psa_format_key_data_for_storage( const uint8_t *data,
297 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200298 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100299 uint8_t *storage_data )
300{
301 psa_persistent_key_storage_format *storage_format =
302 (psa_persistent_key_storage_format *) storage_data;
303
Moran Peker96ebf9e2018-06-28 18:02:17 +0300304 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200305 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200306 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200307 PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
308 PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200309 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
310 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
311 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200312 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100313 memcpy( storage_format->key_data, data, data_length );
314}
315
Moran Peker96ebf9e2018-06-28 18:02:17 +0300316static psa_status_t check_magic_header( const uint8_t *data )
317{
318 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
319 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100320 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300321 return( PSA_SUCCESS );
322}
323
Darryl Greendb2b8db2018-06-15 13:06:04 +0100324psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
325 size_t storage_data_length,
326 uint8_t **key_data,
327 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200328 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100329{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300330 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100331 const psa_persistent_key_storage_format *storage_format =
332 (const psa_persistent_key_storage_format *)storage_data;
333 uint32_t version;
334
Moran Peker96ebf9e2018-06-28 18:02:17 +0300335 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100336 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300337
338 status = check_magic_header( storage_data );
339 if( status != PSA_SUCCESS )
340 return( status );
341
Gilles Peskine274a2632019-07-23 11:27:38 +0200342 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100343 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100344 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100345
Gilles Peskine274a2632019-07-23 11:27:38 +0200346 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100347 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
348 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100349 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100350
Gilles Peskine3495b582019-04-25 13:47:06 +0200351 if( *key_data_length == 0 )
352 {
353 *key_data = NULL;
354 }
355 else
356 {
357 *key_data = mbedtls_calloc( 1, *key_data_length );
358 if( *key_data == NULL )
359 return( PSA_ERROR_INSUFFICIENT_MEMORY );
360 memcpy( *key_data, storage_format->key_data, *key_data_length );
361 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100362
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200363 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200364 GET_UINT16_LE( attr->type, storage_format->type, 0 );
365 GET_UINT16_LE( attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200366 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
367 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
368 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100369
Darryl Greendb2b8db2018-06-15 13:06:04 +0100370 return( PSA_SUCCESS );
371}
372
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200373psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100374 const uint8_t *data,
375 const size_t data_length )
376{
377 size_t storage_data_length;
378 uint8_t *storage_data;
379 psa_status_t status;
380
Steven Cooremand80e8a42021-01-26 12:45:39 +0100381 /* All keys saved to persistent storage always have a key context */
382 if( data == NULL || data_length == 0 )
383 return( PSA_ERROR_INVALID_ARGUMENT );
384
Darryl Greendb2b8db2018-06-15 13:06:04 +0100385 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100386 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100387 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
388
389 storage_data = mbedtls_calloc( 1, storage_data_length );
390 if( storage_data == NULL )
391 return( PSA_ERROR_INSUFFICIENT_MEMORY );
392
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200393 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100394
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200395 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100396 storage_data, storage_data_length );
397
398 mbedtls_free( storage_data );
399
400 return( status );
401}
402
403void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
404{
405 if( key_data != NULL )
406 {
407 mbedtls_platform_zeroize( key_data, key_data_length );
408 }
409 mbedtls_free( key_data );
410}
411
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200412psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100413 uint8_t **data,
414 size_t *data_length )
415{
416 psa_status_t status = PSA_SUCCESS;
417 uint8_t *loaded_data;
418 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200419 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100420
421 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
422 if( status != PSA_SUCCESS )
423 return( status );
424
425 loaded_data = mbedtls_calloc( 1, storage_data_length );
426
427 if( loaded_data == NULL )
428 return( PSA_ERROR_INSUFFICIENT_MEMORY );
429
430 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
431 if( status != PSA_SUCCESS )
432 goto exit;
433
434 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200435 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100436
Steven Cooremand80e8a42021-01-26 12:45:39 +0100437 /* All keys saved to persistent storage always have a key context */
438 if( status == PSA_SUCCESS &&
439 ( *data == NULL || *data_length == 0 ) )
440 status = PSA_ERROR_STORAGE_FAILURE;
441
Darryl Greendb2b8db2018-06-15 13:06:04 +0100442exit:
443 mbedtls_free( loaded_data );
444 return( status );
445}
446
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200447
448
449/****************************************************************/
450/* Transactions */
451/****************************************************************/
452
453#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
454
455psa_crypto_transaction_t psa_crypto_transaction;
456
457psa_status_t psa_crypto_save_transaction( void )
458{
459 struct psa_storage_info_t p_info;
460 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000461 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200462 if( status == PSA_SUCCESS )
463 {
464 /* This shouldn't happen: we're trying to start a transaction while
465 * there is still a transaction that hasn't been replayed. */
466 return( PSA_ERROR_CORRUPTION_DETECTED );
467 }
468 else if( status != PSA_ERROR_DOES_NOT_EXIST )
469 return( status );
470 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
471 sizeof( psa_crypto_transaction ),
472 &psa_crypto_transaction,
473 0 ) );
474}
475
476psa_status_t psa_crypto_load_transaction( void )
477{
Gilles Peskine8b663892019-07-31 17:57:57 +0200478 psa_status_t status;
479 size_t length;
480 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
481 sizeof( psa_crypto_transaction ),
482 &psa_crypto_transaction, &length );
483 if( status != PSA_SUCCESS )
484 return( status );
485 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100486 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200487 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200488}
489
490psa_status_t psa_crypto_stop_transaction( void )
491{
492 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
493 /* Whether or not updating the storage succeeded, the transaction is
494 * finished now. It's too late to go back, so zero out the in-memory
495 * data. */
496 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
497 return( status );
498}
499
500#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
501
502
503
504/****************************************************************/
505/* Random generator state */
506/****************************************************************/
507
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100508#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
509psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
510 size_t seed_size )
511{
512 psa_status_t status;
513 struct psa_storage_info_t p_info;
514
515 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
516
517 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
518 {
519 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
520 }
521 else if( PSA_SUCCESS == status )
522 {
523 /* You should not be here. Seed needs to be injected only once */
524 status = PSA_ERROR_NOT_PERMITTED;
525 }
526 return( status );
527}
528#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
529
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200530
531
532/****************************************************************/
533/* The end */
534/****************************************************************/
535
Darryl Greendb2b8db2018-06-15 13:06:04 +0100536#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */