blob: 773d3aaafc007d6dbda63ba3181a3da852441b27 [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
itayzafrir7723ab12019-02-14 10:28:02 +020032#include "psa_crypto_service_integration.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010033#include "psa/crypto.h"
34#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010035#include "mbedtls/platform_util.h"
36
Gilles Peskine5e80d912019-02-24 17:10:18 +010037#if defined(MBEDTLS_PSA_ITS_FILE_C)
38#include "psa_crypto_its.h"
39#else /* Native ITS implementation */
40#include "psa/error.h"
41#include "psa/internal_trusted_storage.h"
42#endif
43
Darryl Greendb2b8db2018-06-15 13:06:04 +010044#if defined(MBEDTLS_PLATFORM_C)
45#include "mbedtls/platform.h"
46#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000047#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinec8336cb2019-07-22 19:26:12 +020052
53
54/****************************************************************/
55/* Key storage */
56/****************************************************************/
57
Ronald Cron71016a92020-08-28 19:01:50 +020058/* Determine a file name (ITS file identifier) for the given key identifier.
59 * The file name must be distinct from any file that is used for a purpose
60 * other than storing a key. Currently, the only such file is the random seed
61 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
62 * 0xFFFFFF52. */
63static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010064{
Ronald Cronecfb2372020-07-23 17:13:42 +020065#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010066 /* Encode the owner in the upper 32 bits. This means that if
67 * owner values are nonzero (as they are on a PSA platform),
68 * no key file will ever have a value less than 0x100000000, so
69 * the whole range 0..0xffffffff is available for non-key files. */
Ronald Cron79ca4272020-08-25 09:53:53 +020070 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
71 return( ( (uint64_t) unsigned_owner_id << 32 ) |
72 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010073#else
74 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020075 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010076 * is responsible for ensuring that key identifiers do not have a
77 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020078 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010079#endif
80}
81
Gilles Peskine5e80d912019-02-24 17:10:18 +010082/**
83 * \brief Load persistent data for the given key slot number.
84 *
85 * This function reads data from a storage backend and returns the data in a
86 * buffer.
87 *
88 * \param key Persistent identifier of the key to be loaded. This
89 * should be an occupied storage location.
90 * \param[out] data Buffer where the data is to be written.
91 * \param data_size Size of the \c data buffer in bytes.
92 *
Ronald Cron96783552020-10-19 12:06:30 +020093 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010094 * \retval #PSA_ERROR_DATA_INVALID
95 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020096 * \retval #PSA_ERROR_STORAGE_FAILURE
97 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010098 */
Ronald Cron71016a92020-08-28 19:01:50 +020099static psa_status_t psa_crypto_storage_load(
100 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101{
102 psa_status_t status;
103 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
104 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100105 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100106
107 status = psa_its_get_info( data_identifier, &data_identifier_info );
108 if( status != PSA_SUCCESS )
109 return( status );
110
Simon D Hughesbda5a212019-07-10 16:34:21 +0100111 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
112 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100113 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100114
115 return( status );
116}
117
Ronald Cron71016a92020-08-28 19:01:50 +0200118int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100119{
120 psa_status_t ret;
121 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
122 struct psa_storage_info_t data_identifier_info;
123
124 ret = psa_its_get_info( data_identifier, &data_identifier_info );
125
126 if( ret == PSA_ERROR_DOES_NOT_EXIST )
127 return( 0 );
128 return( 1 );
129}
130
Gilles Peskine5e80d912019-02-24 17:10:18 +0100131/**
132 * \brief Store persistent data for the given key slot number.
133 *
134 * This function stores the given data buffer to a persistent storage.
135 *
136 * \param key Persistent identifier of the key to be stored. This
137 * should be an unoccupied storage location.
138 * \param[in] data Buffer containing the data to be stored.
139 * \param data_length The number of bytes
140 * that make up the data.
141 *
Ronald Cron96783552020-10-19 12:06:30 +0200142 * \retval #PSA_SUCCESS
143 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200144 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100145 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100146 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100147 */
Ronald Cron71016a92020-08-28 19:01:50 +0200148static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100149 const uint8_t *data,
150 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100151{
152 psa_status_t status;
153 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
154 struct psa_storage_info_t data_identifier_info;
155
156 if( psa_is_key_present_in_storage( key ) == 1 )
157 return( PSA_ERROR_ALREADY_EXISTS );
158
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100159 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100160 if( status != PSA_SUCCESS )
161 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100162 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100163 }
164
165 status = psa_its_get_info( data_identifier, &data_identifier_info );
166 if( status != PSA_SUCCESS )
167 {
168 goto exit;
169 }
170
171 if( data_identifier_info.size != data_length )
172 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100173 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100174 goto exit;
175 }
176
177exit:
178 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200179 {
180 /* Remove the file in case we managed to create it but something
181 * went wrong. It's ok if the file doesn't exist. If the file exists
182 * but the removal fails, we're already reporting an error so there's
183 * nothing else we can do. */
184 (void) psa_its_remove( data_identifier );
185 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100186 return( status );
187}
188
Ronald Cron71016a92020-08-28 19:01:50 +0200189psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100190{
191 psa_status_t ret;
192 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
193 struct psa_storage_info_t data_identifier_info;
194
195 ret = psa_its_get_info( data_identifier, &data_identifier_info );
196 if( ret == PSA_ERROR_DOES_NOT_EXIST )
197 return( PSA_SUCCESS );
198
199 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100200 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100201
202 ret = psa_its_get_info( data_identifier, &data_identifier_info );
203 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100204 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100205
206 return( PSA_SUCCESS );
207}
208
Gilles Peskine5e80d912019-02-24 17:10:18 +0100209/**
210 * \brief Get data length for given key slot number.
211 *
212 * \param key Persistent identifier whose stored data length
213 * is to be obtained.
214 * \param[out] data_length The number of bytes that make up the data.
215 *
Ronald Cron96783552020-10-19 12:06:30 +0200216 * \retval #PSA_SUCCESS
217 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100218 * \retval #PSA_ERROR_DOES_NOT_EXIST
219 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100220 */
221static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200222 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100223 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100224{
225 psa_status_t status;
226 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
227 struct psa_storage_info_t data_identifier_info;
228
229 status = psa_its_get_info( data_identifier, &data_identifier_info );
230 if( status != PSA_SUCCESS )
231 return( status );
232
233 *data_length = (size_t) data_identifier_info.size;
234
235 return( PSA_SUCCESS );
236}
237
Darryl Greendb2b8db2018-06-15 13:06:04 +0100238/*
239 * 32-bit integer manipulation macros (little endian)
240 */
241#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200242#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100243{ \
244 (n) = ( (uint32_t) (b)[(i) ] ) \
245 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
246 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
247 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
248}
249#endif
250
251#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200252#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100253{ \
254 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
255 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
256 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
257 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
258}
259#endif
260
Torstein Nesse162a1102020-10-07 10:50:15 +0200261/*
262 * 16-bit integer manipulation macros (little endian)
263 */
264#ifndef GET_UINT16_LE
265#define GET_UINT16_LE( n, b, i ) \
266{ \
267 (n) = ( (uint16_t) (b)[(i) ] ) \
268 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
269}
270#endif
271
272#ifndef PUT_UINT16_LE
273#define PUT_UINT16_LE( n, b, i ) \
274{ \
275 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
276 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
277}
278#endif
279
Moran Peker96ebf9e2018-06-28 18:02:17 +0300280/**
281 * Persistent key storage magic header.
282 */
283#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
284#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
285
Darryl Greendb2b8db2018-06-15 13:06:04 +0100286typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300287 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100288 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200289 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200290 uint8_t type[2];
291 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100292 uint8_t policy[sizeof( psa_key_policy_t )];
293 uint8_t data_len[4];
294 uint8_t key_data[];
295} psa_persistent_key_storage_format;
296
297void psa_format_key_data_for_storage( const uint8_t *data,
298 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200299 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100300 uint8_t *storage_data )
301{
302 psa_persistent_key_storage_format *storage_format =
303 (psa_persistent_key_storage_format *) storage_data;
304
Moran Peker96ebf9e2018-06-28 18:02:17 +0300305 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200306 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200307 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200308 PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
309 PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200310 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
311 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
312 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200313 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100314 memcpy( storage_format->key_data, data, data_length );
315}
316
Moran Peker96ebf9e2018-06-28 18:02:17 +0300317static psa_status_t check_magic_header( const uint8_t *data )
318{
319 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
320 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100321 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300322 return( PSA_SUCCESS );
323}
324
Darryl Greendb2b8db2018-06-15 13:06:04 +0100325psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
326 size_t storage_data_length,
327 uint8_t **key_data,
328 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200329 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100330{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300331 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100332 const psa_persistent_key_storage_format *storage_format =
333 (const psa_persistent_key_storage_format *)storage_data;
334 uint32_t version;
335
Moran Peker96ebf9e2018-06-28 18:02:17 +0300336 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100337 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300338
339 status = check_magic_header( storage_data );
340 if( status != PSA_SUCCESS )
341 return( status );
342
Gilles Peskine274a2632019-07-23 11:27:38 +0200343 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100344 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100345 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100346
Gilles Peskine274a2632019-07-23 11:27:38 +0200347 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100348 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
349 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100350 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100351
Gilles Peskine3495b582019-04-25 13:47:06 +0200352 if( *key_data_length == 0 )
353 {
354 *key_data = NULL;
355 }
356 else
357 {
358 *key_data = mbedtls_calloc( 1, *key_data_length );
359 if( *key_data == NULL )
360 return( PSA_ERROR_INSUFFICIENT_MEMORY );
361 memcpy( *key_data, storage_format->key_data, *key_data_length );
362 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100363
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200364 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Torstein Nesse162a1102020-10-07 10:50:15 +0200365 GET_UINT16_LE( attr->type, storage_format->type, 0 );
366 GET_UINT16_LE( attr->bits, storage_format->bits, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200367 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
368 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
369 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100370
Darryl Greendb2b8db2018-06-15 13:06:04 +0100371 return( PSA_SUCCESS );
372}
373
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200374psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100375 const uint8_t *data,
376 const size_t data_length )
377{
378 size_t storage_data_length;
379 uint8_t *storage_data;
380 psa_status_t status;
381
Steven Cooremand80e8a42021-01-26 12:45:39 +0100382 /* All keys saved to persistent storage always have a key context */
383 if( data == NULL || data_length == 0 )
384 return( PSA_ERROR_INVALID_ARGUMENT );
385
Darryl Greendb2b8db2018-06-15 13:06:04 +0100386 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100387 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100388 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
389
390 storage_data = mbedtls_calloc( 1, storage_data_length );
391 if( storage_data == NULL )
392 return( PSA_ERROR_INSUFFICIENT_MEMORY );
393
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200394 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100395
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200396 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100397 storage_data, storage_data_length );
398
399 mbedtls_free( storage_data );
400
401 return( status );
402}
403
404void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
405{
406 if( key_data != NULL )
407 {
408 mbedtls_platform_zeroize( key_data, key_data_length );
409 }
410 mbedtls_free( key_data );
411}
412
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200413psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100414 uint8_t **data,
415 size_t *data_length )
416{
417 psa_status_t status = PSA_SUCCESS;
418 uint8_t *loaded_data;
419 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200420 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100421
422 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
423 if( status != PSA_SUCCESS )
424 return( status );
425
426 loaded_data = mbedtls_calloc( 1, storage_data_length );
427
428 if( loaded_data == NULL )
429 return( PSA_ERROR_INSUFFICIENT_MEMORY );
430
431 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
432 if( status != PSA_SUCCESS )
433 goto exit;
434
435 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200436 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100437
Steven Cooremand80e8a42021-01-26 12:45:39 +0100438 /* All keys saved to persistent storage always have a key context */
439 if( status == PSA_SUCCESS &&
440 ( *data == NULL || *data_length == 0 ) )
441 status = PSA_ERROR_STORAGE_FAILURE;
442
Darryl Greendb2b8db2018-06-15 13:06:04 +0100443exit:
444 mbedtls_free( loaded_data );
445 return( status );
446}
447
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200448
449
450/****************************************************************/
451/* Transactions */
452/****************************************************************/
453
454#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
455
456psa_crypto_transaction_t psa_crypto_transaction;
457
458psa_status_t psa_crypto_save_transaction( void )
459{
460 struct psa_storage_info_t p_info;
461 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000462 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200463 if( status == PSA_SUCCESS )
464 {
465 /* This shouldn't happen: we're trying to start a transaction while
466 * there is still a transaction that hasn't been replayed. */
467 return( PSA_ERROR_CORRUPTION_DETECTED );
468 }
469 else if( status != PSA_ERROR_DOES_NOT_EXIST )
470 return( status );
471 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
472 sizeof( psa_crypto_transaction ),
473 &psa_crypto_transaction,
474 0 ) );
475}
476
477psa_status_t psa_crypto_load_transaction( void )
478{
Gilles Peskine8b663892019-07-31 17:57:57 +0200479 psa_status_t status;
480 size_t length;
481 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
482 sizeof( psa_crypto_transaction ),
483 &psa_crypto_transaction, &length );
484 if( status != PSA_SUCCESS )
485 return( status );
486 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100487 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200488 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200489}
490
491psa_status_t psa_crypto_stop_transaction( void )
492{
493 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
494 /* Whether or not updating the storage succeeded, the transaction is
495 * finished now. It's too late to go back, so zero out the in-memory
496 * data. */
497 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
498 return( status );
499}
500
501#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
502
503
504
505/****************************************************************/
506/* Random generator state */
507/****************************************************************/
508
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100509#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
510psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
511 size_t seed_size )
512{
513 psa_status_t status;
514 struct psa_storage_info_t p_info;
515
516 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
517
518 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
519 {
520 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
521 }
522 else if( PSA_SUCCESS == status )
523 {
524 /* You should not be here. Seed needs to be injected only once */
525 status = PSA_ERROR_NOT_PERMITTED;
526 }
527 return( status );
528}
529#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
530
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200531
532
533/****************************************************************/
534/* The end */
535/****************************************************************/
536
Darryl Greendb2b8db2018-06-15 13:06:04 +0100537#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */