blob: 7d1317b45a11d106272dd1a861cf0a4aea9095b6 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Darryl Greendb2b8db2018-06-15 13:06:04 +01007 */
8
Mateusz Starzyk88fa17d2021-05-19 17:32:14 +02009#include "common.h"
10
Darryl Greendb2b8db2018-06-15 13:06:04 +010011#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
12
13#include <stdlib.h>
14#include <string.h>
15
16#include "psa/crypto.h"
17#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010018#include "mbedtls/platform_util.h"
19
Gilles Peskine5e80d912019-02-24 17:10:18 +010020#if defined(MBEDTLS_PSA_ITS_FILE_C)
21#include "psa_crypto_its.h"
22#else /* Native ITS implementation */
23#include "psa/error.h"
24#include "psa/internal_trusted_storage.h"
25#endif
26
Darryl Greendb2b8db2018-06-15 13:06:04 +010027#include "mbedtls/platform.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010028
Gilles Peskinec8336cb2019-07-22 19:26:12 +020029
30
31/****************************************************************/
32/* Key storage */
33/****************************************************************/
34
Ronald Cron71016a92020-08-28 19:01:50 +020035/* Determine a file name (ITS file identifier) for the given key identifier.
36 * The file name must be distinct from any file that is used for a purpose
37 * other than storing a key. Currently, the only such file is the random seed
38 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
39 * 0xFFFFFF52. */
Gilles Peskine449bd832023-01-11 14:50:10 +010040static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +010041{
Ronald Cronecfb2372020-07-23 17:13:42 +020042#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010043 /* Encode the owner in the upper 32 bits. This means that if
44 * owner values are nonzero (as they are on a PSA platform),
45 * no key file will ever have a value less than 0x100000000, so
46 * the whole range 0..0xffffffff is available for non-key files. */
Gilles Peskine449bd832023-01-11 14:50:10 +010047 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
48 return ((uint64_t) unsigned_owner_id << 32) |
49 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +010050#else
51 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020052 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010053 * is responsible for ensuring that key identifiers do not have a
54 * value that is reserved for non-key files. */
Gilles Peskine449bd832023-01-11 14:50:10 +010055 return key;
Gilles Peskine088b77f2019-02-24 17:00:27 +010056#endif
57}
58
Gilles Peskine5e80d912019-02-24 17:10:18 +010059/**
60 * \brief Load persistent data for the given key slot number.
61 *
62 * This function reads data from a storage backend and returns the data in a
63 * buffer.
64 *
65 * \param key Persistent identifier of the key to be loaded. This
66 * should be an occupied storage location.
67 * \param[out] data Buffer where the data is to be written.
68 * \param data_size Size of the \c data buffer in bytes.
69 *
Gilles Peskineed733552023-02-14 19:21:09 +010070 * \retval #PSA_SUCCESS \emptydescription
71 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
72 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
73 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
74 * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +010075 */
Ronald Cron71016a92020-08-28 19:01:50 +020076static psa_status_t psa_crypto_storage_load(
Gilles Peskine449bd832023-01-11 14:50:10 +010077 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
Gilles Peskine088b77f2019-02-24 17:00:27 +010078{
79 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010080 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +010081 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010082 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 status = psa_its_get_info(data_identifier, &data_identifier_info);
85 if (status != PSA_SUCCESS) {
86 return status;
87 }
Gilles Peskine088b77f2019-02-24 17:00:27 +010088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 status = psa_its_get(data_identifier, 0, (uint32_t) data_size, data, &data_length);
90 if (data_size != data_length) {
91 return PSA_ERROR_DATA_INVALID;
92 }
Gilles Peskine088b77f2019-02-24 17:00:27 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +010095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +010098{
99 psa_status_t ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101 struct psa_storage_info_t data_identifier_info;
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 ret = psa_its_get_info(data_identifier, &data_identifier_info);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (ret == PSA_ERROR_DOES_NOT_EXIST) {
106 return 0;
107 }
108 return 1;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100109}
110
Gilles Peskine5e80d912019-02-24 17:10:18 +0100111/**
112 * \brief Store persistent data for the given key slot number.
113 *
114 * This function stores the given data buffer to a persistent storage.
115 *
116 * \param key Persistent identifier of the key to be stored. This
117 * should be an unoccupied storage location.
118 * \param[in] data Buffer containing the data to be stored.
119 * \param data_length The number of bytes
120 * that make up the data.
121 *
Gilles Peskineed733552023-02-14 19:21:09 +0100122 * \retval #PSA_SUCCESS \emptydescription
123 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
124 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
125 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
126 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +0100127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
129 const uint8_t *data,
130 size_t data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100131{
132 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100134 struct psa_storage_info_t data_identifier_info;
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (psa_is_key_present_in_storage(key) == 1) {
137 return PSA_ERROR_ALREADY_EXISTS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100138 }
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 status = psa_its_set(data_identifier, (uint32_t) data_length, data, 0);
141 if (status != PSA_SUCCESS) {
142 return PSA_ERROR_DATA_INVALID;
143 }
144
145 status = psa_its_get_info(data_identifier, &data_identifier_info);
146 if (status != PSA_SUCCESS) {
Gilles Peskine088b77f2019-02-24 17:00:27 +0100147 goto exit;
148 }
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (data_identifier_info.size != data_length) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100151 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100152 goto exit;
153 }
154
155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (status != PSA_SUCCESS) {
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200157 /* Remove the file in case we managed to create it but something
158 * went wrong. It's ok if the file doesn't exist. If the file exists
159 * but the removal fails, we're already reporting an error so there's
160 * nothing else we can do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 (void) psa_its_remove(data_identifier);
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200162 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100164}
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100167{
168 psa_status_t ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100170 struct psa_storage_info_t data_identifier_info;
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 ret = psa_its_get_info(data_identifier, &data_identifier_info);
173 if (ret == PSA_ERROR_DOES_NOT_EXIST) {
174 return PSA_SUCCESS;
175 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (psa_its_remove(data_identifier) != PSA_SUCCESS) {
178 return PSA_ERROR_DATA_INVALID;
179 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 ret = psa_its_get_info(data_identifier, &data_identifier_info);
182 if (ret != PSA_ERROR_DOES_NOT_EXIST) {
183 return PSA_ERROR_DATA_INVALID;
184 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100187}
188
Gilles Peskine5e80d912019-02-24 17:10:18 +0100189/**
190 * \brief Get data length for given key slot number.
191 *
192 * \param key Persistent identifier whose stored data length
193 * is to be obtained.
194 * \param[out] data_length The number of bytes that make up the data.
195 *
Gilles Peskineed733552023-02-14 19:21:09 +0100196 * \retval #PSA_SUCCESS \emptydescription
197 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
198 * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
199 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +0100200 */
201static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200202 const mbedtls_svc_key_id_t key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 size_t *data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100204{
205 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100207 struct psa_storage_info_t data_identifier_info;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 status = psa_its_get_info(data_identifier, &data_identifier_info);
210 if (status != PSA_SUCCESS) {
211 return status;
212 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100213
214 *data_length = (size_t) data_identifier_info.size;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100217}
218
Moran Peker96ebf9e2018-06-28 18:02:17 +0300219/**
220 * Persistent key storage magic header.
221 */
222#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
Gilles Peskine449bd832023-01-11 14:50:10 +0100223#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
Moran Peker96ebf9e2018-06-28 18:02:17 +0300224
Darryl Greendb2b8db2018-06-15 13:06:04 +0100225typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300226 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100227 uint8_t version[4];
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 uint8_t lifetime[sizeof(psa_key_lifetime_t)];
Torstein Nesse162a1102020-10-07 10:50:15 +0200229 uint8_t type[2];
230 uint8_t bits[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 uint8_t policy[sizeof(psa_key_policy_t)];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100232 uint8_t data_len[4];
233 uint8_t key_data[];
234} psa_persistent_key_storage_format;
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236void psa_format_key_data_for_storage(const uint8_t *data,
237 const size_t data_length,
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100238 const psa_key_attributes_t *attr,
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 uint8_t *storage_data)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100240{
241 psa_persistent_key_storage_format *storage_format =
242 (psa_persistent_key_storage_format *) storage_data;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
245 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
246 MBEDTLS_PUT_UINT32_LE(0, storage_format->version, 0);
247 MBEDTLS_PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
248 MBEDTLS_PUT_UINT16_LE((uint16_t) attr->type, storage_format->type, 0);
249 MBEDTLS_PUT_UINT16_LE((uint16_t) attr->bits, storage_format->bits, 0);
250 MBEDTLS_PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
251 MBEDTLS_PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
252 MBEDTLS_PUT_UINT32_LE(attr->policy.alg2, storage_format->policy, 2 * sizeof(uint32_t));
253 MBEDTLS_PUT_UINT32_LE(data_length, storage_format->data_len, 0);
254 memcpy(storage_format->key_data, data, data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257static psa_status_t check_magic_header(const uint8_t *data)
Moran Peker96ebf9e2018-06-28 18:02:17 +0300258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
260 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0) {
261 return PSA_ERROR_DATA_INVALID;
262 }
263 return PSA_SUCCESS;
Moran Peker96ebf9e2018-06-28 18:02:17 +0300264}
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
267 size_t storage_data_length,
268 uint8_t **key_data,
269 size_t *key_data_length,
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100270 psa_key_attributes_t *attr)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100271{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300272 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100273 const psa_persistent_key_storage_format *storage_format =
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 (const psa_persistent_key_storage_format *) storage_data;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100275 uint32_t version;
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (storage_data_length < sizeof(*storage_format)) {
278 return PSA_ERROR_DATA_INVALID;
279 }
Moran Peker96ebf9e2018-06-28 18:02:17 +0300280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 status = check_magic_header(storage_data);
282 if (status != PSA_SUCCESS) {
283 return status;
284 }
Moran Peker96ebf9e2018-06-28 18:02:17 +0300285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 version = MBEDTLS_GET_UINT32_LE(storage_format->version, 0);
287 if (version != 0) {
288 return PSA_ERROR_DATA_INVALID;
289 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 *key_data_length = MBEDTLS_GET_UINT32_LE(storage_format->data_len, 0);
292 if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
293 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
294 return PSA_ERROR_DATA_INVALID;
295 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (*key_data_length == 0) {
Gilles Peskine3495b582019-04-25 13:47:06 +0200298 *key_data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 } else {
300 *key_data = mbedtls_calloc(1, *key_data_length);
301 if (*key_data == NULL) {
302 return PSA_ERROR_INSUFFICIENT_MEMORY;
303 }
304 memcpy(*key_data, storage_format->key_data, *key_data_length);
Gilles Peskine3495b582019-04-25 13:47:06 +0200305 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 attr->lifetime = MBEDTLS_GET_UINT32_LE(storage_format->lifetime, 0);
308 attr->type = MBEDTLS_GET_UINT16_LE(storage_format->type, 0);
309 attr->bits = MBEDTLS_GET_UINT16_LE(storage_format->bits, 0);
310 attr->policy.usage = MBEDTLS_GET_UINT32_LE(storage_format->policy, 0);
311 attr->policy.alg = MBEDTLS_GET_UINT32_LE(storage_format->policy, sizeof(uint32_t));
312 attr->policy.alg2 = MBEDTLS_GET_UINT32_LE(storage_format->policy, 2 * sizeof(uint32_t));
Darryl Greendb2b8db2018-06-15 13:06:04 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_SUCCESS;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100315}
316
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100317psa_status_t psa_save_persistent_key(const psa_key_attributes_t *attr,
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 const uint8_t *data,
319 const size_t data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100320{
321 size_t storage_data_length;
322 uint8_t *storage_data;
323 psa_status_t status;
324
Steven Cooremand80e8a42021-01-26 12:45:39 +0100325 /* All keys saved to persistent storage always have a key context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (data == NULL || data_length == 0) {
327 return PSA_ERROR_INVALID_ARGUMENT;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100328 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100329
330 if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
331 return PSA_ERROR_INSUFFICIENT_STORAGE;
332 }
333 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 }
339
340 psa_format_key_data_for_storage(data, data_length, attr, storage_data);
341
342 status = psa_crypto_storage_store(attr->id,
343 storage_data, storage_data_length);
344
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100345 mbedtls_zeroize_and_free(storage_data, storage_data_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100346
347 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100348}
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
351{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100352 mbedtls_zeroize_and_free(key_data, key_data_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100353}
354
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100355psa_status_t psa_load_persistent_key(psa_key_attributes_t *attr,
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 uint8_t **data,
357 size_t *data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100358{
359 psa_status_t status = PSA_SUCCESS;
360 uint8_t *loaded_data;
361 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200362 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 status = psa_crypto_storage_get_data_length(key, &storage_data_length);
365 if (status != PSA_SUCCESS) {
366 return status;
367 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 loaded_data = mbedtls_calloc(1, storage_data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (loaded_data == NULL) {
372 return PSA_ERROR_INSUFFICIENT_MEMORY;
373 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
376 if (status != PSA_SUCCESS) {
Darryl Greendb2b8db2018-06-15 13:06:04 +0100377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
381 data, data_length, attr);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100382
Steven Cooremand80e8a42021-01-26 12:45:39 +0100383 /* All keys saved to persistent storage always have a key context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (status == PSA_SUCCESS &&
385 (*data == NULL || *data_length == 0)) {
Steven Cooremand80e8a42021-01-26 12:45:39 +0100386 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Steven Cooremand80e8a42021-01-26 12:45:39 +0100388
Darryl Greendb2b8db2018-06-15 13:06:04 +0100389exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100390 mbedtls_zeroize_and_free(loaded_data, storage_data_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100392}
393
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200394
395
396/****************************************************************/
397/* Transactions */
398/****************************************************************/
399
400#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
401
402psa_crypto_transaction_t psa_crypto_transaction;
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404psa_status_t psa_crypto_save_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200405{
406 struct psa_storage_info_t p_info;
407 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
409 if (status == PSA_SUCCESS) {
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200410 /* This shouldn't happen: we're trying to start a transaction while
411 * there is still a transaction that hasn't been replayed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return PSA_ERROR_CORRUPTION_DETECTED;
413 } else if (status != PSA_ERROR_DOES_NOT_EXIST) {
414 return status;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200415 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
417 sizeof(psa_crypto_transaction),
418 &psa_crypto_transaction,
419 0);
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200420}
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422psa_status_t psa_crypto_load_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200423{
Gilles Peskine8b663892019-07-31 17:57:57 +0200424 psa_status_t status;
425 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
427 sizeof(psa_crypto_transaction),
428 &psa_crypto_transaction, &length);
429 if (status != PSA_SUCCESS) {
430 return status;
431 }
432 if (length != sizeof(psa_crypto_transaction)) {
433 return PSA_ERROR_DATA_INVALID;
434 }
435 return PSA_SUCCESS;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200436}
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438psa_status_t psa_crypto_stop_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200439{
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200441 /* 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
445 return status;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200446}
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)
Gilles Peskine449bd832023-01-11 14:50:10 +0100457psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
458 size_t seed_size)
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100459{
460 psa_status_t status;
461 struct psa_storage_info_t p_info;
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (PSA_ERROR_DOES_NOT_EXIST == status) { /* No seed exists */
466 status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
467 } else if (PSA_SUCCESS == status) {
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100468 /* You should not be here. Seed needs to be injected only once */
469 status = PSA_ERROR_NOT_PERMITTED;
470 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return status;
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100472}
473#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
474
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200475
476
477/****************************************************************/
478/* The end */
479/****************************************************************/
480
Darryl Greendb2b8db2018-06-15 13:06:04 +0100481#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */