blob: 575e9925af00e4038e8968ac8d1309b126efb9b4 [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
itayzafrir7723ab12019-02-14 10:28:02 +020028#include "psa_crypto_service_integration.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010029#include "psa/crypto.h"
30#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010031#include "mbedtls/platform_util.h"
32
Gilles Peskine5e80d912019-02-24 17:10:18 +010033#if defined(MBEDTLS_PSA_ITS_FILE_C)
34#include "psa_crypto_its.h"
35#else /* Native ITS implementation */
36#include "psa/error.h"
37#include "psa/internal_trusted_storage.h"
38#endif
39
Darryl Greendb2b8db2018-06-15 13:06:04 +010040#if defined(MBEDTLS_PLATFORM_C)
41#include "mbedtls/platform.h"
42#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000043#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinec8336cb2019-07-22 19:26:12 +020048
49
50/****************************************************************/
51/* Key storage */
52/****************************************************************/
53
Ronald Cron71016a92020-08-28 19:01:50 +020054/* Determine a file name (ITS file identifier) for the given key identifier.
55 * The file name must be distinct from any file that is used for a purpose
56 * other than storing a key. Currently, the only such file is the random seed
57 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
58 * 0xFFFFFF52. */
59static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010060{
Ronald Cronecfb2372020-07-23 17:13:42 +020061#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010062 /* 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. */
Ronald Cron79ca4272020-08-25 09:53:53 +020066 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
67 return( ( (uint64_t) unsigned_owner_id << 32 ) |
68 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010069#else
70 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020071 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010072 * is responsible for ensuring that key identifiers do not have a
73 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020074 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010075#endif
76}
77
Gilles Peskine5e80d912019-02-24 17:10:18 +010078/**
79 * \brief Load persistent data for the given key slot number.
80 *
81 * This function reads data from a storage backend and returns the data in a
82 * buffer.
83 *
84 * \param key Persistent identifier of the key to be loaded. This
85 * should be an occupied storage location.
86 * \param[out] data Buffer where the data is to be written.
87 * \param data_size Size of the \c data buffer in bytes.
88 *
Ronald Cron96783552020-10-19 12:06:30 +020089 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010090 * \retval #PSA_ERROR_DATA_INVALID
91 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020092 * \retval #PSA_ERROR_STORAGE_FAILURE
93 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010094 */
Ronald Cron71016a92020-08-28 19:01:50 +020095static psa_status_t psa_crypto_storage_load(
96 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010097{
98 psa_status_t status;
99 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
100 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100101 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100102
103 status = psa_its_get_info( data_identifier, &data_identifier_info );
104 if( status != PSA_SUCCESS )
105 return( status );
106
Simon D Hughesbda5a212019-07-10 16:34:21 +0100107 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
108 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100109 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100110
111 return( status );
112}
113
Ronald Cron71016a92020-08-28 19:01:50 +0200114int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100115{
116 psa_status_t ret;
117 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
118 struct psa_storage_info_t data_identifier_info;
119
120 ret = psa_its_get_info( data_identifier, &data_identifier_info );
121
122 if( ret == PSA_ERROR_DOES_NOT_EXIST )
123 return( 0 );
124 return( 1 );
125}
126
Gilles Peskine5e80d912019-02-24 17:10:18 +0100127/**
128 * \brief Store persistent data for the given key slot number.
129 *
130 * This function stores the given data buffer to a persistent storage.
131 *
132 * \param key Persistent identifier of the key to be stored. This
133 * should be an unoccupied storage location.
134 * \param[in] data Buffer containing the data to be stored.
135 * \param data_length The number of bytes
136 * that make up the data.
137 *
Ronald Cron96783552020-10-19 12:06:30 +0200138 * \retval #PSA_SUCCESS
139 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200140 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100141 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100142 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100143 */
Ronald Cron71016a92020-08-28 19:01:50 +0200144static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100145 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 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100158 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100159 }
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 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100169 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100170 goto exit;
171 }
172
173exit:
174 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200175 {
176 /* Remove the file in case we managed to create it but something
177 * went wrong. It's ok if the file doesn't exist. If the file exists
178 * but the removal fails, we're already reporting an error so there's
179 * nothing else we can do. */
180 (void) psa_its_remove( data_identifier );
181 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100182 return( status );
183}
184
Ronald Cron71016a92020-08-28 19:01:50 +0200185psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100186{
187 psa_status_t ret;
188 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
189 struct psa_storage_info_t data_identifier_info;
190
191 ret = psa_its_get_info( data_identifier, &data_identifier_info );
192 if( ret == PSA_ERROR_DOES_NOT_EXIST )
193 return( PSA_SUCCESS );
194
195 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100196 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100197
198 ret = psa_its_get_info( data_identifier, &data_identifier_info );
199 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100200 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100201
202 return( PSA_SUCCESS );
203}
204
Gilles Peskine5e80d912019-02-24 17:10:18 +0100205/**
206 * \brief Get data length for given key slot number.
207 *
208 * \param key Persistent identifier whose stored data length
209 * is to be obtained.
210 * \param[out] data_length The number of bytes that make up the data.
211 *
Ronald Cron96783552020-10-19 12:06:30 +0200212 * \retval #PSA_SUCCESS
213 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100214 * \retval #PSA_ERROR_DOES_NOT_EXIST
215 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100216 */
217static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200218 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100219 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100220{
221 psa_status_t status;
222 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
223 struct psa_storage_info_t data_identifier_info;
224
225 status = psa_its_get_info( data_identifier, &data_identifier_info );
226 if( status != PSA_SUCCESS )
227 return( status );
228
229 *data_length = (size_t) data_identifier_info.size;
230
231 return( PSA_SUCCESS );
232}
233
Darryl Greendb2b8db2018-06-15 13:06:04 +0100234/*
235 * 32-bit integer manipulation macros (little endian)
236 */
237#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200238#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100239{ \
240 (n) = ( (uint32_t) (b)[(i) ] ) \
241 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
242 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
243 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
244}
245#endif
246
247#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200248#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100249{ \
250 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
251 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
252 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
253 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
254}
255#endif
256
Torstein Nesse162a1102020-10-07 10:50:15 +0200257/*
258 * 16-bit integer manipulation macros (little endian)
259 */
260#ifndef GET_UINT16_LE
261#define GET_UINT16_LE( n, b, i ) \
262{ \
263 (n) = ( (uint16_t) (b)[(i) ] ) \
264 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
265}
266#endif
267
268#ifndef PUT_UINT16_LE
269#define PUT_UINT16_LE( n, b, i ) \
270{ \
271 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
272 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
273}
274#endif
275
Moran Peker96ebf9e2018-06-28 18:02:17 +0300276/**
277 * Persistent key storage magic header.
278 */
279#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
280#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
281
Darryl Greendb2b8db2018-06-15 13:06:04 +0100282typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300283 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100284 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200285 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200286 uint8_t type[2];
287 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100288 uint8_t policy[sizeof( psa_key_policy_t )];
289 uint8_t data_len[4];
290 uint8_t key_data[];
291} psa_persistent_key_storage_format;
292
293void psa_format_key_data_for_storage( const uint8_t *data,
294 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200295 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100296 uint8_t *storage_data )
297{
298 psa_persistent_key_storage_format *storage_format =
299 (psa_persistent_key_storage_format *) storage_data;
300
Moran Peker96ebf9e2018-06-28 18:02:17 +0300301 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200302 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200303 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200304 PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
305 PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200306 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
307 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
308 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200309 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100310 memcpy( storage_format->key_data, data, data_length );
311}
312
Moran Peker96ebf9e2018-06-28 18:02:17 +0300313static psa_status_t check_magic_header( const uint8_t *data )
314{
315 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
316 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100317 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300318 return( PSA_SUCCESS );
319}
320
Darryl Greendb2b8db2018-06-15 13:06:04 +0100321psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
322 size_t storage_data_length,
323 uint8_t **key_data,
324 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200325 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100326{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300327 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100328 const psa_persistent_key_storage_format *storage_format =
329 (const psa_persistent_key_storage_format *)storage_data;
330 uint32_t version;
331
Moran Peker96ebf9e2018-06-28 18:02:17 +0300332 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100333 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300334
335 status = check_magic_header( storage_data );
336 if( status != PSA_SUCCESS )
337 return( status );
338
Gilles Peskine274a2632019-07-23 11:27:38 +0200339 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100340 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100341 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100342
Gilles Peskine274a2632019-07-23 11:27:38 +0200343 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100344 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
345 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100346 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100347
Gilles Peskine3495b582019-04-25 13:47:06 +0200348 if( *key_data_length == 0 )
349 {
350 *key_data = NULL;
351 }
352 else
353 {
354 *key_data = mbedtls_calloc( 1, *key_data_length );
355 if( *key_data == NULL )
356 return( PSA_ERROR_INSUFFICIENT_MEMORY );
357 memcpy( *key_data, storage_format->key_data, *key_data_length );
358 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100359
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200360 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200361 GET_UINT16_LE( attr->type, storage_format->type, 0 );
362 GET_UINT16_LE( attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200363 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
364 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
365 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100366
Darryl Greendb2b8db2018-06-15 13:06:04 +0100367 return( PSA_SUCCESS );
368}
369
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200370psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100371 const uint8_t *data,
372 const size_t data_length )
373{
374 size_t storage_data_length;
375 uint8_t *storage_data;
376 psa_status_t status;
377
Steven Cooremand80e8a42021-01-26 12:45:39 +0100378 /* All keys saved to persistent storage always have a key context */
379 if( data == NULL || data_length == 0 )
380 return( PSA_ERROR_INVALID_ARGUMENT );
381
Darryl Greendb2b8db2018-06-15 13:06:04 +0100382 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100383 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100384 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
385
386 storage_data = mbedtls_calloc( 1, storage_data_length );
387 if( storage_data == NULL )
388 return( PSA_ERROR_INSUFFICIENT_MEMORY );
389
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200390 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100391
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200392 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100393 storage_data, storage_data_length );
394
395 mbedtls_free( storage_data );
396
397 return( status );
398}
399
400void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
401{
402 if( key_data != NULL )
403 {
404 mbedtls_platform_zeroize( key_data, key_data_length );
405 }
406 mbedtls_free( key_data );
407}
408
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200409psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100410 uint8_t **data,
411 size_t *data_length )
412{
413 psa_status_t status = PSA_SUCCESS;
414 uint8_t *loaded_data;
415 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200416 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100417
418 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
419 if( status != PSA_SUCCESS )
420 return( status );
421
422 loaded_data = mbedtls_calloc( 1, storage_data_length );
423
424 if( loaded_data == NULL )
425 return( PSA_ERROR_INSUFFICIENT_MEMORY );
426
427 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
428 if( status != PSA_SUCCESS )
429 goto exit;
430
431 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200432 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100433
Steven Cooremand80e8a42021-01-26 12:45:39 +0100434 /* All keys saved to persistent storage always have a key context */
435 if( status == PSA_SUCCESS &&
436 ( *data == NULL || *data_length == 0 ) )
437 status = PSA_ERROR_STORAGE_FAILURE;
438
Darryl Greendb2b8db2018-06-15 13:06:04 +0100439exit:
440 mbedtls_free( loaded_data );
441 return( status );
442}
443
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200444
445
446/****************************************************************/
447/* Transactions */
448/****************************************************************/
449
450#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
451
452psa_crypto_transaction_t psa_crypto_transaction;
453
454psa_status_t psa_crypto_save_transaction( void )
455{
456 struct psa_storage_info_t p_info;
457 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000458 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200459 if( status == PSA_SUCCESS )
460 {
461 /* This shouldn't happen: we're trying to start a transaction while
462 * there is still a transaction that hasn't been replayed. */
463 return( PSA_ERROR_CORRUPTION_DETECTED );
464 }
465 else if( status != PSA_ERROR_DOES_NOT_EXIST )
466 return( status );
467 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
468 sizeof( psa_crypto_transaction ),
469 &psa_crypto_transaction,
470 0 ) );
471}
472
473psa_status_t psa_crypto_load_transaction( void )
474{
Gilles Peskine8b663892019-07-31 17:57:57 +0200475 psa_status_t status;
476 size_t length;
477 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
478 sizeof( psa_crypto_transaction ),
479 &psa_crypto_transaction, &length );
480 if( status != PSA_SUCCESS )
481 return( status );
482 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100483 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200484 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200485}
486
487psa_status_t psa_crypto_stop_transaction( void )
488{
489 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
490 /* Whether or not updating the storage succeeded, the transaction is
491 * finished now. It's too late to go back, so zero out the in-memory
492 * data. */
493 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
494 return( status );
495}
496
497#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
498
499
500
501/****************************************************************/
502/* Random generator state */
503/****************************************************************/
504
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100505#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
506psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
507 size_t seed_size )
508{
509 psa_status_t status;
510 struct psa_storage_info_t p_info;
511
512 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
513
514 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
515 {
516 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
517 }
518 else if( PSA_SUCCESS == status )
519 {
520 /* You should not be here. Seed needs to be injected only once */
521 status = PSA_ERROR_NOT_PERMITTED;
522 }
523 return( status );
524}
525#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
526
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200527
528
529/****************************************************************/
530/* The end */
531/****************************************************************/
532
Darryl Greendb2b8db2018-06-15 13:06:04 +0100533#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */