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 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 58 | /* Determine a file name (ITS file identifier) for the given key identifier. |
| 59 | * The file name must be distinct from any file that is used for a purpose |
| 60 | * other than storing a key. Currently, the only such file is the random seed |
| 61 | * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is |
| 62 | * 0xFFFFFF52. */ |
| 63 | 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] | 64 | { |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 65 | #if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 66 | /* Encode the owner in the upper 32 bits. This means that if |
| 67 | * owner values are nonzero (as they are on a PSA platform), |
| 68 | * no key file will ever have a value less than 0x100000000, so |
| 69 | * the whole range 0..0xffffffff is available for non-key files. */ |
Ronald Cron | 79ca427 | 2020-08-25 09:53:53 +0200 | [diff] [blame] | 70 | uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key ); |
| 71 | return( ( (uint64_t) unsigned_owner_id << 32 ) | |
| 72 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 73 | #else |
| 74 | /* Use the key id directly as a file name. |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 75 | * psa_is_key_id_valid() in psa_crypto_slot_management.c |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 76 | * is responsible for ensuring that key identifiers do not have a |
| 77 | * value that is reserved for non-key files. */ |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 78 | return( key ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 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 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 93 | * \retval #PSA_SUCCESS |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame^] | 94 | * \retval #PSA_ERROR_DATA_INVALID |
| 95 | * \retval #PSA_ERROR_DATA_CORRUPT |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 96 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 97 | */ |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 98 | static psa_status_t psa_crypto_storage_load( |
| 99 | 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] | 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 ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 112 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 113 | |
| 114 | return( status ); |
| 115 | } |
| 116 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 117 | 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] | 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 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 141 | * \retval #PSA_SUCCESS |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 142 | * \retval #PSA_ERROR_ALREADY_EXISTS |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame^] | 143 | * \retval #PSA_ERROR_DATA_INVALID |
| 144 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 145 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 146 | * \retval #PSA_ERROR_DATA_CORRUPT |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 147 | */ |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 148 | static psa_status_t psa_crypto_storage_store( const mbedtls_svc_key_id_t key, |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 149 | const uint8_t *data, |
| 150 | size_t data_length ) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 151 | { |
| 152 | psa_status_t status; |
| 153 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 154 | struct psa_storage_info_t data_identifier_info; |
| 155 | |
| 156 | if( psa_is_key_present_in_storage( key ) == 1 ) |
| 157 | return( PSA_ERROR_ALREADY_EXISTS ); |
| 158 | |
Gilles Peskine | fad3a3e | 2019-02-25 13:36:21 +0100 | [diff] [blame] | 159 | status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 160 | if( status != PSA_SUCCESS ) |
| 161 | { |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 162 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 166 | if( status != PSA_SUCCESS ) |
| 167 | { |
| 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | if( data_identifier_info.size != data_length ) |
| 172 | { |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 173 | status = PSA_ERROR_DATA_INVALID; |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 174 | goto exit; |
| 175 | } |
| 176 | |
| 177 | exit: |
| 178 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 169ca7f | 2020-08-25 22:50:06 +0200 | [diff] [blame] | 179 | { |
| 180 | /* Remove the file in case we managed to create it but something |
| 181 | * went wrong. It's ok if the file doesn't exist. If the file exists |
| 182 | * but the removal fails, we're already reporting an error so there's |
| 183 | * nothing else we can do. */ |
| 184 | (void) psa_its_remove( data_identifier ); |
| 185 | } |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 186 | return( status ); |
| 187 | } |
| 188 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 189 | 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] | 190 | { |
| 191 | psa_status_t ret; |
| 192 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 193 | struct psa_storage_info_t data_identifier_info; |
| 194 | |
| 195 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 196 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
| 197 | return( PSA_SUCCESS ); |
| 198 | |
| 199 | if( psa_its_remove( data_identifier ) != PSA_SUCCESS ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 200 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 201 | |
| 202 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 203 | if( ret != PSA_ERROR_DOES_NOT_EXIST ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 204 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 205 | |
| 206 | return( PSA_SUCCESS ); |
| 207 | } |
| 208 | |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 209 | /** |
| 210 | * \brief Get data length for given key slot number. |
| 211 | * |
| 212 | * \param key Persistent identifier whose stored data length |
| 213 | * is to be obtained. |
| 214 | * \param[out] data_length The number of bytes that make up the data. |
| 215 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 216 | * \retval #PSA_SUCCESS |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame^] | 217 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 218 | * \retval #PSA_ERROR_DATA_CORRUPT |
| 219 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 220 | */ |
| 221 | static psa_status_t psa_crypto_storage_get_data_length( |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 222 | const mbedtls_svc_key_id_t key, |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 223 | size_t *data_length ) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 224 | { |
| 225 | psa_status_t status; |
| 226 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 227 | struct psa_storage_info_t data_identifier_info; |
| 228 | |
| 229 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 230 | if( status != PSA_SUCCESS ) |
| 231 | return( status ); |
| 232 | |
| 233 | *data_length = (size_t) data_identifier_info.size; |
| 234 | |
| 235 | return( PSA_SUCCESS ); |
| 236 | } |
| 237 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 238 | /* |
| 239 | * 32-bit integer manipulation macros (little endian) |
| 240 | */ |
| 241 | #ifndef GET_UINT32_LE |
Gilles Peskine | 274a263 | 2019-07-23 11:27:38 +0200 | [diff] [blame] | 242 | #define GET_UINT32_LE( n, b, i ) \ |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 243 | { \ |
| 244 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
| 245 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
| 246 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
| 247 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | #ifndef PUT_UINT32_LE |
Gilles Peskine | 274a263 | 2019-07-23 11:27:38 +0200 | [diff] [blame] | 252 | #define PUT_UINT32_LE( n, b, i ) \ |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 253 | { \ |
| 254 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 255 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 256 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 257 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 258 | } |
| 259 | #endif |
| 260 | |
Torstein Nesse | 162a110 | 2020-10-07 10:50:15 +0200 | [diff] [blame] | 261 | /* |
| 262 | * 16-bit integer manipulation macros (little endian) |
| 263 | */ |
| 264 | #ifndef GET_UINT16_LE |
| 265 | #define GET_UINT16_LE( n, b, i ) \ |
| 266 | { \ |
| 267 | (n) = ( (uint16_t) (b)[(i) ] ) \ |
| 268 | | ( (uint16_t) (b)[(i) + 1] << 8 ); \ |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | #ifndef PUT_UINT16_LE |
| 273 | #define PUT_UINT16_LE( n, b, i ) \ |
| 274 | { \ |
| 275 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 276 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 277 | } |
| 278 | #endif |
| 279 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 280 | /** |
| 281 | * Persistent key storage magic header. |
| 282 | */ |
| 283 | #define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY" |
| 284 | #define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) ) |
| 285 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 286 | typedef struct { |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 287 | uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH]; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 288 | uint8_t version[4]; |
Gilles Peskine | 0e8d495 | 2019-07-23 14:46:52 +0200 | [diff] [blame] | 289 | uint8_t lifetime[sizeof( psa_key_lifetime_t )]; |
Torstein Nesse | 162a110 | 2020-10-07 10:50:15 +0200 | [diff] [blame] | 290 | uint8_t type[2]; |
| 291 | uint8_t bits[2]; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 292 | uint8_t policy[sizeof( psa_key_policy_t )]; |
| 293 | uint8_t data_len[4]; |
| 294 | uint8_t key_data[]; |
| 295 | } psa_persistent_key_storage_format; |
| 296 | |
| 297 | void psa_format_key_data_for_storage( const uint8_t *data, |
| 298 | const size_t data_length, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 299 | const psa_core_key_attributes_t *attr, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 300 | uint8_t *storage_data ) |
| 301 | { |
| 302 | psa_persistent_key_storage_format *storage_format = |
| 303 | (psa_persistent_key_storage_format *) storage_data; |
| 304 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 305 | 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] | 306 | PUT_UINT32_LE( 0, storage_format->version, 0 ); |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 307 | PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); |
Torstein Nesse | 162a110 | 2020-10-07 10:50:15 +0200 | [diff] [blame] | 308 | PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 ); |
| 309 | PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 ); |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 310 | PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); |
| 311 | PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); |
| 312 | 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] | 313 | PUT_UINT32_LE( data_length, storage_format->data_len, 0 ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 314 | memcpy( storage_format->key_data, data, data_length ); |
| 315 | } |
| 316 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 317 | static psa_status_t check_magic_header( const uint8_t *data ) |
| 318 | { |
| 319 | if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER, |
| 320 | PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 321 | return( PSA_ERROR_DATA_INVALID ); |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 322 | return( PSA_SUCCESS ); |
| 323 | } |
| 324 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 325 | psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, |
| 326 | size_t storage_data_length, |
| 327 | uint8_t **key_data, |
| 328 | size_t *key_data_length, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 329 | psa_core_key_attributes_t *attr ) |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 330 | { |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 331 | psa_status_t status; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 332 | const psa_persistent_key_storage_format *storage_format = |
| 333 | (const psa_persistent_key_storage_format *)storage_data; |
| 334 | uint32_t version; |
| 335 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 336 | if( storage_data_length < sizeof(*storage_format) ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 337 | return( PSA_ERROR_DATA_INVALID ); |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 338 | |
| 339 | status = check_magic_header( storage_data ); |
| 340 | if( status != PSA_SUCCESS ) |
| 341 | return( status ); |
| 342 | |
Gilles Peskine | 274a263 | 2019-07-23 11:27:38 +0200 | [diff] [blame] | 343 | GET_UINT32_LE( version, storage_format->version, 0 ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 344 | if( version != 0 ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 345 | return( PSA_ERROR_DATA_INVALID ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 346 | |
Gilles Peskine | 274a263 | 2019-07-23 11:27:38 +0200 | [diff] [blame] | 347 | GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 348 | if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) || |
| 349 | *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 350 | return( PSA_ERROR_DATA_INVALID ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 351 | |
Gilles Peskine | 3495b58 | 2019-04-25 13:47:06 +0200 | [diff] [blame] | 352 | if( *key_data_length == 0 ) |
| 353 | { |
| 354 | *key_data = NULL; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | *key_data = mbedtls_calloc( 1, *key_data_length ); |
| 359 | if( *key_data == NULL ) |
| 360 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 361 | memcpy( *key_data, storage_format->key_data, *key_data_length ); |
| 362 | } |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 363 | |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 364 | GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); |
Torstein Nesse | 162a110 | 2020-10-07 10:50:15 +0200 | [diff] [blame] | 365 | GET_UINT16_LE( attr->type, storage_format->type, 0 ); |
| 366 | GET_UINT16_LE( attr->bits, storage_format->bits, 0 ); |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 367 | GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); |
| 368 | GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); |
| 369 | 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] | 370 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 371 | return( PSA_SUCCESS ); |
| 372 | } |
| 373 | |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 374 | 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] | 375 | const uint8_t *data, |
| 376 | const size_t data_length ) |
| 377 | { |
| 378 | size_t storage_data_length; |
| 379 | uint8_t *storage_data; |
| 380 | psa_status_t status; |
| 381 | |
| 382 | if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) |
| 383 | return PSA_ERROR_INSUFFICIENT_STORAGE; |
| 384 | storage_data_length = data_length + sizeof( psa_persistent_key_storage_format ); |
| 385 | |
| 386 | storage_data = mbedtls_calloc( 1, storage_data_length ); |
| 387 | if( storage_data == NULL ) |
| 388 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 389 | |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 390 | psa_format_key_data_for_storage( data, data_length, attr, storage_data ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 391 | |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 392 | status = psa_crypto_storage_store( attr->id, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 393 | storage_data, storage_data_length ); |
| 394 | |
| 395 | mbedtls_free( storage_data ); |
| 396 | |
| 397 | return( status ); |
| 398 | } |
| 399 | |
| 400 | void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length ) |
| 401 | { |
| 402 | if( key_data != NULL ) |
| 403 | { |
| 404 | mbedtls_platform_zeroize( key_data, key_data_length ); |
| 405 | } |
| 406 | mbedtls_free( key_data ); |
| 407 | } |
| 408 | |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 409 | 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] | 410 | uint8_t **data, |
| 411 | size_t *data_length ) |
| 412 | { |
| 413 | psa_status_t status = PSA_SUCCESS; |
| 414 | uint8_t *loaded_data; |
| 415 | size_t storage_data_length = 0; |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 416 | mbedtls_svc_key_id_t key = attr->id; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 417 | |
| 418 | status = psa_crypto_storage_get_data_length( key, &storage_data_length ); |
| 419 | if( status != PSA_SUCCESS ) |
| 420 | return( status ); |
| 421 | |
| 422 | loaded_data = mbedtls_calloc( 1, storage_data_length ); |
| 423 | |
| 424 | if( loaded_data == NULL ) |
| 425 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 426 | |
| 427 | status = psa_crypto_storage_load( key, loaded_data, storage_data_length ); |
| 428 | if( status != PSA_SUCCESS ) |
| 429 | goto exit; |
| 430 | |
| 431 | status = psa_parse_key_data_from_storage( loaded_data, storage_data_length, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 432 | data, data_length, attr ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 433 | |
| 434 | exit: |
| 435 | mbedtls_free( loaded_data ); |
| 436 | return( status ); |
| 437 | } |
| 438 | |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 439 | |
| 440 | |
| 441 | /****************************************************************/ |
| 442 | /* Transactions */ |
| 443 | /****************************************************************/ |
| 444 | |
| 445 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 446 | |
| 447 | psa_crypto_transaction_t psa_crypto_transaction; |
| 448 | |
| 449 | psa_status_t psa_crypto_save_transaction( void ) |
| 450 | { |
| 451 | struct psa_storage_info_t p_info; |
| 452 | psa_status_t status; |
Jaeden Amero | 2ce22a5 | 2019-10-28 15:25:10 +0000 | [diff] [blame] | 453 | status = psa_its_get_info( PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info ); |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 454 | if( status == PSA_SUCCESS ) |
| 455 | { |
| 456 | /* This shouldn't happen: we're trying to start a transaction while |
| 457 | * there is still a transaction that hasn't been replayed. */ |
| 458 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
| 459 | } |
| 460 | else if( status != PSA_ERROR_DOES_NOT_EXIST ) |
| 461 | return( status ); |
| 462 | return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID, |
| 463 | sizeof( psa_crypto_transaction ), |
| 464 | &psa_crypto_transaction, |
| 465 | 0 ) ); |
| 466 | } |
| 467 | |
| 468 | psa_status_t psa_crypto_load_transaction( void ) |
| 469 | { |
Gilles Peskine | 8b66389 | 2019-07-31 17:57:57 +0200 | [diff] [blame] | 470 | psa_status_t status; |
| 471 | size_t length; |
| 472 | status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0, |
| 473 | sizeof( psa_crypto_transaction ), |
| 474 | &psa_crypto_transaction, &length ); |
| 475 | if( status != PSA_SUCCESS ) |
| 476 | return( status ); |
| 477 | if( length != sizeof( psa_crypto_transaction ) ) |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 478 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | 8b66389 | 2019-07-31 17:57:57 +0200 | [diff] [blame] | 479 | return( PSA_SUCCESS ); |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | psa_status_t psa_crypto_stop_transaction( void ) |
| 483 | { |
| 484 | psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID ); |
| 485 | /* Whether or not updating the storage succeeded, the transaction is |
| 486 | * finished now. It's too late to go back, so zero out the in-memory |
| 487 | * data. */ |
| 488 | memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) ); |
| 489 | return( status ); |
| 490 | } |
| 491 | |
| 492 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 493 | |
| 494 | |
| 495 | |
| 496 | /****************************************************************/ |
| 497 | /* Random generator state */ |
| 498 | /****************************************************************/ |
| 499 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 500 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 501 | psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed, |
| 502 | size_t seed_size ) |
| 503 | { |
| 504 | psa_status_t status; |
| 505 | struct psa_storage_info_t p_info; |
| 506 | |
| 507 | status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info ); |
| 508 | |
| 509 | if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */ |
| 510 | { |
| 511 | status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 ); |
| 512 | } |
| 513 | else if( PSA_SUCCESS == status ) |
| 514 | { |
| 515 | /* You should not be here. Seed needs to be injected only once */ |
| 516 | status = PSA_ERROR_NOT_PERMITTED; |
| 517 | } |
| 518 | return( status ); |
| 519 | } |
| 520 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 521 | |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 522 | |
| 523 | |
| 524 | /****************************************************************/ |
| 525 | /* The end */ |
| 526 | /****************************************************************/ |
| 527 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 528 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |