xref: /optee_os/lib/libmbedtls/mbedtls/library/psa_crypto_storage.c (revision b0563631928755fe864b97785160fb3088e9efdc)
1*b0563631STom Van Eyck /*
2*b0563631STom Van Eyck  *  PSA persistent key storage
3*b0563631STom Van Eyck  */
4*b0563631STom Van Eyck /*
5*b0563631STom Van Eyck  *  Copyright The Mbed TLS Contributors
6*b0563631STom Van Eyck  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7*b0563631STom Van Eyck  */
8*b0563631STom Van Eyck 
9*b0563631STom Van Eyck #include "common.h"
10*b0563631STom Van Eyck 
11*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
12*b0563631STom Van Eyck 
13*b0563631STom Van Eyck #include <stdlib.h>
14*b0563631STom Van Eyck #include <string.h>
15*b0563631STom Van Eyck 
16*b0563631STom Van Eyck #include "psa/crypto.h"
17*b0563631STom Van Eyck #include "psa_crypto_storage.h"
18*b0563631STom Van Eyck #include "mbedtls/platform_util.h"
19*b0563631STom Van Eyck 
20*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_ITS_FILE_C)
21*b0563631STom Van Eyck #include "psa_crypto_its.h"
22*b0563631STom Van Eyck #else /* Native ITS implementation */
23*b0563631STom Van Eyck #include "psa/error.h"
24*b0563631STom Van Eyck #include "psa/internal_trusted_storage.h"
25*b0563631STom Van Eyck #endif
26*b0563631STom Van Eyck 
27*b0563631STom Van Eyck #include "mbedtls/platform.h"
28*b0563631STom Van Eyck 
29*b0563631STom Van Eyck 
30*b0563631STom Van Eyck 
31*b0563631STom Van Eyck /****************************************************************/
32*b0563631STom Van Eyck /* Key storage */
33*b0563631STom Van Eyck /****************************************************************/
34*b0563631STom Van Eyck 
35*b0563631STom Van Eyck /* Determine a file name (ITS file identifier) for the given key identifier.
36*b0563631STom Van Eyck  * The file name must be distinct from any file that is used for a purpose
37*b0563631STom Van Eyck  * other than storing a key. Currently, the only such file is the random seed
38*b0563631STom Van Eyck  * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
39*b0563631STom Van Eyck  * 0xFFFFFF52. */
psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)40*b0563631STom Van Eyck static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
41*b0563631STom Van Eyck {
42*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
43*b0563631STom Van Eyck     /* Encode the owner in the upper 32 bits. This means that if
44*b0563631STom Van Eyck      * owner values are nonzero (as they are on a PSA platform),
45*b0563631STom Van Eyck      * no key file will ever have a value less than 0x100000000, so
46*b0563631STom Van Eyck      * the whole range 0..0xffffffff is available for non-key files. */
47*b0563631STom Van Eyck     uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
48*b0563631STom Van Eyck     return ((uint64_t) unsigned_owner_id << 32) |
49*b0563631STom Van Eyck            MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
50*b0563631STom Van Eyck #else
51*b0563631STom Van Eyck     /* Use the key id directly as a file name.
52*b0563631STom Van Eyck      * psa_is_key_id_valid() in psa_crypto_slot_management.c
53*b0563631STom Van Eyck      * is responsible for ensuring that key identifiers do not have a
54*b0563631STom Van Eyck      * value that is reserved for non-key files. */
55*b0563631STom Van Eyck     return key;
56*b0563631STom Van Eyck #endif
57*b0563631STom Van Eyck }
58*b0563631STom Van Eyck 
59*b0563631STom Van Eyck /**
60*b0563631STom Van Eyck  * \brief Load persistent data for the given key slot number.
61*b0563631STom Van Eyck  *
62*b0563631STom Van Eyck  * This function reads data from a storage backend and returns the data in a
63*b0563631STom Van Eyck  * buffer.
64*b0563631STom Van Eyck  *
65*b0563631STom Van Eyck  * \param key               Persistent identifier of the key to be loaded. This
66*b0563631STom Van Eyck  *                          should be an occupied storage location.
67*b0563631STom Van Eyck  * \param[out] data         Buffer where the data is to be written.
68*b0563631STom Van Eyck  * \param data_size         Size of the \c data buffer in bytes.
69*b0563631STom Van Eyck  *
70*b0563631STom Van Eyck  * \retval #PSA_SUCCESS \emptydescription
71*b0563631STom Van Eyck  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
72*b0563631STom Van Eyck  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
73*b0563631STom Van Eyck  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
74*b0563631STom Van Eyck  * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
75*b0563631STom Van Eyck  */
psa_crypto_storage_load(const mbedtls_svc_key_id_t key,uint8_t * data,size_t data_size)76*b0563631STom Van Eyck static psa_status_t psa_crypto_storage_load(
77*b0563631STom Van Eyck     const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
78*b0563631STom Van Eyck {
79*b0563631STom Van Eyck     psa_status_t status;
80*b0563631STom Van Eyck     psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
81*b0563631STom Van Eyck     struct psa_storage_info_t data_identifier_info;
82*b0563631STom Van Eyck     size_t data_length = 0;
83*b0563631STom Van Eyck 
84*b0563631STom Van Eyck     status = psa_its_get_info(data_identifier, &data_identifier_info);
85*b0563631STom Van Eyck     if (status  != PSA_SUCCESS) {
86*b0563631STom Van Eyck         return status;
87*b0563631STom Van Eyck     }
88*b0563631STom Van Eyck 
89*b0563631STom Van Eyck     status = psa_its_get(data_identifier, 0, (uint32_t) data_size, data, &data_length);
90*b0563631STom Van Eyck     if (data_size  != data_length) {
91*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
92*b0563631STom Van Eyck     }
93*b0563631STom Van Eyck 
94*b0563631STom Van Eyck     return status;
95*b0563631STom Van Eyck }
96*b0563631STom Van Eyck 
psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)97*b0563631STom Van Eyck int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
98*b0563631STom Van Eyck {
99*b0563631STom Van Eyck     psa_status_t ret;
100*b0563631STom Van Eyck     psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
101*b0563631STom Van Eyck     struct psa_storage_info_t data_identifier_info;
102*b0563631STom Van Eyck 
103*b0563631STom Van Eyck     ret = psa_its_get_info(data_identifier, &data_identifier_info);
104*b0563631STom Van Eyck 
105*b0563631STom Van Eyck     if (ret == PSA_ERROR_DOES_NOT_EXIST) {
106*b0563631STom Van Eyck         return 0;
107*b0563631STom Van Eyck     }
108*b0563631STom Van Eyck     return 1;
109*b0563631STom Van Eyck }
110*b0563631STom Van Eyck 
111*b0563631STom Van Eyck /**
112*b0563631STom Van Eyck  * \brief Store persistent data for the given key slot number.
113*b0563631STom Van Eyck  *
114*b0563631STom Van Eyck  * This function stores the given data buffer to a persistent storage.
115*b0563631STom Van Eyck  *
116*b0563631STom Van Eyck  * \param key           Persistent identifier of the key to be stored. This
117*b0563631STom Van Eyck  *                      should be an unoccupied storage location.
118*b0563631STom Van Eyck  * \param[in] data      Buffer containing the data to be stored.
119*b0563631STom Van Eyck  * \param data_length   The number of bytes
120*b0563631STom Van Eyck  *                      that make up the data.
121*b0563631STom Van Eyck  *
122*b0563631STom Van Eyck  * \retval #PSA_SUCCESS \emptydescription
123*b0563631STom Van Eyck  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
124*b0563631STom Van Eyck  * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
125*b0563631STom Van Eyck  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
126*b0563631STom Van Eyck  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
127*b0563631STom Van Eyck  */
psa_crypto_storage_store(const mbedtls_svc_key_id_t key,const uint8_t * data,size_t data_length)128*b0563631STom Van Eyck static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
129*b0563631STom Van Eyck                                              const uint8_t *data,
130*b0563631STom Van Eyck                                              size_t data_length)
131*b0563631STom Van Eyck {
132*b0563631STom Van Eyck     psa_status_t status;
133*b0563631STom Van Eyck     psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
134*b0563631STom Van Eyck     struct psa_storage_info_t data_identifier_info;
135*b0563631STom Van Eyck 
136*b0563631STom Van Eyck     if (psa_is_key_present_in_storage(key) == 1) {
137*b0563631STom Van Eyck         return PSA_ERROR_ALREADY_EXISTS;
138*b0563631STom Van Eyck     }
139*b0563631STom Van Eyck 
140*b0563631STom Van Eyck     status = psa_its_set(data_identifier, (uint32_t) data_length, data, 0);
141*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
142*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
143*b0563631STom Van Eyck     }
144*b0563631STom Van Eyck 
145*b0563631STom Van Eyck     status = psa_its_get_info(data_identifier, &data_identifier_info);
146*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
147*b0563631STom Van Eyck         goto exit;
148*b0563631STom Van Eyck     }
149*b0563631STom Van Eyck 
150*b0563631STom Van Eyck     if (data_identifier_info.size != data_length) {
151*b0563631STom Van Eyck         status = PSA_ERROR_DATA_INVALID;
152*b0563631STom Van Eyck         goto exit;
153*b0563631STom Van Eyck     }
154*b0563631STom Van Eyck 
155*b0563631STom Van Eyck exit:
156*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
157*b0563631STom Van Eyck         /* Remove the file in case we managed to create it but something
158*b0563631STom Van Eyck          * went wrong. It's ok if the file doesn't exist. If the file exists
159*b0563631STom Van Eyck          * but the removal fails, we're already reporting an error so there's
160*b0563631STom Van Eyck          * nothing else we can do. */
161*b0563631STom Van Eyck         (void) psa_its_remove(data_identifier);
162*b0563631STom Van Eyck     }
163*b0563631STom Van Eyck     return status;
164*b0563631STom Van Eyck }
165*b0563631STom Van Eyck 
psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)166*b0563631STom Van Eyck psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
167*b0563631STom Van Eyck {
168*b0563631STom Van Eyck     psa_status_t ret;
169*b0563631STom Van Eyck     psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
170*b0563631STom Van Eyck     struct psa_storage_info_t data_identifier_info;
171*b0563631STom Van Eyck 
172*b0563631STom Van Eyck     ret = psa_its_get_info(data_identifier, &data_identifier_info);
173*b0563631STom Van Eyck     if (ret == PSA_ERROR_DOES_NOT_EXIST) {
174*b0563631STom Van Eyck         return PSA_SUCCESS;
175*b0563631STom Van Eyck     }
176*b0563631STom Van Eyck 
177*b0563631STom Van Eyck     if (psa_its_remove(data_identifier) != PSA_SUCCESS) {
178*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
179*b0563631STom Van Eyck     }
180*b0563631STom Van Eyck 
181*b0563631STom Van Eyck     ret = psa_its_get_info(data_identifier, &data_identifier_info);
182*b0563631STom Van Eyck     if (ret != PSA_ERROR_DOES_NOT_EXIST) {
183*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
184*b0563631STom Van Eyck     }
185*b0563631STom Van Eyck 
186*b0563631STom Van Eyck     return PSA_SUCCESS;
187*b0563631STom Van Eyck }
188*b0563631STom Van Eyck 
189*b0563631STom Van Eyck /**
190*b0563631STom Van Eyck  * \brief Get data length for given key slot number.
191*b0563631STom Van Eyck  *
192*b0563631STom Van Eyck  * \param key               Persistent identifier whose stored data length
193*b0563631STom Van Eyck  *                          is to be obtained.
194*b0563631STom Van Eyck  * \param[out] data_length  The number of bytes that make up the data.
195*b0563631STom Van Eyck  *
196*b0563631STom Van Eyck  * \retval #PSA_SUCCESS \emptydescription
197*b0563631STom Van Eyck  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
198*b0563631STom Van Eyck  * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
199*b0563631STom Van Eyck  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
200*b0563631STom Van Eyck  */
psa_crypto_storage_get_data_length(const mbedtls_svc_key_id_t key,size_t * data_length)201*b0563631STom Van Eyck static psa_status_t psa_crypto_storage_get_data_length(
202*b0563631STom Van Eyck     const mbedtls_svc_key_id_t key,
203*b0563631STom Van Eyck     size_t *data_length)
204*b0563631STom Van Eyck {
205*b0563631STom Van Eyck     psa_status_t status;
206*b0563631STom Van Eyck     psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
207*b0563631STom Van Eyck     struct psa_storage_info_t data_identifier_info;
208*b0563631STom Van Eyck 
209*b0563631STom Van Eyck     status = psa_its_get_info(data_identifier, &data_identifier_info);
210*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
211*b0563631STom Van Eyck         return status;
212*b0563631STom Van Eyck     }
213*b0563631STom Van Eyck 
214*b0563631STom Van Eyck     *data_length = (size_t) data_identifier_info.size;
215*b0563631STom Van Eyck 
216*b0563631STom Van Eyck     return PSA_SUCCESS;
217*b0563631STom Van Eyck }
218*b0563631STom Van Eyck 
219*b0563631STom Van Eyck /**
220*b0563631STom Van Eyck  * Persistent key storage magic header.
221*b0563631STom Van Eyck  */
222*b0563631STom Van Eyck #define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
223*b0563631STom Van Eyck #define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
224*b0563631STom Van Eyck 
225*b0563631STom Van Eyck typedef struct {
226*b0563631STom Van Eyck     uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
227*b0563631STom Van Eyck     uint8_t version[4];
228*b0563631STom Van Eyck     uint8_t lifetime[sizeof(psa_key_lifetime_t)];
229*b0563631STom Van Eyck     uint8_t type[2];
230*b0563631STom Van Eyck     uint8_t bits[2];
231*b0563631STom Van Eyck     uint8_t policy[sizeof(psa_key_policy_t)];
232*b0563631STom Van Eyck     uint8_t data_len[4];
233*b0563631STom Van Eyck     uint8_t key_data[];
234*b0563631STom Van Eyck } psa_persistent_key_storage_format;
235*b0563631STom Van Eyck 
psa_format_key_data_for_storage(const uint8_t * data,const size_t data_length,const psa_key_attributes_t * attr,uint8_t * storage_data)236*b0563631STom Van Eyck void psa_format_key_data_for_storage(const uint8_t *data,
237*b0563631STom Van Eyck                                      const size_t data_length,
238*b0563631STom Van Eyck                                      const psa_key_attributes_t *attr,
239*b0563631STom Van Eyck                                      uint8_t *storage_data)
240*b0563631STom Van Eyck {
241*b0563631STom Van Eyck     psa_persistent_key_storage_format *storage_format =
242*b0563631STom Van Eyck         (psa_persistent_key_storage_format *) storage_data;
243*b0563631STom Van Eyck 
244*b0563631STom Van Eyck     memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
245*b0563631STom Van Eyck            PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
246*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(0, storage_format->version, 0);
247*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
248*b0563631STom Van Eyck     MBEDTLS_PUT_UINT16_LE((uint16_t) attr->type, storage_format->type, 0);
249*b0563631STom Van Eyck     MBEDTLS_PUT_UINT16_LE((uint16_t) attr->bits, storage_format->bits, 0);
250*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
251*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
252*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(attr->policy.alg2, storage_format->policy, 2 * sizeof(uint32_t));
253*b0563631STom Van Eyck     MBEDTLS_PUT_UINT32_LE(data_length, storage_format->data_len, 0);
254*b0563631STom Van Eyck     memcpy(storage_format->key_data, data, data_length);
255*b0563631STom Van Eyck }
256*b0563631STom Van Eyck 
check_magic_header(const uint8_t * data)257*b0563631STom Van Eyck static psa_status_t check_magic_header(const uint8_t *data)
258*b0563631STom Van Eyck {
259*b0563631STom Van Eyck     if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
260*b0563631STom Van Eyck                PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0) {
261*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
262*b0563631STom Van Eyck     }
263*b0563631STom Van Eyck     return PSA_SUCCESS;
264*b0563631STom Van Eyck }
265*b0563631STom Van Eyck 
psa_parse_key_data_from_storage(const uint8_t * storage_data,size_t storage_data_length,uint8_t ** key_data,size_t * key_data_length,psa_key_attributes_t * attr)266*b0563631STom Van Eyck psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
267*b0563631STom Van Eyck                                              size_t storage_data_length,
268*b0563631STom Van Eyck                                              uint8_t **key_data,
269*b0563631STom Van Eyck                                              size_t *key_data_length,
270*b0563631STom Van Eyck                                              psa_key_attributes_t *attr)
271*b0563631STom Van Eyck {
272*b0563631STom Van Eyck     psa_status_t status;
273*b0563631STom Van Eyck     const psa_persistent_key_storage_format *storage_format =
274*b0563631STom Van Eyck         (const psa_persistent_key_storage_format *) storage_data;
275*b0563631STom Van Eyck     uint32_t version;
276*b0563631STom Van Eyck 
277*b0563631STom Van Eyck     if (storage_data_length < sizeof(*storage_format)) {
278*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
279*b0563631STom Van Eyck     }
280*b0563631STom Van Eyck 
281*b0563631STom Van Eyck     status = check_magic_header(storage_data);
282*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
283*b0563631STom Van Eyck         return status;
284*b0563631STom Van Eyck     }
285*b0563631STom Van Eyck 
286*b0563631STom Van Eyck     version = MBEDTLS_GET_UINT32_LE(storage_format->version, 0);
287*b0563631STom Van Eyck     if (version != 0) {
288*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
289*b0563631STom Van Eyck     }
290*b0563631STom Van Eyck 
291*b0563631STom Van Eyck     *key_data_length = MBEDTLS_GET_UINT32_LE(storage_format->data_len, 0);
292*b0563631STom Van Eyck     if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
293*b0563631STom Van Eyck         *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
294*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
295*b0563631STom Van Eyck     }
296*b0563631STom Van Eyck 
297*b0563631STom Van Eyck     if (*key_data_length == 0) {
298*b0563631STom Van Eyck         *key_data = NULL;
299*b0563631STom Van Eyck     } else {
300*b0563631STom Van Eyck         *key_data = mbedtls_calloc(1, *key_data_length);
301*b0563631STom Van Eyck         if (*key_data == NULL) {
302*b0563631STom Van Eyck             return PSA_ERROR_INSUFFICIENT_MEMORY;
303*b0563631STom Van Eyck         }
304*b0563631STom Van Eyck         memcpy(*key_data, storage_format->key_data, *key_data_length);
305*b0563631STom Van Eyck     }
306*b0563631STom Van Eyck 
307*b0563631STom Van Eyck     attr->lifetime = MBEDTLS_GET_UINT32_LE(storage_format->lifetime, 0);
308*b0563631STom Van Eyck     attr->type = MBEDTLS_GET_UINT16_LE(storage_format->type, 0);
309*b0563631STom Van Eyck     attr->bits = MBEDTLS_GET_UINT16_LE(storage_format->bits, 0);
310*b0563631STom Van Eyck     attr->policy.usage = MBEDTLS_GET_UINT32_LE(storage_format->policy, 0);
311*b0563631STom Van Eyck     attr->policy.alg = MBEDTLS_GET_UINT32_LE(storage_format->policy, sizeof(uint32_t));
312*b0563631STom Van Eyck     attr->policy.alg2 = MBEDTLS_GET_UINT32_LE(storage_format->policy, 2 * sizeof(uint32_t));
313*b0563631STom Van Eyck 
314*b0563631STom Van Eyck     return PSA_SUCCESS;
315*b0563631STom Van Eyck }
316*b0563631STom Van Eyck 
psa_save_persistent_key(const psa_key_attributes_t * attr,const uint8_t * data,const size_t data_length)317*b0563631STom Van Eyck psa_status_t psa_save_persistent_key(const psa_key_attributes_t *attr,
318*b0563631STom Van Eyck                                      const uint8_t *data,
319*b0563631STom Van Eyck                                      const size_t data_length)
320*b0563631STom Van Eyck {
321*b0563631STom Van Eyck     size_t storage_data_length;
322*b0563631STom Van Eyck     uint8_t *storage_data;
323*b0563631STom Van Eyck     psa_status_t status;
324*b0563631STom Van Eyck 
325*b0563631STom Van Eyck     /* All keys saved to persistent storage always have a key context */
326*b0563631STom Van Eyck     if (data == NULL || data_length == 0) {
327*b0563631STom Van Eyck         return PSA_ERROR_INVALID_ARGUMENT;
328*b0563631STom Van Eyck     }
329*b0563631STom Van Eyck 
330*b0563631STom Van Eyck     if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
331*b0563631STom Van Eyck         return PSA_ERROR_INSUFFICIENT_STORAGE;
332*b0563631STom Van Eyck     }
333*b0563631STom Van Eyck     storage_data_length = data_length + sizeof(psa_persistent_key_storage_format);
334*b0563631STom Van Eyck 
335*b0563631STom Van Eyck     storage_data = mbedtls_calloc(1, storage_data_length);
336*b0563631STom Van Eyck     if (storage_data == NULL) {
337*b0563631STom Van Eyck         return PSA_ERROR_INSUFFICIENT_MEMORY;
338*b0563631STom Van Eyck     }
339*b0563631STom Van Eyck 
340*b0563631STom Van Eyck     psa_format_key_data_for_storage(data, data_length, attr, storage_data);
341*b0563631STom Van Eyck 
342*b0563631STom Van Eyck     status = psa_crypto_storage_store(attr->id,
343*b0563631STom Van Eyck                                       storage_data, storage_data_length);
344*b0563631STom Van Eyck 
345*b0563631STom Van Eyck     mbedtls_zeroize_and_free(storage_data, storage_data_length);
346*b0563631STom Van Eyck 
347*b0563631STom Van Eyck     return status;
348*b0563631STom Van Eyck }
349*b0563631STom Van Eyck 
psa_free_persistent_key_data(uint8_t * key_data,size_t key_data_length)350*b0563631STom Van Eyck void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
351*b0563631STom Van Eyck {
352*b0563631STom Van Eyck     mbedtls_zeroize_and_free(key_data, key_data_length);
353*b0563631STom Van Eyck }
354*b0563631STom Van Eyck 
psa_load_persistent_key(psa_key_attributes_t * attr,uint8_t ** data,size_t * data_length)355*b0563631STom Van Eyck psa_status_t psa_load_persistent_key(psa_key_attributes_t *attr,
356*b0563631STom Van Eyck                                      uint8_t **data,
357*b0563631STom Van Eyck                                      size_t *data_length)
358*b0563631STom Van Eyck {
359*b0563631STom Van Eyck     psa_status_t status = PSA_SUCCESS;
360*b0563631STom Van Eyck     uint8_t *loaded_data;
361*b0563631STom Van Eyck     size_t storage_data_length = 0;
362*b0563631STom Van Eyck     mbedtls_svc_key_id_t key = attr->id;
363*b0563631STom Van Eyck 
364*b0563631STom Van Eyck     status = psa_crypto_storage_get_data_length(key, &storage_data_length);
365*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
366*b0563631STom Van Eyck         return status;
367*b0563631STom Van Eyck     }
368*b0563631STom Van Eyck 
369*b0563631STom Van Eyck     loaded_data = mbedtls_calloc(1, storage_data_length);
370*b0563631STom Van Eyck 
371*b0563631STom Van Eyck     if (loaded_data == NULL) {
372*b0563631STom Van Eyck         return PSA_ERROR_INSUFFICIENT_MEMORY;
373*b0563631STom Van Eyck     }
374*b0563631STom Van Eyck 
375*b0563631STom Van Eyck     status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
376*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
377*b0563631STom Van Eyck         goto exit;
378*b0563631STom Van Eyck     }
379*b0563631STom Van Eyck 
380*b0563631STom Van Eyck     status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
381*b0563631STom Van Eyck                                              data, data_length, attr);
382*b0563631STom Van Eyck 
383*b0563631STom Van Eyck     /* All keys saved to persistent storage always have a key context */
384*b0563631STom Van Eyck     if (status == PSA_SUCCESS &&
385*b0563631STom Van Eyck         (*data == NULL || *data_length == 0)) {
386*b0563631STom Van Eyck         status = PSA_ERROR_STORAGE_FAILURE;
387*b0563631STom Van Eyck     }
388*b0563631STom Van Eyck 
389*b0563631STom Van Eyck exit:
390*b0563631STom Van Eyck     mbedtls_zeroize_and_free(loaded_data, storage_data_length);
391*b0563631STom Van Eyck     return status;
392*b0563631STom Van Eyck }
393*b0563631STom Van Eyck 
394*b0563631STom Van Eyck 
395*b0563631STom Van Eyck 
396*b0563631STom Van Eyck /****************************************************************/
397*b0563631STom Van Eyck /* Transactions */
398*b0563631STom Van Eyck /****************************************************************/
399*b0563631STom Van Eyck 
400*b0563631STom Van Eyck #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
401*b0563631STom Van Eyck 
402*b0563631STom Van Eyck psa_crypto_transaction_t psa_crypto_transaction;
403*b0563631STom Van Eyck 
psa_crypto_save_transaction(void)404*b0563631STom Van Eyck psa_status_t psa_crypto_save_transaction(void)
405*b0563631STom Van Eyck {
406*b0563631STom Van Eyck     struct psa_storage_info_t p_info;
407*b0563631STom Van Eyck     psa_status_t status;
408*b0563631STom Van Eyck     status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
409*b0563631STom Van Eyck     if (status == PSA_SUCCESS) {
410*b0563631STom Van Eyck         /* This shouldn't happen: we're trying to start a transaction while
411*b0563631STom Van Eyck          * there is still a transaction that hasn't been replayed. */
412*b0563631STom Van Eyck         return PSA_ERROR_CORRUPTION_DETECTED;
413*b0563631STom Van Eyck     } else if (status != PSA_ERROR_DOES_NOT_EXIST) {
414*b0563631STom Van Eyck         return status;
415*b0563631STom Van Eyck     }
416*b0563631STom Van Eyck     return psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
417*b0563631STom Van Eyck                        sizeof(psa_crypto_transaction),
418*b0563631STom Van Eyck                        &psa_crypto_transaction,
419*b0563631STom Van Eyck                        0);
420*b0563631STom Van Eyck }
421*b0563631STom Van Eyck 
psa_crypto_load_transaction(void)422*b0563631STom Van Eyck psa_status_t psa_crypto_load_transaction(void)
423*b0563631STom Van Eyck {
424*b0563631STom Van Eyck     psa_status_t status;
425*b0563631STom Van Eyck     size_t length;
426*b0563631STom Van Eyck     status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
427*b0563631STom Van Eyck                          sizeof(psa_crypto_transaction),
428*b0563631STom Van Eyck                          &psa_crypto_transaction, &length);
429*b0563631STom Van Eyck     if (status != PSA_SUCCESS) {
430*b0563631STom Van Eyck         return status;
431*b0563631STom Van Eyck     }
432*b0563631STom Van Eyck     if (length != sizeof(psa_crypto_transaction)) {
433*b0563631STom Van Eyck         return PSA_ERROR_DATA_INVALID;
434*b0563631STom Van Eyck     }
435*b0563631STom Van Eyck     return PSA_SUCCESS;
436*b0563631STom Van Eyck }
437*b0563631STom Van Eyck 
psa_crypto_stop_transaction(void)438*b0563631STom Van Eyck psa_status_t psa_crypto_stop_transaction(void)
439*b0563631STom Van Eyck {
440*b0563631STom Van Eyck     psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
441*b0563631STom Van Eyck     /* Whether or not updating the storage succeeded, the transaction is
442*b0563631STom Van Eyck      * finished now. It's too late to go back, so zero out the in-memory
443*b0563631STom Van Eyck      * data. */
444*b0563631STom Van Eyck     memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
445*b0563631STom Van Eyck     return status;
446*b0563631STom Van Eyck }
447*b0563631STom Van Eyck 
448*b0563631STom Van Eyck #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
449*b0563631STom Van Eyck 
450*b0563631STom Van Eyck 
451*b0563631STom Van Eyck 
452*b0563631STom Van Eyck /****************************************************************/
453*b0563631STom Van Eyck /* Random generator state */
454*b0563631STom Van Eyck /****************************************************************/
455*b0563631STom Van Eyck 
456*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
mbedtls_psa_storage_inject_entropy(const unsigned char * seed,size_t seed_size)457*b0563631STom Van Eyck psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
458*b0563631STom Van Eyck                                                 size_t seed_size)
459*b0563631STom Van Eyck {
460*b0563631STom Van Eyck     psa_status_t status;
461*b0563631STom Van Eyck     struct psa_storage_info_t p_info;
462*b0563631STom Van Eyck 
463*b0563631STom Van Eyck     status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
464*b0563631STom Van Eyck 
465*b0563631STom Van Eyck     if (PSA_ERROR_DOES_NOT_EXIST == status) { /* No seed exists */
466*b0563631STom Van Eyck         status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
467*b0563631STom Van Eyck     } else if (PSA_SUCCESS == status) {
468*b0563631STom Van Eyck         /* You should not be here. Seed needs to be injected only once */
469*b0563631STom Van Eyck         status = PSA_ERROR_NOT_PERMITTED;
470*b0563631STom Van Eyck     }
471*b0563631STom Van Eyck     return status;
472*b0563631STom Van Eyck }
473*b0563631STom Van Eyck #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
474*b0563631STom Van Eyck 
475*b0563631STom Van Eyck 
476*b0563631STom Van Eyck 
477*b0563631STom Van Eyck /****************************************************************/
478*b0563631STom Van Eyck /* The end */
479*b0563631STom Van Eyck /****************************************************************/
480*b0563631STom Van Eyck 
481*b0563631STom Van Eyck #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
482