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