blob: 688940b5f41a22195bb7be8f965c08e60bfe894f [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/*
2 * PSA persistent key storage
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Darryl Greendb2b8db2018-06-15 13:06:04 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Darryl Greendb2b8db2018-06-15 13:06:04 +010019 */
20
Joe Subbianid6ea0632021-08-18 12:57:54 +010021#include "common.h"
22
Darryl Greendb2b8db2018-06-15 13:06:04 +010023#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
24
25#include <stdlib.h>
26#include <string.h>
27
28#include "psa/crypto.h"
29#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010030#include "mbedtls/platform_util.h"
31
Gilles Peskine5e80d912019-02-24 17:10:18 +010032#if defined(MBEDTLS_PSA_ITS_FILE_C)
33#include "psa_crypto_its.h"
34#else /* Native ITS implementation */
35#include "psa/error.h"
36#include "psa/internal_trusted_storage.h"
37#endif
38
Darryl Greendb2b8db2018-06-15 13:06:04 +010039#include "mbedtls/platform.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010040
Gilles Peskinec8336cb2019-07-22 19:26:12 +020041/****************************************************************/
42/* Key storage */
43/****************************************************************/
44
Ronald Cron71016a92020-08-28 19:01:50 +020045/* Determine a file name (ITS file identifier) for the given key identifier.
46 * The file name must be distinct from any file that is used for a purpose
47 * other than storing a key. Currently, the only such file is the random seed
48 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
49 * 0xFFFFFF52. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +010051{
Ronald Cronecfb2372020-07-23 17:13:42 +020052#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010053 /* Encode the owner in the upper 32 bits. This means that if
54 * owner values are nonzero (as they are on a PSA platform),
55 * no key file will ever have a value less than 0x100000000, so
56 * the whole range 0..0xffffffff is available for non-key files. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
58 return ((uint64_t) unsigned_owner_id << 32) |
59 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +010060#else
61 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020062 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010063 * is responsible for ensuring that key identifiers do not have a
64 * value that is reserved for non-key files. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 return key;
Gilles Peskine088b77f2019-02-24 17:00:27 +010066#endif
67}
68
Gilles Peskine5e80d912019-02-24 17:10:18 +010069/**
70 * \brief Load persistent data for the given key slot number.
71 *
72 * This function reads data from a storage backend and returns the data in a
73 * buffer.
74 *
75 * \param key Persistent identifier of the key to be loaded. This
76 * should be an occupied storage location.
77 * \param[out] data Buffer where the data is to be written.
78 * \param data_size Size of the \c data buffer in bytes.
79 *
Gilles Peskineec1eff32023-02-14 19:21:09 +010080 * \retval #PSA_SUCCESS \emptydescription
81 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
82 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
83 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
84 * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +010085 */
Ronald Cron71016a92020-08-28 19:01:50 +020086static psa_status_t psa_crypto_storage_load(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
Gilles Peskine088b77f2019-02-24 17:00:27 +010088{
89 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +010091 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010092 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +010093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 status = psa_its_get_info(data_identifier, &data_identifier_info);
95 if (status != PSA_SUCCESS) {
96 return status;
97 }
Gilles Peskine088b77f2019-02-24 17:00:27 +010098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 status = psa_its_get(data_identifier, 0, (uint32_t) data_size, data, &data_length);
100 if (data_size != data_length) {
101 return PSA_ERROR_DATA_INVALID;
102 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100105}
106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107int 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;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100111 struct psa_storage_info_t data_identifier_info;
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 ret = psa_its_get_info(data_identifier, &data_identifier_info);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (ret == PSA_ERROR_DOES_NOT_EXIST) {
116 return 0;
117 }
118 return 1;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100119}
120
Gilles Peskine5e80d912019-02-24 17:10:18 +0100121/**
122 * \brief Store persistent data for the given key slot number.
123 *
124 * This function stores the given data buffer to a persistent storage.
125 *
126 * \param key Persistent identifier of the key to be stored. This
127 * should be an unoccupied storage location.
128 * \param[in] data Buffer containing the data to be stored.
129 * \param data_length The number of bytes
130 * that make up the data.
131 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100132 * \retval #PSA_SUCCESS \emptydescription
133 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
134 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
135 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
136 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +0100137 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
139 const uint8_t *data,
140 size_t data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100141{
142 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100144 struct psa_storage_info_t data_identifier_info;
145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (psa_is_key_present_in_storage(key) == 1) {
147 return PSA_ERROR_ALREADY_EXISTS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100148 }
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 status = psa_its_set(data_identifier, (uint32_t) data_length, data, 0);
151 if (status != PSA_SUCCESS) {
152 return PSA_ERROR_DATA_INVALID;
153 }
154
155 status = psa_its_get_info(data_identifier, &data_identifier_info);
156 if (status != PSA_SUCCESS) {
Gilles Peskine088b77f2019-02-24 17:00:27 +0100157 goto exit;
158 }
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (data_identifier_info.size != data_length) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100161 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100162 goto exit;
163 }
164
165exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 if (status != PSA_SUCCESS) {
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200167 /* Remove the file in case we managed to create it but something
168 * went wrong. It's ok if the file doesn't exist. If the file exists
169 * but the removal fails, we're already reporting an error so there's
170 * nothing else we can do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 (void) psa_its_remove(data_identifier);
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200172 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100174}
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100177{
178 psa_status_t ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100180 struct psa_storage_info_t data_identifier_info;
181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 ret = psa_its_get_info(data_identifier, &data_identifier_info);
183 if (ret == PSA_ERROR_DOES_NOT_EXIST) {
184 return PSA_SUCCESS;
185 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (psa_its_remove(data_identifier) != PSA_SUCCESS) {
188 return PSA_ERROR_DATA_INVALID;
189 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 ret = psa_its_get_info(data_identifier, &data_identifier_info);
192 if (ret != PSA_ERROR_DOES_NOT_EXIST) {
193 return PSA_ERROR_DATA_INVALID;
194 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100197}
198
Gilles Peskine5e80d912019-02-24 17:10:18 +0100199/**
200 * \brief Get data length for given key slot number.
201 *
202 * \param key Persistent identifier whose stored data length
203 * is to be obtained.
204 * \param[out] data_length The number of bytes that make up the data.
205 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100206 * \retval #PSA_SUCCESS \emptydescription
207 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
208 * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
209 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskine5e80d912019-02-24 17:10:18 +0100210 */
211static psa_status_t psa_crypto_storage_get_data_length(
Ronald Cron71016a92020-08-28 19:01:50 +0200212 const mbedtls_svc_key_id_t key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 size_t *data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100214{
215 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100217 struct psa_storage_info_t data_identifier_info;
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 status = psa_its_get_info(data_identifier, &data_identifier_info);
220 if (status != PSA_SUCCESS) {
221 return status;
222 }
Gilles Peskine088b77f2019-02-24 17:00:27 +0100223
224 *data_length = (size_t) data_identifier_info.size;
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100227}
228
Moran Peker96ebf9e2018-06-28 18:02:17 +0300229/**
230 * Persistent key storage magic header.
231 */
232#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
Moran Peker96ebf9e2018-06-28 18:02:17 +0300234
Darryl Greendb2b8db2018-06-15 13:06:04 +0100235typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300236 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100237 uint8_t version[4];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 uint8_t lifetime[sizeof(psa_key_lifetime_t)];
Torstein Nesse162a1102020-10-07 10:50:15 +0200239 uint8_t type[2];
240 uint8_t bits[2];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 uint8_t policy[sizeof(psa_key_policy_t)];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100242 uint8_t data_len[4];
243 uint8_t key_data[];
244} psa_persistent_key_storage_format;
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246void psa_format_key_data_for_storage(const uint8_t *data,
247 const size_t data_length,
248 const psa_core_key_attributes_t *attr,
249 uint8_t *storage_data)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100250{
251 psa_persistent_key_storage_format *storage_format =
252 (psa_persistent_key_storage_format *) storage_data;
253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
255 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
256 MBEDTLS_PUT_UINT32_LE(0, storage_format->version, 0);
257 MBEDTLS_PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
258 MBEDTLS_PUT_UINT16_LE((uint16_t) attr->type, storage_format->type, 0);
259 MBEDTLS_PUT_UINT16_LE((uint16_t) attr->bits, storage_format->bits, 0);
260 MBEDTLS_PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
261 MBEDTLS_PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
262 MBEDTLS_PUT_UINT32_LE(attr->policy.alg2, storage_format->policy, 2 * sizeof(uint32_t));
263 MBEDTLS_PUT_UINT32_LE(data_length, storage_format->data_len, 0);
264 memcpy(storage_format->key_data, data, data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100265}
266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267static psa_status_t check_magic_header(const uint8_t *data)
Moran Peker96ebf9e2018-06-28 18:02:17 +0300268{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
270 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0) {
271 return PSA_ERROR_DATA_INVALID;
272 }
273 return PSA_SUCCESS;
Moran Peker96ebf9e2018-06-28 18:02:17 +0300274}
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
277 size_t storage_data_length,
278 uint8_t **key_data,
279 size_t *key_data_length,
280 psa_core_key_attributes_t *attr)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100281{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300282 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100283 const psa_persistent_key_storage_format *storage_format =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 (const psa_persistent_key_storage_format *) storage_data;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100285 uint32_t version;
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if (storage_data_length < sizeof(*storage_format)) {
288 return PSA_ERROR_DATA_INVALID;
289 }
Moran Peker96ebf9e2018-06-28 18:02:17 +0300290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 status = check_magic_header(storage_data);
292 if (status != PSA_SUCCESS) {
293 return status;
294 }
Moran Peker96ebf9e2018-06-28 18:02:17 +0300295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 version = MBEDTLS_GET_UINT32_LE(storage_format->version, 0);
297 if (version != 0) {
298 return PSA_ERROR_DATA_INVALID;
299 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 *key_data_length = MBEDTLS_GET_UINT32_LE(storage_format->data_len, 0);
302 if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
303 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
304 return PSA_ERROR_DATA_INVALID;
305 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if (*key_data_length == 0) {
Gilles Peskine3495b582019-04-25 13:47:06 +0200308 *key_data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 } else {
310 *key_data = mbedtls_calloc(1, *key_data_length);
311 if (*key_data == NULL) {
312 return PSA_ERROR_INSUFFICIENT_MEMORY;
313 }
314 memcpy(*key_data, storage_format->key_data, *key_data_length);
Gilles Peskine3495b582019-04-25 13:47:06 +0200315 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 attr->lifetime = MBEDTLS_GET_UINT32_LE(storage_format->lifetime, 0);
318 attr->type = MBEDTLS_GET_UINT16_LE(storage_format->type, 0);
319 attr->bits = MBEDTLS_GET_UINT16_LE(storage_format->bits, 0);
320 attr->policy.usage = MBEDTLS_GET_UINT32_LE(storage_format->policy, 0);
321 attr->policy.alg = MBEDTLS_GET_UINT32_LE(storage_format->policy, sizeof(uint32_t));
322 attr->policy.alg2 = MBEDTLS_GET_UINT32_LE(storage_format->policy, 2 * sizeof(uint32_t));
Darryl Greendb2b8db2018-06-15 13:06:04 +0100323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 return PSA_SUCCESS;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100325}
326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327psa_status_t psa_save_persistent_key(const psa_core_key_attributes_t *attr,
328 const uint8_t *data,
329 const size_t data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100330{
331 size_t storage_data_length;
332 uint8_t *storage_data;
333 psa_status_t status;
334
Steven Cooremand80e8a42021-01-26 12:45:39 +0100335 /* All keys saved to persistent storage always have a key context */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (data == NULL || data_length == 0) {
337 return PSA_ERROR_INVALID_ARGUMENT;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100338 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339
340 if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
341 return PSA_ERROR_INSUFFICIENT_STORAGE;
342 }
343 storage_data_length = data_length + sizeof(psa_persistent_key_storage_format);
344
345 storage_data = mbedtls_calloc(1, storage_data_length);
346 if (storage_data == NULL) {
347 return PSA_ERROR_INSUFFICIENT_MEMORY;
348 }
349
350 psa_format_key_data_for_storage(data, data_length, attr, storage_data);
351
352 status = psa_crypto_storage_store(attr->id,
353 storage_data, storage_data_length);
354
355 mbedtls_platform_zeroize(storage_data, storage_data_length);
356 mbedtls_free(storage_data);
357
358 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100359}
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
362{
363 if (key_data != NULL) {
364 mbedtls_platform_zeroize(key_data, key_data_length);
365 }
366 mbedtls_free(key_data);
367}
368
369psa_status_t psa_load_persistent_key(psa_core_key_attributes_t *attr,
370 uint8_t **data,
371 size_t *data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100372{
373 psa_status_t status = PSA_SUCCESS;
374 uint8_t *loaded_data;
375 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200376 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 status = psa_crypto_storage_get_data_length(key, &storage_data_length);
379 if (status != PSA_SUCCESS) {
380 return status;
381 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 loaded_data = mbedtls_calloc(1, storage_data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 if (loaded_data == NULL) {
386 return PSA_ERROR_INSUFFICIENT_MEMORY;
387 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
390 if (status != PSA_SUCCESS) {
Darryl Greendb2b8db2018-06-15 13:06:04 +0100391 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
395 data, data_length, attr);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100396
Steven Cooremand80e8a42021-01-26 12:45:39 +0100397 /* All keys saved to persistent storage always have a key context */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if (status == PSA_SUCCESS &&
399 (*data == NULL || *data_length == 0)) {
Steven Cooremand80e8a42021-01-26 12:45:39 +0100400 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 }
Steven Cooremand80e8a42021-01-26 12:45:39 +0100402
Darryl Greendb2b8db2018-06-15 13:06:04 +0100403exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 mbedtls_platform_zeroize(loaded_data, storage_data_length);
405 mbedtls_free(loaded_data);
406 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100407}
408
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200409
410
411/****************************************************************/
412/* Transactions */
413/****************************************************************/
414
415#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
416
417psa_crypto_transaction_t psa_crypto_transaction;
418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419psa_status_t psa_crypto_save_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200420{
421 struct psa_storage_info_t p_info;
422 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
424 if (status == PSA_SUCCESS) {
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200425 /* This shouldn't happen: we're trying to start a transaction while
426 * there is still a transaction that hasn't been replayed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 return PSA_ERROR_CORRUPTION_DETECTED;
428 } else if (status != PSA_ERROR_DOES_NOT_EXIST) {
429 return status;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200430 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 return psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
432 sizeof(psa_crypto_transaction),
433 &psa_crypto_transaction,
434 0);
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200435}
436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437psa_status_t psa_crypto_load_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200438{
Gilles Peskine8b663892019-07-31 17:57:57 +0200439 psa_status_t status;
440 size_t length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
442 sizeof(psa_crypto_transaction),
443 &psa_crypto_transaction, &length);
444 if (status != PSA_SUCCESS) {
445 return status;
446 }
447 if (length != sizeof(psa_crypto_transaction)) {
448 return PSA_ERROR_DATA_INVALID;
449 }
450 return PSA_SUCCESS;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200451}
452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453psa_status_t psa_crypto_stop_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200454{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200456 /* Whether or not updating the storage succeeded, the transaction is
457 * finished now. It's too late to go back, so zero out the in-memory
458 * data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
460 return status;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200461}
462
463#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
464
465
466
467/****************************************************************/
468/* Random generator state */
469/****************************************************************/
470
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100471#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
473 size_t seed_size)
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100474{
475 psa_status_t status;
476 struct psa_storage_info_t p_info;
477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 if (PSA_ERROR_DOES_NOT_EXIST == status) { /* No seed exists */
481 status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
482 } else if (PSA_SUCCESS == status) {
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100483 /* You should not be here. Seed needs to be injected only once */
484 status = PSA_ERROR_NOT_PERMITTED;
485 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 return status;
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100487}
488#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
489
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200490
491
492/****************************************************************/
493/* The end */
494/****************************************************************/
495
Darryl Greendb2b8db2018-06-15 13:06:04 +0100496#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */