blob: 3186a36855d4eb7b3cc17e082c1df262066b936c [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#include "mbedtls/platform.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010040
Gilles Peskinec8336cb2019-07-22 19:26:12 +020041
42
43/****************************************************************/
44/* Key storage */
45/****************************************************************/
46
Ronald Cron71016a92020-08-28 19:01:50 +020047/* Determine a file name (ITS file identifier) for the given key identifier.
48 * The file name must be distinct from any file that is used for a purpose
49 * other than storing a key. Currently, the only such file is the random seed
50 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
51 * 0xFFFFFF52. */
52static psa_storage_uid_t psa_its_identifier_of_slot( mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +010053{
Ronald Cronecfb2372020-07-23 17:13:42 +020054#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010055 /* Encode the owner in the upper 32 bits. This means that if
56 * owner values are nonzero (as they are on a PSA platform),
57 * no key file will ever have a value less than 0x100000000, so
58 * the whole range 0..0xffffffff is available for non-key files. */
Ronald Cron79ca4272020-08-25 09:53:53 +020059 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key );
60 return( ( (uint64_t) unsigned_owner_id << 32 ) |
61 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
Gilles Peskine088b77f2019-02-24 17:00:27 +010062#else
63 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020064 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010065 * is responsible for ensuring that key identifiers do not have a
66 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020067 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010068#endif
69}
70
Gilles Peskine5e80d912019-02-24 17:10:18 +010071/**
72 * \brief Load persistent data for the given key slot number.
73 *
74 * This function reads data from a storage backend and returns the data in a
75 * buffer.
76 *
77 * \param key Persistent identifier of the key to be loaded. This
78 * should be an occupied storage location.
79 * \param[out] data Buffer where the data is to be written.
80 * \param data_size Size of the \c data buffer in bytes.
81 *
Ronald Cron96783552020-10-19 12:06:30 +020082 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010083 * \retval #PSA_ERROR_DATA_INVALID
84 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020085 * \retval #PSA_ERROR_STORAGE_FAILURE
86 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010087 */
Ronald Cron71016a92020-08-28 19:01:50 +020088static psa_status_t psa_crypto_storage_load(
89 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010090{
91 psa_status_t status;
92 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
93 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010094 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +010095
96 status = psa_its_get_info( data_identifier, &data_identifier_info );
97 if( status != PSA_SUCCESS )
98 return( status );
99
Simon D Hughesbda5a212019-07-10 16:34:21 +0100100 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
101 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100102 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100103
104 return( status );
105}
106
Ronald Cron71016a92020-08-28 19:01:50 +0200107int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100108{
109 psa_status_t ret;
110 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
111 struct psa_storage_info_t data_identifier_info;
112
113 ret = psa_its_get_info( data_identifier, &data_identifier_info );
114
115 if( ret == PSA_ERROR_DOES_NOT_EXIST )
116 return( 0 );
117 return( 1 );
118}
119
Gilles Peskine5e80d912019-02-24 17:10:18 +0100120/**
121 * \brief Store persistent data for the given key slot number.
122 *
123 * This function stores the given data buffer to a persistent storage.
124 *
125 * \param key Persistent identifier of the key to be stored. This
126 * should be an unoccupied storage location.
127 * \param[in] data Buffer containing the data to be stored.
128 * \param data_length The number of bytes
129 * that make up the data.
130 *
Ronald Cron96783552020-10-19 12:06:30 +0200131 * \retval #PSA_SUCCESS
132 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200133 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100134 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100135 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100136 */
Ronald Cron71016a92020-08-28 19:01:50 +0200137static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100138 const uint8_t *data,
139 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100140{
141 psa_status_t status;
142 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
143 struct psa_storage_info_t data_identifier_info;
144
145 if( psa_is_key_present_in_storage( key ) == 1 )
146 return( PSA_ERROR_ALREADY_EXISTS );
147
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100148 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100149 if( status != PSA_SUCCESS )
150 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100151 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100152 }
153
154 status = psa_its_get_info( data_identifier, &data_identifier_info );
155 if( status != PSA_SUCCESS )
156 {
157 goto exit;
158 }
159
160 if( data_identifier_info.size != data_length )
161 {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100162 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100163 goto exit;
164 }
165
166exit:
167 if( status != PSA_SUCCESS )
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200168 {
169 /* Remove the file in case we managed to create it but something
170 * went wrong. It's ok if the file doesn't exist. If the file exists
171 * but the removal fails, we're already reporting an error so there's
172 * nothing else we can do. */
173 (void) psa_its_remove( data_identifier );
174 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100175 return( status );
176}
177
Ronald Cron71016a92020-08-28 19:01:50 +0200178psa_status_t psa_destroy_persistent_key( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100179{
180 psa_status_t ret;
181 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
182 struct psa_storage_info_t data_identifier_info;
183
184 ret = psa_its_get_info( data_identifier, &data_identifier_info );
185 if( ret == PSA_ERROR_DOES_NOT_EXIST )
186 return( PSA_SUCCESS );
187
188 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100189 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100190
191 ret = psa_its_get_info( data_identifier, &data_identifier_info );
192 if( ret != PSA_ERROR_DOES_NOT_EXIST )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100193 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100194
195 return( PSA_SUCCESS );
196}
197
Gilles Peskine5e80d912019-02-24 17:10:18 +0100198/**
199 * \brief Get data length for given key slot number.
200 *
201 * \param key Persistent identifier whose stored data length
202 * is to be obtained.
203 * \param[out] data_length The number of bytes that make up the data.
204 *
Ronald Cron96783552020-10-19 12:06:30 +0200205 * \retval #PSA_SUCCESS
206 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100207 * \retval #PSA_ERROR_DOES_NOT_EXIST
208 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100209 */
210static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200211 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100212 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100213{
214 psa_status_t status;
215 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
216 struct psa_storage_info_t data_identifier_info;
217
218 status = psa_its_get_info( data_identifier, &data_identifier_info );
219 if( status != PSA_SUCCESS )
220 return( status );
221
222 *data_length = (size_t) data_identifier_info.size;
223
224 return( PSA_SUCCESS );
225}
226
Moran Peker96ebf9e2018-06-28 18:02:17 +0300227/**
228 * Persistent key storage magic header.
229 */
230#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
231#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
232
Darryl Greendb2b8db2018-06-15 13:06:04 +0100233typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300234 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100235 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200236 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Torstein Nesse162a1102020-10-07 10:50:15 +0200237 uint8_t type[2];
238 uint8_t bits[2];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100239 uint8_t policy[sizeof( psa_key_policy_t )];
240 uint8_t data_len[4];
241 uint8_t key_data[];
242} psa_persistent_key_storage_format;
243
244void psa_format_key_data_for_storage( const uint8_t *data,
245 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200246 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100247 uint8_t *storage_data )
248{
249 psa_persistent_key_storage_format *storage_format =
250 (psa_persistent_key_storage_format *) storage_data;
251
Moran Peker96ebf9e2018-06-28 18:02:17 +0300252 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Joe Subbiani5ecac212021-06-24 13:00:03 +0100253 MBEDTLS_PUT_UINT32_LE( 0, storage_format->version, 0 );
254 MBEDTLS_PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100255 MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 );
256 MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 );
Joe Subbiani5ecac212021-06-24 13:00:03 +0100257 MBEDTLS_PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
258 MBEDTLS_PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
259 MBEDTLS_PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
260 MBEDTLS_PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100261 memcpy( storage_format->key_data, data, data_length );
262}
263
Moran Peker96ebf9e2018-06-28 18:02:17 +0300264static psa_status_t check_magic_header( const uint8_t *data )
265{
266 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
267 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100268 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300269 return( PSA_SUCCESS );
270}
271
Darryl Greendb2b8db2018-06-15 13:06:04 +0100272psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
273 size_t storage_data_length,
274 uint8_t **key_data,
275 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200276 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100277{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300278 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100279 const psa_persistent_key_storage_format *storage_format =
280 (const psa_persistent_key_storage_format *)storage_data;
281 uint32_t version;
282
Moran Peker96ebf9e2018-06-28 18:02:17 +0300283 if( storage_data_length < sizeof(*storage_format) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100284 return( PSA_ERROR_DATA_INVALID );
Moran Peker96ebf9e2018-06-28 18:02:17 +0300285
286 status = check_magic_header( storage_data );
287 if( status != PSA_SUCCESS )
288 return( status );
289
Joe Subbiani6a506312021-07-07 16:56:29 +0100290 version = MBEDTLS_GET_UINT32_LE( storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100291 if( version != 0 )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100292 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100293
Joe Subbiani6a506312021-07-07 16:56:29 +0100294 *key_data_length = MBEDTLS_GET_UINT32_LE( storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100295 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
296 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100297 return( PSA_ERROR_DATA_INVALID );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100298
Gilles Peskine3495b582019-04-25 13:47:06 +0200299 if( *key_data_length == 0 )
300 {
301 *key_data = NULL;
302 }
303 else
304 {
305 *key_data = mbedtls_calloc( 1, *key_data_length );
306 if( *key_data == NULL )
307 return( PSA_ERROR_INSUFFICIENT_MEMORY );
308 memcpy( *key_data, storage_format->key_data, *key_data_length );
309 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100310
Joe Subbiani6a506312021-07-07 16:56:29 +0100311 attr->lifetime = MBEDTLS_GET_UINT32_LE( storage_format->lifetime, 0 );
312 attr->type = MBEDTLS_GET_UINT16_LE( storage_format->type, 0 );
313 attr->bits = MBEDTLS_GET_UINT16_LE( storage_format->bits, 0 );
314 attr->policy.usage = MBEDTLS_GET_UINT32_LE( storage_format->policy, 0 );
315 attr->policy.alg = MBEDTLS_GET_UINT32_LE( storage_format->policy, sizeof( uint32_t ) );
316 attr->policy.alg2 = MBEDTLS_GET_UINT32_LE( storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100317
Darryl Greendb2b8db2018-06-15 13:06:04 +0100318 return( PSA_SUCCESS );
319}
320
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200321psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100322 const uint8_t *data,
323 const size_t data_length )
324{
325 size_t storage_data_length;
326 uint8_t *storage_data;
327 psa_status_t status;
328
Steven Cooremand80e8a42021-01-26 12:45:39 +0100329 /* All keys saved to persistent storage always have a key context */
330 if( data == NULL || data_length == 0 )
331 return( PSA_ERROR_INVALID_ARGUMENT );
332
Darryl Greendb2b8db2018-06-15 13:06:04 +0100333 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
Steven Cooremand80e8a42021-01-26 12:45:39 +0100334 return( PSA_ERROR_INSUFFICIENT_STORAGE );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100335 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
336
337 storage_data = mbedtls_calloc( 1, storage_data_length );
338 if( storage_data == NULL )
339 return( PSA_ERROR_INSUFFICIENT_MEMORY );
340
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200341 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100342
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200343 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100344 storage_data, storage_data_length );
345
Steven Cooremancd5be322022-02-25 11:14:59 +0100346 mbedtls_platform_zeroize( storage_data, storage_data_length );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100347 mbedtls_free( storage_data );
348
349 return( status );
350}
351
352void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
353{
354 if( key_data != NULL )
355 {
356 mbedtls_platform_zeroize( key_data, key_data_length );
357 }
358 mbedtls_free( key_data );
359}
360
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200361psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100362 uint8_t **data,
363 size_t *data_length )
364{
365 psa_status_t status = PSA_SUCCESS;
366 uint8_t *loaded_data;
367 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200368 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100369
370 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
371 if( status != PSA_SUCCESS )
372 return( status );
373
374 loaded_data = mbedtls_calloc( 1, storage_data_length );
375
376 if( loaded_data == NULL )
377 return( PSA_ERROR_INSUFFICIENT_MEMORY );
378
379 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
380 if( status != PSA_SUCCESS )
381 goto exit;
382
383 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200384 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100385
Steven Cooremand80e8a42021-01-26 12:45:39 +0100386 /* All keys saved to persistent storage always have a key context */
387 if( status == PSA_SUCCESS &&
388 ( *data == NULL || *data_length == 0 ) )
389 status = PSA_ERROR_STORAGE_FAILURE;
390
Darryl Greendb2b8db2018-06-15 13:06:04 +0100391exit:
Steven Cooremancd5be322022-02-25 11:14:59 +0100392 mbedtls_platform_zeroize( loaded_data, storage_data_length );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100393 mbedtls_free( loaded_data );
394 return( status );
395}
396
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200397
398
399/****************************************************************/
400/* Transactions */
401/****************************************************************/
402
403#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
404
405psa_crypto_transaction_t psa_crypto_transaction;
406
407psa_status_t psa_crypto_save_transaction( void )
408{
409 struct psa_storage_info_t p_info;
410 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000411 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200412 if( status == PSA_SUCCESS )
413 {
414 /* This shouldn't happen: we're trying to start a transaction while
415 * there is still a transaction that hasn't been replayed. */
416 return( PSA_ERROR_CORRUPTION_DETECTED );
417 }
418 else if( status != PSA_ERROR_DOES_NOT_EXIST )
419 return( status );
420 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
421 sizeof( psa_crypto_transaction ),
422 &psa_crypto_transaction,
423 0 ) );
424}
425
426psa_status_t psa_crypto_load_transaction( void )
427{
Gilles Peskine8b663892019-07-31 17:57:57 +0200428 psa_status_t status;
429 size_t length;
430 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
431 sizeof( psa_crypto_transaction ),
432 &psa_crypto_transaction, &length );
433 if( status != PSA_SUCCESS )
434 return( status );
435 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100436 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200437 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200438}
439
440psa_status_t psa_crypto_stop_transaction( void )
441{
442 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
443 /* Whether or not updating the storage succeeded, the transaction is
444 * finished now. It's too late to go back, so zero out the in-memory
445 * data. */
446 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
447 return( status );
448}
449
450#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
451
452
453
454/****************************************************************/
455/* Random generator state */
456/****************************************************************/
457
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100458#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
459psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
460 size_t seed_size )
461{
462 psa_status_t status;
463 struct psa_storage_info_t p_info;
464
465 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
466
467 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
468 {
469 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
470 }
471 else if( PSA_SUCCESS == status )
472 {
473 /* You should not be here. Seed needs to be injected only once */
474 status = PSA_ERROR_NOT_PERMITTED;
475 }
476 return( status );
477}
478#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
479
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200480
481
482/****************************************************************/
483/* The end */
484/****************************************************************/
485
Darryl Greendb2b8db2018-06-15 13:06:04 +0100486#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */