blob: 86e416dd8adf215953e1fe5957e35f9ab93cbf07 [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_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010097 */
Ronald Cron71016a92020-08-28 19:01:50 +020098static psa_status_t psa_crypto_storage_load(
99 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100100{
101 psa_status_t status;
102 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
103 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100104 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100105
106 status = psa_its_get_info( data_identifier, &data_identifier_info );
107 if( status != PSA_SUCCESS )
108 return( status );
109
Simon D Hughesbda5a212019-07-10 16:34:21 +0100110 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
111 if( data_size != data_length )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100112 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100113
114 return( status );
115}
116
Ronald Cron71016a92020-08-28 19:01:50 +0200117int psa_is_key_present_in_storage( const mbedtls_svc_key_id_t key )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100118{
119 psa_status_t ret;
120 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
121 struct psa_storage_info_t data_identifier_info;
122
123 ret = psa_its_get_info( data_identifier, &data_identifier_info );
124
125 if( ret == PSA_ERROR_DOES_NOT_EXIST )
126 return( 0 );
127 return( 1 );
128}
129
Gilles Peskine5e80d912019-02-24 17:10:18 +0100130/**
131 * \brief Store persistent data for the given key slot number.
132 *
133 * This function stores the given data buffer to a persistent storage.
134 *
135 * \param key Persistent identifier of the key to be stored. This
136 * should be an unoccupied storage location.
137 * \param[in] data Buffer containing the data to be stored.
138 * \param data_length The number of bytes
139 * that make up the data.
140 *
Ronald Cron96783552020-10-19 12:06:30 +0200141 * \retval #PSA_SUCCESS
Ronald Cron96783552020-10-19 12:06:30 +0200142 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100143 * \retval #PSA_ERROR_DATA_INVALID
144 * \retval #PSA_ERROR_INVALID_ARGUMENT
145 * \retval #PSA_ERROR_DOES_NOT_EXIST
146 * \retval #PSA_ERROR_DATA_CORRUPT
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
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100217 * \retval #PSA_ERROR_DOES_NOT_EXIST
218 * \retval #PSA_ERROR_DATA_CORRUPT
219 * \retval #PSA_ERROR_INVALID_ARGUMENT
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
382 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
383 return PSA_ERROR_INSUFFICIENT_STORAGE;
384 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
434exit:
435 mbedtls_free( loaded_data );
436 return( status );
437}
438
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200439
440
441/****************************************************************/
442/* Transactions */
443/****************************************************************/
444
445#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
446
447psa_crypto_transaction_t psa_crypto_transaction;
448
449psa_status_t psa_crypto_save_transaction( void )
450{
451 struct psa_storage_info_t p_info;
452 psa_status_t status;
Jaeden Amero2ce22a52019-10-28 15:25:10 +0000453 status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200454 if( status == PSA_SUCCESS )
455 {
456 /* This shouldn't happen: we're trying to start a transaction while
457 * there is still a transaction that hasn't been replayed. */
458 return( PSA_ERROR_CORRUPTION_DETECTED );
459 }
460 else if( status != PSA_ERROR_DOES_NOT_EXIST )
461 return( status );
462 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
463 sizeof( psa_crypto_transaction ),
464 &psa_crypto_transaction,
465 0 ) );
466}
467
468psa_status_t psa_crypto_load_transaction( void )
469{
Gilles Peskine8b663892019-07-31 17:57:57 +0200470 psa_status_t status;
471 size_t length;
472 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
473 sizeof( psa_crypto_transaction ),
474 &psa_crypto_transaction, &length );
475 if( status != PSA_SUCCESS )
476 return( status );
477 if( length != sizeof( psa_crypto_transaction ) )
gabor-mezei-armfe309242020-11-09 17:39:56 +0100478 return( PSA_ERROR_DATA_INVALID );
Gilles Peskine8b663892019-07-31 17:57:57 +0200479 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200480}
481
482psa_status_t psa_crypto_stop_transaction( void )
483{
484 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
485 /* Whether or not updating the storage succeeded, the transaction is
486 * finished now. It's too late to go back, so zero out the in-memory
487 * data. */
488 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
489 return( status );
490}
491
492#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
493
494
495
496/****************************************************************/
497/* Random generator state */
498/****************************************************************/
499
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100500#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
501psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
502 size_t seed_size )
503{
504 psa_status_t status;
505 struct psa_storage_info_t p_info;
506
507 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
508
509 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
510 {
511 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
512 }
513 else if( PSA_SUCCESS == status )
514 {
515 /* You should not be here. Seed needs to be injected only once */
516 status = PSA_ERROR_NOT_PERMITTED;
517 }
518 return( status );
519}
520#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
521
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200522
523
524/****************************************************************/
525/* The end */
526/****************************************************************/
527
Darryl Greendb2b8db2018-06-15 13:06:04 +0100528#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */