0.2.0-dev Download ZIP
201 lines ·7.8 KB Raw
| 1 | /** |
| 2 | * \file psa_crypto_storage.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS key storage |
| 5 | */ |
| 6 | /* |
| 7 | * Copyright The Mbed TLS Contributors |
| 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | #ifndef TF_PSA_CRYPTO_PSA_CRYPTO_STORAGE_H |
| 12 | #define TF_PSA_CRYPTO_PSA_CRYPTO_STORAGE_H |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | #include "psa/crypto.h" |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* Limit the maximum key size in storage. */ |
| 24 | #if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS) |
| 25 | /* Reflect the maximum size for the key buffer. */ |
| 26 | #define PSA_CRYPTO_MAX_STORAGE_SIZE (MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE) |
| 27 | #else |
| 28 | /* Just set an upper boundary but it should have no effect since the key size |
| 29 | * is limited in memory. */ |
| 30 | #define PSA_CRYPTO_MAX_STORAGE_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_KEY_BITS)) |
| 31 | #endif |
| 32 | |
| 33 | /* Sanity check: a file size must fit in 32 bits. Allow a generous |
| 34 | * 64kB of metadata. */ |
| 35 | #if PSA_CRYPTO_MAX_STORAGE_SIZE > 0xffff0000 |
| 36 | #error "PSA_CRYPTO_MAX_STORAGE_SIZE > 0xffff0000" |
| 37 | #endif |
| 38 | |
| 39 | /** The maximum permitted persistent slot number. |
| 40 | * |
| 41 | * In Mbed Crypto 0.1.0b: |
| 42 | * - Using the file backend, all key ids are ok except 0. |
| 43 | * - Using the ITS backend, all key ids are ok except 0xFFFFFF52 |
| 44 | * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the |
| 45 | * device's random seed (if this feature is enabled). |
| 46 | * - Only key ids from 1 to #MBEDTLS_PSA_KEY_SLOT_COUNT are actually used. |
| 47 | * |
| 48 | * Since we need to preserve the random seed, avoid using that key slot. |
| 49 | * Reserve a whole range of key slots just in case something else comes up. |
| 50 | * |
| 51 | * This limitation will probably become moot when we implement client |
| 52 | * separation for key storage. |
| 53 | */ |
| 54 | #define PSA_MAX_PERSISTENT_KEY_IDENTIFIER PSA_KEY_ID_VENDOR_MAX |
| 55 | |
| 56 | /** |
| 57 | * \brief Checks if persistent data is stored for the given key slot number |
| 58 | * |
| 59 | * This function checks if any key data or metadata exists for the key slot in |
| 60 | * the persistent storage. |
| 61 | * |
| 62 | * \param key Persistent identifier to check. |
| 63 | * |
| 64 | * \retval 0 |
| 65 | * No persistent data present for slot number |
| 66 | * \retval 1 |
| 67 | * Persistent data present for slot number |
| 68 | */ |
| 69 | int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key); |
| 70 | |
| 71 | /** |
| 72 | * \brief Format key data and metadata and save to a location for given key |
| 73 | * slot. |
| 74 | * |
| 75 | * This function formats the key data and metadata and saves it to a |
| 76 | * persistent storage backend. The storage location corresponding to the |
| 77 | * key slot must be empty, otherwise this function will fail. This function |
| 78 | * should be called after loading the key into an internal slot to ensure the |
| 79 | * persistent key is not saved into a storage location corresponding to an |
| 80 | * already occupied non-persistent key, as well as ensuring the key data is |
| 81 | * validated. |
| 82 | * |
| 83 | * Note: This function will only succeed for key buffers which are not |
| 84 | * empty. If passed a NULL pointer or zero-length, the function will fail |
| 85 | * with #PSA_ERROR_INVALID_ARGUMENT. |
| 86 | * |
| 87 | * \param[in] attr The attributes of the key to save. |
| 88 | * The key identifier field in the attributes |
| 89 | * determines the key's location. |
| 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 \emptydescription |
| 94 | * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription |
| 95 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 96 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 97 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 98 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 99 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 100 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 101 | */ |
| 102 | psa_status_t psa_save_persistent_key(const psa_key_attributes_t *attr, |
| 103 | const uint8_t *data, |
| 104 | const size_t data_length); |
| 105 | |
| 106 | /** |
| 107 | * \brief Parses key data and metadata and load persistent key for given |
| 108 | * key slot number. |
| 109 | * |
| 110 | * This function reads from a storage backend, parses the key data and |
| 111 | * metadata and writes them to the appropriate output parameters. |
| 112 | * |
| 113 | * Note: This function allocates a buffer and returns a pointer to it through |
| 114 | * the data parameter. On successful return, the pointer is guaranteed to be |
| 115 | * valid and the buffer contains at least one byte of data. |
| 116 | * psa_free_persistent_key_data() must be called on the data buffer |
| 117 | * afterwards to zeroize and free this buffer. |
| 118 | * |
| 119 | * \param[in,out] attr On input, the key identifier field identifies |
| 120 | * the key to load. Other fields are ignored. |
| 121 | * On success, the attribute structure contains |
| 122 | * the key metadata that was loaded from storage. |
| 123 | * \param[out] data Pointer to an allocated key data buffer on return. |
| 124 | * \param[out] data_length The number of bytes that make up the key data. |
| 125 | * |
| 126 | * \retval #PSA_SUCCESS \emptydescription |
| 127 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 128 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 129 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 130 | * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription |
| 131 | */ |
| 132 | psa_status_t psa_load_persistent_key(psa_key_attributes_t *attr, |
| 133 | uint8_t **data, |
| 134 | size_t *data_length); |
| 135 | |
| 136 | /** |
| 137 | * \brief Remove persistent data for the given key slot number. |
| 138 | * |
| 139 | * \param key Persistent identifier of the key to remove |
| 140 | * from persistent storage. |
| 141 | * |
| 142 | * \retval #PSA_SUCCESS |
| 143 | * The key was successfully removed, |
| 144 | * or the key did not exist. |
| 145 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 146 | */ |
| 147 | psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key); |
| 148 | |
| 149 | /** |
| 150 | * \brief Free the temporary buffer allocated by psa_load_persistent_key(). |
| 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 containing the key data. |
| 165 | * \param data_length Length of the key data buffer. |
| 166 | * \param[in] attr The core attributes of the key. |
| 167 | * \param[out] storage_data Output buffer for the formatted data. |
| 168 | * |
| 169 | */ |
| 170 | void psa_format_key_data_for_storage(const uint8_t *data, |
| 171 | const size_t data_length, |
| 172 | const psa_key_attributes_t *attr, |
| 173 | uint8_t *storage_data); |
| 174 | |
| 175 | /** |
| 176 | * \brief Parses persistent storage data into key data and metadata |
| 177 | * |
| 178 | * \param[in] storage_data Buffer for the storage data. |
| 179 | * \param storage_data_length Length of the storage data buffer |
| 180 | * \param[out] key_data On output, pointer to a newly allocated buffer |
| 181 | * containing the key data. This must be freed |
| 182 | * using psa_free_persistent_key_data() |
| 183 | * \param[out] key_data_length Length of the key data buffer |
| 184 | * \param[out] attr On success, the attribute structure is filled |
| 185 | * with the loaded key metadata. |
| 186 | * |
| 187 | * \retval #PSA_SUCCESS \emptydescription |
| 188 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 189 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 190 | */ |
| 191 | psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data, |
| 192 | size_t storage_data_length, |
| 193 | uint8_t **key_data, |
| 194 | size_t *key_data_length, |
| 195 | psa_key_attributes_t *attr); |
| 196 | |
| 197 | #ifdef __cplusplus |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | #endif /* TF_PSA_CRYPTO_PSA_CRYPTO_STORAGE_H */ |