blob: 09cbab4c4de3fbad99cb612847939b562c312164 [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
Mateusz Starzyk88fa17d2021-05-19 17:32:14 +020021#include "common.h"
22
Darryl Greendb2b8db2018-06-15 13:06:04 +010023#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
24
25#include <stdlib.h>
26#include <string.h>
27
28#include "psa/crypto.h"
29#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010030#include "mbedtls/platform_util.h"
31
Gilles Peskine5e80d912019-02-24 17:10:18 +010032#if defined(MBEDTLS_PSA_ITS_FILE_C)
33#include "psa_crypto_its.h"
34#else /* Native ITS implementation */
35#include "psa/error.h"
36#include "psa/internal_trusted_storage.h"
37#endif
38
Darryl Greendb2b8db2018-06-15 13:06:04 +010039#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000042#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010043#define mbedtls_calloc calloc
44#define mbedtls_free free
45#endif
46
Gilles Peskinec8336cb2019-07-22 19:26:12 +020047
48
49/****************************************************************/
50/* Key storage */
51/****************************************************************/
52
Ronald Cron71016a92020-08-28 19:01:50 +020053/* Determine a file name (ITS file identifier) for the given key identifier.
54 * The file name must be distinct from any file that is used for a purpose
55 * other than storing a key. Currently, the only such file is the random seed
56 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
57 * 0xFFFFFF52. */
58static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010059{
Ronald Cronecfb2372020-07-23 17:13:42 +020060#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010061 /* Encode the owner in the upper 32 bits. This means that if
62 * owner values are nonzero (as they are on a PSA platform),
63 * no key file will ever have a value less than 0x100000000, so
64 * the whole range 0..0xffffffff is available for non-key files. */
Ronald Cron79ca4272020-08-25 09:53:53 +020065 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
66 return( ( (uint64_t) unsigned_owner_id << 32 ) |
67 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010068#else
69 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020070 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010071 * is responsible for ensuring that key identifiers do not have a
72 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020073 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010074#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 *
Ronald Cron96783552020-10-19 12:06:30 +020088 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010089 * \retval #PSA_ERROR_DATA_INVALID
90 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020091 * \retval #PSA_ERROR_STORAGE_FAILURE
92 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010093 */
Ronald Cron71016a92020-08-28 19:01:50 +020094static psa_status_t psa_crypto_storage_load(
95 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010096{
97 psa_status_t status;
98 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
99 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100100 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101
102 status = psa_its_get_info( data_identifier, &data_identifier_info );
103 if( status != PSA_SUCCESS )
104 return( status );
105
Simon D Hughesbda5a212019-07-10 16:34:21 +0100106 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
107 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100108 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100109
110 return( status );
111}
112
Ronald Cron71016a92020-08-28 19:01:50 +0200113int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100114{
115 psa_status_t ret;
116 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
117 struct psa_storage_info_t data_identifier_info;
118
119 ret = psa_its_get_info( data_identifier, &data_identifier_info );
120
121 if( ret == PSA_ERROR_DOES_NOT_EXIST )
122 return( 0 );
123 return( 1 );
124}
125
Gilles Peskine5e80d912019-02-24 17:10:18 +0100126/**
127 * \brief Store persistent data for the given key slot number.
128 *
129 * This function stores the given data buffer to a persistent storage.
130 *
131 * \param key Persistent identifier of the key to be stored. This
132 * should be an unoccupied storage location.
133 * \param[in] data Buffer containing the data to be stored.
134 * \param data_length The number of bytes
135 * that make up the data.
136 *
Ronald Cron96783552020-10-19 12:06:30 +0200137 * \retval #PSA_SUCCESS
138 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200139 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100140 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100141 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100142 */
Ronald Cron71016a92020-08-28 19:01:50 +0200143static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100144 const uint8_t *data,
145 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100146{
147 psa_status_t status;
148 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
149 struct psa_storage_info_t data_identifier_info;
150
151 if( psa_is_key_present_in_storage( key ) == 1 )
152 return( PSA_ERROR_ALREADY_EXISTS );
153
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100154 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100155 if( status != PSA_SUCCESS )
156 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100157 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100158 }
159
160 status = psa_its_get_info( data_identifier, &data_identifier_info );
161 if( status != PSA_SUCCESS )
162 {
163 goto exit;
164 }
165
166 if( data_identifier_info.size != data_length )
167 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100168 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100169 goto exit;
170 }
171
172exit:
173 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200174 {
175 /* Remove the file in case we managed to create it but something
176 * went wrong. It's ok if the file doesn't exist. If the file exists
177 * but the removal fails, we're already reporting an error so there's
178 * nothing else we can do. */
179 (void) psa_its_remove( data_identifier );
180 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100181 return( status );
182}
183
Ronald Cron71016a92020-08-28 19:01:50 +0200184psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100185{
186 psa_status_t ret;
187 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
188 struct psa_storage_info_t data_identifier_info;
189
190 ret = psa_its_get_info( data_identifier, &data_identifier_info );
191 if( ret == PSA_ERROR_DOES_NOT_EXIST )
192 return( PSA_SUCCESS );
193
194 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100195 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100196
197 ret = psa_its_get_info( data_identifier, &data_identifier_info );
198 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100199 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100200
201 return( PSA_SUCCESS );
202}
203
Gilles Peskine5e80d912019-02-24 17:10:18 +0100204/**
205 * \brief Get data length for given key slot number.
206 *
207 * \param key Persistent identifier whose stored data length
208 * is to be obtained.
209 * \param[out] data_length The number of bytes that make up the data.
210 *
Ronald Cron96783552020-10-19 12:06:30 +0200211 * \retval #PSA_SUCCESS
212 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100213 * \retval #PSA_ERROR_DOES_NOT_EXIST
214 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100215 */
216static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200217 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100218 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100219{
220 psa_status_t status;
221 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
222 struct psa_storage_info_t data_identifier_info;
223
224 status = psa_its_get_info( data_identifier, &data_identifier_info );
225 if( status != PSA_SUCCESS )
226 return( status );
227
228 *data_length = (size_t) data_identifier_info.size;
229
230 return( PSA_SUCCESS );
231}
232
Darryl Greendb2b8db2018-06-15 13:06:04 +0100233/*
234 * 32-bit integer manipulation macros (little endian)
235 */
236#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200237#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100238{ \
239 (n) = ( (uint32_t) (b)[(i) ] ) \
240 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
241 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
242 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
243}
244#endif
245
246#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200247#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100248{ \
249 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
250 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
251 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
252 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
253}
254#endif
255
Torstein Nesse162a1102020-10-07 10:50:15 +0200256/*
257 * 16-bit integer manipulation macros (little endian)
258 */
259#ifndef GET_UINT16_LE
260#define GET_UINT16_LE( n, b, i ) \
261{ \
262 (n) = ( (uint16_t) (b)[(i) ] ) \
263 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
264}
265#endif
266
267#ifndef PUT_UINT16_LE
268#define PUT_UINT16_LE( n, b, i ) \
269{ \
270 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
271 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
272}
273#endif
274
Moran Peker96ebf9e2018-06-28 18:02:17 +0300275/**
276 * Persistent key storage magic header.
277 */
278#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
279#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
280
Darryl Greendb2b8db2018-06-15 13:06:04 +0100281typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300282 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100283 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200284 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200285 uint8_t type[2];
286 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100287 uint8_t policy[sizeof( psa_key_policy_t )];
288 uint8_t data_len[4];
289 uint8_t key_data[];
290} psa_persistent_key_storage_format;
291
292void psa_format_key_data_for_storage( const uint8_t *data,
293 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200294 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100295 uint8_t *storage_data )
296{
297 psa_persistent_key_storage_format *storage_format =
298 (psa_persistent_key_storage_format *) storage_data;
299
Moran Peker96ebf9e2018-06-28 18:02:17 +0300300 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200301 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200302 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200303 PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
304 PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200305 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
306 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
307 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200308 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100309 memcpy( storage_format->key_data, data, data_length );
310}
311
Moran Peker96ebf9e2018-06-28 18:02:17 +0300312static psa_status_t check_magic_header( const uint8_t *data )
313{
314 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
315 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100316 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300317 return( PSA_SUCCESS );
318}
319
Darryl Greendb2b8db2018-06-15 13:06:04 +0100320psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
321 size_t storage_data_length,
322 uint8_t **key_data,
323 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200324 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100325{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300326 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100327 const psa_persistent_key_storage_format *storage_format =
328 (const psa_persistent_key_storage_format *)storage_data;
329 uint32_t version;
330
Moran Peker96ebf9e2018-06-28 18:02:17 +0300331 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100332 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300333
334 status = check_magic_header( storage_data );
335 if( status != PSA_SUCCESS )
336 return( status );
337
Gilles Peskine274a2632019-07-23 11:27:38 +0200338 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100339 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100340 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100341
Gilles Peskine274a2632019-07-23 11:27:38 +0200342 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100343 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
344 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100345 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100346
Gilles Peskine3495b582019-04-25 13:47:06 +0200347 if( *key_data_length == 0 )
348 {
349 *key_data = NULL;
350 }
351 else
352 {
353 *key_data = mbedtls_calloc( 1, *key_data_length );
354 if( *key_data == NULL )
355 return( PSA_ERROR_INSUFFICIENT_MEMORY );
356 memcpy( *key_data, storage_format->key_data, *key_data_length );
357 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100358
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200359 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200360 GET_UINT16_LE( attr->type, storage_format->type, 0 );
361 GET_UINT16_LE( attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200362 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
363 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
364 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100365
Darryl Greendb2b8db2018-06-15 13:06:04 +0100366 return( PSA_SUCCESS );
367}
368
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200369psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100370 const uint8_t *data,
371 const size_t data_length )
372{
373 size_t storage_data_length;
374 uint8_t *storage_data;
375 psa_status_t status;
376
Steven Cooremand80e8a42021-01-26 12:45:39 +0100377 /* All keys saved to persistent storage always have a key context */
378 if( data == NULL || data_length == 0 )
379 return( PSA_ERROR_INVALID_ARGUMENT );
380
Darryl Greendb2b8db2018-06-15 13:06:04 +0100381 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100382 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100383 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
384
385 storage_data = mbedtls_calloc( 1, storage_data_length );
386 if( storage_data == NULL )
387 return( PSA_ERROR_INSUFFICIENT_MEMORY );
388
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200389 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100390
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200391 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100392 storage_data, storage_data_length );
393
394 mbedtls_free( storage_data );
395
396 return( status );
397}
398
399void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
400{
401 if( key_data != NULL )
402 {
403 mbedtls_platform_zeroize( key_data, key_data_length );
404 }
405 mbedtls_free( key_data );
406}
407
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200408psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100409 uint8_t **data,
410 size_t *data_length )
411{
412 psa_status_t status = PSA_SUCCESS;
413 uint8_t *loaded_data;
414 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200415 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100416
417 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
418 if( status != PSA_SUCCESS )
419 return( status );
420
421 loaded_data = mbedtls_calloc( 1, storage_data_length );
422
423 if( loaded_data == NULL )
424 return( PSA_ERROR_INSUFFICIENT_MEMORY );
425
426 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
427 if( status != PSA_SUCCESS )
428 goto exit;
429
430 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200431 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100432
Steven Cooremand80e8a42021-01-26 12:45:39 +0100433 /* All keys saved to persistent storage always have a key context */
434 if( status == PSA_SUCCESS &&
435 ( *data == NULL || *data_length == 0 ) )
436 status = PSA_ERROR_STORAGE_FAILURE;
437
Darryl Greendb2b8db2018-06-15 13:06:04 +0100438exit:
439 mbedtls_free( loaded_data );
440 return( status );
441}
442
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200443
444
445/****************************************************************/
446/* Transactions */
447/****************************************************************/
448
449#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
450
451psa_crypto_transaction_t psa_crypto_transaction;
452
453psa_status_t psa_crypto_save_transaction( void )
454{
455 struct psa_storage_info_t p_info;
456 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000457 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200458 if( status == PSA_SUCCESS )
459 {
460 /* This shouldn't happen: we're trying to start a transaction while
461 * there is still a transaction that hasn't been replayed. */
462 return( PSA_ERROR_CORRUPTION_DETECTED );
463 }
464 else if( status != PSA_ERROR_DOES_NOT_EXIST )
465 return( status );
466 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
467 sizeof( psa_crypto_transaction ),
468 &psa_crypto_transaction,
469 0 ) );
470}
471
472psa_status_t psa_crypto_load_transaction( void )
473{
Gilles Peskine8b663892019-07-31 17:57:57 +0200474 psa_status_t status;
475 size_t length;
476 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
477 sizeof( psa_crypto_transaction ),
478 &psa_crypto_transaction, &length );
479 if( status != PSA_SUCCESS )
480 return( status );
481 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100482 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200483 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200484}
485
486psa_status_t psa_crypto_stop_transaction( void )
487{
488 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
489 /* Whether or not updating the storage succeeded, the transaction is
490 * finished now. It's too late to go back, so zero out the in-memory
491 * data. */
492 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
493 return( status );
494}
495
496#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
497
498
499
500/****************************************************************/
501/* Random generator state */
502/****************************************************************/
503
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100504#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
505psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
506 size_t seed_size )
507{
508 psa_status_t status;
509 struct psa_storage_info_t p_info;
510
511 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
512
513 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
514 {
515 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
516 }
517 else if( PSA_SUCCESS == status )
518 {
519 /* You should not be here. Seed needs to be injected only once */
520 status = PSA_ERROR_NOT_PERMITTED;
521 }
522 return( status );
523}
524#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
525
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200526
527
528/****************************************************************/
529/* The end */
530/****************************************************************/
531
Darryl Greendb2b8db2018-06-15 13:06:04 +0100532#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */