Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto_extra.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS vendor extensions |
Gilles Peskine | 07c91f5 | 2018-06-28 18:02:53 +0200 | [diff] [blame] | 5 | * |
| 6 | * \note This file may not be included directly. Applications must |
| 7 | * include psa/crypto.h. |
| 8 | * |
| 9 | * This file is reserved for vendor-specific definitions. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 10 | */ |
| 11 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 12 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 13 | * SPDX-License-Identifier: Apache-2.0 |
| 14 | * |
| 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 16 | * not use this file except in compliance with the License. |
| 17 | * You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, software |
| 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | * See the License for the specific language governing permissions and |
| 25 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #ifndef PSA_CRYPTO_EXTRA_H |
| 29 | #define PSA_CRYPTO_EXTRA_H |
| 30 | |
Jaeden Amero | 81cefed | 2019-02-25 08:51:27 +0000 | [diff] [blame] | 31 | #include "mbedtls/platform_util.h" |
| 32 | |
Gilles Peskine | 9501bd7 | 2021-11-25 20:30:47 +0100 | [diff] [blame] | 33 | #include "crypto_types.h" |
Gilles Peskine | 7a894f2 | 2019-11-26 16:06:46 +0100 | [diff] [blame] | 34 | #include "crypto_compat.h" |
| 35 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 40 | /* UID for secure storage seed */ |
avolinski | 0d2c266 | 2018-11-21 17:31:07 +0200 | [diff] [blame] | 41 | #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52 |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 42 | |
Steven Cooreman | 1f968fd | 2021-02-15 14:00:24 +0100 | [diff] [blame] | 43 | /* See config.h for definition */ |
Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 44 | #if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT) |
| 45 | #define MBEDTLS_PSA_KEY_SLOT_COUNT 32 |
Steven Cooreman | 1f968fd | 2021-02-15 14:00:24 +0100 | [diff] [blame] | 46 | #endif |
Jaeden Amero | 5e6d24c | 2019-02-21 10:41:29 +0000 | [diff] [blame] | 47 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 48 | /** \addtogroup attributes |
| 49 | * @{ |
| 50 | */ |
| 51 | |
| 52 | /** \brief Declare the enrollment algorithm for a key. |
| 53 | * |
| 54 | * An operation on a key may indifferently use the algorithm set with |
| 55 | * psa_set_key_algorithm() or with this function. |
| 56 | * |
| 57 | * \param[out] attributes The attribute structure to write to. |
| 58 | * \param alg2 A second algorithm that the key may be used |
| 59 | * for, in addition to the algorithm set with |
| 60 | * psa_set_key_algorithm(). |
| 61 | * |
| 62 | * \warning Setting an enrollment algorithm is not recommended, because |
| 63 | * using the same key with different algorithms can allow some |
| 64 | * attacks based on arithmetic relations between different |
| 65 | * computations made with the same key, or can escalate harmless |
| 66 | * side channels into exploitable ones. Use this function only |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 67 | * if it is necessary to support a protocol for which it has been |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 68 | * verified that the usage of the key with multiple algorithms |
| 69 | * is safe. |
| 70 | */ |
| 71 | static inline void psa_set_key_enrollment_algorithm( |
| 72 | psa_key_attributes_t *attributes, |
| 73 | psa_algorithm_t alg2) |
| 74 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 75 | attributes->core.policy.alg2 = alg2; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** Retrieve the enrollment algorithm policy from key attributes. |
| 79 | * |
| 80 | * \param[in] attributes The key attribute structure to query. |
| 81 | * |
| 82 | * \return The enrollment algorithm stored in the attribute structure. |
| 83 | */ |
| 84 | static inline psa_algorithm_t psa_get_key_enrollment_algorithm( |
| 85 | const psa_key_attributes_t *attributes) |
| 86 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 87 | return( attributes->core.policy.alg2 ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 90 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 91 | |
| 92 | /** Retrieve the slot number where a key is stored. |
| 93 | * |
| 94 | * A slot number is only defined for keys that are stored in a secure |
| 95 | * element. |
| 96 | * |
| 97 | * This information is only useful if the secure element is not entirely |
| 98 | * managed through the PSA Cryptography API. It is up to the secure |
| 99 | * element driver to decide how PSA slot numbers map to any other interface |
| 100 | * that the secure element may have. |
| 101 | * |
| 102 | * \param[in] attributes The key attribute structure to query. |
| 103 | * \param[out] slot_number On success, the slot number containing the key. |
| 104 | * |
| 105 | * \retval #PSA_SUCCESS |
| 106 | * The key is located in a secure element, and \p *slot_number |
| 107 | * indicates the slot number that contains it. |
| 108 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 109 | * The caller is not permitted to query the slot number. |
| 110 | * Mbed Crypto currently does not return this error. |
| 111 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 112 | * The key is not located in a secure element. |
| 113 | */ |
| 114 | psa_status_t psa_get_key_slot_number( |
| 115 | const psa_key_attributes_t *attributes, |
| 116 | psa_key_slot_number_t *slot_number ); |
| 117 | |
| 118 | /** Choose the slot number where a key is stored. |
| 119 | * |
| 120 | * This function declares a slot number in the specified attribute |
| 121 | * structure. |
| 122 | * |
| 123 | * A slot number is only meaningful for keys that are stored in a secure |
| 124 | * element. It is up to the secure element driver to decide how PSA slot |
| 125 | * numbers map to any other interface that the secure element may have. |
| 126 | * |
| 127 | * \note Setting a slot number in key attributes for a key creation can |
| 128 | * cause the following errors when creating the key: |
| 129 | * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does |
| 130 | * not support choosing a specific slot number. |
| 131 | * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to |
| 132 | * choose slot numbers in general or to choose this specific slot. |
| 133 | * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not |
| 134 | * valid in general or not valid for this specific key. |
| 135 | * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the |
| 136 | * selected slot. |
| 137 | * |
| 138 | * \param[out] attributes The attribute structure to write to. |
| 139 | * \param slot_number The slot number to set. |
| 140 | */ |
| 141 | static inline void psa_set_key_slot_number( |
| 142 | psa_key_attributes_t *attributes, |
| 143 | psa_key_slot_number_t slot_number ) |
| 144 | { |
| 145 | attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER; |
| 146 | attributes->slot_number = slot_number; |
| 147 | } |
| 148 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 149 | /** Remove the slot number attribute from a key attribute structure. |
| 150 | * |
| 151 | * This function undoes the action of psa_set_key_slot_number(). |
| 152 | * |
| 153 | * \param[out] attributes The attribute structure to write to. |
| 154 | */ |
| 155 | static inline void psa_clear_key_slot_number( |
| 156 | psa_key_attributes_t *attributes ) |
| 157 | { |
| 158 | attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER; |
| 159 | } |
| 160 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 161 | /** Register a key that is already present in a secure element. |
| 162 | * |
| 163 | * The key must be located in a secure element designated by the |
| 164 | * lifetime field in \p attributes, in the slot set with |
| 165 | * psa_set_key_slot_number() in the attribute structure. |
| 166 | * This function makes the key available through the key identifier |
| 167 | * specified in \p attributes. |
| 168 | * |
| 169 | * \param[in] attributes The attributes of the existing key. |
| 170 | * |
| 171 | * \retval #PSA_SUCCESS |
| 172 | * The key was successfully registered. |
| 173 | * Note that depending on the design of the driver, this may or may |
| 174 | * not guarantee that a key actually exists in the designated slot |
| 175 | * and is compatible with the specified attributes. |
| 176 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 177 | * There is already a key with the identifier specified in |
| 178 | * \p attributes. |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 179 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 180 | * The secure element driver for the specified lifetime does not |
| 181 | * support registering a key. |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 182 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Ronald Cron | d3b458c | 2021-03-31 17:51:29 +0200 | [diff] [blame] | 183 | * The identifier in \p attributes is invalid, namely the identifier is |
Andrzej Kurek | fcaef2e | 2022-02-04 07:54:59 -0500 | [diff] [blame^] | 184 | * not in the user range, or |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 185 | * \p attributes specifies a lifetime which is not located |
Andrzej Kurek | fcaef2e | 2022-02-04 07:54:59 -0500 | [diff] [blame^] | 186 | * in a secure element, or no slot number is specified in \p attributes, |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 187 | * or the specified slot number is not valid. |
| 188 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 189 | * The caller is not authorized to register the specified key slot. |
| 190 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 191 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 192 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 193 | * \retval #PSA_ERROR_DATA_INVALID |
| 194 | * \retval #PSA_ERROR_DATA_CORRUPT |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 195 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 196 | * \retval #PSA_ERROR_BAD_STATE |
| 197 | * The library has not been previously initialized by psa_crypto_init(). |
| 198 | * It is implementation-dependent whether a failure to initialize |
| 199 | * results in this error code. |
| 200 | */ |
| 201 | psa_status_t mbedtls_psa_register_se_key( |
| 202 | const psa_key_attributes_t *attributes); |
| 203 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 204 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 205 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 206 | /**@}*/ |
| 207 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 208 | /** |
| 209 | * \brief Library deinitialization. |
| 210 | * |
| 211 | * This function clears all data associated with the PSA layer, |
| 212 | * including the whole key store. |
| 213 | * |
| 214 | * This is an Mbed TLS extension. |
| 215 | */ |
| 216 | void mbedtls_psa_crypto_free( void ); |
| 217 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 218 | /** \brief Statistics about |
| 219 | * resource consumption related to the PSA keystore. |
| 220 | * |
| 221 | * \note The content of this structure is not part of the stable API and ABI |
| 222 | * of Mbed Crypto and may change arbitrarily from version to version. |
| 223 | */ |
| 224 | typedef struct mbedtls_psa_stats_s |
| 225 | { |
| 226 | /** Number of slots containing key material for a volatile key. */ |
| 227 | size_t volatile_slots; |
| 228 | /** Number of slots containing key material for a key which is in |
| 229 | * internal persistent storage. */ |
| 230 | size_t persistent_slots; |
| 231 | /** Number of slots containing a reference to a key in a |
| 232 | * secure element. */ |
| 233 | size_t external_slots; |
| 234 | /** Number of slots which are occupied, but do not contain |
| 235 | * key material yet. */ |
| 236 | size_t half_filled_slots; |
| 237 | /** Number of slots that contain cache data. */ |
| 238 | size_t cache_slots; |
| 239 | /** Number of slots that are not used for anything. */ |
| 240 | size_t empty_slots; |
Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame] | 241 | /** Number of slots that are locked. */ |
| 242 | size_t locked_slots; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 243 | /** Largest key id value among open keys in internal persistent storage. */ |
Ronald Cron | 039a98b | 2020-07-23 16:07:42 +0200 | [diff] [blame] | 244 | psa_key_id_t max_open_internal_key_id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 245 | /** Largest key id value among open keys in secure elements. */ |
Ronald Cron | 039a98b | 2020-07-23 16:07:42 +0200 | [diff] [blame] | 246 | psa_key_id_t max_open_external_key_id; |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 247 | } mbedtls_psa_stats_t; |
| 248 | |
| 249 | /** \brief Get statistics about |
| 250 | * resource consumption related to the PSA keystore. |
| 251 | * |
| 252 | * \note When Mbed Crypto is built as part of a service, with isolation |
| 253 | * between the application and the keystore, the service may or |
| 254 | * may not expose this function. |
| 255 | */ |
| 256 | void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 257 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 258 | /** |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 259 | * \brief Inject an initial entropy seed for the random generator into |
| 260 | * secure storage. |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 261 | * |
| 262 | * This function injects data to be used as a seed for the random generator |
| 263 | * used by the PSA Crypto implementation. On devices that lack a trusted |
| 264 | * entropy source (preferably a hardware random number generator), |
| 265 | * the Mbed PSA Crypto implementation uses this value to seed its |
| 266 | * random generator. |
| 267 | * |
| 268 | * On devices without a trusted entropy source, this function must be |
| 269 | * called exactly once in the lifetime of the device. On devices with |
| 270 | * a trusted entropy source, calling this function is optional. |
| 271 | * In all cases, this function may only be called before calling any |
| 272 | * other function in the PSA Crypto API, including psa_crypto_init(). |
| 273 | * |
| 274 | * When this function returns successfully, it populates a file in |
| 275 | * persistent storage. Once the file has been created, this function |
| 276 | * can no longer succeed. |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 277 | * |
| 278 | * If any error occurs, this function does not change the system state. |
| 279 | * You can call this function again after correcting the reason for the |
| 280 | * error if possible. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 281 | * |
| 282 | * \warning This function **can** fail! Callers MUST check the return status. |
| 283 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 284 | * \warning If you use this function, you should use it as part of a |
| 285 | * factory provisioning process. The value of the injected seed |
| 286 | * is critical to the security of the device. It must be |
| 287 | * *secret*, *unpredictable* and (statistically) *unique per device*. |
| 288 | * You should be generate it randomly using a cryptographically |
| 289 | * secure random generator seeded from trusted entropy sources. |
| 290 | * You should transmit it securely to the device and ensure |
| 291 | * that its value is not leaked or stored anywhere beyond the |
| 292 | * needs of transmitting it from the point of generation to |
| 293 | * the call of this function, and erase all copies of the value |
| 294 | * once this function returns. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 295 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 296 | * This is an Mbed TLS extension. |
| 297 | * |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 298 | * \note This function is only available on the following platforms: |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 299 | * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled. |
| 300 | * Note that you must provide compatible implementations of |
| 301 | * mbedtls_nv_seed_read and mbedtls_nv_seed_write. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 302 | * * In a client-server integration of PSA Cryptography, on the client side, |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 303 | * if the server supports this feature. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 304 | * \param[in] seed Buffer containing the seed value to inject. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 305 | * \param[in] seed_size Size of the \p seed buffer. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 306 | * The size of the seed in bytes must be greater |
| 307 | * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM |
| 308 | * and #MBEDTLS_ENTROPY_BLOCK_SIZE. |
| 309 | * It must be less or equal to |
| 310 | * #MBEDTLS_ENTROPY_MAX_SEED_SIZE. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 311 | * |
| 312 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 313 | * The seed value was injected successfully. The random generator |
| 314 | * of the PSA Crypto implementation is now ready for use. |
| 315 | * You may now call psa_crypto_init() and use the PSA Crypto |
| 316 | * implementation. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 317 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 318 | * \p seed_size is out of range. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 319 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 320 | * There was a failure reading or writing from storage. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 321 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 322 | * The library has already been initialized. It is no longer |
| 323 | * possible to call this function. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 324 | */ |
Jaeden Amero | c7529c9 | 2019-08-19 11:08:04 +0100 | [diff] [blame] | 325 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 326 | size_t seed_size); |
| 327 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 328 | /** \addtogroup crypto_types |
| 329 | * @{ |
| 330 | */ |
| 331 | |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 332 | /** DSA public key. |
| 333 | * |
| 334 | * The import and export format is the |
| 335 | * representation of the public key `y = g^x mod p` as a big-endian byte |
| 336 | * string. The length of the byte string is the length of the base prime `p` |
| 337 | * in bytes. |
| 338 | */ |
Gilles Peskine | 7cfcb3f | 2019-12-04 18:58:44 +0100 | [diff] [blame] | 339 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 340 | |
| 341 | /** DSA key pair (private and public key). |
| 342 | * |
| 343 | * The import and export format is the |
| 344 | * representation of the private key `x` as a big-endian byte string. The |
| 345 | * length of the byte string is the private key size in bytes (leading zeroes |
| 346 | * are not stripped). |
| 347 | * |
| 348 | * Determinstic DSA key derivation with psa_generate_derived_key follows |
| 349 | * FIPS 186-4 §B.1.2: interpret the byte string as integer |
| 350 | * in big-endian order. Discard it if it is not in the range |
| 351 | * [0, *N* - 2] where *N* is the boundary of the private key domain |
| 352 | * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA, |
| 353 | * or the order of the curve's base point for ECC). |
| 354 | * Add 1 to the resulting integer and use this as the private key *x*. |
| 355 | * |
| 356 | */ |
Gilles Peskine | 7cfcb3f | 2019-12-04 18:58:44 +0100 | [diff] [blame] | 357 | #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 358 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 359 | /** Whether a key type is an DSA key (pair or public-only). */ |
| 360 | #define PSA_KEY_TYPE_IS_DSA(type) \ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 361 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 362 | |
Bence Szépkúti | a294551 | 2020-12-03 21:40:17 +0100 | [diff] [blame] | 363 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x06000400) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 364 | /** DSA signature with hashing. |
| 365 | * |
| 366 | * This is the signature scheme defined by FIPS 186-4, |
| 367 | * with a random per-message secret number (*k*). |
| 368 | * |
| 369 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 370 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 371 | * This includes #PSA_ALG_ANY_HASH |
| 372 | * when specifying the algorithm in a usage policy. |
| 373 | * |
| 374 | * \return The corresponding DSA signature algorithm. |
| 375 | * \return Unspecified if \p hash_alg is not a supported |
| 376 | * hash algorithm. |
| 377 | */ |
| 378 | #define PSA_ALG_DSA(hash_alg) \ |
| 379 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
Bence Szépkúti | a294551 | 2020-12-03 21:40:17 +0100 | [diff] [blame] | 380 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x06000500) |
Gilles Peskine | 972630e | 2019-11-29 11:55:48 +0100 | [diff] [blame] | 381 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 382 | /** Deterministic DSA signature with hashing. |
| 383 | * |
| 384 | * This is the deterministic variant defined by RFC 6979 of |
| 385 | * the signature scheme defined by FIPS 186-4. |
| 386 | * |
| 387 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 388 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 389 | * This includes #PSA_ALG_ANY_HASH |
| 390 | * when specifying the algorithm in a usage policy. |
| 391 | * |
| 392 | * \return The corresponding DSA signature algorithm. |
| 393 | * \return Unspecified if \p hash_alg is not a supported |
| 394 | * hash algorithm. |
| 395 | */ |
| 396 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 397 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 398 | #define PSA_ALG_IS_DSA(alg) \ |
| 399 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 400 | PSA_ALG_DSA_BASE) |
| 401 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 402 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
| 403 | #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \ |
| 404 | (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 405 | #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \ |
| 406 | (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 407 | |
| 408 | |
| 409 | /* We need to expand the sample definition of this macro from |
| 410 | * the API definition. */ |
Gilles Peskine | 6d40085 | 2021-02-24 21:39:52 +0100 | [diff] [blame] | 411 | #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN |
| 412 | #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \ |
| 413 | PSA_ALG_IS_DSA(alg) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 414 | |
| 415 | /**@}*/ |
| 416 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 417 | /** \addtogroup attributes |
| 418 | * @{ |
| 419 | */ |
| 420 | |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 421 | /** Custom Diffie-Hellman group. |
| 422 | * |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 423 | * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or |
| 424 | * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 425 | * from domain parameters set by psa_set_key_domain_parameters(). |
| 426 | */ |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 427 | #define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e) |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 428 | |
| 429 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 430 | /** |
| 431 | * \brief Set domain parameters for a key. |
| 432 | * |
| 433 | * Some key types require additional domain parameters in addition to |
| 434 | * the key type identifier and the key size. Use this function instead |
| 435 | * of psa_set_key_type() when you need to specify domain parameters. |
| 436 | * |
| 437 | * The format for the required domain parameters varies based on the key type. |
| 438 | * |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 439 | * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR), |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 440 | * the domain parameter data consists of the public exponent, |
| 441 | * represented as a big-endian integer with no leading zeros. |
| 442 | * This information is used when generating an RSA key pair. |
| 443 | * When importing a key, the public exponent is read from the imported |
| 444 | * key data and the exponent recorded in the attribute structure is ignored. |
| 445 | * As an exception, the public exponent 65537 is represented by an empty |
| 446 | * byte string. |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 447 | * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR), |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 448 | * the `Dss-Parms` format as defined by RFC 3279 §2.3.2. |
| 449 | * ``` |
| 450 | * Dss-Parms ::= SEQUENCE { |
| 451 | * p INTEGER, |
| 452 | * q INTEGER, |
| 453 | * g INTEGER |
| 454 | * } |
| 455 | * ``` |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 456 | * - For Diffie-Hellman key exchange keys |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 457 | * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or |
| 458 | * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 459 | * `DomainParameters` format as defined by RFC 3279 §2.3.3. |
| 460 | * ``` |
| 461 | * DomainParameters ::= SEQUENCE { |
| 462 | * p INTEGER, -- odd prime, p=jq +1 |
| 463 | * g INTEGER, -- generator, g |
| 464 | * q INTEGER, -- factor of p-1 |
| 465 | * j INTEGER OPTIONAL, -- subgroup factor |
| 466 | * validationParms ValidationParms OPTIONAL |
| 467 | * } |
| 468 | * ValidationParms ::= SEQUENCE { |
| 469 | * seed BIT STRING, |
| 470 | * pgenCounter INTEGER |
| 471 | * } |
| 472 | * ``` |
| 473 | * |
| 474 | * \note This function may allocate memory or other resources. |
| 475 | * Once you have called this function on an attribute structure, |
| 476 | * you must call psa_reset_key_attributes() to free these resources. |
| 477 | * |
| 478 | * \note This is an experimental extension to the interface. It may change |
| 479 | * in future versions of the library. |
| 480 | * |
| 481 | * \param[in,out] attributes Attribute structure where the specified domain |
| 482 | * parameters will be stored. |
| 483 | * If this function fails, the content of |
| 484 | * \p attributes is not modified. |
| 485 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 486 | * \param[in] data Buffer containing the key domain parameters. |
| 487 | * The content of this buffer is interpreted |
| 488 | * according to \p type as described above. |
| 489 | * \param data_length Size of the \p data buffer in bytes. |
| 490 | * |
| 491 | * \retval #PSA_SUCCESS |
| 492 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 493 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 494 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 495 | */ |
| 496 | psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
| 497 | psa_key_type_t type, |
| 498 | const uint8_t *data, |
| 499 | size_t data_length); |
| 500 | |
| 501 | /** |
| 502 | * \brief Get domain parameters for a key. |
| 503 | * |
| 504 | * Get the domain parameters for a key with this function, if any. The format |
| 505 | * of the domain parameters written to \p data is specified in the |
| 506 | * documentation for psa_set_key_domain_parameters(). |
| 507 | * |
| 508 | * \note This is an experimental extension to the interface. It may change |
| 509 | * in future versions of the library. |
| 510 | * |
| 511 | * \param[in] attributes The key attribute structure to query. |
| 512 | * \param[out] data On success, the key domain parameters. |
| 513 | * \param data_size Size of the \p data buffer in bytes. |
| 514 | * The buffer is guaranteed to be large |
| 515 | * enough if its size in bytes is at least |
| 516 | * the value given by |
| 517 | * PSA_KEY_DOMAIN_PARAMETERS_SIZE(). |
| 518 | * \param[out] data_length On success, the number of bytes |
| 519 | * that make up the key domain parameters data. |
| 520 | * |
| 521 | * \retval #PSA_SUCCESS |
| 522 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 523 | */ |
| 524 | psa_status_t psa_get_key_domain_parameters( |
| 525 | const psa_key_attributes_t *attributes, |
| 526 | uint8_t *data, |
| 527 | size_t data_size, |
| 528 | size_t *data_length); |
| 529 | |
| 530 | /** Safe output buffer size for psa_get_key_domain_parameters(). |
| 531 | * |
| 532 | * This macro returns a compile-time constant if its arguments are |
| 533 | * compile-time constants. |
| 534 | * |
| 535 | * \warning This function may call its arguments multiple times or |
| 536 | * zero times, so you should not pass arguments that contain |
| 537 | * side effects. |
| 538 | * |
| 539 | * \note This is an experimental extension to the interface. It may change |
| 540 | * in future versions of the library. |
| 541 | * |
| 542 | * \param key_type A supported key type. |
| 543 | * \param key_bits The size of the key in bits. |
| 544 | * |
| 545 | * \return If the parameters are valid and supported, return |
| 546 | * a buffer size in bytes that guarantees that |
| 547 | * psa_get_key_domain_parameters() will not fail with |
| 548 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 549 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 550 | * by the implementation, this macro shall return either a |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 551 | * sensible size or 0. |
| 552 | * If the parameters are not valid, the |
| 553 | * return value is unspecified. |
| 554 | */ |
| 555 | #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \ |
| 556 | (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \ |
| 557 | PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 558 | PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 559 | 0) |
| 560 | #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 561 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/) |
| 562 | #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 563 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/) |
| 564 | |
| 565 | /**@}*/ |
| 566 | |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 567 | /** \defgroup psa_tls_helpers TLS helper functions |
| 568 | * @{ |
| 569 | */ |
| 570 | |
| 571 | #if defined(MBEDTLS_ECP_C) |
| 572 | #include <mbedtls/ecp.h> |
| 573 | |
| 574 | /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA. |
| 575 | * |
| 576 | * \note This function is provided solely for the convenience of |
| 577 | * Mbed TLS and may be removed at any time without notice. |
| 578 | * |
| 579 | * \param grpid An Mbed TLS elliptic curve identifier |
| 580 | * (`MBEDTLS_ECP_DP_xxx`). |
| 581 | * \param[out] bits On success, the bit size of the curve. |
| 582 | * |
| 583 | * \return The corresponding PSA elliptic curve identifier |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 584 | * (`PSA_ECC_FAMILY_xxx`). |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 585 | * \return \c 0 on failure (\p grpid is not recognized). |
| 586 | */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 587 | static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid, |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 588 | size_t *bits ) |
| 589 | { |
| 590 | switch( grpid ) |
| 591 | { |
| 592 | case MBEDTLS_ECP_DP_SECP192R1: |
| 593 | *bits = 192; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 594 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 595 | case MBEDTLS_ECP_DP_SECP224R1: |
| 596 | *bits = 224; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 597 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 598 | case MBEDTLS_ECP_DP_SECP256R1: |
| 599 | *bits = 256; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 600 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 601 | case MBEDTLS_ECP_DP_SECP384R1: |
| 602 | *bits = 384; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 603 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 604 | case MBEDTLS_ECP_DP_SECP521R1: |
| 605 | *bits = 521; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 606 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 607 | case MBEDTLS_ECP_DP_BP256R1: |
| 608 | *bits = 256; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 609 | return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 610 | case MBEDTLS_ECP_DP_BP384R1: |
| 611 | *bits = 384; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 612 | return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 613 | case MBEDTLS_ECP_DP_BP512R1: |
| 614 | *bits = 512; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 615 | return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 616 | case MBEDTLS_ECP_DP_CURVE25519: |
| 617 | *bits = 255; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 618 | return( PSA_ECC_FAMILY_MONTGOMERY ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 619 | case MBEDTLS_ECP_DP_SECP192K1: |
| 620 | *bits = 192; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 621 | return( PSA_ECC_FAMILY_SECP_K1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 622 | case MBEDTLS_ECP_DP_SECP224K1: |
| 623 | *bits = 224; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 624 | return( PSA_ECC_FAMILY_SECP_K1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 625 | case MBEDTLS_ECP_DP_SECP256K1: |
| 626 | *bits = 256; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 627 | return( PSA_ECC_FAMILY_SECP_K1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 628 | case MBEDTLS_ECP_DP_CURVE448: |
| 629 | *bits = 448; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 630 | return( PSA_ECC_FAMILY_MONTGOMERY ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 631 | default: |
| 632 | *bits = 0; |
| 633 | return( 0 ); |
| 634 | } |
| 635 | } |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 636 | |
| 637 | /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS. |
| 638 | * |
| 639 | * \note This function is provided solely for the convenience of |
| 640 | * Mbed TLS and may be removed at any time without notice. |
| 641 | * |
| 642 | * \param curve A PSA elliptic curve identifier |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 643 | * (`PSA_ECC_FAMILY_xxx`). |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 644 | * \param bits The bit-length of a private key on \p curve. |
| 645 | * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up |
| 646 | * to the nearest multiple of 8. This allows the caller |
| 647 | * to infer the exact curve from the length of a key |
| 648 | * which is supplied as a byte string. |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 649 | * |
| 650 | * \return The corresponding Mbed TLS elliptic curve identifier |
| 651 | * (`MBEDTLS_ECP_DP_xxx`). |
| 652 | * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized. |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 653 | * \return #MBEDTLS_ECP_DP_NONE if \p bits is not |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 654 | * correct for \p curve. |
| 655 | */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 656 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 657 | size_t bits, |
| 658 | int bits_is_sloppy ); |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 659 | #endif /* MBEDTLS_ECP_C */ |
| 660 | |
| 661 | /**@}*/ |
| 662 | |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 663 | /** \defgroup psa_external_rng External random generator |
| 664 | * @{ |
| 665 | */ |
| 666 | |
| 667 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 668 | /** External random generator function, implemented by the platform. |
| 669 | * |
| 670 | * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, |
| 671 | * this function replaces Mbed TLS's entropy and DRBG modules for all |
| 672 | * random generation triggered via PSA crypto interfaces. |
| 673 | * |
Gilles Peskine | b663a60 | 2020-11-18 15:27:37 +0100 | [diff] [blame] | 674 | * \note This random generator must deliver random numbers with cryptographic |
| 675 | * quality and high performance. It must supply unpredictable numbers |
| 676 | * with a uniform distribution. The implementation of this function |
| 677 | * is responsible for ensuring that the random generator is seeded |
| 678 | * with sufficient entropy. If you have a hardware TRNG which is slow |
| 679 | * or delivers non-uniform output, declare it as an entropy source |
| 680 | * with mbedtls_entropy_add_source() instead of enabling this option. |
| 681 | * |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 682 | * \param[in,out] context Pointer to the random generator context. |
| 683 | * This is all-bits-zero on the first call |
| 684 | * and preserved between successive calls. |
| 685 | * \param[out] output Output buffer. On success, this buffer |
| 686 | * contains random data with a uniform |
| 687 | * distribution. |
| 688 | * \param output_size The size of the \p output buffer in bytes. |
| 689 | * \param[out] output_length On success, set this value to \p output_size. |
| 690 | * |
| 691 | * \retval #PSA_SUCCESS |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 692 | * Success. The output buffer contains \p output_size bytes of |
| 693 | * cryptographic-quality random data, and \c *output_length is |
| 694 | * set to \p output_size. |
| 695 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 696 | * The random generator requires extra entropy and there is no |
| 697 | * way to obtain entropy under current environment conditions. |
| 698 | * This error should not happen under normal circumstances since |
| 699 | * this function is responsible for obtaining as much entropy as |
| 700 | * it needs. However implementations of this function may return |
| 701 | * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain |
| 702 | * entropy without blocking indefinitely. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 703 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 704 | * A failure of the random generator hardware that isn't covered |
| 705 | * by #PSA_ERROR_INSUFFICIENT_ENTROPY. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 706 | */ |
| 707 | psa_status_t mbedtls_psa_external_get_random( |
| 708 | mbedtls_psa_external_random_context_t *context, |
| 709 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 710 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 711 | |
| 712 | /**@}*/ |
| 713 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 714 | /** \defgroup psa_builtin_keys Built-in keys |
| 715 | * @{ |
| 716 | */ |
| 717 | |
| 718 | /** The minimum value for a key identifier that is built into the |
| 719 | * implementation. |
| 720 | * |
| 721 | * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN |
| 722 | * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from |
| 723 | * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect |
| 724 | * with any other set of implementation-chosen key identifiers. |
| 725 | * |
| 726 | * This value is part of the library's ABI since changing it would invalidate |
| 727 | * the values of built-in key identifiers in applications. |
| 728 | */ |
| 729 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t)0x7fff0000) |
| 730 | |
| 731 | /** The maximum value for a key identifier that is built into the |
| 732 | * implementation. |
| 733 | * |
| 734 | * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information. |
| 735 | */ |
| 736 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t)0x7fffefff) |
| 737 | |
| 738 | /** A slot number identifying a key in a driver. |
| 739 | * |
| 740 | * Values of this type are used to identify built-in keys. |
| 741 | */ |
| 742 | typedef uint64_t psa_drv_slot_number_t; |
| 743 | |
| 744 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 745 | /** Test whether a key identifier belongs to the builtin key range. |
| 746 | * |
| 747 | * \param key_id Key identifier to test. |
| 748 | * |
| 749 | * \retval 1 |
| 750 | * The key identifier is a builtin key identifier. |
| 751 | * \retval 0 |
| 752 | * The key identifier is not a builtin key identifier. |
| 753 | */ |
| 754 | static inline int psa_key_id_is_builtin( psa_key_id_t key_id ) |
| 755 | { |
| 756 | return( ( key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ) && |
| 757 | ( key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ) ); |
| 758 | } |
| 759 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 760 | /** Platform function to obtain the location and slot number of a built-in key. |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 761 | * |
| 762 | * An application-specific implementation of this function must be provided if |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 763 | * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 764 | * as part of a platform's system image. |
| 765 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 766 | * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 767 | * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX. |
| 768 | * |
| 769 | * In a multi-application configuration |
| 770 | * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined), |
| 771 | * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id) |
| 772 | * is allowed to use the given key. |
| 773 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 774 | * \param key_id The key ID for which to retrieve the |
| 775 | * location and slot attributes. |
| 776 | * \param[out] lifetime On success, the lifetime associated with the key |
| 777 | * corresponding to \p key_id. Lifetime is a |
| 778 | * combination of which driver contains the key, |
Steven Cooreman | 31e27af | 2021-04-14 10:32:05 +0200 | [diff] [blame] | 779 | * and with what persistence level the key is |
| 780 | * intended to be used. If the platform |
| 781 | * implementation does not contain specific |
| 782 | * information about the intended key persistence |
| 783 | * level, the persistence level may be reported as |
| 784 | * #PSA_KEY_PERSISTENCE_DEFAULT. |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 785 | * \param[out] slot_number On success, the slot number known to the driver |
| 786 | * registered at the lifetime location reported |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 787 | * through \p lifetime which corresponds to the |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 788 | * requested built-in key. |
| 789 | * |
| 790 | * \retval #PSA_SUCCESS |
| 791 | * The requested key identifier designates a built-in key. |
| 792 | * In a multi-application configuration, the requested owner |
| 793 | * is allowed to access it. |
| 794 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 795 | * The requested key identifier is not a built-in key which is known |
| 796 | * to this function. If a key exists in the key storage with this |
| 797 | * identifier, the data from the storage will be used. |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 798 | * \return (any other error) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 799 | * Any other error is propagated to the function that requested the key. |
| 800 | * Common errors include: |
| 801 | * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner |
| 802 | * is not allowed to access it. |
| 803 | */ |
| 804 | psa_status_t mbedtls_psa_platform_get_builtin_key( |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 805 | mbedtls_svc_key_id_t key_id, |
| 806 | psa_key_lifetime_t *lifetime, |
| 807 | psa_drv_slot_number_t *slot_number ); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 808 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 809 | |
| 810 | /** @} */ |
| 811 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 812 | #ifdef __cplusplus |
| 813 | } |
| 814 | #endif |
| 815 | |
| 816 | #endif /* PSA_CRYPTO_EXTRA_H */ |