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