blob: ae274384d34d2a3cc8dd74b191694932f51a0d49 [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/*
2 * PSA persistent key storage
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Darryl Greendb2b8db2018-06-15 13:06:04 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Darryl Greendb2b8db2018-06-15 13:06:04 +010019 */
20
Mateusz Starzyk88fa17d2021-05-19 17:32:14 +020021#include "common.h"
22
Darryl Greendb2b8db2018-06-15 13:06:04 +010023#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
24
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdlib.h>
26# include <string.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010027
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020028# include "psa/crypto.h"
29# include "psa_crypto_storage.h"
30# include "mbedtls/platform_util.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# 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
Gilles Peskine5e80d912019-02-24 17:10:18 +010038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# if defined(MBEDTLS_PLATFORM_C)
40# include "mbedtls/platform.h"
41# else
42# include <stdlib.h>
43# define mbedtls_calloc calloc
44# define mbedtls_free free
45# endif
Gilles Peskinec8336cb2019-07-22 19:26:12 +020046
47/****************************************************************/
48/* Key storage */
49/****************************************************************/
50
Ronald Cron71016a92020-08-28 19:01:50 +020051/* Determine a file name (ITS file identifier) for the given key identifier.
52 * The file name must be distinct from any file that is used for a purpose
53 * other than storing a key. Currently, the only such file is the random seed
54 * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
55 * 0xFFFFFF52. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +010057{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058# if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine088b77f2019-02-24 17:00:27 +010059 /* Encode the owner in the upper 32 bits. This means that if
60 * owner values are nonzero (as they are on a PSA platform),
61 * no key file will ever have a value less than 0x100000000, so
62 * the whole range 0..0xffffffff is available for non-key files. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063 uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
64 return (((uint64_t)unsigned_owner_id << 32) |
65 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key));
66# else
Gilles Peskine088b77f2019-02-24 17:00:27 +010067 /* Use the key id directly as a file name.
Ronald Cron71016a92020-08-28 19:01:50 +020068 * psa_is_key_id_valid() in psa_crypto_slot_management.c
Gilles Peskine088b77f2019-02-24 17:00:27 +010069 * is responsible for ensuring that key identifiers do not have a
70 * value that is reserved for non-key files. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 return key;
72# endif
Gilles Peskine088b77f2019-02-24 17:00:27 +010073}
74
Gilles Peskine5e80d912019-02-24 17:10:18 +010075/**
76 * \brief Load persistent data for the given key slot number.
77 *
78 * This function reads data from a storage backend and returns the data in a
79 * buffer.
80 *
81 * \param key Persistent identifier of the key to be loaded. This
82 * should be an occupied storage location.
83 * \param[out] data Buffer where the data is to be written.
84 * \param data_size Size of the \c data buffer in bytes.
85 *
Ronald Cron96783552020-10-19 12:06:30 +020086 * \retval #PSA_SUCCESS
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010087 * \retval #PSA_ERROR_DATA_INVALID
88 * \retval #PSA_ERROR_DATA_CORRUPT
Ronald Cron96783552020-10-19 12:06:30 +020089 * \retval #PSA_ERROR_STORAGE_FAILURE
90 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine5e80d912019-02-24 17:10:18 +010091 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092static psa_status_t psa_crypto_storage_load(const mbedtls_svc_key_id_t key,
93 uint8_t *data,
94 size_t data_size)
Gilles Peskine088b77f2019-02-24 17:00:27 +010095{
96 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +010098 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +010099 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100100
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101 status = psa_its_get_info(data_identifier, &data_identifier_info);
102 if (status != PSA_SUCCESS)
103 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 status = psa_its_get(data_identifier, 0, (uint32_t)data_size, data,
106 &data_length);
107 if (data_size != data_length)
108 return PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100109
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100111}
112
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100114{
115 psa_status_t ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100117 struct psa_storage_info_t data_identifier_info;
118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119 ret = psa_its_get_info(data_identifier, &data_identifier_info);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 if (ret == PSA_ERROR_DOES_NOT_EXIST)
122 return 0;
123 return 1;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100124}
125
Gilles Peskine5e80d912019-02-24 17:10:18 +0100126/**
127 * \brief Store persistent data for the given key slot number.
128 *
129 * This function stores the given data buffer to a persistent storage.
130 *
131 * \param key Persistent identifier of the key to be stored. This
132 * should be an unoccupied storage location.
133 * \param[in] data Buffer containing the data to be stored.
134 * \param data_length The number of bytes
135 * that make up the data.
136 *
Ronald Cron96783552020-10-19 12:06:30 +0200137 * \retval #PSA_SUCCESS
138 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Ronald Cron96783552020-10-19 12:06:30 +0200139 * \retval #PSA_ERROR_ALREADY_EXISTS
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100140 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100141 * \retval #PSA_ERROR_DATA_INVALID
Gilles Peskine5e80d912019-02-24 17:10:18 +0100142 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
144 const uint8_t *data,
145 size_t data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100146{
147 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100149 struct psa_storage_info_t data_identifier_info;
150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 if (psa_is_key_present_in_storage(key) == 1)
152 return PSA_ERROR_ALREADY_EXISTS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 status = psa_its_set(data_identifier, (uint32_t)data_length, data, 0);
155 if (status != PSA_SUCCESS) {
156 return PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100157 }
158
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 status = psa_its_get_info(data_identifier, &data_identifier_info);
160 if (status != PSA_SUCCESS) {
Gilles Peskine088b77f2019-02-24 17:00:27 +0100161 goto exit;
162 }
163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 if (data_identifier_info.size != data_length) {
gabor-mezei-armfe309242020-11-09 17:39:56 +0100165 status = PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100166 goto exit;
167 }
168
169exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 if (status != PSA_SUCCESS) {
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200171 /* Remove the file in case we managed to create it but something
172 * went wrong. It's ok if the file doesn't exist. If the file exists
173 * but the removal fails, we're already reporting an error so there's
174 * nothing else we can do. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 (void)psa_its_remove(data_identifier);
Gilles Peskine169ca7f2020-08-25 22:50:06 +0200176 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100178}
179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100181{
182 psa_status_t ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200183 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100184 struct psa_storage_info_t data_identifier_info;
185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 ret = psa_its_get_info(data_identifier, &data_identifier_info);
187 if (ret == PSA_ERROR_DOES_NOT_EXIST)
188 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190 if (psa_its_remove(data_identifier) != PSA_SUCCESS)
191 return PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 ret = psa_its_get_info(data_identifier, &data_identifier_info);
194 if (ret != PSA_ERROR_DOES_NOT_EXIST)
195 return PSA_ERROR_DATA_INVALID;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100196
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100198}
199
Gilles Peskine5e80d912019-02-24 17:10:18 +0100200/**
201 * \brief Get data length for given key slot number.
202 *
203 * \param key Persistent identifier whose stored data length
204 * is to be obtained.
205 * \param[out] data_length The number of bytes that make up the data.
206 *
Ronald Cron96783552020-10-19 12:06:30 +0200207 * \retval #PSA_SUCCESS
208 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100209 * \retval #PSA_ERROR_DOES_NOT_EXIST
210 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine5e80d912019-02-24 17:10:18 +0100211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212static psa_status_t
213psa_crypto_storage_get_data_length(const mbedtls_svc_key_id_t key,
214 size_t *data_length)
Gilles Peskine088b77f2019-02-24 17:00:27 +0100215{
216 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
Gilles Peskine088b77f2019-02-24 17:00:27 +0100218 struct psa_storage_info_t data_identifier_info;
219
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200220 status = psa_its_get_info(data_identifier, &data_identifier_info);
221 if (status != PSA_SUCCESS)
222 return status;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100223
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224 *data_length = (size_t)data_identifier_info.size;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100225
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226 return PSA_SUCCESS;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100227}
228
Darryl Greendb2b8db2018-06-15 13:06:04 +0100229/*
230 * 32-bit integer manipulation macros (little endian)
231 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200232# ifndef GET_UINT32_LE
233# define GET_UINT32_LE(n, b, i) \
234 { \
235 (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
236 ((uint32_t)(b)[(i) + 2] << 16) | \
237 ((uint32_t)(b)[(i) + 3] << 24); \
238 }
239# endif
Darryl Greendb2b8db2018-06-15 13:06:04 +0100240
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241# ifndef PUT_UINT32_LE
242# define PUT_UINT32_LE(n, b, i) \
243 { \
244 (b)[(i)] = (unsigned char)(((n)) & 0xFF); \
245 (b)[(i) + 1] = (unsigned char)(((n) >> 8) & 0xFF); \
246 (b)[(i) + 2] = (unsigned char)(((n) >> 16) & 0xFF); \
247 (b)[(i) + 3] = (unsigned char)(((n) >> 24) & 0xFF); \
248 }
249# endif
Darryl Greendb2b8db2018-06-15 13:06:04 +0100250
Torstein Nesse162a1102020-10-07 10:50:15 +0200251/*
252 * 16-bit integer manipulation macros (little endian)
253 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200254# ifndef GET_UINT16_LE
255# define GET_UINT16_LE(n, b, i) \
256 { \
257 (n) = ((uint16_t)(b)[(i)]) | ((uint16_t)(b)[(i) + 1] << 8); \
258 }
259# endif
Torstein Nesse162a1102020-10-07 10:50:15 +0200260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261# ifndef PUT_UINT16_LE
262# define PUT_UINT16_LE(n, b, i) \
263 { \
264 (b)[(i)] = (unsigned char)(((n)) & 0xFF); \
265 (b)[(i) + 1] = (unsigned char)(((n) >> 8) & 0xFF); \
266 }
267# endif
Torstein Nesse162a1102020-10-07 10:50:15 +0200268
Moran Peker96ebf9e2018-06-28 18:02:17 +0300269/**
270 * Persistent key storage magic header.
271 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200272# define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
273# define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH \
274 (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
Moran Peker96ebf9e2018-06-28 18:02:17 +0300275
Darryl Greendb2b8db2018-06-15 13:06:04 +0100276typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300277 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100278 uint8_t version[4];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 uint8_t lifetime[sizeof(psa_key_lifetime_t)];
Torstein Nesse162a1102020-10-07 10:50:15 +0200280 uint8_t type[2];
281 uint8_t bits[2];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 uint8_t policy[sizeof(psa_key_policy_t)];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100283 uint8_t data_len[4];
284 uint8_t key_data[];
285} psa_persistent_key_storage_format;
286
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200287void psa_format_key_data_for_storage(const uint8_t *data,
288 const size_t data_length,
289 const psa_core_key_attributes_t *attr,
290 uint8_t *storage_data)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100291{
292 psa_persistent_key_storage_format *storage_format =
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 (psa_persistent_key_storage_format *)storage_data;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100294
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200295 memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
296 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
297 PUT_UINT32_LE(0, storage_format->version, 0);
298 PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
299 PUT_UINT16_LE((uint16_t)attr->type, storage_format->type, 0);
300 PUT_UINT16_LE((uint16_t)attr->bits, storage_format->bits, 0);
301 PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
302 PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
303 PUT_UINT32_LE(attr->policy.alg2, storage_format->policy,
304 2 * sizeof(uint32_t));
305 PUT_UINT32_LE(data_length, storage_format->data_len, 0);
306 memcpy(storage_format->key_data, data, data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100307}
308
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200309static psa_status_t check_magic_header(const uint8_t *data)
Moran Peker96ebf9e2018-06-28 18:02:17 +0300310{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
312 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0)
313 return PSA_ERROR_DATA_INVALID;
314 return PSA_SUCCESS;
Moran Peker96ebf9e2018-06-28 18:02:17 +0300315}
316
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200317psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
318 size_t storage_data_length,
319 uint8_t **key_data,
320 size_t *key_data_length,
321 psa_core_key_attributes_t *attr)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100322{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300323 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100324 const psa_persistent_key_storage_format *storage_format =
325 (const psa_persistent_key_storage_format *)storage_data;
326 uint32_t version;
327
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200328 if (storage_data_length < sizeof(*storage_format))
329 return PSA_ERROR_DATA_INVALID;
Moran Peker96ebf9e2018-06-28 18:02:17 +0300330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 status = check_magic_header(storage_data);
332 if (status != PSA_SUCCESS)
333 return status;
Moran Peker96ebf9e2018-06-28 18:02:17 +0300334
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200335 GET_UINT32_LE(version, storage_format->version, 0);
336 if (version != 0)
337 return PSA_ERROR_DATA_INVALID;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100338
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 GET_UINT32_LE(*key_data_length, storage_format->data_len, 0);
340 if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
341 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE)
342 return PSA_ERROR_DATA_INVALID;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100343
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 if (*key_data_length == 0) {
Gilles Peskine3495b582019-04-25 13:47:06 +0200345 *key_data = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200346 } else {
347 *key_data = mbedtls_calloc(1, *key_data_length);
348 if (*key_data == NULL)
349 return PSA_ERROR_INSUFFICIENT_MEMORY;
350 memcpy(*key_data, storage_format->key_data, *key_data_length);
Gilles Peskine3495b582019-04-25 13:47:06 +0200351 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100352
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200353 GET_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
354 GET_UINT16_LE(attr->type, storage_format->type, 0);
355 GET_UINT16_LE(attr->bits, storage_format->bits, 0);
356 GET_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
357 GET_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
358 GET_UINT32_LE(attr->policy.alg2, storage_format->policy,
359 2 * sizeof(uint32_t));
Darryl Greendb2b8db2018-06-15 13:06:04 +0100360
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361 return PSA_SUCCESS;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100362}
363
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200364psa_status_t psa_save_persistent_key(const psa_core_key_attributes_t *attr,
365 const uint8_t *data,
366 const size_t data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100367{
368 size_t storage_data_length;
369 uint8_t *storage_data;
370 psa_status_t status;
371
Steven Cooremand80e8a42021-01-26 12:45:39 +0100372 /* All keys saved to persistent storage always have a key context */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200373 if (data == NULL || data_length == 0)
374 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremand80e8a42021-01-26 12:45:39 +0100375
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200376 if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE)
377 return PSA_ERROR_INSUFFICIENT_STORAGE;
378 storage_data_length =
379 data_length + sizeof(psa_persistent_key_storage_format);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100380
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200381 storage_data = mbedtls_calloc(1, storage_data_length);
382 if (storage_data == NULL)
383 return PSA_ERROR_INSUFFICIENT_MEMORY;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200385 psa_format_key_data_for_storage(data, data_length, attr, storage_data);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100386
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 status =
388 psa_crypto_storage_store(attr->id, storage_data, storage_data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100389
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390 mbedtls_free(storage_data);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100391
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100393}
394
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200395void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100396{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200397 if (key_data != NULL) {
398 mbedtls_platform_zeroize(key_data, key_data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100399 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 mbedtls_free(key_data);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100401}
402
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200403psa_status_t psa_load_persistent_key(psa_core_key_attributes_t *attr,
404 uint8_t **data,
405 size_t *data_length)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100406{
407 psa_status_t status = PSA_SUCCESS;
408 uint8_t *loaded_data;
409 size_t storage_data_length = 0;
Ronald Cron71016a92020-08-28 19:01:50 +0200410 mbedtls_svc_key_id_t key = attr->id;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100411
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412 status = psa_crypto_storage_get_data_length(key, &storage_data_length);
413 if (status != PSA_SUCCESS)
414 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200416 loaded_data = mbedtls_calloc(1, storage_data_length);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100417
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200418 if (loaded_data == NULL)
419 return PSA_ERROR_INSUFFICIENT_MEMORY;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421 status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
422 if (status != PSA_SUCCESS)
Darryl Greendb2b8db2018-06-15 13:06:04 +0100423 goto exit;
424
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200425 status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
426 data, data_length, attr);
Darryl Greendb2b8db2018-06-15 13:06:04 +0100427
Steven Cooremand80e8a42021-01-26 12:45:39 +0100428 /* All keys saved to persistent storage always have a key context */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200429 if (status == PSA_SUCCESS && (*data == NULL || *data_length == 0))
Steven Cooremand80e8a42021-01-26 12:45:39 +0100430 status = PSA_ERROR_STORAGE_FAILURE;
431
Darryl Greendb2b8db2018-06-15 13:06:04 +0100432exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200433 mbedtls_free(loaded_data);
434 return status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100435}
436
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200437/****************************************************************/
438/* Transactions */
439/****************************************************************/
440
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200441# if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200442
443psa_crypto_transaction_t psa_crypto_transaction;
444
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445psa_status_t psa_crypto_save_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200446{
447 struct psa_storage_info_t p_info;
448 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200449 status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
450 if (status == PSA_SUCCESS) {
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200451 /* This shouldn't happen: we're trying to start a transaction while
452 * there is still a transaction that hasn't been replayed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200453 return PSA_ERROR_CORRUPTION_DETECTED;
454 } else if (status != PSA_ERROR_DOES_NOT_EXIST)
455 return status;
456 return (psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
457 sizeof(psa_crypto_transaction), &psa_crypto_transaction,
458 0));
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200459}
460
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461psa_status_t psa_crypto_load_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200462{
Gilles Peskine8b663892019-07-31 17:57:57 +0200463 psa_status_t status;
464 size_t length;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200465 status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
466 sizeof(psa_crypto_transaction),
467 &psa_crypto_transaction, &length);
468 if (status != PSA_SUCCESS)
469 return status;
470 if (length != sizeof(psa_crypto_transaction))
471 return PSA_ERROR_DATA_INVALID;
472 return PSA_SUCCESS;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200473}
474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475psa_status_t psa_crypto_stop_transaction(void)
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200476{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200477 psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200478 /* Whether or not updating the storage succeeded, the transaction is
479 * finished now. It's too late to go back, so zero out the in-memory
480 * data. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200481 memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
482 return status;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200483}
484
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200485# endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200486
487/****************************************************************/
488/* Random generator state */
489/****************************************************************/
490
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200491# if defined(MBEDTLS_PSA_INJECT_ENTROPY)
492psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
493 size_t seed_size)
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100494{
495 psa_status_t status;
496 struct psa_storage_info_t p_info;
497
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200498 status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100499
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200500 if (PSA_ERROR_DOES_NOT_EXIST == status) /* No seed exists */
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100501 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 status =
503 psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
504 } else if (PSA_SUCCESS == status) {
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100505 /* You should not be here. Seed needs to be injected only once */
506 status = PSA_ERROR_NOT_PERMITTED;
507 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508 return status;
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100509}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200510# endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200511
512/****************************************************************/
513/* The end */
514/****************************************************************/
515
Darryl Greendb2b8db2018-06-15 13:06:04 +0100516#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */