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