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