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 |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 30 | #include "mbedtls/private_access.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | |
Jaeden Amero | 81cefed | 2019-02-25 08:51:27 +0000 | [diff] [blame] | 32 | #include "mbedtls/platform_util.h" |
| 33 | |
Gilles Peskine | 09c02ee | 2021-11-25 20:30:47 +0100 | [diff] [blame] | 34 | #include "crypto_types.h" |
Gilles Peskine | 7a894f2 | 2019-11-26 16:06:46 +0100 | [diff] [blame] | 35 | #include "crypto_compat.h" |
| 36 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 41 | /* UID for secure storage seed */ |
avolinski | 0d2c266 | 2018-11-21 17:31:07 +0200 | [diff] [blame] | 42 | #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52 |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 43 | |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 44 | /* See mbedtls_config.h for definition */ |
Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 45 | #if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT) |
| 46 | #define MBEDTLS_PSA_KEY_SLOT_COUNT 32 |
Steven Cooreman | 1f968fd | 2021-02-15 14:00:24 +0100 | [diff] [blame] | 47 | #endif |
Jaeden Amero | 5e6d24c | 2019-02-21 10:41:29 +0000 | [diff] [blame] | 48 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 49 | /** \addtogroup attributes |
| 50 | * @{ |
| 51 | */ |
| 52 | |
| 53 | /** \brief Declare the enrollment algorithm for a key. |
| 54 | * |
| 55 | * An operation on a key may indifferently use the algorithm set with |
| 56 | * psa_set_key_algorithm() or with this function. |
| 57 | * |
| 58 | * \param[out] attributes The attribute structure to write to. |
| 59 | * \param alg2 A second algorithm that the key may be used |
| 60 | * for, in addition to the algorithm set with |
| 61 | * psa_set_key_algorithm(). |
| 62 | * |
| 63 | * \warning Setting an enrollment algorithm is not recommended, because |
| 64 | * using the same key with different algorithms can allow some |
| 65 | * attacks based on arithmetic relations between different |
| 66 | * computations made with the same key, or can escalate harmless |
| 67 | * side channels into exploitable ones. Use this function only |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 68 | * 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] | 69 | * verified that the usage of the key with multiple algorithms |
| 70 | * is safe. |
| 71 | */ |
| 72 | static inline void psa_set_key_enrollment_algorithm( |
| 73 | psa_key_attributes_t *attributes, |
| 74 | psa_algorithm_t alg2) |
| 75 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 76 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** Retrieve the enrollment algorithm policy from key attributes. |
| 80 | * |
| 81 | * \param[in] attributes The key attribute structure to query. |
| 82 | * |
| 83 | * \return The enrollment algorithm stored in the attribute structure. |
| 84 | */ |
| 85 | static inline psa_algorithm_t psa_get_key_enrollment_algorithm( |
| 86 | const psa_key_attributes_t *attributes) |
| 87 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 88 | return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 91 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 92 | |
| 93 | /** Retrieve the slot number where a key is stored. |
| 94 | * |
| 95 | * A slot number is only defined for keys that are stored in a secure |
| 96 | * element. |
| 97 | * |
| 98 | * This information is only useful if the secure element is not entirely |
| 99 | * managed through the PSA Cryptography API. It is up to the secure |
| 100 | * element driver to decide how PSA slot numbers map to any other interface |
| 101 | * that the secure element may have. |
| 102 | * |
| 103 | * \param[in] attributes The key attribute structure to query. |
| 104 | * \param[out] slot_number On success, the slot number containing the key. |
| 105 | * |
| 106 | * \retval #PSA_SUCCESS |
| 107 | * The key is located in a secure element, and \p *slot_number |
| 108 | * indicates the slot number that contains it. |
| 109 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 110 | * The caller is not permitted to query the slot number. |
| 111 | * Mbed Crypto currently does not return this error. |
| 112 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 113 | * The key is not located in a secure element. |
| 114 | */ |
| 115 | psa_status_t psa_get_key_slot_number( |
| 116 | const psa_key_attributes_t *attributes, |
| 117 | psa_key_slot_number_t *slot_number ); |
| 118 | |
| 119 | /** Choose the slot number where a key is stored. |
| 120 | * |
| 121 | * This function declares a slot number in the specified attribute |
| 122 | * structure. |
| 123 | * |
| 124 | * A slot number is only meaningful for keys that are stored in a secure |
| 125 | * element. It is up to the secure element driver to decide how PSA slot |
| 126 | * numbers map to any other interface that the secure element may have. |
| 127 | * |
| 128 | * \note Setting a slot number in key attributes for a key creation can |
| 129 | * cause the following errors when creating the key: |
| 130 | * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does |
| 131 | * not support choosing a specific slot number. |
| 132 | * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to |
| 133 | * choose slot numbers in general or to choose this specific slot. |
| 134 | * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not |
| 135 | * valid in general or not valid for this specific key. |
| 136 | * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the |
| 137 | * selected slot. |
| 138 | * |
| 139 | * \param[out] attributes The attribute structure to write to. |
| 140 | * \param slot_number The slot number to set. |
| 141 | */ |
| 142 | static inline void psa_set_key_slot_number( |
| 143 | psa_key_attributes_t *attributes, |
| 144 | psa_key_slot_number_t slot_number ) |
| 145 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 146 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER; |
| 147 | attributes->MBEDTLS_PRIVATE(slot_number) = slot_number; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 150 | /** Remove the slot number attribute from a key attribute structure. |
| 151 | * |
| 152 | * This function undoes the action of psa_set_key_slot_number(). |
| 153 | * |
| 154 | * \param[out] attributes The attribute structure to write to. |
| 155 | */ |
| 156 | static inline void psa_clear_key_slot_number( |
| 157 | psa_key_attributes_t *attributes ) |
| 158 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 159 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER; |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 162 | /** Register a key that is already present in a secure element. |
| 163 | * |
| 164 | * The key must be located in a secure element designated by the |
| 165 | * lifetime field in \p attributes, in the slot set with |
| 166 | * psa_set_key_slot_number() in the attribute structure. |
| 167 | * This function makes the key available through the key identifier |
| 168 | * specified in \p attributes. |
| 169 | * |
| 170 | * \param[in] attributes The attributes of the existing key. |
| 171 | * |
| 172 | * \retval #PSA_SUCCESS |
| 173 | * The key was successfully registered. |
| 174 | * Note that depending on the design of the driver, this may or may |
| 175 | * not guarantee that a key actually exists in the designated slot |
| 176 | * and is compatible with the specified attributes. |
| 177 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 178 | * There is already a key with the identifier specified in |
| 179 | * \p attributes. |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 180 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 181 | * The secure element driver for the specified lifetime does not |
| 182 | * support registering a key. |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 183 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Ronald Cron | d3b458c | 2021-03-31 17:51:29 +0200 | [diff] [blame] | 184 | * The identifier in \p attributes is invalid, namely the identifier is |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 185 | * not in the user range, or |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 186 | * \p attributes specifies a lifetime which is not located |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 187 | * 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] | 188 | * or the specified slot number is not valid. |
| 189 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 190 | * The caller is not authorized to register the specified key slot. |
| 191 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 192 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 193 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 194 | * \retval #PSA_ERROR_DATA_INVALID |
| 195 | * \retval #PSA_ERROR_DATA_CORRUPT |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 196 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 197 | * \retval #PSA_ERROR_BAD_STATE |
| 198 | * The library has not been previously initialized by psa_crypto_init(). |
| 199 | * It is implementation-dependent whether a failure to initialize |
| 200 | * results in this error code. |
| 201 | */ |
| 202 | psa_status_t mbedtls_psa_register_se_key( |
| 203 | const psa_key_attributes_t *attributes); |
| 204 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 205 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 206 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 207 | /**@}*/ |
| 208 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 209 | /** |
| 210 | * \brief Library deinitialization. |
| 211 | * |
| 212 | * This function clears all data associated with the PSA layer, |
| 213 | * including the whole key store. |
| 214 | * |
| 215 | * This is an Mbed TLS extension. |
| 216 | */ |
| 217 | void mbedtls_psa_crypto_free( void ); |
| 218 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 219 | /** \brief Statistics about |
| 220 | * resource consumption related to the PSA keystore. |
| 221 | * |
| 222 | * \note The content of this structure is not part of the stable API and ABI |
| 223 | * of Mbed Crypto and may change arbitrarily from version to version. |
| 224 | */ |
| 225 | typedef struct mbedtls_psa_stats_s |
| 226 | { |
| 227 | /** Number of slots containing key material for a volatile key. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 228 | size_t MBEDTLS_PRIVATE(volatile_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 229 | /** Number of slots containing key material for a key which is in |
| 230 | * internal persistent storage. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 231 | size_t MBEDTLS_PRIVATE(persistent_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 232 | /** Number of slots containing a reference to a key in a |
| 233 | * secure element. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 234 | size_t MBEDTLS_PRIVATE(external_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 235 | /** Number of slots which are occupied, but do not contain |
| 236 | * key material yet. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 237 | size_t MBEDTLS_PRIVATE(half_filled_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 238 | /** Number of slots that contain cache data. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 239 | size_t MBEDTLS_PRIVATE(cache_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 240 | /** Number of slots that are not used for anything. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 241 | size_t MBEDTLS_PRIVATE(empty_slots); |
Ronald Cron | 1ad1eee | 2020-11-15 14:21:04 +0100 | [diff] [blame] | 242 | /** Number of slots that are locked. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 243 | size_t MBEDTLS_PRIVATE(locked_slots); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 244 | /** Largest key id value among open keys in internal persistent storage. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 245 | psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 246 | /** Largest key id value among open keys in secure elements. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 247 | psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id); |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 248 | } mbedtls_psa_stats_t; |
| 249 | |
| 250 | /** \brief Get statistics about |
| 251 | * resource consumption related to the PSA keystore. |
| 252 | * |
| 253 | * \note When Mbed Crypto is built as part of a service, with isolation |
| 254 | * between the application and the keystore, the service may or |
| 255 | * may not expose this function. |
| 256 | */ |
| 257 | void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 258 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 259 | /** |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 260 | * \brief Inject an initial entropy seed for the random generator into |
| 261 | * secure storage. |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 262 | * |
| 263 | * This function injects data to be used as a seed for the random generator |
| 264 | * used by the PSA Crypto implementation. On devices that lack a trusted |
| 265 | * entropy source (preferably a hardware random number generator), |
| 266 | * the Mbed PSA Crypto implementation uses this value to seed its |
| 267 | * random generator. |
| 268 | * |
| 269 | * On devices without a trusted entropy source, this function must be |
| 270 | * called exactly once in the lifetime of the device. On devices with |
| 271 | * a trusted entropy source, calling this function is optional. |
| 272 | * In all cases, this function may only be called before calling any |
| 273 | * other function in the PSA Crypto API, including psa_crypto_init(). |
| 274 | * |
| 275 | * When this function returns successfully, it populates a file in |
| 276 | * persistent storage. Once the file has been created, this function |
| 277 | * can no longer succeed. |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 278 | * |
| 279 | * If any error occurs, this function does not change the system state. |
| 280 | * You can call this function again after correcting the reason for the |
| 281 | * error if possible. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 282 | * |
| 283 | * \warning This function **can** fail! Callers MUST check the return status. |
| 284 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 285 | * \warning If you use this function, you should use it as part of a |
| 286 | * factory provisioning process. The value of the injected seed |
| 287 | * is critical to the security of the device. It must be |
| 288 | * *secret*, *unpredictable* and (statistically) *unique per device*. |
| 289 | * You should be generate it randomly using a cryptographically |
| 290 | * secure random generator seeded from trusted entropy sources. |
| 291 | * You should transmit it securely to the device and ensure |
| 292 | * that its value is not leaked or stored anywhere beyond the |
| 293 | * needs of transmitting it from the point of generation to |
| 294 | * the call of this function, and erase all copies of the value |
| 295 | * once this function returns. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 296 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 297 | * This is an Mbed TLS extension. |
| 298 | * |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 299 | * \note This function is only available on the following platforms: |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 300 | * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled. |
| 301 | * Note that you must provide compatible implementations of |
| 302 | * mbedtls_nv_seed_read and mbedtls_nv_seed_write. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 303 | * * In a client-server integration of PSA Cryptography, on the client side, |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 304 | * if the server supports this feature. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 305 | * \param[in] seed Buffer containing the seed value to inject. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 306 | * \param[in] seed_size Size of the \p seed buffer. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 307 | * The size of the seed in bytes must be greater |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 308 | * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE |
| 309 | * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM |
| 310 | * in `library/entropy_poll.h` in the Mbed TLS source |
| 311 | * code. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 312 | * It must be less or equal to |
| 313 | * #MBEDTLS_ENTROPY_MAX_SEED_SIZE. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 314 | * |
| 315 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 316 | * The seed value was injected successfully. The random generator |
| 317 | * of the PSA Crypto implementation is now ready for use. |
| 318 | * You may now call psa_crypto_init() and use the PSA Crypto |
| 319 | * implementation. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 320 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 321 | * \p seed_size is out of range. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 322 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 323 | * There was a failure reading or writing from storage. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 324 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 325 | * The library has already been initialized. It is no longer |
| 326 | * possible to call this function. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 327 | */ |
Jaeden Amero | c7529c9 | 2019-08-19 11:08:04 +0100 | [diff] [blame] | 328 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 329 | size_t seed_size); |
| 330 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 331 | /** \addtogroup crypto_types |
| 332 | * @{ |
| 333 | */ |
| 334 | |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 335 | /** DSA public key. |
| 336 | * |
| 337 | * The import and export format is the |
| 338 | * representation of the public key `y = g^x mod p` as a big-endian byte |
| 339 | * string. The length of the byte string is the length of the base prime `p` |
| 340 | * in bytes. |
| 341 | */ |
Gilles Peskine | 7cfcb3f | 2019-12-04 18:58:44 +0100 | [diff] [blame] | 342 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 343 | |
| 344 | /** DSA key pair (private and public key). |
| 345 | * |
| 346 | * The import and export format is the |
| 347 | * representation of the private key `x` as a big-endian byte string. The |
| 348 | * length of the byte string is the private key size in bytes (leading zeroes |
| 349 | * are not stripped). |
| 350 | * |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 351 | * Deterministic DSA key derivation with psa_generate_derived_key follows |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 352 | * FIPS 186-4 §B.1.2: interpret the byte string as integer |
| 353 | * in big-endian order. Discard it if it is not in the range |
| 354 | * [0, *N* - 2] where *N* is the boundary of the private key domain |
| 355 | * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA, |
| 356 | * or the order of the curve's base point for ECC). |
| 357 | * Add 1 to the resulting integer and use this as the private key *x*. |
| 358 | * |
| 359 | */ |
Gilles Peskine | 7cfcb3f | 2019-12-04 18:58:44 +0100 | [diff] [blame] | 360 | #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 361 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 362 | /** Whether a key type is an DSA key (pair or public-only). */ |
| 363 | #define PSA_KEY_TYPE_IS_DSA(type) \ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 364 | (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] | 365 | |
Bence Szépkúti | a294551 | 2020-12-03 21:40:17 +0100 | [diff] [blame] | 366 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x06000400) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 367 | /** DSA signature with hashing. |
| 368 | * |
| 369 | * This is the signature scheme defined by FIPS 186-4, |
| 370 | * with a random per-message secret number (*k*). |
| 371 | * |
| 372 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 373 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 374 | * This includes #PSA_ALG_ANY_HASH |
| 375 | * when specifying the algorithm in a usage policy. |
| 376 | * |
| 377 | * \return The corresponding DSA signature algorithm. |
| 378 | * \return Unspecified if \p hash_alg is not a supported |
| 379 | * hash algorithm. |
| 380 | */ |
| 381 | #define PSA_ALG_DSA(hash_alg) \ |
| 382 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
Bence Szépkúti | a294551 | 2020-12-03 21:40:17 +0100 | [diff] [blame] | 383 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x06000500) |
Gilles Peskine | 972630e | 2019-11-29 11:55:48 +0100 | [diff] [blame] | 384 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 385 | /** Deterministic DSA signature with hashing. |
| 386 | * |
| 387 | * This is the deterministic variant defined by RFC 6979 of |
| 388 | * the signature scheme defined by FIPS 186-4. |
| 389 | * |
| 390 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 391 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 392 | * This includes #PSA_ALG_ANY_HASH |
| 393 | * when specifying the algorithm in a usage policy. |
| 394 | * |
| 395 | * \return The corresponding DSA signature algorithm. |
| 396 | * \return Unspecified if \p hash_alg is not a supported |
| 397 | * hash algorithm. |
| 398 | */ |
| 399 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 400 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 401 | #define PSA_ALG_IS_DSA(alg) \ |
| 402 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 403 | PSA_ALG_DSA_BASE) |
| 404 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 405 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
| 406 | #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \ |
| 407 | (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 408 | #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \ |
| 409 | (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 410 | |
| 411 | |
| 412 | /* We need to expand the sample definition of this macro from |
| 413 | * the API definition. */ |
Gilles Peskine | 6d40085 | 2021-02-24 21:39:52 +0100 | [diff] [blame] | 414 | #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN |
| 415 | #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \ |
| 416 | PSA_ALG_IS_DSA(alg) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 417 | |
| 418 | /**@}*/ |
| 419 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 420 | /** \addtogroup attributes |
| 421 | * @{ |
| 422 | */ |
| 423 | |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 424 | /** Custom Diffie-Hellman group. |
| 425 | * |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 426 | * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or |
| 427 | * #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] | 428 | * from domain parameters set by psa_set_key_domain_parameters(). |
| 429 | */ |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 430 | #define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e) |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 431 | |
| 432 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 433 | /** |
| 434 | * \brief Set domain parameters for a key. |
| 435 | * |
| 436 | * Some key types require additional domain parameters in addition to |
| 437 | * the key type identifier and the key size. Use this function instead |
| 438 | * of psa_set_key_type() when you need to specify domain parameters. |
| 439 | * |
| 440 | * The format for the required domain parameters varies based on the key type. |
| 441 | * |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 442 | * - 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] | 443 | * the domain parameter data consists of the public exponent, |
| 444 | * represented as a big-endian integer with no leading zeros. |
| 445 | * This information is used when generating an RSA key pair. |
| 446 | * When importing a key, the public exponent is read from the imported |
| 447 | * key data and the exponent recorded in the attribute structure is ignored. |
| 448 | * As an exception, the public exponent 65537 is represented by an empty |
| 449 | * byte string. |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 450 | * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR), |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 451 | * the `Dss-Params` format as defined by RFC 3279 §2.3.2. |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 452 | * ``` |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 453 | * Dss-Params ::= SEQUENCE { |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 454 | * p INTEGER, |
| 455 | * q INTEGER, |
| 456 | * g INTEGER |
| 457 | * } |
| 458 | * ``` |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 459 | * - For Diffie-Hellman key exchange keys |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 460 | * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or |
| 461 | * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 462 | * `DomainParameters` format as defined by RFC 3279 §2.3.3. |
| 463 | * ``` |
| 464 | * DomainParameters ::= SEQUENCE { |
| 465 | * p INTEGER, -- odd prime, p=jq +1 |
| 466 | * g INTEGER, -- generator, g |
| 467 | * q INTEGER, -- factor of p-1 |
| 468 | * j INTEGER OPTIONAL, -- subgroup factor |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 469 | * validationParams ValidationParams OPTIONAL |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 470 | * } |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 471 | * ValidationParams ::= SEQUENCE { |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 472 | * seed BIT STRING, |
| 473 | * pgenCounter INTEGER |
| 474 | * } |
| 475 | * ``` |
| 476 | * |
| 477 | * \note This function may allocate memory or other resources. |
| 478 | * Once you have called this function on an attribute structure, |
| 479 | * you must call psa_reset_key_attributes() to free these resources. |
| 480 | * |
| 481 | * \note This is an experimental extension to the interface. It may change |
| 482 | * in future versions of the library. |
| 483 | * |
| 484 | * \param[in,out] attributes Attribute structure where the specified domain |
| 485 | * parameters will be stored. |
| 486 | * If this function fails, the content of |
| 487 | * \p attributes is not modified. |
| 488 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 489 | * \param[in] data Buffer containing the key domain parameters. |
| 490 | * The content of this buffer is interpreted |
| 491 | * according to \p type as described above. |
| 492 | * \param data_length Size of the \p data buffer in bytes. |
| 493 | * |
| 494 | * \retval #PSA_SUCCESS |
| 495 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 496 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 497 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 498 | */ |
| 499 | psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
| 500 | psa_key_type_t type, |
| 501 | const uint8_t *data, |
| 502 | size_t data_length); |
| 503 | |
| 504 | /** |
| 505 | * \brief Get domain parameters for a key. |
| 506 | * |
| 507 | * Get the domain parameters for a key with this function, if any. The format |
| 508 | * of the domain parameters written to \p data is specified in the |
| 509 | * documentation for psa_set_key_domain_parameters(). |
| 510 | * |
| 511 | * \note This is an experimental extension to the interface. It may change |
| 512 | * in future versions of the library. |
| 513 | * |
| 514 | * \param[in] attributes The key attribute structure to query. |
| 515 | * \param[out] data On success, the key domain parameters. |
| 516 | * \param data_size Size of the \p data buffer in bytes. |
| 517 | * The buffer is guaranteed to be large |
| 518 | * enough if its size in bytes is at least |
| 519 | * the value given by |
| 520 | * PSA_KEY_DOMAIN_PARAMETERS_SIZE(). |
| 521 | * \param[out] data_length On success, the number of bytes |
| 522 | * that make up the key domain parameters data. |
| 523 | * |
| 524 | * \retval #PSA_SUCCESS |
| 525 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 526 | */ |
| 527 | psa_status_t psa_get_key_domain_parameters( |
| 528 | const psa_key_attributes_t *attributes, |
| 529 | uint8_t *data, |
| 530 | size_t data_size, |
| 531 | size_t *data_length); |
| 532 | |
| 533 | /** Safe output buffer size for psa_get_key_domain_parameters(). |
| 534 | * |
| 535 | * This macro returns a compile-time constant if its arguments are |
| 536 | * compile-time constants. |
| 537 | * |
| 538 | * \warning This function may call its arguments multiple times or |
| 539 | * zero times, so you should not pass arguments that contain |
| 540 | * side effects. |
| 541 | * |
| 542 | * \note This is an experimental extension to the interface. It may change |
| 543 | * in future versions of the library. |
| 544 | * |
| 545 | * \param key_type A supported key type. |
| 546 | * \param key_bits The size of the key in bits. |
| 547 | * |
| 548 | * \return If the parameters are valid and supported, return |
| 549 | * a buffer size in bytes that guarantees that |
| 550 | * psa_get_key_domain_parameters() will not fail with |
| 551 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 552 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 553 | * by the implementation, this macro shall return either a |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 554 | * sensible size or 0. |
| 555 | * If the parameters are not valid, the |
| 556 | * return value is unspecified. |
| 557 | */ |
| 558 | #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \ |
| 559 | (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \ |
| 560 | PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 561 | PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 562 | 0) |
| 563 | #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 564 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/) |
| 565 | #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 566 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/) |
| 567 | |
| 568 | /**@}*/ |
| 569 | |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 570 | /** \defgroup psa_tls_helpers TLS helper functions |
| 571 | * @{ |
| 572 | */ |
| 573 | |
| 574 | #if defined(MBEDTLS_ECP_C) |
| 575 | #include <mbedtls/ecp.h> |
| 576 | |
| 577 | /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA. |
| 578 | * |
| 579 | * \note This function is provided solely for the convenience of |
| 580 | * Mbed TLS and may be removed at any time without notice. |
| 581 | * |
| 582 | * \param grpid An Mbed TLS elliptic curve identifier |
| 583 | * (`MBEDTLS_ECP_DP_xxx`). |
| 584 | * \param[out] bits On success, the bit size of the curve. |
| 585 | * |
| 586 | * \return The corresponding PSA elliptic curve identifier |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 587 | * (`PSA_ECC_FAMILY_xxx`). |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 588 | * \return \c 0 on failure (\p grpid is not recognized). |
| 589 | */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 590 | 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] | 591 | size_t *bits ) |
| 592 | { |
| 593 | switch( grpid ) |
| 594 | { |
| 595 | case MBEDTLS_ECP_DP_SECP192R1: |
| 596 | *bits = 192; |
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_SECP224R1: |
| 599 | *bits = 224; |
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_SECP256R1: |
| 602 | *bits = 256; |
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_SECP384R1: |
| 605 | *bits = 384; |
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_SECP521R1: |
| 608 | *bits = 521; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 609 | return( PSA_ECC_FAMILY_SECP_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 610 | case MBEDTLS_ECP_DP_BP256R1: |
| 611 | *bits = 256; |
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_BP384R1: |
| 614 | *bits = 384; |
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_BP512R1: |
| 617 | *bits = 512; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 618 | return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 619 | case MBEDTLS_ECP_DP_CURVE25519: |
| 620 | *bits = 255; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 621 | return( PSA_ECC_FAMILY_MONTGOMERY ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 622 | case MBEDTLS_ECP_DP_SECP192K1: |
| 623 | *bits = 192; |
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_SECP224K1: |
| 626 | *bits = 224; |
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_SECP256K1: |
| 629 | *bits = 256; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 630 | return( PSA_ECC_FAMILY_SECP_K1 ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 631 | case MBEDTLS_ECP_DP_CURVE448: |
| 632 | *bits = 448; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 633 | return( PSA_ECC_FAMILY_MONTGOMERY ); |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 634 | default: |
| 635 | *bits = 0; |
| 636 | return( 0 ); |
| 637 | } |
| 638 | } |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 639 | |
| 640 | /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS. |
| 641 | * |
| 642 | * \note This function is provided solely for the convenience of |
| 643 | * Mbed TLS and may be removed at any time without notice. |
| 644 | * |
| 645 | * \param curve A PSA elliptic curve identifier |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 646 | * (`PSA_ECC_FAMILY_xxx`). |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 647 | * \param bits The bit-length of a private key on \p curve. |
| 648 | * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up |
| 649 | * to the nearest multiple of 8. This allows the caller |
| 650 | * to infer the exact curve from the length of a key |
| 651 | * which is supplied as a byte string. |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 652 | * |
| 653 | * \return The corresponding Mbed TLS elliptic curve identifier |
| 654 | * (`MBEDTLS_ECP_DP_xxx`). |
| 655 | * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized. |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 656 | * \return #MBEDTLS_ECP_DP_NONE if \p bits is not |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 657 | * correct for \p curve. |
| 658 | */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 659 | 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] | 660 | size_t bits, |
| 661 | int bits_is_sloppy ); |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 662 | #endif /* MBEDTLS_ECP_C */ |
| 663 | |
| 664 | /**@}*/ |
| 665 | |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 666 | /** \defgroup psa_external_rng External random generator |
| 667 | * @{ |
| 668 | */ |
| 669 | |
| 670 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 671 | /** External random generator function, implemented by the platform. |
| 672 | * |
| 673 | * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, |
| 674 | * this function replaces Mbed TLS's entropy and DRBG modules for all |
| 675 | * random generation triggered via PSA crypto interfaces. |
| 676 | * |
Gilles Peskine | b663a60 | 2020-11-18 15:27:37 +0100 | [diff] [blame] | 677 | * \note This random generator must deliver random numbers with cryptographic |
| 678 | * quality and high performance. It must supply unpredictable numbers |
| 679 | * with a uniform distribution. The implementation of this function |
| 680 | * is responsible for ensuring that the random generator is seeded |
| 681 | * with sufficient entropy. If you have a hardware TRNG which is slow |
| 682 | * or delivers non-uniform output, declare it as an entropy source |
| 683 | * with mbedtls_entropy_add_source() instead of enabling this option. |
| 684 | * |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 685 | * \param[in,out] context Pointer to the random generator context. |
| 686 | * This is all-bits-zero on the first call |
| 687 | * and preserved between successive calls. |
| 688 | * \param[out] output Output buffer. On success, this buffer |
| 689 | * contains random data with a uniform |
| 690 | * distribution. |
| 691 | * \param output_size The size of the \p output buffer in bytes. |
| 692 | * \param[out] output_length On success, set this value to \p output_size. |
| 693 | * |
| 694 | * \retval #PSA_SUCCESS |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 695 | * Success. The output buffer contains \p output_size bytes of |
| 696 | * cryptographic-quality random data, and \c *output_length is |
| 697 | * set to \p output_size. |
| 698 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 699 | * The random generator requires extra entropy and there is no |
| 700 | * way to obtain entropy under current environment conditions. |
| 701 | * This error should not happen under normal circumstances since |
| 702 | * this function is responsible for obtaining as much entropy as |
| 703 | * it needs. However implementations of this function may return |
| 704 | * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain |
| 705 | * entropy without blocking indefinitely. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 706 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 707 | * A failure of the random generator hardware that isn't covered |
| 708 | * by #PSA_ERROR_INSUFFICIENT_ENTROPY. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 709 | */ |
| 710 | psa_status_t mbedtls_psa_external_get_random( |
| 711 | mbedtls_psa_external_random_context_t *context, |
| 712 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 713 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 714 | |
| 715 | /**@}*/ |
| 716 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 717 | /** \defgroup psa_builtin_keys Built-in keys |
| 718 | * @{ |
| 719 | */ |
| 720 | |
| 721 | /** The minimum value for a key identifier that is built into the |
| 722 | * implementation. |
| 723 | * |
| 724 | * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN |
| 725 | * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from |
| 726 | * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect |
| 727 | * with any other set of implementation-chosen key identifiers. |
| 728 | * |
| 729 | * This value is part of the library's ABI since changing it would invalidate |
| 730 | * the values of built-in key identifiers in applications. |
| 731 | */ |
| 732 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t)0x7fff0000) |
| 733 | |
| 734 | /** The maximum value for a key identifier that is built into the |
| 735 | * implementation. |
| 736 | * |
| 737 | * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information. |
| 738 | */ |
| 739 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t)0x7fffefff) |
| 740 | |
| 741 | /** A slot number identifying a key in a driver. |
| 742 | * |
| 743 | * Values of this type are used to identify built-in keys. |
| 744 | */ |
| 745 | typedef uint64_t psa_drv_slot_number_t; |
| 746 | |
| 747 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 748 | /** Test whether a key identifier belongs to the builtin key range. |
| 749 | * |
| 750 | * \param key_id Key identifier to test. |
| 751 | * |
| 752 | * \retval 1 |
| 753 | * The key identifier is a builtin key identifier. |
| 754 | * \retval 0 |
| 755 | * The key identifier is not a builtin key identifier. |
| 756 | */ |
| 757 | static inline int psa_key_id_is_builtin( psa_key_id_t key_id ) |
| 758 | { |
| 759 | return( ( key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ) && |
| 760 | ( key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ) ); |
| 761 | } |
| 762 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 763 | /** 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] | 764 | * |
| 765 | * An application-specific implementation of this function must be provided if |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 766 | * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 767 | * as part of a platform's system image. |
| 768 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 769 | * #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] | 770 | * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX. |
| 771 | * |
| 772 | * In a multi-application configuration |
| 773 | * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined), |
| 774 | * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id) |
| 775 | * is allowed to use the given key. |
| 776 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 777 | * \param key_id The key ID for which to retrieve the |
| 778 | * location and slot attributes. |
| 779 | * \param[out] lifetime On success, the lifetime associated with the key |
| 780 | * corresponding to \p key_id. Lifetime is a |
| 781 | * combination of which driver contains the key, |
Steven Cooreman | 31e27af | 2021-04-14 10:32:05 +0200 | [diff] [blame] | 782 | * and with what persistence level the key is |
| 783 | * intended to be used. If the platform |
| 784 | * implementation does not contain specific |
| 785 | * information about the intended key persistence |
| 786 | * level, the persistence level may be reported as |
| 787 | * #PSA_KEY_PERSISTENCE_DEFAULT. |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 788 | * \param[out] slot_number On success, the slot number known to the driver |
| 789 | * registered at the lifetime location reported |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 790 | * through \p lifetime which corresponds to the |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 791 | * requested built-in key. |
| 792 | * |
| 793 | * \retval #PSA_SUCCESS |
| 794 | * The requested key identifier designates a built-in key. |
| 795 | * In a multi-application configuration, the requested owner |
| 796 | * is allowed to access it. |
| 797 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 798 | * The requested key identifier is not a built-in key which is known |
| 799 | * to this function. If a key exists in the key storage with this |
| 800 | * identifier, the data from the storage will be used. |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 801 | * \return (any other error) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 802 | * Any other error is propagated to the function that requested the key. |
| 803 | * Common errors include: |
| 804 | * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner |
| 805 | * is not allowed to access it. |
| 806 | */ |
| 807 | psa_status_t mbedtls_psa_platform_get_builtin_key( |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 808 | mbedtls_svc_key_id_t key_id, |
| 809 | psa_key_lifetime_t *lifetime, |
| 810 | psa_drv_slot_number_t *slot_number ); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 811 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 812 | |
| 813 | /** @} */ |
| 814 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 815 | /** \addtogroup crypto_types |
| 816 | * @{ |
| 817 | */ |
| 818 | |
| 819 | #define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t)0x0a000000) |
| 820 | |
| 821 | /** Whether the specified algorithm is a password-authenticated key exchange. |
| 822 | * |
| 823 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 824 | * |
| 825 | * \return 1 if \p alg is a password-authenticated key exchange (PAKE) |
| 826 | * algorithm, 0 otherwise. |
| 827 | * This macro may return either 0 or 1 if \p alg is not a supported |
| 828 | * algorithm identifier. |
| 829 | */ |
| 830 | #define PSA_ALG_IS_PAKE(alg) \ |
| 831 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE) |
| 832 | |
| 833 | /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm. |
| 834 | * |
| 835 | * This is J-PAKE as defined by RFC 8236, instantiated with the following |
| 836 | * parameters: |
| 837 | * |
| 838 | * - The group can be either an elliptic curve or defined over a finite field. |
| 839 | * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the |
| 840 | * J-PAKE algorithm. |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 841 | * - A cryptographic hash function. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 842 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 843 | * To select these parameters and set up the cipher suite, call these functions |
| 844 | * in any order: |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 845 | * |
| 846 | * \code |
| 847 | * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE); |
| 848 | * psa_pake_cs_set_primitive(cipher_suite, |
| 849 | * PSA_PAKE_PRIMITIVE(type, family, bits)); |
| 850 | * psa_pake_cs_set_hash(cipher_suite, hash); |
| 851 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 852 | * |
| 853 | * For more information on how to set a specific curve or field, refer to the |
| 854 | * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants. |
| 855 | * |
| 856 | * After initializing a J-PAKE operation, call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 857 | * |
| 858 | * \code |
| 859 | * psa_pake_setup(operation, cipher_suite); |
| 860 | * psa_pake_set_user(operation, ...); |
| 861 | * psa_pake_set_peer(operation, ...); |
| 862 | * psa_pake_set_password_key(operation, ...); |
| 863 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 864 | * |
Janos Follath | 559f05e | 2021-05-26 15:44:30 +0100 | [diff] [blame] | 865 | * The password is read as a byte array and must be non-empty. This can be the |
| 866 | * password itself (in some pre-defined character encoding) or some value |
| 867 | * derived from the password as mandated by some higher level protocol. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 868 | * |
| 869 | * (The implementation converts this byte array to a number as described in |
| 870 | * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_ |
| 871 | * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here |
| 872 | * \c q is order of the group defined by the primitive set in the cipher suite. |
| 873 | * The \c psa_pake_set_password_xxx() functions return an error if the result |
| 874 | * of the reduction is 0.) |
| 875 | * |
| 876 | * The key exchange flow for J-PAKE is as follows: |
| 877 | * -# To get the first round data that needs to be sent to the peer, call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 878 | * \code |
| 879 | * // Get g1 |
| 880 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 881 | * // Get the ZKP public key for x1 |
| 882 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 883 | * // Get the ZKP proof for x1 |
| 884 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 885 | * // Get g2 |
| 886 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 887 | * // Get the ZKP public key for x2 |
| 888 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 889 | * // Get the ZKP proof for x2 |
| 890 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 891 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 892 | * -# To provide the first round data received from the peer to the operation, |
| 893 | * call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 894 | * \code |
| 895 | * // Set g3 |
| 896 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 897 | * // Set the ZKP public key for x3 |
| 898 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 899 | * // Set the ZKP proof for x3 |
| 900 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 901 | * // Set g4 |
| 902 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 903 | * // Set the ZKP public key for x4 |
| 904 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 905 | * // Set the ZKP proof for x4 |
| 906 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 907 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 908 | * -# To get the second round data that needs to be sent to the peer, call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 909 | * \code |
| 910 | * // Get A |
| 911 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 912 | * // Get ZKP public key for x2*s |
| 913 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 914 | * // Get ZKP proof for x2*s |
| 915 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 916 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 917 | * -# To provide the second round data received from the peer to the operation, |
| 918 | * call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 919 | * \code |
| 920 | * // Set B |
| 921 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 922 | * // Set ZKP public key for x4*s |
| 923 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 924 | * // Set ZKP proof for x4*s |
| 925 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 926 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 927 | * -# To access the shared secret call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 928 | * \code |
| 929 | * // Get Ka=Kb=K |
| 930 | * psa_pake_get_implicit_key() |
| 931 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 932 | * |
| 933 | * For more information consult the documentation of the individual |
| 934 | * \c PSA_PAKE_STEP_XXX constants. |
| 935 | * |
| 936 | * At this point there is a cryptographic guarantee that only the authenticated |
| 937 | * party who used the same password is able to compute the key. But there is no |
Janos Follath | a46e28f | 2021-06-03 13:07:03 +0100 | [diff] [blame] | 938 | * guarantee that the peer is the party it claims to be and was able to do so. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 939 | * |
| 940 | * That is, the authentication is only implicit (the peer is not authenticated |
| 941 | * at this point, and no action should be taken that assume that they are - like |
| 942 | * for example accessing restricted files). |
| 943 | * |
| 944 | * To make the authentication explicit there are various methods, see Section 5 |
| 945 | * of RFC 8236 for two examples. |
| 946 | * |
| 947 | */ |
| 948 | #define PSA_ALG_JPAKE ((psa_algorithm_t)0x0a000100) |
| 949 | |
| 950 | /** @} */ |
| 951 | |
| 952 | /** \defgroup pake Password-authenticated key exchange (PAKE) |
Janos Follath | 7d69b3a | 2021-05-26 13:10:56 +0100 | [diff] [blame] | 953 | * |
| 954 | * This is a proposed PAKE interface for the PSA Crypto API. It is not part of |
| 955 | * the official PSA Crypto API yet. |
| 956 | * |
| 957 | * \note The content of this section is not part of the stable API and ABI |
| 958 | * of Mbed Crypto and may change arbitrarily from version to version. |
| 959 | * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and |
| 960 | * #PSA_ALG_JPAKE. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 961 | * @{ |
| 962 | */ |
| 963 | |
| 964 | /** \brief Encoding of the side of PAKE |
| 965 | * |
| 966 | * Encodes which side of the algorithm is being executed. For more information |
| 967 | * see the documentation of individual \c PSA_PAKE_SIDE_XXX constants. |
| 968 | */ |
| 969 | typedef uint8_t psa_pake_side_t; |
| 970 | |
| 971 | /** Encoding of input and output indicators for PAKE. |
| 972 | * |
| 973 | * Some PAKE algorithms need to exchange more data than just a single key share. |
| 974 | * This type is for encoding additional input and output data for such |
| 975 | * algorithms. |
| 976 | */ |
| 977 | typedef uint8_t psa_pake_step_t; |
| 978 | |
| 979 | /** Encoding of the type of the PAKE's primitive. |
| 980 | * |
| 981 | * Values defined by this standard will never be in the range 0x80-0xff. |
| 982 | * Vendors who define additional types must use an encoding in this range. |
| 983 | * |
| 984 | * For more information see the documentation of individual |
| 985 | * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants. |
| 986 | */ |
| 987 | typedef uint8_t psa_pake_primitive_type_t; |
| 988 | |
| 989 | /** \brief Encoding of the family of the primitive associated with the PAKE. |
| 990 | * |
| 991 | * For more information see the documentation of individual |
| 992 | * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants. |
| 993 | */ |
| 994 | typedef uint8_t psa_pake_family_t; |
| 995 | |
| 996 | /** \brief Encoding of the primitive associated with the PAKE. |
| 997 | * |
| 998 | * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro. |
| 999 | */ |
| 1000 | typedef uint32_t psa_pake_primitive_t; |
| 1001 | |
| 1002 | /** The first peer in a balanced PAKE. |
| 1003 | * |
| 1004 | * Although balanced PAKE algorithms are symmetric, some of them needs an |
| 1005 | * ordering of peers for the transcript calculations. If the algorithm does not |
| 1006 | * need this, both #PSA_PAKE_SIDE_FIRST and #PSA_PAKE_SIDE_SECOND are |
| 1007 | * accepted. |
| 1008 | */ |
| 1009 | #define PSA_PAKE_SIDE_FIRST ((psa_pake_side_t)0x01) |
| 1010 | |
| 1011 | /** The second peer in a balanced PAKE. |
| 1012 | * |
| 1013 | * Although balanced PAKE algorithms are symmetric, some of them needs an |
| 1014 | * ordering of peers for the transcript calculations. If the algorithm does not |
| 1015 | * need this, either #PSA_PAKE_SIDE_FIRST or #PSA_PAKE_SIDE_SECOND are |
| 1016 | * accepted. |
| 1017 | */ |
| 1018 | #define PSA_PAKE_SIDE_SECOND ((psa_pake_side_t)0x02) |
| 1019 | |
| 1020 | /** The client in an augmented PAKE. |
| 1021 | * |
| 1022 | * Augmented PAKE algorithms need to differentiate between client and server. |
| 1023 | */ |
| 1024 | #define PSA_PAKE_SIDE_CLIENT ((psa_pake_side_t)0x11) |
| 1025 | |
| 1026 | /** The server in an augmented PAKE. |
| 1027 | * |
| 1028 | * Augmented PAKE algorithms need to differentiate between client and server. |
| 1029 | */ |
| 1030 | #define PSA_PAKE_SIDE_SERVER ((psa_pake_side_t)0x12) |
| 1031 | |
| 1032 | /** The PAKE primitive type indicating the use of elliptic curves. |
| 1033 | * |
| 1034 | * The values of the \c family and \c bits fields of the cipher suite identify a |
| 1035 | * specific elliptic curve, using the same mapping that is used for ECC |
| 1036 | * (::psa_ecc_family_t) keys. |
| 1037 | * |
| 1038 | * (Here \c family means the value returned by psa_pake_cs_get_family() and |
| 1039 | * \c bits means the value returned by psa_pake_cs_get_bits().) |
| 1040 | * |
| 1041 | * Input and output during the operation can involve group elements and scalar |
| 1042 | * values: |
| 1043 | * -# The format for group elements is the same as for public keys on the |
| 1044 | * specific curve would be. For more information, consult the documentation of |
| 1045 | * psa_export_public_key(). |
| 1046 | * -# The format for scalars is the same as for private keys on the specific |
| 1047 | * curve would be. For more information, consult the documentation of |
| 1048 | * psa_export_key(). |
| 1049 | */ |
| 1050 | #define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t)0x01) |
| 1051 | |
| 1052 | /** The PAKE primitive type indicating the use of Diffie-Hellman groups. |
| 1053 | * |
| 1054 | * The values of the \c family and \c bits fields of the cipher suite identify |
| 1055 | * a specific Diffie-Hellman group, using the same mapping that is used for |
| 1056 | * Diffie-Hellman (::psa_dh_family_t) keys. |
| 1057 | * |
| 1058 | * (Here \c family means the value returned by psa_pake_cs_get_family() and |
| 1059 | * \c bits means the value returned by psa_pake_cs_get_bits().) |
| 1060 | * |
| 1061 | * Input and output during the operation can involve group elements and scalar |
| 1062 | * values: |
| 1063 | * -# The format for group elements is the same as for public keys on the |
| 1064 | * specific group would be. For more information, consult the documentation of |
| 1065 | * psa_export_public_key(). |
| 1066 | * -# The format for scalars is the same as for private keys on the specific |
| 1067 | * group would be. For more information, consult the documentation of |
| 1068 | * psa_export_key(). |
| 1069 | */ |
| 1070 | #define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t)0x02) |
| 1071 | |
| 1072 | /** Construct a PAKE primitive from type, family and bit-size. |
| 1073 | * |
| 1074 | * \param pake_type The type of the primitive |
| 1075 | * (value of type ::psa_pake_primitive_type_t). |
| 1076 | * \param pake_family The family of the primitive |
| 1077 | * (the type and interpretation of this parameter depends |
| 1078 | * on \p type, for more information consult the |
| 1079 | * documentation of individual ::psa_pake_primitive_type_t |
| 1080 | * constants). |
| 1081 | * \param pake_bits The bit-size of the primitive |
| 1082 | * (Value of type \c size_t. The interpretation |
| 1083 | * of this parameter depends on \p family, for more |
| 1084 | * information consult the documentation of individual |
| 1085 | * ::psa_pake_primitive_type_t constants). |
| 1086 | * |
| 1087 | * \return The constructed primitive value of type ::psa_pake_primitive_t. |
| 1088 | * Return 0 if the requested primitive can't be encoded as |
| 1089 | * ::psa_pake_primitive_t. |
| 1090 | */ |
| 1091 | #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \ |
| 1092 | ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \ |
| 1093 | ((psa_pake_primitive_t) (((pake_type) << 24 | \ |
| 1094 | (pake_family) << 16) | (pake_bits))) |
| 1095 | |
| 1096 | /** The key share being sent to or received from the peer. |
| 1097 | * |
| 1098 | * The format for both input and output at this step is the same as for public |
| 1099 | * keys on the group determined by the primitive (::psa_pake_primitive_t) would |
| 1100 | * be. |
| 1101 | * |
| 1102 | * For more information on the format, consult the documentation of |
| 1103 | * psa_export_public_key(). |
| 1104 | * |
| 1105 | * For information regarding how the group is determined, consult the |
| 1106 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1107 | */ |
| 1108 | #define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t)0x01) |
| 1109 | |
| 1110 | /** A Schnorr NIZKP public key. |
| 1111 | * |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1112 | * This is the ephemeral public key in the Schnorr Non-Interactive |
| 1113 | * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235). |
| 1114 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1115 | * The format for both input and output at this step is the same as for public |
| 1116 | * keys on the group determined by the primitive (::psa_pake_primitive_t) would |
| 1117 | * be. |
| 1118 | * |
| 1119 | * For more information on the format, consult the documentation of |
| 1120 | * psa_export_public_key(). |
| 1121 | * |
| 1122 | * For information regarding how the group is determined, consult the |
| 1123 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1124 | */ |
| 1125 | #define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t)0x02) |
| 1126 | |
| 1127 | /** A Schnorr NIZKP proof. |
| 1128 | * |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1129 | * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the |
| 1130 | * value denoted by the letter 'r' in RFC 8235). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1131 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1132 | * Both for input and output, the value at this step is an integer less than |
| 1133 | * the order of the group selected in the cipher suite. The format depends on |
| 1134 | * the group as well: |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1135 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1136 | * - For Montgomery curves, the encoding is little endian. |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1137 | * - For everything else the encoding is big endian (see Section 2.3.8 of |
| 1138 | * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1139 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1140 | * In both cases leading zeroes are allowed as long as the length in bytes does |
| 1141 | * not exceed the byte length of the group order. |
| 1142 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1143 | * For information regarding how the group is determined, consult the |
| 1144 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1145 | */ |
| 1146 | #define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t)0x03) |
| 1147 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1148 | /** The type of the data structure for PAKE cipher suites. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1149 | * |
| 1150 | * This is an implementation-defined \c struct. Applications should not |
| 1151 | * make any assumptions about the content of this structure. |
| 1152 | * Implementation details can change in future versions without notice. |
| 1153 | */ |
| 1154 | typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t; |
| 1155 | |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1156 | /** Return an initial value for a PAKE cipher suite object. |
| 1157 | */ |
| 1158 | static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void); |
| 1159 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1160 | /** Retrieve the PAKE algorithm from a PAKE cipher suite. |
| 1161 | * |
| 1162 | * This function may be declared as `static` (i.e. without external |
| 1163 | * linkage). This function may be provided as a function-like macro, |
| 1164 | * but in this case it must evaluate its argument exactly once. |
| 1165 | * |
| 1166 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1167 | * |
| 1168 | * \return The PAKE algorithm stored in the cipher suite structure. |
| 1169 | */ |
| 1170 | static psa_algorithm_t psa_pake_cs_get_algorithm( |
| 1171 | const psa_pake_cipher_suite_t* cipher_suite |
| 1172 | ); |
| 1173 | |
| 1174 | /** Declare the PAKE algorithm for the cipher suite. |
| 1175 | * |
| 1176 | * This function overwrites any PAKE algorithm |
| 1177 | * previously set in \p cipher_suite. |
| 1178 | * |
| 1179 | * This function may be declared as `static` (i.e. without external |
| 1180 | * linkage). This function may be provided as a function-like macro, |
| 1181 | * but in this case it must evaluate each of its arguments exactly once. |
| 1182 | * |
| 1183 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1184 | * \param algorithm The PAKE algorithm to write. |
| 1185 | * (`PSA_ALG_XXX` values of type ::psa_algorithm_t |
| 1186 | * such that #PSA_ALG_IS_PAKE(\c alg) is true.) |
| 1187 | * If this is 0, the PAKE algorithm in |
| 1188 | * \p cipher_suite becomes unspecified. |
| 1189 | */ |
| 1190 | static void psa_pake_cs_set_algorithm( |
| 1191 | psa_pake_cipher_suite_t* cipher_suite, |
| 1192 | psa_algorithm_t algorithm |
| 1193 | ); |
| 1194 | |
| 1195 | /** Retrieve the primitive from a PAKE cipher suite. |
| 1196 | * |
| 1197 | * This function may be declared as `static` (i.e. without external linkage). |
| 1198 | * This function may be provided as a function-like macro, but in this case it |
| 1199 | * must evaluate its argument exactly once. |
| 1200 | * |
| 1201 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1202 | * |
| 1203 | * \return The primitive stored in the cipher suite structure. |
| 1204 | */ |
| 1205 | static psa_pake_primitive_t psa_pake_cs_get_primitive( |
| 1206 | const psa_pake_cipher_suite_t* cipher_suite |
| 1207 | ); |
| 1208 | |
| 1209 | /** Declare the primitive for a PAKE cipher suite. |
| 1210 | * |
| 1211 | * This function overwrites any primitive previously set in \p cipher_suite. |
| 1212 | * |
| 1213 | * This function may be declared as `static` (i.e. without external |
| 1214 | * linkage). This function may be provided as a function-like macro, |
| 1215 | * but in this case it must evaluate each of its arguments exactly once. |
| 1216 | * |
| 1217 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1218 | * \param primitive The primitive to write. If this is 0, the |
| 1219 | * primitive type in \p cipher_suite becomes |
| 1220 | * unspecified. |
| 1221 | */ |
| 1222 | static void psa_pake_cs_set_primitive( |
| 1223 | psa_pake_cipher_suite_t* cipher_suite, |
| 1224 | psa_pake_primitive_t primitive |
| 1225 | ); |
| 1226 | |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1227 | /** Retrieve the PAKE family from a PAKE cipher suite. |
| 1228 | * |
| 1229 | * This function may be declared as `static` (i.e. without external |
| 1230 | * linkage). This function may be provided as a function-like macro, |
| 1231 | * but in this case it must evaluate its argument exactly once. |
| 1232 | * |
| 1233 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1234 | * |
| 1235 | * \return The PAKE family stored in the cipher suite structure. |
| 1236 | */ |
| 1237 | static psa_pake_family_t psa_pake_cs_get_family( |
| 1238 | const psa_pake_cipher_suite_t* cipher_suite |
| 1239 | ); |
| 1240 | |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1241 | /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite. |
| 1242 | * |
| 1243 | * This function may be declared as `static` (i.e. without external |
| 1244 | * linkage). This function may be provided as a function-like macro, |
| 1245 | * but in this case it must evaluate its argument exactly once. |
| 1246 | * |
| 1247 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1248 | * |
| 1249 | * \return The PAKE primitive bit-size stored in the cipher suite structure. |
| 1250 | */ |
| 1251 | static uint16_t psa_pake_cs_get_bits( |
| 1252 | const psa_pake_cipher_suite_t* cipher_suite |
| 1253 | ); |
| 1254 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1255 | /** Retrieve the hash algorithm from a PAKE cipher suite. |
| 1256 | * |
| 1257 | * This function may be declared as `static` (i.e. without external |
| 1258 | * linkage). This function may be provided as a function-like macro, |
| 1259 | * but in this case it must evaluate its argument exactly once. |
| 1260 | * |
| 1261 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1262 | * |
| 1263 | * \return The hash algorithm stored in the cipher suite structure. The return |
| 1264 | * value is 0 if the PAKE is not parametrised by a hash algorithm or if |
| 1265 | * the hash algorithm is not set. |
| 1266 | */ |
| 1267 | static psa_algorithm_t psa_pake_cs_get_hash( |
| 1268 | const psa_pake_cipher_suite_t* cipher_suite |
| 1269 | ); |
| 1270 | |
| 1271 | /** Declare the hash algorithm for a PAKE cipher suite. |
| 1272 | * |
| 1273 | * This function overwrites any hash algorithm |
| 1274 | * previously set in \p cipher_suite. |
| 1275 | * |
| 1276 | * This function may be declared as `static` (i.e. without external |
| 1277 | * linkage). This function may be provided as a function-like macro, |
| 1278 | * but in this case it must evaluate each of its arguments exactly once. |
| 1279 | * |
| 1280 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1281 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1282 | * for more information. |
| 1283 | * |
| 1284 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1285 | * \param hash The hash involved in the cipher suite. |
| 1286 | * (`PSA_ALG_XXX` values of type ::psa_algorithm_t |
| 1287 | * such that #PSA_ALG_IS_HASH(\c alg) is true.) |
| 1288 | * If this is 0, the hash algorithm in |
| 1289 | * \p cipher_suite becomes unspecified. |
| 1290 | */ |
| 1291 | static void psa_pake_cs_set_hash( |
| 1292 | psa_pake_cipher_suite_t* cipher_suite, |
| 1293 | psa_algorithm_t hash |
| 1294 | ); |
| 1295 | |
| 1296 | /** The type of the state data structure for PAKE operations. |
| 1297 | * |
| 1298 | * Before calling any function on a PAKE operation object, the application |
| 1299 | * must initialize it by any of the following means: |
| 1300 | * - Set the structure to all-bits-zero, for example: |
| 1301 | * \code |
| 1302 | * psa_pake_operation_t operation; |
| 1303 | * memset(&operation, 0, sizeof(operation)); |
| 1304 | * \endcode |
| 1305 | * - Initialize the structure to logical zero values, for example: |
| 1306 | * \code |
| 1307 | * psa_pake_operation_t operation = {0}; |
| 1308 | * \endcode |
| 1309 | * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT, |
| 1310 | * for example: |
| 1311 | * \code |
| 1312 | * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT; |
| 1313 | * \endcode |
| 1314 | * - Assign the result of the function psa_pake_operation_init() |
| 1315 | * to the structure, for example: |
| 1316 | * \code |
| 1317 | * psa_pake_operation_t operation; |
| 1318 | * operation = psa_pake_operation_init(); |
| 1319 | * \endcode |
| 1320 | * |
| 1321 | * This is an implementation-defined \c struct. Applications should not |
| 1322 | * make any assumptions about the content of this structure. |
| 1323 | * Implementation details can change in future versions without notice. */ |
| 1324 | typedef struct psa_pake_operation_s psa_pake_operation_t; |
| 1325 | |
| 1326 | /** Return an initial value for an PAKE operation object. |
| 1327 | */ |
| 1328 | static psa_pake_operation_t psa_pake_operation_init(void); |
| 1329 | |
| 1330 | /** Set the session information for a password-authenticated key exchange. |
| 1331 | * |
| 1332 | * The sequence of operations to set up a password-authenticated key exchange |
| 1333 | * is as follows: |
| 1334 | * -# Allocate an operation object which will be passed to all the functions |
| 1335 | * listed here. |
| 1336 | * -# Initialize the operation object with one of the methods described in the |
| 1337 | * documentation for #psa_pake_operation_t, e.g. |
| 1338 | * #PSA_PAKE_OPERATION_INIT. |
| 1339 | * -# Call psa_pake_setup() to specify the cipher suite. |
| 1340 | * -# Call \c psa_pake_set_xxx() functions on the operation to complete the |
| 1341 | * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs |
| 1342 | * to be called depends on the algorithm in use. |
| 1343 | * |
| 1344 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1345 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1346 | * for more information. |
| 1347 | * |
| 1348 | * A typical sequence of calls to perform a password-authenticated key |
| 1349 | * exchange: |
| 1350 | * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the |
| 1351 | * key share that needs to be sent to the peer. |
| 1352 | * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide |
| 1353 | * the key share that was received from the peer. |
| 1354 | * -# Depending on the algorithm additional calls to psa_pake_output() and |
| 1355 | * psa_pake_input() might be necessary. |
| 1356 | * -# Call psa_pake_get_implicit_key() for accessing the shared secret. |
| 1357 | * |
| 1358 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1359 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1360 | * for more information. |
| 1361 | * |
| 1362 | * If an error occurs at any step after a call to psa_pake_setup(), |
| 1363 | * the operation will need to be reset by a call to psa_pake_abort(). The |
| 1364 | * application may call psa_pake_abort() at any time after the operation |
| 1365 | * has been initialized. |
| 1366 | * |
| 1367 | * After a successful call to psa_pake_setup(), the application must |
| 1368 | * eventually terminate the operation. The following events terminate an |
| 1369 | * operation: |
| 1370 | * - A call to psa_pake_abort(). |
| 1371 | * - A successful call to psa_pake_get_implicit_key(). |
| 1372 | * |
| 1373 | * \param[in,out] operation The operation object to set up. It must have |
Janos Follath | 3293dae | 2021-06-03 13:21:33 +0100 | [diff] [blame] | 1374 | * been initialized but not set up yet. |
Neil Armstrong | 47e700e | 2022-05-20 10:16:41 +0200 | [diff] [blame] | 1375 | * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1376 | * characterizes a PAKE algorithm and determines |
| 1377 | * the algorithm as well.) |
| 1378 | * |
| 1379 | * \retval #PSA_SUCCESS |
| 1380 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1381 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1382 | * The \p cipher_suite is not supported or is not valid. |
| 1383 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1384 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1385 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1386 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1387 | * The operation state is not valid, or |
| 1388 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1389 | * It is implementation-dependent whether a failure to initialize |
| 1390 | * results in this error code. |
| 1391 | */ |
| 1392 | psa_status_t psa_pake_setup(psa_pake_operation_t *operation, |
Neil Armstrong | 47e700e | 2022-05-20 10:16:41 +0200 | [diff] [blame] | 1393 | const psa_pake_cipher_suite_t *cipher_suite); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1394 | |
| 1395 | /** Set the password for a password-authenticated key exchange from key ID. |
| 1396 | * |
| 1397 | * Call this function when the password, or a value derived from the password, |
Janos Follath | 52f9efa | 2021-05-27 08:40:16 +0100 | [diff] [blame] | 1398 | * is already present in the key store. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1399 | * |
| 1400 | * \param[in,out] operation The operation object to set the password for. It |
| 1401 | * must have been set up by psa_pake_setup() and |
| 1402 | * not yet in use (neither psa_pake_output() nor |
| 1403 | * psa_pake_input() has been called yet). It must |
| 1404 | * be on operation for which the password hasn't |
Janos Follath | 52f9efa | 2021-05-27 08:40:16 +0100 | [diff] [blame] | 1405 | * been set yet (psa_pake_set_password_key() |
Janos Follath | 559f05e | 2021-05-26 15:44:30 +0100 | [diff] [blame] | 1406 | * hasn't been called yet). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1407 | * \param password Identifier of the key holding the password or a |
| 1408 | * value derived from the password (eg. by a |
| 1409 | * memory-hard function). It must remain valid |
| 1410 | * until the operation terminates. It must be of |
| 1411 | * type #PSA_KEY_TYPE_PASSWORD or |
| 1412 | * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow |
| 1413 | * the usage #PSA_KEY_USAGE_DERIVE. |
| 1414 | * |
| 1415 | * \retval #PSA_SUCCESS |
| 1416 | * Success. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1417 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1418 | * \p key is not compatible with the algorithm or the cipher suite. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1419 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1420 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1421 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1422 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1423 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 1424 | * \retval #PSA_ERROR_NOT_PERMITTED |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1425 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1426 | * The operation state is not valid (it must have been set up.), or |
| 1427 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1428 | * It is implementation-dependent whether a failure to initialize |
| 1429 | * results in this error code. |
| 1430 | */ |
Janos Follath | 52f9efa | 2021-05-27 08:40:16 +0100 | [diff] [blame] | 1431 | psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation, |
| 1432 | mbedtls_svc_key_id_t password); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1433 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1434 | /** Set the user ID for a password-authenticated key exchange. |
| 1435 | * |
| 1436 | * Call this function to set the user ID. For PAKE algorithms that associate a |
| 1437 | * user identifier with each side of the session you need to call |
| 1438 | * psa_pake_set_peer() as well. For PAKE algorithms that associate a single |
| 1439 | * user identifier with the session, call psa_pake_set_user() only. |
| 1440 | * |
| 1441 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1442 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1443 | * for more information. |
| 1444 | * |
| 1445 | * \param[in,out] operation The operation object to set the user ID for. It |
| 1446 | * must have been set up by psa_pake_setup() and |
| 1447 | * not yet in use (neither psa_pake_output() nor |
| 1448 | * psa_pake_input() has been called yet). It must |
| 1449 | * be on operation for which the user ID hasn't |
| 1450 | * been set (psa_pake_set_user() hasn't been |
| 1451 | * called yet). |
| 1452 | * \param[in] user_id The user ID to authenticate with. |
| 1453 | * \param user_id_len Size of the \p user_id buffer in bytes. |
| 1454 | * |
| 1455 | * \retval #PSA_SUCCESS |
| 1456 | * Success. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1457 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1458 | * \p user_id is NULL. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1459 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1460 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1461 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1462 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1463 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1464 | * The operation state is not valid, or |
| 1465 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1466 | * It is implementation-dependent whether a failure to initialize |
| 1467 | * results in this error code. |
| 1468 | */ |
| 1469 | psa_status_t psa_pake_set_user(psa_pake_operation_t *operation, |
| 1470 | const uint8_t *user_id, |
| 1471 | size_t user_id_len); |
| 1472 | |
| 1473 | /** Set the peer ID for a password-authenticated key exchange. |
| 1474 | * |
| 1475 | * Call this function in addition to psa_pake_set_user() for PAKE algorithms |
| 1476 | * that associate a user identifier with each side of the session. For PAKE |
| 1477 | * algorithms that associate a single user identifier with the session, call |
| 1478 | * psa_pake_set_user() only. |
| 1479 | * |
| 1480 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1481 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1482 | * for more information. |
| 1483 | * |
| 1484 | * \param[in,out] operation The operation object to set the peer ID for. It |
| 1485 | * must have been set up by psa_pake_setup() and |
| 1486 | * not yet in use (neither psa_pake_output() nor |
| 1487 | * psa_pake_input() has been called yet). It must |
| 1488 | * be on operation for which the peer ID hasn't |
| 1489 | * been set (psa_pake_set_peer() hasn't been |
| 1490 | * called yet). |
| 1491 | * \param[in] peer_id The peer's ID to authenticate. |
| 1492 | * \param peer_id_len Size of the \p peer_id buffer in bytes. |
| 1493 | * |
| 1494 | * \retval #PSA_SUCCESS |
| 1495 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1496 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1497 | * The algorithm doesn't associate a second identity with the session. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1498 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1499 | * \p user_id is NULL. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1500 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1501 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1502 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1503 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1504 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1505 | * The operation state is not valid, or the library has not |
| 1506 | * been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1507 | * It is implementation-dependent whether a failure to initialize |
| 1508 | * results in this error code. |
| 1509 | */ |
| 1510 | psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation, |
| 1511 | const uint8_t *peer_id, |
| 1512 | size_t peer_id_len); |
| 1513 | |
| 1514 | /** Set the side for a password-authenticated key exchange. |
| 1515 | * |
| 1516 | * Not all PAKE algorithms need to differentiate the communicating entities. |
| 1517 | * It is optional to call this function for PAKEs that don't require a side |
| 1518 | * parameter. For such PAKEs the side parameter is ignored. |
| 1519 | * |
| 1520 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1521 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1522 | * for more information. |
| 1523 | * |
| 1524 | * \param[in,out] operation The operation object to set the side for. It |
| 1525 | * must have been set up by psa_pake_setup() and |
| 1526 | * not yet in use (neither psa_pake_output() nor |
| 1527 | * psa_pake_input() has been called yet). It must |
| 1528 | * be on operation for which the side hasn't been |
| 1529 | * set (psa_pake_set_side() hasn't been called |
| 1530 | * yet). |
| 1531 | * \param side A value of type ::psa_pake_side_t signaling the |
| 1532 | * side of the algorithm that is being set up. For |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 1533 | * more information see the documentation of |
| 1534 | * \c PSA_PAKE_SIDE_XXX constants. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1535 | * |
| 1536 | * \retval #PSA_SUCCESS |
| 1537 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1538 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1539 | * The \p side for this algorithm is not supported or is not valid. |
| 1540 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1541 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1542 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1543 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1544 | * The operation state is not valid, or |
| 1545 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1546 | * It is implementation-dependent whether a failure to initialize |
| 1547 | * results in this error code. |
| 1548 | */ |
| 1549 | psa_status_t psa_pake_set_side(psa_pake_operation_t *operation, |
| 1550 | psa_pake_side_t side); |
| 1551 | |
| 1552 | /** Get output for a step of a password-authenticated key exchange. |
| 1553 | * |
| 1554 | * Depending on the algorithm being executed, you might need to call this |
| 1555 | * function several times or you might not need to call this at all. |
| 1556 | * |
| 1557 | * The exact sequence of calls to perform a password-authenticated key |
| 1558 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1559 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1560 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1561 | * information. |
| 1562 | * |
| 1563 | * If this function returns an error status, the operation enters an error |
| 1564 | * state and must be aborted by calling psa_pake_abort(). |
| 1565 | * |
| 1566 | * \param[in,out] operation Active PAKE operation. |
| 1567 | * \param step The step of the algorithm for which the output is |
| 1568 | * requested. |
| 1569 | * \param[out] output Buffer where the output is to be written in the |
| 1570 | * format appropriate for this \p step. Refer to |
| 1571 | * the documentation of the individual |
| 1572 | * \c PSA_PAKE_STEP_XXX constants for more |
| 1573 | * information. |
| 1574 | * \param output_size Size of the \p output buffer in bytes. This must |
| 1575 | * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \c |
| 1576 | * cipher_suite, \p type). |
| 1577 | * |
| 1578 | * \param[out] output_length On success, the number of bytes of the returned |
| 1579 | * output. |
| 1580 | * |
| 1581 | * \retval #PSA_SUCCESS |
| 1582 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1583 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1584 | * The size of the \p output buffer is too small. |
| 1585 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1586 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1587 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1588 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1589 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 1590 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1591 | * The operation state is not valid (it must be active, but beyond that |
| 1592 | * validity is specific to the algorithm), or |
| 1593 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1594 | * It is implementation-dependent whether a failure to initialize |
| 1595 | * results in this error code. |
| 1596 | */ |
| 1597 | psa_status_t psa_pake_output(psa_pake_operation_t *operation, |
| 1598 | psa_pake_step_t step, |
| 1599 | uint8_t *output, |
| 1600 | size_t output_size, |
| 1601 | size_t *output_length); |
| 1602 | |
| 1603 | /** Provide input for a step of a password-authenticated key exchange. |
| 1604 | * |
| 1605 | * Depending on the algorithm being executed, you might need to call this |
| 1606 | * function several times or you might not need to call this at all. |
| 1607 | * |
| 1608 | * The exact sequence of calls to perform a password-authenticated key |
| 1609 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1610 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1611 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1612 | * information. |
| 1613 | * |
| 1614 | * If this function returns an error status, the operation enters an error |
| 1615 | * state and must be aborted by calling psa_pake_abort(). |
| 1616 | * |
| 1617 | * \param[in,out] operation Active PAKE operation. |
| 1618 | * \param step The step for which the input is provided. |
Neil Armstrong | 799106b | 2022-05-20 10:18:53 +0200 | [diff] [blame] | 1619 | * \param[in] input Buffer containing the input in the format |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1620 | * appropriate for this \p step. Refer to the |
| 1621 | * documentation of the individual |
| 1622 | * \c PSA_PAKE_STEP_XXX constants for more |
| 1623 | * information. |
Neil Armstrong | 799106b | 2022-05-20 10:18:53 +0200 | [diff] [blame] | 1624 | * \param input_length Size of the \p input buffer in bytes. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1625 | * |
| 1626 | * \retval #PSA_SUCCESS |
| 1627 | * Success. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1628 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1629 | * The input is not valid for the algorithm, ciphersuite or \p step. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1630 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1631 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1632 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1633 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1634 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1635 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1636 | * The operation state is not valid (it must be active, but beyond that |
| 1637 | * validity is specific to the algorithm), or |
| 1638 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1639 | * It is implementation-dependent whether a failure to initialize |
| 1640 | * results in this error code. |
| 1641 | */ |
| 1642 | psa_status_t psa_pake_input(psa_pake_operation_t *operation, |
| 1643 | psa_pake_step_t step, |
Neil Armstrong | 799106b | 2022-05-20 10:18:53 +0200 | [diff] [blame] | 1644 | const uint8_t *input, |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1645 | size_t input_length); |
| 1646 | |
| 1647 | /** Get implicitly confirmed shared secret from a PAKE. |
| 1648 | * |
| 1649 | * At this point there is a cryptographic guarantee that only the authenticated |
| 1650 | * party who used the same password is able to compute the key. But there is no |
Janos Follath | a46e28f | 2021-06-03 13:07:03 +0100 | [diff] [blame] | 1651 | * guarantee that the peer is the party it claims to be and was able to do so. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1652 | * |
Janos Follath | b4db90f | 2021-06-03 13:17:09 +0100 | [diff] [blame] | 1653 | * That is, the authentication is only implicit. Since the peer is not |
| 1654 | * authenticated yet, no action should be taken yet that assumes that the peer |
| 1655 | * is who it claims to be. For example, do not access restricted files on the |
| 1656 | * peer's behalf until an explicit authentication has succeeded. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1657 | * |
| 1658 | * This function can be called after the key exchange phase of the operation |
| 1659 | * has completed. It imports the shared secret output of the PAKE into the |
| 1660 | * provided derivation operation. The input step |
| 1661 | * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key |
| 1662 | * material in the key derivation operation. |
| 1663 | * |
| 1664 | * The exact sequence of calls to perform a password-authenticated key |
| 1665 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1666 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1667 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1668 | * information. |
| 1669 | * |
| 1670 | * When this function returns successfully, \p operation becomes inactive. |
| 1671 | * If this function returns an error status, both \p operation |
| 1672 | * and \p key_derivation operations enter an error state and must be aborted by |
| 1673 | * calling psa_pake_abort() and psa_key_derivation_abort() respectively. |
| 1674 | * |
| 1675 | * \param[in,out] operation Active PAKE operation. |
| 1676 | * \param[out] output A key derivation operation that is ready |
| 1677 | * for an input step of type |
| 1678 | * #PSA_KEY_DERIVATION_INPUT_SECRET. |
| 1679 | * |
| 1680 | * \retval #PSA_SUCCESS |
| 1681 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1682 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1683 | * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the output’s |
| 1684 | * algorithm. |
| 1685 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1686 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1687 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1688 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1689 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 1690 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1691 | * The PAKE operation state is not valid (it must be active, but beyond |
| 1692 | * that validity is specific to the algorithm), or |
| 1693 | * the library has not been previously initialized by psa_crypto_init(), |
| 1694 | * or the state of \p output is not valid for |
| 1695 | * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the |
| 1696 | * step is out of order or the application has done this step already |
| 1697 | * and it may not be repeated. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1698 | * It is implementation-dependent whether a failure to initialize |
| 1699 | * results in this error code. |
| 1700 | */ |
| 1701 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 1702 | psa_key_derivation_operation_t *output); |
| 1703 | |
Neil Armstrong | 0c8ef93 | 2022-05-20 10:23:51 +0200 | [diff] [blame] | 1704 | /** Abort a PAKE operation. |
| 1705 | * |
| 1706 | * Aborting an operation frees all associated resources except for the \c |
| 1707 | * operation structure itself. Once aborted, the operation object can be reused |
| 1708 | * for another operation by calling psa_pake_setup() again. |
| 1709 | * |
| 1710 | * This function may be called at any time after the operation |
| 1711 | * object has been initialized as described in #psa_pake_operation_t. |
| 1712 | * |
| 1713 | * In particular, calling psa_pake_abort() after the operation has been |
| 1714 | * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key() |
| 1715 | * is safe and has no effect. |
| 1716 | * |
| 1717 | * \param[in,out] operation The operation to abort. |
| 1718 | * |
| 1719 | * \retval #PSA_SUCCESS |
| 1720 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1721 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 1722 | * \retval #PSA_ERROR_BAD_STATE |
| 1723 | * The library has not been previously initialized by psa_crypto_init(). |
| 1724 | * It is implementation-dependent whether a failure to initialize |
| 1725 | * results in this error code. |
| 1726 | */ |
| 1727 | psa_status_t psa_pake_abort(psa_pake_operation_t * operation); |
| 1728 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1729 | /**@}*/ |
| 1730 | |
| 1731 | /** A sufficient output buffer size for psa_pake_output(). |
| 1732 | * |
| 1733 | * If the size of the output buffer is at least this large, it is guaranteed |
| 1734 | * that psa_pake_output() will not fail due to an insufficient output buffer |
| 1735 | * size. The actual size of the output might be smaller in any given call. |
| 1736 | * |
| 1737 | * See also #PSA_PAKE_OUTPUT_MAX_SIZE |
| 1738 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 1739 | * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1740 | * #PSA_ALG_IS_PAKE(\p alg) is true). |
| 1741 | * \param primitive A primitive of type ::psa_pake_primitive_t that is |
| 1742 | * compatible with algorithm \p alg. |
| 1743 | * \param output_step A value of type ::psa_pake_step_t that is valid for the |
| 1744 | * algorithm \p alg. |
| 1745 | * \return A sufficient output buffer size for the specified |
Neil Armstrong | cd974d5 | 2022-05-20 10:30:12 +0200 | [diff] [blame^] | 1746 | * PAKE algorithm, primitive, and output step. If the |
| 1747 | * PAKE algorithm, primitive, or output step is not |
| 1748 | * recognized, or the parameters are incompatible, |
| 1749 | * return 0. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1750 | */ |
| 1751 | #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) 0 |
| 1752 | |
| 1753 | /** A sufficient input buffer size for psa_pake_input(). |
| 1754 | * |
Janos Follath | b4db90f | 2021-06-03 13:17:09 +0100 | [diff] [blame] | 1755 | * The value returned by this macro is guaranteed to be large enough for any |
| 1756 | * valid input to psa_pake_input() in an operation with the specified |
| 1757 | * parameters. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1758 | * |
| 1759 | * See also #PSA_PAKE_INPUT_MAX_SIZE |
| 1760 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 1761 | * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1762 | * #PSA_ALG_IS_PAKE(\p alg) is true). |
| 1763 | * \param primitive A primitive of type ::psa_pake_primitive_t that is |
| 1764 | * compatible with algorithm \p alg. |
Janos Follath | ec83eb6 | 2021-05-27 08:41:59 +0100 | [diff] [blame] | 1765 | * \param input_step A value of type ::psa_pake_step_t that is valid for the |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1766 | * algorithm \p alg. |
Janos Follath | 38d29db | 2021-06-03 13:14:42 +0100 | [diff] [blame] | 1767 | * \return A sufficient input buffer size for the specified |
| 1768 | * input, cipher suite and algorithm. If the cipher suite, |
| 1769 | * the input type or PAKE algorithm is not recognized, or |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1770 | * the parameters are incompatible, return 0. |
| 1771 | */ |
| 1772 | #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) 0 |
| 1773 | |
| 1774 | /** Output buffer size for psa_pake_output() for any of the supported cipher |
| 1775 | * suites and PAKE algorithms. |
| 1776 | * |
| 1777 | * This macro must expand to a compile-time constant integer. |
| 1778 | * |
| 1779 | * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p cipher_suite, \p output). |
| 1780 | */ |
| 1781 | #define PSA_PAKE_OUTPUT_MAX_SIZE 0 |
| 1782 | |
| 1783 | /** Input buffer size for psa_pake_input() for any of the supported cipher |
| 1784 | * suites and PAKE algorithms. |
| 1785 | * |
| 1786 | * This macro must expand to a compile-time constant integer. |
| 1787 | * |
Janos Follath | 38d29db | 2021-06-03 13:14:42 +0100 | [diff] [blame] | 1788 | * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p cipher_suite, \p input). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1789 | */ |
| 1790 | #define PSA_PAKE_INPUT_MAX_SIZE 0 |
| 1791 | |
Neil Armstrong | fb99302 | 2022-05-20 10:08:58 +0200 | [diff] [blame] | 1792 | /** Returns a suitable initializer for a PAKE cipher suite object of type |
| 1793 | * psa_pake_cipher_suite_t. |
| 1794 | */ |
| 1795 | #define PSA_PAKE_CIPHER_SUITE_INIT {PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE} |
| 1796 | |
Neil Armstrong | 0151c55 | 2022-05-20 10:13:53 +0200 | [diff] [blame] | 1797 | /** Returns a suitable initializer for a PAKE operation object of type |
| 1798 | * psa_pake_operation_t. |
| 1799 | */ |
| 1800 | #define PSA_PAKE_OPERATION_INIT {0, {0}} |
| 1801 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1802 | struct psa_pake_cipher_suite_s |
| 1803 | { |
| 1804 | psa_algorithm_t algorithm; |
| 1805 | psa_pake_primitive_type_t type; |
| 1806 | psa_pake_family_t family; |
| 1807 | uint16_t bits; |
| 1808 | psa_algorithm_t hash; |
| 1809 | }; |
| 1810 | |
| 1811 | static inline psa_algorithm_t psa_pake_cs_get_algorithm( |
| 1812 | const psa_pake_cipher_suite_t *cipher_suite) |
| 1813 | { |
| 1814 | return(cipher_suite->algorithm); |
| 1815 | } |
| 1816 | |
| 1817 | static inline void psa_pake_cs_set_algorithm( |
| 1818 | psa_pake_cipher_suite_t *cipher_suite, |
| 1819 | psa_algorithm_t algorithm) |
| 1820 | { |
| 1821 | if(!PSA_ALG_IS_PAKE(algorithm)) |
| 1822 | cipher_suite->algorithm = 0; |
| 1823 | else |
| 1824 | cipher_suite->algorithm = algorithm; |
| 1825 | } |
| 1826 | |
| 1827 | static inline psa_pake_primitive_t psa_pake_cs_get_primitive( |
| 1828 | const psa_pake_cipher_suite_t *cipher_suite) |
| 1829 | { |
| 1830 | return(PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family, |
| 1831 | cipher_suite->bits)); |
| 1832 | } |
| 1833 | |
| 1834 | static inline void psa_pake_cs_set_primitive( |
| 1835 | psa_pake_cipher_suite_t *cipher_suite, |
| 1836 | psa_pake_primitive_t primitive) |
| 1837 | { |
| 1838 | cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24); |
| 1839 | cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16)); |
| 1840 | cipher_suite->bits = (uint16_t) (0xFFFF & primitive); |
| 1841 | } |
| 1842 | |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1843 | static inline psa_pake_family_t psa_pake_cs_get_family( |
| 1844 | const psa_pake_cipher_suite_t *cipher_suite) |
| 1845 | { |
| 1846 | return(cipher_suite->family); |
| 1847 | } |
| 1848 | |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1849 | static inline uint16_t psa_pake_cs_get_bits( |
| 1850 | const psa_pake_cipher_suite_t *cipher_suite) |
| 1851 | { |
| 1852 | return(cipher_suite->bits); |
| 1853 | } |
| 1854 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1855 | static inline psa_algorithm_t psa_pake_cs_get_hash( |
| 1856 | const psa_pake_cipher_suite_t *cipher_suite) |
| 1857 | { |
| 1858 | return(cipher_suite->hash); |
| 1859 | } |
| 1860 | |
| 1861 | static inline void psa_pake_cs_set_hash( |
| 1862 | psa_pake_cipher_suite_t *cipher_suite, |
| 1863 | psa_algorithm_t hash) |
| 1864 | { |
| 1865 | if(!PSA_ALG_IS_HASH(hash)) |
| 1866 | cipher_suite->hash = 0; |
| 1867 | else |
| 1868 | cipher_suite->hash = hash; |
| 1869 | } |
| 1870 | |
| 1871 | struct psa_pake_operation_s |
| 1872 | { |
| 1873 | psa_algorithm_t alg; |
| 1874 | union |
| 1875 | { |
| 1876 | /* Make the union non-empty even with no supported algorithms. */ |
| 1877 | uint8_t dummy; |
| 1878 | } ctx; |
| 1879 | }; |
| 1880 | |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1881 | static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void) |
| 1882 | { |
| 1883 | const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT; |
| 1884 | return(v); |
| 1885 | } |
| 1886 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1887 | static inline struct psa_pake_operation_s psa_pake_operation_init(void) |
| 1888 | { |
| 1889 | const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT; |
| 1890 | return(v); |
| 1891 | } |
| 1892 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1893 | #ifdef __cplusplus |
| 1894 | } |
| 1895 | #endif |
| 1896 | |
| 1897 | #endif /* PSA_CRYPTO_EXTRA_H */ |