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 | |
| 32 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 33 | * in each of its header files. */ |
| 34 | #if defined(MBEDTLS_CONFIG_FILE) |
| 35 | #include MBEDTLS_CONFIG_FILE |
| 36 | #else |
| 37 | #include "mbedtls/config.h" |
| 38 | #endif |
| 39 | |
| 40 | #include "psa/crypto.h" |
| 41 | #include <stdint.h> |
| 42 | |
| 43 | /* Limit the maximum key size to 30kB (just in case someone tries to |
| 44 | * inadvertently store an obscene amount of data) */ |
| 45 | #define PSA_CRYPTO_MAX_STORAGE_SIZE ( 30 * 1024 ) |
| 46 | |
Gilles Peskine | 4886812 | 2018-12-10 17:30:29 +0100 | [diff] [blame] | 47 | /** The maximum permitted persistent slot number. |
| 48 | * |
| 49 | * In Mbed Crypto 0.1.0b: |
| 50 | * - Using the file backend, all key ids are ok except 0. |
| 51 | * - Using the ITS backend, all key ids are ok except 0xFFFFFF52 |
| 52 | * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the |
| 53 | * device's random seed (if this feature is enabled). |
| 54 | * - Only key ids from 1 to #PSA_KEY_SLOT_COUNT are actually used. |
| 55 | * |
| 56 | * Since we need to preserve the random seed, avoid using that key slot. |
| 57 | * Reserve a whole range of key slots just in case something else comes up. |
| 58 | * |
| 59 | * This limitation will probably become moot when we implement client |
| 60 | * separation for key storage. |
| 61 | */ |
Gilles Peskine | e988a66 | 2019-02-18 17:33:52 +0100 | [diff] [blame] | 62 | #define PSA_MAX_PERSISTENT_KEY_IDENTIFIER 0xfffeffff |
Gilles Peskine | 4886812 | 2018-12-10 17:30:29 +0100 | [diff] [blame] | 63 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 64 | /** |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame] | 65 | * \brief Checks if persistent data is stored for the given key slot number |
| 66 | * |
| 67 | * This function checks if any key data or metadata exists for the key slot in |
| 68 | * the persistent storage. |
| 69 | * |
| 70 | * \param key Persistent identifier to check. |
| 71 | * |
| 72 | * \retval 0 |
| 73 | * No persistent data present for slot number |
| 74 | * \retval 1 |
| 75 | * Persistent data present for slot number |
| 76 | */ |
| 77 | int psa_is_key_present_in_storage( const psa_key_file_id_t key ); |
| 78 | |
| 79 | /** |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 80 | * \brief Format key data and metadata and save to a location for given key |
| 81 | * slot. |
| 82 | * |
| 83 | * This function formats the key data and metadata and saves it to a |
| 84 | * persistent storage backend. The storage location corresponding to the |
| 85 | * key slot must be empty, otherwise this function will fail. This function |
| 86 | * should be called after psa_import_key_into_slot() to ensure the |
| 87 | * persistent key is not saved into a storage location corresponding to an |
| 88 | * already occupied non-persistent key, as well as validating the key data. |
| 89 | * |
| 90 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 91 | * \param key Persistent identifier of the key to be stored. This |
| 92 | * should be an unoccupied storage location. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 93 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 94 | * \param[in] policy The key policy to save. |
| 95 | * \param[in] data Buffer containing the key data. |
| 96 | * \param data_length The number of bytes that make up the key data. |
| 97 | * |
| 98 | * \retval PSA_SUCCESS |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 99 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 100 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 101 | * \retval PSA_ERROR_STORAGE_FAILURE |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 102 | * \retval PSA_ERROR_ALREADY_EXISTS |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 103 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 104 | 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] | 105 | const psa_key_type_t type, |
| 106 | const psa_key_policy_t *policy, |
| 107 | const uint8_t *data, |
| 108 | const size_t data_length ); |
| 109 | |
| 110 | /** |
| 111 | * \brief Parses key data and metadata and load persistent key for given |
| 112 | * key slot number. |
| 113 | * |
| 114 | * This function reads from a storage backend, parses the key data and |
| 115 | * metadata and writes them to the appropriate output parameters. |
| 116 | * |
| 117 | * Note: This function allocates a buffer and returns a pointer to it through |
| 118 | * the data parameter. psa_free_persistent_key_data() must be called after |
| 119 | * this function to zeroize and free this buffer, regardless of whether this |
| 120 | * function succeeds or fails. |
| 121 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 122 | * \param key Persistent identifier of the key to be loaded. This |
| 123 | * should be an occupied storage location. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 124 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX |
| 125 | * value). |
| 126 | * \param[out] policy On success, the key's policy. |
| 127 | * \param[out] data Pointer to an allocated key data buffer on return. |
| 128 | * \param[out] data_length The number of bytes that make up the key data. |
| 129 | * |
| 130 | * \retval PSA_SUCCESS |
| 131 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 132 | * \retval PSA_ERROR_STORAGE_FAILURE |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 133 | * \retval PSA_ERROR_DOES_NOT_EXIST |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 134 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 135 | 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] | 136 | psa_key_type_t *type, |
| 137 | psa_key_policy_t *policy, |
| 138 | uint8_t **data, |
| 139 | size_t *data_length ); |
| 140 | |
| 141 | /** |
| 142 | * \brief Remove persistent data for the given key slot number. |
| 143 | * |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 144 | * \param key Persistent identifier of the key to remove |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 145 | * from persistent storage. |
| 146 | * |
| 147 | * \retval PSA_SUCCESS |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 148 | * The key was successfully removed, |
| 149 | * or the key did not exist. |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 150 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 151 | */ |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 152 | 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] | 153 | |
| 154 | /** |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 155 | * \brief Free the temporary buffer allocated by psa_load_persistent_key(). |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 156 | * |
| 157 | * This function must be called at some point after psa_load_persistent_key() |
| 158 | * to zeroize and free the memory allocated to the buffer in that function. |
| 159 | * |
| 160 | * \param key_data Buffer for the key data. |
| 161 | * \param key_data_length Size of the key data buffer. |
| 162 | * |
| 163 | */ |
| 164 | void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length ); |
| 165 | |
| 166 | /** |
| 167 | * \brief Formats key data and metadata for persistent storage |
| 168 | * |
| 169 | * \param[in] data Buffer for the key data. |
| 170 | * \param data_length Length of the key data buffer. |
| 171 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 172 | * \param policy The key policy. |
| 173 | * \param[out] storage_data Output buffer for the formatted data. |
| 174 | * |
| 175 | */ |
| 176 | void psa_format_key_data_for_storage( const uint8_t *data, |
| 177 | const size_t data_length, |
| 178 | const psa_key_type_t type, |
| 179 | const psa_key_policy_t *policy, |
| 180 | uint8_t *storage_data ); |
| 181 | |
| 182 | /** |
| 183 | * \brief Parses persistent storage data into key data and metadata |
| 184 | * |
| 185 | * \param[in] storage_data Buffer for the storage data. |
| 186 | * \param storage_data_length Length of the storage data buffer |
| 187 | * \param[out] key_data On output, pointer to a newly allocated buffer |
| 188 | * containing the key data. This must be freed |
| 189 | * using psa_free_persistent_key_data() |
| 190 | * \param[out] key_data_length Length of the key data buffer |
| 191 | * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 192 | * \param[out] policy The key policy. |
| 193 | * |
| 194 | * \retval PSA_SUCCESS |
| 195 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 196 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 197 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 198 | */ |
| 199 | psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, |
| 200 | size_t storage_data_length, |
| 201 | uint8_t **key_data, |
| 202 | size_t *key_data_length, |
| 203 | psa_key_type_t *type, |
| 204 | psa_key_policy_t *policy ); |
| 205 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame^] | 206 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 207 | /** Backend side of mbedtls_psa_inject_entropy(). |
| 208 | * |
| 209 | * This function stores the supplied data into the entropy seed file. |
| 210 | * |
| 211 | * \retval #PSA_SUCCESS |
| 212 | * Success |
| 213 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 214 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 215 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 216 | * The entropy seed file already exists. |
| 217 | */ |
| 218 | psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed, |
| 219 | size_t seed_size ); |
| 220 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 221 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 222 | #ifdef __cplusplus |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | #endif /* PSA_CRYPTO_STORAGE_H */ |