blob: 24bfdae127be9d9bf873242f363c69018cfff112 [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
Joe Subbianid6ea0632021-08-18 12:57:54 +010021#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#include "mbedtls/platform.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010040
Gilles Peskinec8336cb2019-07-22 19:26:12 +020041/****************************************************************/
42/* Key storage */
43/****************************************************************/
44
Ronald Cron71016a92020-08-28 19:01:50 +020045/* Determine a file name (ITS file identifier) for the given key identifier.
46 * The file name must be distinct from any file that is used for a purpose
47 * other than storing a key. Currently, the only such file is the random seed
48 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
49 * 0xFFFFFF52. */
50static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010051{
Ronald Cronecfb2372020-07-23 17:13:42 +020052#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010053 /* Encode the owner in the upper 32 bits. This means that if
54 * owner values are nonzero (as they are on a PSA platform),
55 * no key file will ever have a value less than 0x100000000, so
56 * the whole range 0..0xffffffff is available for non-key files. */
Ronald Cron79ca4272020-08-25 09:53:53 +020057 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
58 return( ( (uint64_t) unsigned_owner_id << 32 ) |
59 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010060#else
61 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020062 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010063 * is responsible for ensuring that key identifiers do not have a
64 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020065 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010066#endif
67}
68
Gilles Peskine5e80d912019-02-24 17:10:18 +010069/**
70 * \brief Load persistent data for the given key slot number.
71 *
72 * This function reads data from a storage backend and returns the data in a
73 * buffer.
74 *
75 * \param key Persistent identifier of the key to be loaded. This
76 * should be an occupied storage location.
77 * \param[out] data Buffer where the data is to be written.
78 * \param data_size Size of the \c data buffer in bytes.
79 *
Ronald Cron96783552020-10-19 12:06:30 +020080 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010081 * \retval #PSA_ERROR_DATA_INVALID
82 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020083 * \retval #PSA_ERROR_STORAGE_FAILURE
84 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010085 */
Ronald Cron71016a92020-08-28 19:01:50 +020086static psa_status_t psa_crypto_storage_load(
87 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010088{
89 psa_status_t status;
90 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
91 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010092 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +010093
94 status = psa_its_get_info( data_identifier, &data_identifier_info );
95 if( status != PSA_SUCCESS )
96 return( status );
97
Simon D Hughesbda5a212019-07-10 16:34:21 +010098 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
99 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100100 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101
102 return( status );
103}
104
Ronald Cron71016a92020-08-28 19:01:50 +0200105int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100106{
107 psa_status_t ret;
108 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
109 struct psa_storage_info_t data_identifier_info;
110
111 ret = psa_its_get_info( data_identifier, &data_identifier_info );
112
113 if( ret == PSA_ERROR_DOES_NOT_EXIST )
114 return( 0 );
115 return( 1 );
116}
117
Gilles Peskine5e80d912019-02-24 17:10:18 +0100118/**
119 * \brief Store persistent data for the given key slot number.
120 *
121 * This function stores the given data buffer to a persistent storage.
122 *
123 * \param key Persistent identifier of the key to be stored. This
124 * should be an unoccupied storage location.
125 * \param[in] data Buffer containing the data to be stored.
126 * \param data_length The number of bytes
127 * that make up the data.
128 *
Ronald Cron96783552020-10-19 12:06:30 +0200129 * \retval #PSA_SUCCESS
130 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200131 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100132 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100133 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100134 */
Ronald Cron71016a92020-08-28 19:01:50 +0200135static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100136 const uint8_t *data,
137 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100138{
139 psa_status_t status;
140 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
141 struct psa_storage_info_t data_identifier_info;
142
143 if( psa_is_key_present_in_storage( key ) == 1 )
144 return( PSA_ERROR_ALREADY_EXISTS );
145
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100146 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100147 if( status != PSA_SUCCESS )
148 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100149 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100150 }
151
152 status = psa_its_get_info( data_identifier, &data_identifier_info );
153 if( status != PSA_SUCCESS )
154 {
155 goto exit;
156 }
157
158 if( data_identifier_info.size != data_length )
159 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100160 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100161 goto exit;
162 }
163
164exit:
165 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200166 {
167 /* Remove the file in case we managed to create it but something
168 * went wrong. It's ok if the file doesn't exist. If the file exists
169 * but the removal fails, we're already reporting an error so there's
170 * nothing else we can do. */
171 (void) psa_its_remove( data_identifier );
172 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100173 return( status );
174}
175
Ronald Cron71016a92020-08-28 19:01:50 +0200176psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100177{
178 psa_status_t ret;
179 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
180 struct psa_storage_info_t data_identifier_info;
181
182 ret = psa_its_get_info( data_identifier, &data_identifier_info );
183 if( ret == PSA_ERROR_DOES_NOT_EXIST )
184 return( PSA_SUCCESS );
185
186 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100187 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100188
189 ret = psa_its_get_info( data_identifier, &data_identifier_info );
190 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100191 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100192
193 return( PSA_SUCCESS );
194}
195
Gilles Peskine5e80d912019-02-24 17:10:18 +0100196/**
197 * \brief Get data length for given key slot number.
198 *
199 * \param key Persistent identifier whose stored data length
200 * is to be obtained.
201 * \param[out] data_length The number of bytes that make up the data.
202 *
Ronald Cron96783552020-10-19 12:06:30 +0200203 * \retval #PSA_SUCCESS
204 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100205 * \retval #PSA_ERROR_DOES_NOT_EXIST
206 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100207 */
208static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200209 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100210 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100211{
212 psa_status_t status;
213 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
214 struct psa_storage_info_t data_identifier_info;
215
216 status = psa_its_get_info( data_identifier, &data_identifier_info );
217 if( status != PSA_SUCCESS )
218 return( status );
219
220 *data_length = (size_t) data_identifier_info.size;
221
222 return( PSA_SUCCESS );
223}
224
Moran Peker96ebf9e2018-06-28 18:02:17 +0300225/**
226 * Persistent key storage magic header.
227 */
228#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
229#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
230
Darryl Greendb2b8db2018-06-15 13:06:04 +0100231typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300232 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100233 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200234 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200235 uint8_t type[2];
236 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100237 uint8_t policy[sizeof( psa_key_policy_t )];
238 uint8_t data_len[4];
239 uint8_t key_data[];
240} psa_persistent_key_storage_format;
241
242void psa_format_key_data_for_storage( const uint8_t *data,
243 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200244 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100245 uint8_t *storage_data )
246{
247 psa_persistent_key_storage_format *storage_format =
248 (psa_persistent_key_storage_format *) storage_data;
249
Moran Peker96ebf9e2018-06-28 18:02:17 +0300250 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100251 MBEDTLS_PUT_UINT32_LE( 0, storage_format->version, 0 );
252 MBEDTLS_PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Joe Subbiani4530b272021-07-05 15:37:39 +0100253 MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
254 MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100255 MBEDTLS_PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
256 MBEDTLS_PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
257 MBEDTLS_PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
258 MBEDTLS_PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100259 memcpy( storage_format->key_data, data, data_length );
260}
261
Moran Peker96ebf9e2018-06-28 18:02:17 +0300262static psa_status_t check_magic_header( const uint8_t *data )
263{
264 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
265 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100266 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300267 return( PSA_SUCCESS );
268}
269
Darryl Greendb2b8db2018-06-15 13:06:04 +0100270psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
271 size_t storage_data_length,
272 uint8_t **key_data,
273 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200274 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100275{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300276 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100277 const psa_persistent_key_storage_format *storage_format =
278 (const psa_persistent_key_storage_format *)storage_data;
279 uint32_t version;
280
Moran Peker96ebf9e2018-06-28 18:02:17 +0300281 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100282 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300283
284 status = check_magic_header( storage_data );
285 if( status != PSA_SUCCESS )
286 return( status );
287
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100288 version = MBEDTLS_GET_UINT32_LE( storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100289 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100290 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100291
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100292 *key_data_length = MBEDTLS_GET_UINT32_LE( storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100293 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
294 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100295 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100296
Gilles Peskine3495b582019-04-25 13:47:06 +0200297 if( *key_data_length == 0 )
298 {
299 *key_data = NULL;
300 }
301 else
302 {
303 *key_data = mbedtls_calloc( 1, *key_data_length );
304 if( *key_data == NULL )
305 return( PSA_ERROR_INSUFFICIENT_MEMORY );
306 memcpy( *key_data, storage_format->key_data, *key_data_length );
307 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100308
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100309 attr->lifetime = MBEDTLS_GET_UINT32_LE( storage_format->lifetime, 0 );
310 attr->type = MBEDTLS_GET_UINT16_LE( storage_format->type, 0 );
311 attr->bits = MBEDTLS_GET_UINT16_LE( storage_format->bits, 0 );
312 attr->policy.usage = MBEDTLS_GET_UINT32_LE( storage_format->policy, 0 );
313 attr->policy.alg = MBEDTLS_GET_UINT32_LE( storage_format->policy, sizeof( uint32_t ) );
314 attr->policy.alg2 = MBEDTLS_GET_UINT32_LE( storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100315
Darryl Greendb2b8db2018-06-15 13:06:04 +0100316 return( PSA_SUCCESS );
317}
318
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200319psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100320 const uint8_t *data,
321 const size_t data_length )
322{
323 size_t storage_data_length;
324 uint8_t *storage_data;
325 psa_status_t status;
326
Steven Cooremand80e8a42021-01-26 12:45:39 +0100327 /* All keys saved to persistent storage always have a key context */
328 if( data == NULL || data_length == 0 )
329 return( PSA_ERROR_INVALID_ARGUMENT );
330
Darryl Greendb2b8db2018-06-15 13:06:04 +0100331 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100332 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100333 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
334
335 storage_data = mbedtls_calloc( 1, storage_data_length );
336 if( storage_data == NULL )
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200339 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100340
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200341 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100342 storage_data, storage_data_length );
343
Steven Cooreman901c9b72022-02-25 11:14:59 +0100344 mbedtls_platform_zeroize( storage_data, storage_data_length );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100345 mbedtls_free( storage_data );
346
347 return( status );
348}
349
350void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
351{
352 if( key_data != NULL )
353 {
354 mbedtls_platform_zeroize( key_data, key_data_length );
355 }
356 mbedtls_free( key_data );
357}
358
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200359psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100360 uint8_t **data,
361 size_t *data_length )
362{
363 psa_status_t status = PSA_SUCCESS;
364 uint8_t *loaded_data;
365 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200366 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100367
368 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
369 if( status != PSA_SUCCESS )
370 return( status );
371
372 loaded_data = mbedtls_calloc( 1, storage_data_length );
373
374 if( loaded_data == NULL )
375 return( PSA_ERROR_INSUFFICIENT_MEMORY );
376
377 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
378 if( status != PSA_SUCCESS )
379 goto exit;
380
381 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200382 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100383
Steven Cooremand80e8a42021-01-26 12:45:39 +0100384 /* All keys saved to persistent storage always have a key context */
385 if( status == PSA_SUCCESS &&
386 ( *data == NULL || *data_length == 0 ) )
387 status = PSA_ERROR_STORAGE_FAILURE;
388
Darryl Greendb2b8db2018-06-15 13:06:04 +0100389exit:
Steven Cooreman901c9b72022-02-25 11:14:59 +0100390 mbedtls_platform_zeroize( loaded_data, storage_data_length );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100391 mbedtls_free( loaded_data );
392 return( status );
393}
394
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200395
396
397/****************************************************************/
398/* Transactions */
399/****************************************************************/
400
401#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
402
403psa_crypto_transaction_t psa_crypto_transaction;
404
405psa_status_t psa_crypto_save_transaction( void )
406{
407 struct psa_storage_info_t p_info;
408 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000409 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200410 if( status == PSA_SUCCESS )
411 {
412 /* This shouldn't happen: we're trying to start a transaction while
413 * there is still a transaction that hasn't been replayed. */
414 return( PSA_ERROR_CORRUPTION_DETECTED );
415 }
416 else if( status != PSA_ERROR_DOES_NOT_EXIST )
417 return( status );
418 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
419 sizeof( psa_crypto_transaction ),
420 &psa_crypto_transaction,
421 0 ) );
422}
423
424psa_status_t psa_crypto_load_transaction( void )
425{
Gilles Peskine8b663892019-07-31 17:57:57 +0200426 psa_status_t status;
427 size_t length;
428 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
429 sizeof( psa_crypto_transaction ),
430 &psa_crypto_transaction, &length );
431 if( status != PSA_SUCCESS )
432 return( status );
433 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100434 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200435 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200436}
437
438psa_status_t psa_crypto_stop_transaction( void )
439{
440 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
441 /* Whether or not updating the storage succeeded, the transaction is
442 * finished now. It's too late to go back, so zero out the in-memory
443 * data. */
444 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
445 return( status );
446}
447
448#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
449
450
451
452/****************************************************************/
453/* Random generator state */
454/****************************************************************/
455
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100456#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
457psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
458 size_t seed_size )
459{
460 psa_status_t status;
461 struct psa_storage_info_t p_info;
462
463 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
464
465 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
466 {
467 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
468 }
469 else if( PSA_SUCCESS == status )
470 {
471 /* You should not be here. Seed needs to be injected only once */
472 status = PSA_ERROR_NOT_PERMITTED;
473 }
474 return( status );
475}
476#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
477
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200478
479
480/****************************************************************/
481/* The end */
482/****************************************************************/
483
Darryl Greendb2b8db2018-06-15 13:06:04 +0100484#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */