Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa_crypto_storage.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS key storage |
| 5 | */ |
| 6 | /* |
| 7 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
| 21 | * |
| 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 23 | */ |
| 24 | |
| 25 | #ifndef PSA_CRYPTO_STORAGE_H |
| 26 | #define PSA_CRYPTO_STORAGE_H |
| 27 | |
| 28 | #ifdef __cplusplus |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 32 | #include "psa/crypto.h" |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame^] | 33 | #include "psa/crypto_se_driver.h" |
| 34 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 35 | #include <stdint.h> |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 36 | #include <string.h> |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 37 | |
| 38 | /* Limit the maximum key size to 30kB (just in case someone tries to |
| 39 | * inadvertently store an obscene amount of data) */ |
| 40 | #define PSA_CRYPTO_MAX_STORAGE_SIZE ( 30 * 1024 ) |
| 41 | |
Gilles Peskine | 4886812 | 2018-12-10 17:30:29 +0100 | [diff] [blame] | 42 | /** The maximum permitted persistent slot number. |
| 43 | * |
| 44 | * In Mbed Crypto 0.1.0b: |
| 45 | * - Using the file backend, all key ids are ok except 0. |
| 46 | * - Using the ITS backend, all key ids are ok except 0xFFFFFF52 |
| 47 | * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the |
| 48 | * device's random seed (if this feature is enabled). |
| 49 | * - Only key ids from 1 to #PSA_KEY_SLOT_COUNT are actually used. |
| 50 | * |
| 51 | * Since we need to preserve the random seed, avoid using that key slot. |
| 52 | * Reserve a whole range of key slots just in case something else comes up. |
| 53 | * |
| 54 | * This limitation will probably become moot when we implement client |
| 55 | * separation for key storage. |
| 56 | */ |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 57 | #define PSA_MAX_PERSISTENT_KEY_IDENTIFIER PSA_KEY_ID_VENDOR_MAX |
Gilles Peskine | 4886812 | 2018-12-10 17:30:29 +0100 | [diff] [blame] | 58 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 59 | /** |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 60 | * \brief Checks if persistent data is stored for the given key slot number |
| 61 | * |
| 62 | * This function checks if any key data or metadata exists for the key slot in |
| 63 | * the persistent storage. |
| 64 | * |
| 65 | * \param key Persistent identifier to check. |
| 66 | * |
| 67 | * \retval 0 |
| 68 | * No persistent data present for slot number |
| 69 | * \retval 1 |
| 70 | * Persistent data present for slot number |
| 71 | */ |
| 72 | int psa_is_key_present_in_storage( const psa_key_file_id_t key ); |
| 73 | |
| 74 | /** |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 75 | * \brief Format key data and metadata and save to a location for given key |
| 76 | * slot. |
| 77 | * |
| 78 | * This function formats the key data and metadata and saves it to a |
| 79 | * persistent storage backend. The storage location corresponding to the |
| 80 | * key slot must be empty, otherwise this function will fail. This function |
| 81 | * should be called after psa_import_key_into_slot() to ensure the |
| 82 | * persistent key is not saved into a storage location corresponding to an |
| 83 | * already occupied non-persistent key, as well as validating the key data. |
| 84 | * |
| 85 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 86 | * \param key Persistent identifier of the key to be stored. This |
| 87 | * should be an unoccupied storage location. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 88 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 89 | * \param[in] policy The key policy to save. |
| 90 | * \param[in] data Buffer containing the key data. |
| 91 | * \param data_length The number of bytes that make up the key data. |
| 92 | * |
| 93 | * \retval PSA_SUCCESS |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 94 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 95 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 96 | * \retval PSA_ERROR_STORAGE_FAILURE |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 97 | * \retval PSA_ERROR_ALREADY_EXISTS |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 98 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 99 | psa_status_t psa_save_persistent_key( const psa_key_file_id_t key, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 100 | const psa_key_type_t type, |
| 101 | const psa_key_policy_t *policy, |
| 102 | const uint8_t *data, |
| 103 | const size_t data_length ); |
| 104 | |
| 105 | /** |
| 106 | * \brief Parses key data and metadata and load persistent key for given |
| 107 | * key slot number. |
| 108 | * |
| 109 | * This function reads from a storage backend, parses the key data and |
| 110 | * metadata and writes them to the appropriate output parameters. |
| 111 | * |
| 112 | * Note: This function allocates a buffer and returns a pointer to it through |
| 113 | * the data parameter. psa_free_persistent_key_data() must be called after |
| 114 | * this function to zeroize and free this buffer, regardless of whether this |
| 115 | * function succeeds or fails. |
| 116 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 117 | * \param key Persistent identifier of the key to be loaded. This |
| 118 | * should be an occupied storage location. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 119 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX |
| 120 | * value). |
| 121 | * \param[out] policy On success, the key's policy. |
| 122 | * \param[out] data Pointer to an allocated key data buffer on return. |
| 123 | * \param[out] data_length The number of bytes that make up the key data. |
| 124 | * |
| 125 | * \retval PSA_SUCCESS |
| 126 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 127 | * \retval PSA_ERROR_STORAGE_FAILURE |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 128 | * \retval PSA_ERROR_DOES_NOT_EXIST |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 129 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 130 | psa_status_t psa_load_persistent_key( psa_key_file_id_t key, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 131 | psa_key_type_t *type, |
| 132 | psa_key_policy_t *policy, |
| 133 | uint8_t **data, |
| 134 | size_t *data_length ); |
| 135 | |
| 136 | /** |
| 137 | * \brief Remove persistent data for the given key slot number. |
| 138 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 139 | * \param key Persistent identifier of the key to remove |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 140 | * from persistent storage. |
| 141 | * |
| 142 | * \retval PSA_SUCCESS |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 143 | * The key was successfully removed, |
| 144 | * or the key did not exist. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 145 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 146 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 147 | psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 148 | |
| 149 | /** |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 150 | * \brief Free the temporary buffer allocated by psa_load_persistent_key(). |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 151 | * |
| 152 | * This function must be called at some point after psa_load_persistent_key() |
| 153 | * to zeroize and free the memory allocated to the buffer in that function. |
| 154 | * |
| 155 | * \param key_data Buffer for the key data. |
| 156 | * \param key_data_length Size of the key data buffer. |
| 157 | * |
| 158 | */ |
| 159 | void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length ); |
| 160 | |
| 161 | /** |
| 162 | * \brief Formats key data and metadata for persistent storage |
| 163 | * |
| 164 | * \param[in] data Buffer for the key data. |
| 165 | * \param data_length Length of the key data buffer. |
| 166 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 167 | * \param policy The key policy. |
| 168 | * \param[out] storage_data Output buffer for the formatted data. |
| 169 | * |
| 170 | */ |
| 171 | void psa_format_key_data_for_storage( const uint8_t *data, |
| 172 | const size_t data_length, |
| 173 | const psa_key_type_t type, |
| 174 | const psa_key_policy_t *policy, |
| 175 | uint8_t *storage_data ); |
| 176 | |
| 177 | /** |
| 178 | * \brief Parses persistent storage data into key data and metadata |
| 179 | * |
| 180 | * \param[in] storage_data Buffer for the storage data. |
| 181 | * \param storage_data_length Length of the storage data buffer |
| 182 | * \param[out] key_data On output, pointer to a newly allocated buffer |
| 183 | * containing the key data. This must be freed |
| 184 | * using psa_free_persistent_key_data() |
| 185 | * \param[out] key_data_length Length of the key data buffer |
| 186 | * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 187 | * \param[out] policy The key policy. |
| 188 | * |
| 189 | * \retval PSA_SUCCESS |
| 190 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 191 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 192 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 193 | */ |
| 194 | psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, |
| 195 | size_t storage_data_length, |
| 196 | uint8_t **key_data, |
| 197 | size_t *key_data_length, |
| 198 | psa_key_type_t *type, |
| 199 | psa_key_policy_t *policy ); |
| 200 | |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 201 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 202 | /** This symbol is defined if transaction support is required. */ |
| 203 | #define PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS |
| 204 | #endif |
| 205 | |
| 206 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 207 | |
| 208 | /** The type of transaction that is in progress. |
| 209 | */ |
| 210 | /* This is an integer type rather than an enum for two reasons: to support |
| 211 | * unknown values when loading a transaction file, and to ensure that the |
| 212 | * type has a known size. |
| 213 | */ |
| 214 | typedef uint16_t psa_crypto_transaction_type_t; |
| 215 | |
| 216 | /** No transaction is in progress. |
| 217 | */ |
| 218 | #define PSA_CRYPTO_TRANSACTION_NONE ( (psa_crypto_transaction_type_t) 0x0000 ) |
| 219 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame^] | 220 | /** A key creation transaction. |
| 221 | * |
| 222 | * This is only used for keys in an external cryptoprocessor (secure element). |
| 223 | * Keys in RAM or in internal storage are created atomically in storage |
| 224 | * (simple file creation), so they do not need a transaction mechanism. |
| 225 | */ |
| 226 | #define PSA_CRYPTO_TRANSACTION_CREATE_KEY ( (psa_crypto_transaction_type_t) 0x0001 ) |
| 227 | |
| 228 | /** A key destruction transaction. |
| 229 | * |
| 230 | * This is only used for keys in an external cryptoprocessor (secure element). |
| 231 | * Keys in RAM or in internal storage are destroyed atomically in storage |
| 232 | * (simple file deletion), so they do not need a transaction mechanism. |
| 233 | */ |
| 234 | #define PSA_CRYPTO_TRANSACTION_DESTROY_KEY ( (psa_crypto_transaction_type_t) 0x0002 ) |
| 235 | |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 236 | /** Transaction data. |
| 237 | * |
| 238 | * This type is designed to be serialized by writing the memory representation |
| 239 | * and reading it back on the same device. |
| 240 | * |
| 241 | * \note The transaction mechanism is designed for a single active transaction |
| 242 | * at a time. The transaction object is #psa_crypto_transaction. |
| 243 | * |
| 244 | * \note If an API call starts a transaction, it must complete this transaction |
| 245 | * before returning to the application. |
| 246 | * |
| 247 | * The lifetime of a transaction is the following (note that only one |
| 248 | * transaction may be active at a time): |
| 249 | * |
| 250 | * -# Call psa_crypto_prepare_transaction() to initialize the transaction |
| 251 | * object in memory and declare the type of transaction that is starting. |
| 252 | * -# Fill in the type-specific fields of #psa_crypto_transaction. |
| 253 | * -# Call psa_crypto_save_transaction() to start the transaction. This |
| 254 | * saves the transaction data to internal storage. |
| 255 | * -# If there are intermediate stages in the transaction, update |
| 256 | * the fields of #psa_crypto_transaction and call |
| 257 | * psa_crypto_save_transaction() again when each stage is reached. |
| 258 | * -# When the transaction is over, whether it has been committed or aborted, |
| 259 | * call psa_crypto_stop_transaction() to remove the transaction data in |
| 260 | * storage and in memory. |
| 261 | * |
| 262 | * If the system crashes while a transaction is in progress, psa_crypto_init() |
| 263 | * calls psa_crypto_load_transaction() and takes care of completing or |
| 264 | * rewinding the transaction. |
| 265 | */ |
| 266 | typedef union |
| 267 | { |
| 268 | /* Each element of this union must have the following properties |
| 269 | * to facilitate serialization and deserialization: |
| 270 | * |
| 271 | * - The element is a struct. |
| 272 | * - The first field of the struct is `psa_crypto_transaction_type_t type`. |
| 273 | * - Elements of the struct are arranged such a way that there is |
| 274 | * no padding. |
| 275 | */ |
| 276 | struct psa_crypto_transaction_unknown_s |
| 277 | { |
| 278 | psa_crypto_transaction_type_t type; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame^] | 279 | uint16_t unused1; |
| 280 | uint32_t unused2; |
| 281 | uint64_t unused3; |
| 282 | uint64_t unused4; |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 283 | } unknown; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame^] | 284 | /* ::type is #PSA_CRYPTO_TRANSACTION_CREATE_KEY or |
| 285 | * #PSA_CRYPTO_TRANSACTION_DESTROY_KEY. */ |
| 286 | struct psa_crypto_transaction_key_s |
| 287 | { |
| 288 | psa_crypto_transaction_type_t type; |
| 289 | uint16_t unused1; |
| 290 | psa_key_lifetime_t lifetime; |
| 291 | psa_key_slot_number_t slot; |
| 292 | psa_key_id_t id; |
| 293 | } key; |
Gilles Peskine | c8336cb | 2019-07-22 19:26:12 +0200 | [diff] [blame] | 294 | } psa_crypto_transaction_t; |
| 295 | |
| 296 | /** The single active transaction. |
| 297 | */ |
| 298 | extern psa_crypto_transaction_t psa_crypto_transaction; |
| 299 | |
| 300 | /** Prepare for a transaction. |
| 301 | * |
| 302 | * There must not be an ongoing transaction. |
| 303 | * |
| 304 | * \param type The type of transaction to start. |
| 305 | */ |
| 306 | static inline void psa_crypto_prepare_transaction( |
| 307 | psa_crypto_transaction_type_t type ) |
| 308 | { |
| 309 | psa_crypto_transaction.unknown.type = type; |
| 310 | } |
| 311 | |
| 312 | /** Save the transaction data to storage. |
| 313 | * |
| 314 | * You may call this function multiple times during a transaction to |
| 315 | * atomically update the transaction state. |
| 316 | * |
| 317 | * \retval #PSA_SUCCESS |
| 318 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 319 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 320 | */ |
| 321 | psa_status_t psa_crypto_save_transaction( void ); |
| 322 | |
| 323 | /** Load the transaction data from storage, if any. |
| 324 | * |
| 325 | * This function is meant to be called from psa_crypto_init() to recover |
| 326 | * in case a transaction was interrupted by a system crash. |
| 327 | * |
| 328 | * \retval #PSA_SUCCESS |
| 329 | * The data about the ongoing transaction has been loaded to |
| 330 | * #psa_crypto_transaction. |
| 331 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 332 | * There is no ongoing transaction. |
| 333 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 334 | */ |
| 335 | psa_status_t psa_crypto_load_transaction( void ); |
| 336 | |
| 337 | /** Indicate that the current transaction is finished. |
| 338 | * |
| 339 | * Call this function at the very end of transaction processing, whether |
| 340 | * the transaction has been committed or aborted. |
| 341 | * |
| 342 | * This function erases the transaction data in storage (if any) and |
| 343 | * resets the transaction data in memory. |
| 344 | * |
| 345 | * \retval #PSA_SUCCESS |
| 346 | * There was transaction data in storage. |
| 347 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 348 | * There was no transaction data in storage. |
| 349 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 350 | * It was impossible to determine whether there was transaction data |
| 351 | * in storage, or the transaction data could not be erased. |
| 352 | */ |
| 353 | psa_status_t psa_crypto_stop_transaction( void ); |
| 354 | |
| 355 | /** The ITS file identifier for the transaction data. |
| 356 | * |
| 357 | * 0xffffffNN = special file; 0x74 = 't' for transaction. |
| 358 | */ |
| 359 | #define PSA_CRYPTO_ITS_TRANSACTION_UID ( (psa_key_id_t) 0xffffff74 ) |
| 360 | |
| 361 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 362 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 363 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 364 | /** Backend side of mbedtls_psa_inject_entropy(). |
| 365 | * |
| 366 | * This function stores the supplied data into the entropy seed file. |
| 367 | * |
| 368 | * \retval #PSA_SUCCESS |
| 369 | * Success |
| 370 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 371 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 372 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 373 | * The entropy seed file already exists. |
| 374 | */ |
| 375 | psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed, |
| 376 | size_t seed_size ); |
| 377 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 378 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 379 | #ifdef __cplusplus |
| 380 | } |
| 381 | #endif |
| 382 | |
| 383 | #endif /* PSA_CRYPTO_STORAGE_H */ |