blob: e48bc282f850e9669b73a1be4ab8a6e2e142c5e8 [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 Cron71016a92020-08-28 19:01:50 +020070 uint32_t unsigned_owner = (uint32_t) key.owner;
71 return( (uint64_t) unsigned_owner << 32 | key.key_id );
Gilles Peskine088b77f2019-02-24 17:00:27 +010072#else
73 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020074 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010075 * is responsible for ensuring that key identifiers do not have a
76 * value that is reserved for non-key files. */
Ronald Cron71016a92020-08-28 19:01:50 +020077 return( key );
Gilles Peskine088b77f2019-02-24 17:00:27 +010078#endif
79}
80
Gilles Peskine5e80d912019-02-24 17:10:18 +010081/**
82 * \brief Load persistent data for the given key slot number.
83 *
84 * This function reads data from a storage backend and returns the data in a
85 * buffer.
86 *
87 * \param key Persistent identifier of the key to be loaded. This
88 * should be an occupied storage location.
89 * \param[out] data Buffer where the data is to be written.
90 * \param data_size Size of the \c data buffer in bytes.
91 *
92 * \retval PSA_SUCCESS
93 * \retval PSA_ERROR_STORAGE_FAILURE
94 * \retval PSA_ERROR_DOES_NOT_EXIST
95 */
Ronald Cron71016a92020-08-28 19:01:50 +020096static psa_status_t psa_crypto_storage_load(
97 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +010098{
99 psa_status_t status;
100 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
101 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100102 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100103
104 status = psa_its_get_info( data_identifier, &data_identifier_info );
105 if( status != PSA_SUCCESS )
106 return( status );
107
Simon D Hughesbda5a212019-07-10 16:34:21 +0100108 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
109 if( data_size != data_length )
110 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100111
112 return( status );
113}
114
Ronald Cron71016a92020-08-28 19:01:50 +0200115int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100116{
117 psa_status_t ret;
118 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
119 struct psa_storage_info_t data_identifier_info;
120
121 ret = psa_its_get_info( data_identifier, &data_identifier_info );
122
123 if( ret == PSA_ERROR_DOES_NOT_EXIST )
124 return( 0 );
125 return( 1 );
126}
127
Gilles Peskine5e80d912019-02-24 17:10:18 +0100128/**
129 * \brief Store persistent data for the given key slot number.
130 *
131 * This function stores the given data buffer to a persistent storage.
132 *
133 * \param key Persistent identifier of the key to be stored. This
134 * should be an unoccupied storage location.
135 * \param[in] data Buffer containing the data to be stored.
136 * \param data_length The number of bytes
137 * that make up the data.
138 *
139 * \retval PSA_SUCCESS
140 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
141 * \retval PSA_ERROR_STORAGE_FAILURE
142 * \retval PSA_ERROR_ALREADY_EXISTS
143 */
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 {
158 return( PSA_ERROR_STORAGE_FAILURE );
159 }
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 {
169 status = PSA_ERROR_STORAGE_FAILURE;
170 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 )
196 return( PSA_ERROR_STORAGE_FAILURE );
197
198 ret = psa_its_get_info( data_identifier, &data_identifier_info );
199 if( ret != PSA_ERROR_DOES_NOT_EXIST )
200 return( PSA_ERROR_STORAGE_FAILURE );
201
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 *
212 * \retval PSA_SUCCESS
213 * \retval PSA_ERROR_STORAGE_FAILURE
214 */
215static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200216 const mbedtls_svc_key_id_t key,
Gilles Peskine5e80d912019-02-24 17:10:18 +0100217 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100218{
219 psa_status_t status;
220 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
221 struct psa_storage_info_t data_identifier_info;
222
223 status = psa_its_get_info( data_identifier, &data_identifier_info );
224 if( status != PSA_SUCCESS )
225 return( status );
226
227 *data_length = (size_t) data_identifier_info.size;
228
229 return( PSA_SUCCESS );
230}
231
Darryl Greendb2b8db2018-06-15 13:06:04 +0100232/*
233 * 32-bit integer manipulation macros (little endian)
234 */
235#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200236#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100237{ \
238 (n) = ( (uint32_t) (b)[(i) ] ) \
239 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
240 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
241 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
242}
243#endif
244
245#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200246#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100247{ \
248 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
249 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
250 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
251 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
252}
253#endif
254
Moran Peker96ebf9e2018-06-28 18:02:17 +0300255/**
256 * Persistent key storage magic header.
257 */
258#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
259#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
260
Darryl Greendb2b8db2018-06-15 13:06:04 +0100261typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300262 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100263 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200264 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100265 uint8_t type[4]; /* Size=4 for a 2-byte type to keep the structure more
266 * regular and aligned and to make potential future
267 * extensibility easier. */
Darryl Greendb2b8db2018-06-15 13:06:04 +0100268 uint8_t policy[sizeof( psa_key_policy_t )];
269 uint8_t data_len[4];
270 uint8_t key_data[];
271} psa_persistent_key_storage_format;
272
273void psa_format_key_data_for_storage( const uint8_t *data,
274 const size_t data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200275 const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100276 uint8_t *storage_data )
277{
278 psa_persistent_key_storage_format *storage_format =
279 (psa_persistent_key_storage_format *) storage_data;
280
Moran Peker96ebf9e2018-06-28 18:02:17 +0300281 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200282 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200283 PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100284 PUT_UINT32_LE( (uint32_t) attr->type, storage_format->type, 0 );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200285 PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
286 PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
287 PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200288 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100289 memcpy( storage_format->key_data, data, data_length );
290}
291
Moran Peker96ebf9e2018-06-28 18:02:17 +0300292static psa_status_t check_magic_header( const uint8_t *data )
293{
294 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
295 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
296 return( PSA_ERROR_STORAGE_FAILURE );
297 return( PSA_SUCCESS );
298}
299
Darryl Greendb2b8db2018-06-15 13:06:04 +0100300psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
301 size_t storage_data_length,
302 uint8_t **key_data,
303 size_t *key_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200304 psa_core_key_attributes_t *attr )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100305{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300306 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100307 const psa_persistent_key_storage_format *storage_format =
308 (const psa_persistent_key_storage_format *)storage_data;
309 uint32_t version;
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100310 uint32_t type;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100311
Moran Peker96ebf9e2018-06-28 18:02:17 +0300312 if( storage_data_length < sizeof(*storage_format) )
313 return( PSA_ERROR_STORAGE_FAILURE );
314
315 status = check_magic_header( storage_data );
316 if( status != PSA_SUCCESS )
317 return( status );
318
Gilles Peskine274a2632019-07-23 11:27:38 +0200319 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100320 if( version != 0 )
321 return( PSA_ERROR_STORAGE_FAILURE );
322
Gilles Peskine274a2632019-07-23 11:27:38 +0200323 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100324 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
325 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
326 return( PSA_ERROR_STORAGE_FAILURE );
327
Gilles Peskine3495b582019-04-25 13:47:06 +0200328 if( *key_data_length == 0 )
329 {
330 *key_data = NULL;
331 }
332 else
333 {
334 *key_data = mbedtls_calloc( 1, *key_data_length );
335 if( *key_data == NULL )
336 return( PSA_ERROR_INSUFFICIENT_MEMORY );
337 memcpy( *key_data, storage_format->key_data, *key_data_length );
338 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100339
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200340 GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 );
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100341 GET_UINT32_LE( type, storage_format->type, 0 );
342 if( type <= (psa_key_type_t) -1 )
343 attr->type = (psa_key_type_t) type;
344 else
345 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200346 GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 );
347 GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) );
348 GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100349
Darryl Greendb2b8db2018-06-15 13:06:04 +0100350 return( PSA_SUCCESS );
351}
352
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200353psa_status_t psa_save_persistent_key( const psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100354 const uint8_t *data,
355 const size_t data_length )
356{
357 size_t storage_data_length;
358 uint8_t *storage_data;
359 psa_status_t status;
360
361 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
362 return PSA_ERROR_INSUFFICIENT_STORAGE;
363 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
364
365 storage_data = mbedtls_calloc( 1, storage_data_length );
366 if( storage_data == NULL )
367 return( PSA_ERROR_INSUFFICIENT_MEMORY );
368
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200369 psa_format_key_data_for_storage( data, data_length, attr, storage_data );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100370
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200371 status = psa_crypto_storage_store( attr->id,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100372 storage_data, storage_data_length );
373
374 mbedtls_free( storage_data );
375
376 return( status );
377}
378
379void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
380{
381 if( key_data != NULL )
382 {
383 mbedtls_platform_zeroize( key_data, key_data_length );
384 }
385 mbedtls_free( key_data );
386}
387
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200388psa_status_t psa_load_persistent_key( psa_core_key_attributes_t *attr,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100389 uint8_t **data,
390 size_t *data_length )
391{
392 psa_status_t status = PSA_SUCCESS;
393 uint8_t *loaded_data;
394 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200395 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100396
397 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
398 if( status != PSA_SUCCESS )
399 return( status );
400
401 loaded_data = mbedtls_calloc( 1, storage_data_length );
402
403 if( loaded_data == NULL )
404 return( PSA_ERROR_INSUFFICIENT_MEMORY );
405
406 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
407 if( status != PSA_SUCCESS )
408 goto exit;
409
410 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +0200411 data, data_length, attr );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100412
413exit:
414 mbedtls_free( loaded_data );
415 return( status );
416}
417
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200418
419
420/****************************************************************/
421/* Transactions */
422/****************************************************************/
423
424#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
425
426psa_crypto_transaction_t psa_crypto_transaction;
427
428psa_status_t psa_crypto_save_transaction( void )
429{
430 struct psa_storage_info_t p_info;
431 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000432 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200433 if( status == PSA_SUCCESS )
434 {
435 /* This shouldn't happen: we're trying to start a transaction while
436 * there is still a transaction that hasn't been replayed. */
437 return( PSA_ERROR_CORRUPTION_DETECTED );
438 }
439 else if( status != PSA_ERROR_DOES_NOT_EXIST )
440 return( status );
441 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
442 sizeof( psa_crypto_transaction ),
443 &psa_crypto_transaction,
444 0 ) );
445}
446
447psa_status_t psa_crypto_load_transaction( void )
448{
Gilles Peskine8b663892019-07-31 17:57:57 +0200449 psa_status_t status;
450 size_t length;
451 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
452 sizeof( psa_crypto_transaction ),
453 &psa_crypto_transaction, &length );
454 if( status != PSA_SUCCESS )
455 return( status );
456 if( length != sizeof( psa_crypto_transaction ) )
457 return( PSA_ERROR_STORAGE_FAILURE );
458 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200459}
460
461psa_status_t psa_crypto_stop_transaction( void )
462{
463 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
464 /* Whether or not updating the storage succeeded, the transaction is
465 * finished now. It's too late to go back, so zero out the in-memory
466 * data. */
467 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
468 return( status );
469}
470
471#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
472
473
474
475/****************************************************************/
476/* Random generator state */
477/****************************************************************/
478
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100479#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
480psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
481 size_t seed_size )
482{
483 psa_status_t status;
484 struct psa_storage_info_t p_info;
485
486 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
487
488 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
489 {
490 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
491 }
492 else if( PSA_SUCCESS == status )
493 {
494 /* You should not be here. Seed needs to be injected only once */
495 status = PSA_ERROR_NOT_PERMITTED;
496 }
497 return( status );
498}
499#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
500
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200501
502
503/****************************************************************/
504/* The end */
505/****************************************************************/
506
Darryl Greendb2b8db2018-06-15 13:06:04 +0100507#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */