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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | psa_key_slot_number_t *slot_number); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 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, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | psa_key_slot_number_t slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 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( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | psa_key_attributes_t *attributes) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 158 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &= |
| 160 | ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER; |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 163 | /** Register a key that is already present in a secure element. |
| 164 | * |
| 165 | * The key must be located in a secure element designated by the |
| 166 | * lifetime field in \p attributes, in the slot set with |
| 167 | * psa_set_key_slot_number() in the attribute structure. |
| 168 | * This function makes the key available through the key identifier |
| 169 | * specified in \p attributes. |
| 170 | * |
| 171 | * \param[in] attributes The attributes of the existing key. |
| 172 | * |
| 173 | * \retval #PSA_SUCCESS |
| 174 | * The key was successfully registered. |
| 175 | * Note that depending on the design of the driver, this may or may |
| 176 | * not guarantee that a key actually exists in the designated slot |
| 177 | * and is compatible with the specified attributes. |
| 178 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 179 | * There is already a key with the identifier specified in |
| 180 | * \p attributes. |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 181 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 182 | * The secure element driver for the specified lifetime does not |
| 183 | * support registering a key. |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 184 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Ronald Cron | d3b458c | 2021-03-31 17:51:29 +0200 | [diff] [blame] | 185 | * The identifier in \p attributes is invalid, namely the identifier is |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 186 | * not in the user range, or |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 187 | * \p attributes specifies a lifetime which is not located |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 188 | * 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] | 189 | * or the specified slot number is not valid. |
| 190 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 191 | * The caller is not authorized to register the specified key slot. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 192 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 193 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 194 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 195 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 196 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 197 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 198 | * \retval #PSA_ERROR_BAD_STATE |
| 199 | * The library has not been previously initialized by psa_crypto_init(). |
| 200 | * It is implementation-dependent whether a failure to initialize |
| 201 | * results in this error code. |
| 202 | */ |
| 203 | psa_status_t mbedtls_psa_register_se_key( |
| 204 | const psa_key_attributes_t *attributes); |
| 205 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 206 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 207 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 208 | /**@}*/ |
| 209 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 210 | /** |
| 211 | * \brief Library deinitialization. |
| 212 | * |
| 213 | * This function clears all data associated with the PSA layer, |
| 214 | * including the whole key store. |
| 215 | * |
| 216 | * This is an Mbed TLS extension. |
| 217 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | void mbedtls_psa_crypto_free(void); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 219 | |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 220 | /** \brief Statistics about |
| 221 | * resource consumption related to the PSA keystore. |
| 222 | * |
| 223 | * \note The content of this structure is not part of the stable API and ABI |
| 224 | * of Mbed Crypto and may change arbitrarily from version to version. |
| 225 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | typedef struct mbedtls_psa_stats_s { |
Gilles Peskine | 4bac9a4 | 2019-05-23 20:32:30 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 362 | /** Whether a key type is a DSA key (pair or public-only). */ |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | * |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 494 | * \retval #PSA_SUCCESS \emptydescription |
| 495 | * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription |
| 496 | * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription |
| 497 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 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 | * |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 524 | * \retval #PSA_SUCCESS \emptydescription |
| 525 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | static inline psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid, |
| 591 | size_t *bits) |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 592 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | switch (grpid) { |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 594 | case MBEDTLS_ECP_DP_SECP192R1: |
| 595 | *bits = 192; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | return PSA_ECC_FAMILY_SECP_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 597 | case MBEDTLS_ECP_DP_SECP224R1: |
| 598 | *bits = 224; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | return PSA_ECC_FAMILY_SECP_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 600 | case MBEDTLS_ECP_DP_SECP256R1: |
| 601 | *bits = 256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | return PSA_ECC_FAMILY_SECP_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 603 | case MBEDTLS_ECP_DP_SECP384R1: |
| 604 | *bits = 384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | return PSA_ECC_FAMILY_SECP_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 606 | case MBEDTLS_ECP_DP_SECP521R1: |
| 607 | *bits = 521; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | return PSA_ECC_FAMILY_SECP_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 609 | case MBEDTLS_ECP_DP_BP256R1: |
| 610 | *bits = 256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 611 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 612 | case MBEDTLS_ECP_DP_BP384R1: |
| 613 | *bits = 384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 615 | case MBEDTLS_ECP_DP_BP512R1: |
| 616 | *bits = 512; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | return PSA_ECC_FAMILY_BRAINPOOL_P_R1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 618 | case MBEDTLS_ECP_DP_CURVE25519: |
| 619 | *bits = 255; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | return PSA_ECC_FAMILY_MONTGOMERY; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 621 | case MBEDTLS_ECP_DP_SECP192K1: |
| 622 | *bits = 192; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | return PSA_ECC_FAMILY_SECP_K1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 624 | case MBEDTLS_ECP_DP_SECP224K1: |
| 625 | *bits = 224; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | return PSA_ECC_FAMILY_SECP_K1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 627 | case MBEDTLS_ECP_DP_SECP256K1: |
| 628 | *bits = 256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | return PSA_ECC_FAMILY_SECP_K1; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 630 | case MBEDTLS_ECP_DP_CURVE448: |
| 631 | *bits = 448; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | return PSA_ECC_FAMILY_MONTGOMERY; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 633 | default: |
| 634 | *bits = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | return 0; |
Darryl Green | 2f0eb51 | 2020-04-24 15:21:14 +0100 | [diff] [blame] | 636 | } |
| 637 | } |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 638 | |
| 639 | /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS. |
| 640 | * |
| 641 | * \note This function is provided solely for the convenience of |
| 642 | * Mbed TLS and may be removed at any time without notice. |
| 643 | * |
| 644 | * \param curve A PSA elliptic curve identifier |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 645 | * (`PSA_ECC_FAMILY_xxx`). |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 646 | * \param bits The bit-length of a private key on \p curve. |
| 647 | * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up |
| 648 | * to the nearest multiple of 8. This allows the caller |
| 649 | * to infer the exact curve from the length of a key |
| 650 | * which is supplied as a byte string. |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 651 | * |
| 652 | * \return The corresponding Mbed TLS elliptic curve identifier |
| 653 | * (`MBEDTLS_ECP_DP_xxx`). |
| 654 | * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized. |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 655 | * \return #MBEDTLS_ECP_DP_NONE if \p bits is not |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 656 | * correct for \p curve. |
| 657 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 659 | size_t bits, |
| 660 | int bits_is_sloppy); |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 661 | #endif /* MBEDTLS_ECP_C */ |
| 662 | |
| 663 | /**@}*/ |
| 664 | |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 665 | /** \defgroup psa_external_rng External random generator |
| 666 | * @{ |
| 667 | */ |
| 668 | |
| 669 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 670 | /** External random generator function, implemented by the platform. |
| 671 | * |
| 672 | * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, |
| 673 | * this function replaces Mbed TLS's entropy and DRBG modules for all |
| 674 | * random generation triggered via PSA crypto interfaces. |
| 675 | * |
Gilles Peskine | b663a60 | 2020-11-18 15:27:37 +0100 | [diff] [blame] | 676 | * \note This random generator must deliver random numbers with cryptographic |
| 677 | * quality and high performance. It must supply unpredictable numbers |
| 678 | * with a uniform distribution. The implementation of this function |
| 679 | * is responsible for ensuring that the random generator is seeded |
| 680 | * with sufficient entropy. If you have a hardware TRNG which is slow |
| 681 | * or delivers non-uniform output, declare it as an entropy source |
| 682 | * with mbedtls_entropy_add_source() instead of enabling this option. |
| 683 | * |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 684 | * \param[in,out] context Pointer to the random generator context. |
| 685 | * This is all-bits-zero on the first call |
| 686 | * and preserved between successive calls. |
| 687 | * \param[out] output Output buffer. On success, this buffer |
| 688 | * contains random data with a uniform |
| 689 | * distribution. |
| 690 | * \param output_size The size of the \p output buffer in bytes. |
| 691 | * \param[out] output_length On success, set this value to \p output_size. |
| 692 | * |
| 693 | * \retval #PSA_SUCCESS |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 694 | * Success. The output buffer contains \p output_size bytes of |
| 695 | * cryptographic-quality random data, and \c *output_length is |
| 696 | * set to \p output_size. |
| 697 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 698 | * The random generator requires extra entropy and there is no |
| 699 | * way to obtain entropy under current environment conditions. |
| 700 | * This error should not happen under normal circumstances since |
| 701 | * this function is responsible for obtaining as much entropy as |
| 702 | * it needs. However implementations of this function may return |
| 703 | * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain |
| 704 | * entropy without blocking indefinitely. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 705 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
Gilles Peskine | e995b9b | 2020-11-30 12:08:00 +0100 | [diff] [blame] | 706 | * A failure of the random generator hardware that isn't covered |
| 707 | * by #PSA_ERROR_INSUFFICIENT_ENTROPY. |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 708 | */ |
| 709 | psa_status_t mbedtls_psa_external_get_random( |
| 710 | mbedtls_psa_external_random_context_t *context, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | uint8_t *output, size_t output_size, size_t *output_length); |
Gilles Peskine | b8af228 | 2020-11-13 18:00:34 +0100 | [diff] [blame] | 712 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 713 | |
| 714 | /**@}*/ |
| 715 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 716 | /** \defgroup psa_builtin_keys Built-in keys |
| 717 | * @{ |
| 718 | */ |
| 719 | |
| 720 | /** The minimum value for a key identifier that is built into the |
| 721 | * implementation. |
| 722 | * |
| 723 | * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN |
| 724 | * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from |
| 725 | * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect |
| 726 | * with any other set of implementation-chosen key identifiers. |
| 727 | * |
| 728 | * This value is part of the library's ABI since changing it would invalidate |
| 729 | * the values of built-in key identifiers in applications. |
| 730 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 732 | |
| 733 | /** The maximum value for a key identifier that is built into the |
| 734 | * implementation. |
| 735 | * |
| 736 | * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information. |
| 737 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 739 | |
| 740 | /** A slot number identifying a key in a driver. |
| 741 | * |
| 742 | * Values of this type are used to identify built-in keys. |
| 743 | */ |
| 744 | typedef uint64_t psa_drv_slot_number_t; |
| 745 | |
| 746 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 747 | /** Test whether a key identifier belongs to the builtin key range. |
| 748 | * |
| 749 | * \param key_id Key identifier to test. |
| 750 | * |
| 751 | * \retval 1 |
| 752 | * The key identifier is a builtin key identifier. |
| 753 | * \retval 0 |
| 754 | * The key identifier is not a builtin key identifier. |
| 755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | static inline int psa_key_id_is_builtin(psa_key_id_t key_id) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 757 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) && |
| 759 | (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 760 | } |
| 761 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 762 | /** 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] | 763 | * |
| 764 | * An application-specific implementation of this function must be provided if |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 765 | * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 766 | * as part of a platform's system image. |
| 767 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 768 | * #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] | 769 | * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX. |
| 770 | * |
| 771 | * In a multi-application configuration |
| 772 | * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined), |
| 773 | * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id) |
| 774 | * is allowed to use the given key. |
| 775 | * |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 776 | * \param key_id The key ID for which to retrieve the |
| 777 | * location and slot attributes. |
| 778 | * \param[out] lifetime On success, the lifetime associated with the key |
| 779 | * corresponding to \p key_id. Lifetime is a |
| 780 | * combination of which driver contains the key, |
Steven Cooreman | 31e27af | 2021-04-14 10:32:05 +0200 | [diff] [blame] | 781 | * and with what persistence level the key is |
| 782 | * intended to be used. If the platform |
| 783 | * implementation does not contain specific |
| 784 | * information about the intended key persistence |
| 785 | * level, the persistence level may be reported as |
| 786 | * #PSA_KEY_PERSISTENCE_DEFAULT. |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 787 | * \param[out] slot_number On success, the slot number known to the driver |
| 788 | * registered at the lifetime location reported |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 789 | * through \p lifetime which corresponds to the |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 790 | * requested built-in key. |
| 791 | * |
| 792 | * \retval #PSA_SUCCESS |
| 793 | * The requested key identifier designates a built-in key. |
| 794 | * In a multi-application configuration, the requested owner |
| 795 | * is allowed to access it. |
| 796 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 797 | * The requested key identifier is not a built-in key which is known |
| 798 | * to this function. If a key exists in the key storage with this |
| 799 | * identifier, the data from the storage will be used. |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 800 | * \return (any other error) |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 801 | * Any other error is propagated to the function that requested the key. |
| 802 | * Common errors include: |
| 803 | * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner |
| 804 | * is not allowed to access it. |
| 805 | */ |
| 806 | psa_status_t mbedtls_psa_platform_get_builtin_key( |
Steven Cooreman | c8b9534 | 2021-03-18 20:48:06 +0100 | [diff] [blame] | 807 | mbedtls_svc_key_id_t key_id, |
| 808 | psa_key_lifetime_t *lifetime, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | psa_drv_slot_number_t *slot_number); |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 810 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
| 811 | |
| 812 | /** @} */ |
| 813 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 814 | /** \addtogroup crypto_types |
| 815 | * @{ |
| 816 | */ |
| 817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 818 | #define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 819 | |
| 820 | /** Whether the specified algorithm is a password-authenticated key exchange. |
| 821 | * |
| 822 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 823 | * |
| 824 | * \return 1 if \p alg is a password-authenticated key exchange (PAKE) |
| 825 | * algorithm, 0 otherwise. |
| 826 | * This macro may return either 0 or 1 if \p alg is not a supported |
| 827 | * algorithm identifier. |
| 828 | */ |
| 829 | #define PSA_ALG_IS_PAKE(alg) \ |
| 830 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE) |
| 831 | |
| 832 | /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm. |
| 833 | * |
| 834 | * This is J-PAKE as defined by RFC 8236, instantiated with the following |
| 835 | * parameters: |
| 836 | * |
| 837 | * - The group can be either an elliptic curve or defined over a finite field. |
| 838 | * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the |
| 839 | * J-PAKE algorithm. |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 840 | * - A cryptographic hash function. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 841 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 842 | * To select these parameters and set up the cipher suite, call these functions |
| 843 | * in any order: |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 844 | * |
| 845 | * \code |
| 846 | * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE); |
| 847 | * psa_pake_cs_set_primitive(cipher_suite, |
| 848 | * PSA_PAKE_PRIMITIVE(type, family, bits)); |
| 849 | * psa_pake_cs_set_hash(cipher_suite, hash); |
| 850 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 851 | * |
| 852 | * For more information on how to set a specific curve or field, refer to the |
| 853 | * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants. |
| 854 | * |
| 855 | * After initializing a J-PAKE operation, call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 856 | * |
| 857 | * \code |
| 858 | * psa_pake_setup(operation, cipher_suite); |
| 859 | * psa_pake_set_user(operation, ...); |
| 860 | * psa_pake_set_peer(operation, ...); |
| 861 | * psa_pake_set_password_key(operation, ...); |
| 862 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 863 | * |
Neil Armstrong | 1614537 | 2022-05-20 10:42:36 +0200 | [diff] [blame] | 864 | * The password is provided as a key. This can be the password text itself, |
| 865 | * in an agreed character encoding, or some value derived from the password |
| 866 | * as required by a higher level protocol. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 867 | * |
Neil Armstrong | 1614537 | 2022-05-20 10:42:36 +0200 | [diff] [blame] | 868 | * (The implementation converts the key material to a number as described in |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 869 | * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_ |
| 870 | * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here |
| 871 | * \c q is order of the group defined by the primitive set in the cipher suite. |
Neil Armstrong | 5892aa6 | 2022-05-27 09:44:47 +0200 | [diff] [blame] | 872 | * The \c psa_pake_set_password_key() function returns an error if the result |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 873 | * of the reduction is 0.) |
| 874 | * |
| 875 | * The key exchange flow for J-PAKE is as follows: |
| 876 | * -# 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] | 877 | * \code |
| 878 | * // Get g1 |
| 879 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 880 | * // Get the ZKP public key for x1 |
| 881 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 882 | * // Get the ZKP proof for x1 |
| 883 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 884 | * // Get g2 |
| 885 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 886 | * // Get the ZKP public key for x2 |
| 887 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 888 | * // Get the ZKP proof for x2 |
| 889 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 890 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 891 | * -# To provide the first round data received from the peer to the operation, |
| 892 | * call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 893 | * \code |
| 894 | * // Set g3 |
| 895 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 896 | * // Set the ZKP public key for x3 |
| 897 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 898 | * // Set the ZKP proof for x3 |
| 899 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 900 | * // Set g4 |
| 901 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 902 | * // Set the ZKP public key for x4 |
| 903 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 904 | * // Set the ZKP proof for x4 |
| 905 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 906 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 907 | * -# 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] | 908 | * \code |
| 909 | * // Get A |
| 910 | * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 911 | * // Get ZKP public key for x2*s |
| 912 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 913 | * // Get ZKP proof for x2*s |
| 914 | * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 915 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 916 | * -# To provide the second round data received from the peer to the operation, |
| 917 | * call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 918 | * \code |
| 919 | * // Set B |
| 920 | * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...); |
| 921 | * // Set ZKP public key for x4*s |
| 922 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...); |
| 923 | * // Set ZKP proof for x4*s |
| 924 | * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...); |
| 925 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 926 | * -# To access the shared secret call |
Janos Follath | b384ec1 | 2021-06-03 14:48:51 +0100 | [diff] [blame] | 927 | * \code |
| 928 | * // Get Ka=Kb=K |
| 929 | * psa_pake_get_implicit_key() |
| 930 | * \endcode |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 931 | * |
| 932 | * For more information consult the documentation of the individual |
| 933 | * \c PSA_PAKE_STEP_XXX constants. |
| 934 | * |
| 935 | * At this point there is a cryptographic guarantee that only the authenticated |
| 936 | * 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] | 937 | * 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] | 938 | * |
| 939 | * That is, the authentication is only implicit (the peer is not authenticated |
| 940 | * at this point, and no action should be taken that assume that they are - like |
| 941 | * for example accessing restricted files). |
| 942 | * |
| 943 | * To make the authentication explicit there are various methods, see Section 5 |
| 944 | * of RFC 8236 for two examples. |
| 945 | * |
| 946 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | #define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 948 | |
| 949 | /** @} */ |
| 950 | |
| 951 | /** \defgroup pake Password-authenticated key exchange (PAKE) |
Janos Follath | 7d69b3a | 2021-05-26 13:10:56 +0100 | [diff] [blame] | 952 | * |
| 953 | * This is a proposed PAKE interface for the PSA Crypto API. It is not part of |
| 954 | * the official PSA Crypto API yet. |
| 955 | * |
| 956 | * \note The content of this section is not part of the stable API and ABI |
| 957 | * of Mbed Crypto and may change arbitrarily from version to version. |
| 958 | * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and |
| 959 | * #PSA_ALG_JPAKE. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 960 | * @{ |
| 961 | */ |
| 962 | |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 963 | /** \brief Encoding of the application role of PAKE |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 964 | * |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 965 | * Encodes the application's role in the algorithm is being executed. For more |
| 966 | * information see the documentation of individual \c PSA_PAKE_ROLE_XXX |
| 967 | * constants. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 968 | */ |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 969 | typedef uint8_t psa_pake_role_t; |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 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 | |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1002 | /** A value to indicate no role in a PAKE algorithm. |
| 1003 | * This value can be used in a call to psa_pake_set_role() for symmetric PAKE |
| 1004 | * algorithms which do not assign roles. |
| 1005 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | #define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00) |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1007 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1008 | /** The first peer in a balanced PAKE. |
| 1009 | * |
| 1010 | * Although balanced PAKE algorithms are symmetric, some of them needs an |
| 1011 | * ordering of peers for the transcript calculations. If the algorithm does not |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1012 | * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1013 | * accepted. |
| 1014 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | #define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1016 | |
| 1017 | /** The second peer in a balanced PAKE. |
| 1018 | * |
| 1019 | * Although balanced PAKE algorithms are symmetric, some of them needs an |
| 1020 | * ordering of peers for the transcript calculations. If the algorithm does not |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1021 | * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1022 | * accepted. |
| 1023 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | #define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1025 | |
| 1026 | /** The client in an augmented PAKE. |
| 1027 | * |
| 1028 | * Augmented PAKE algorithms need to differentiate between client and server. |
| 1029 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | #define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1031 | |
| 1032 | /** The server in an augmented PAKE. |
| 1033 | * |
| 1034 | * Augmented PAKE algorithms need to differentiate between client and server. |
| 1035 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1036 | #define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1037 | |
| 1038 | /** The PAKE primitive type indicating the use of elliptic curves. |
| 1039 | * |
| 1040 | * The values of the \c family and \c bits fields of the cipher suite identify a |
| 1041 | * specific elliptic curve, using the same mapping that is used for ECC |
| 1042 | * (::psa_ecc_family_t) keys. |
| 1043 | * |
| 1044 | * (Here \c family means the value returned by psa_pake_cs_get_family() and |
| 1045 | * \c bits means the value returned by psa_pake_cs_get_bits().) |
| 1046 | * |
| 1047 | * Input and output during the operation can involve group elements and scalar |
| 1048 | * values: |
| 1049 | * -# The format for group elements is the same as for public keys on the |
| 1050 | * specific curve would be. For more information, consult the documentation of |
| 1051 | * psa_export_public_key(). |
| 1052 | * -# The format for scalars is the same as for private keys on the specific |
| 1053 | * curve would be. For more information, consult the documentation of |
| 1054 | * psa_export_key(). |
| 1055 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | #define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1057 | |
| 1058 | /** The PAKE primitive type indicating the use of Diffie-Hellman groups. |
| 1059 | * |
| 1060 | * The values of the \c family and \c bits fields of the cipher suite identify |
| 1061 | * a specific Diffie-Hellman group, using the same mapping that is used for |
| 1062 | * Diffie-Hellman (::psa_dh_family_t) keys. |
| 1063 | * |
| 1064 | * (Here \c family means the value returned by psa_pake_cs_get_family() and |
| 1065 | * \c bits means the value returned by psa_pake_cs_get_bits().) |
| 1066 | * |
| 1067 | * Input and output during the operation can involve group elements and scalar |
| 1068 | * values: |
| 1069 | * -# The format for group elements is the same as for public keys on the |
| 1070 | * specific group would be. For more information, consult the documentation of |
| 1071 | * psa_export_public_key(). |
| 1072 | * -# The format for scalars is the same as for private keys on the specific |
| 1073 | * group would be. For more information, consult the documentation of |
| 1074 | * psa_export_key(). |
| 1075 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | #define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1077 | |
| 1078 | /** Construct a PAKE primitive from type, family and bit-size. |
| 1079 | * |
| 1080 | * \param pake_type The type of the primitive |
| 1081 | * (value of type ::psa_pake_primitive_type_t). |
| 1082 | * \param pake_family The family of the primitive |
| 1083 | * (the type and interpretation of this parameter depends |
| 1084 | * on \p type, for more information consult the |
| 1085 | * documentation of individual ::psa_pake_primitive_type_t |
| 1086 | * constants). |
| 1087 | * \param pake_bits The bit-size of the primitive |
| 1088 | * (Value of type \c size_t. The interpretation |
| 1089 | * of this parameter depends on \p family, for more |
| 1090 | * information consult the documentation of individual |
| 1091 | * ::psa_pake_primitive_type_t constants). |
| 1092 | * |
| 1093 | * \return The constructed primitive value of type ::psa_pake_primitive_t. |
| 1094 | * Return 0 if the requested primitive can't be encoded as |
| 1095 | * ::psa_pake_primitive_t. |
| 1096 | */ |
| 1097 | #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \ |
| 1098 | ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \ |
| 1099 | ((psa_pake_primitive_t) (((pake_type) << 24 | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | (pake_family) << 16) | (pake_bits))) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1101 | |
| 1102 | /** The key share being sent to or received from the peer. |
| 1103 | * |
| 1104 | * The format for both input and output at this step is the same as for public |
| 1105 | * keys on the group determined by the primitive (::psa_pake_primitive_t) would |
| 1106 | * be. |
| 1107 | * |
| 1108 | * For more information on the format, consult the documentation of |
| 1109 | * psa_export_public_key(). |
| 1110 | * |
| 1111 | * For information regarding how the group is determined, consult the |
| 1112 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | #define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1115 | |
| 1116 | /** A Schnorr NIZKP public key. |
| 1117 | * |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1118 | * This is the ephemeral public key in the Schnorr Non-Interactive |
| 1119 | * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235). |
| 1120 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1121 | * The format for both input and output at this step is the same as for public |
| 1122 | * keys on the group determined by the primitive (::psa_pake_primitive_t) would |
| 1123 | * be. |
| 1124 | * |
| 1125 | * For more information on the format, consult the documentation of |
| 1126 | * psa_export_public_key(). |
| 1127 | * |
| 1128 | * For information regarding how the group is determined, consult the |
| 1129 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1130 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | #define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1132 | |
| 1133 | /** A Schnorr NIZKP proof. |
| 1134 | * |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1135 | * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the |
| 1136 | * value denoted by the letter 'r' in RFC 8235). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1137 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1138 | * Both for input and output, the value at this step is an integer less than |
| 1139 | * the order of the group selected in the cipher suite. The format depends on |
| 1140 | * the group as well: |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1141 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1142 | * - For Montgomery curves, the encoding is little endian. |
Janos Follath | 55dd5dc | 2021-06-03 15:51:09 +0100 | [diff] [blame] | 1143 | * - For everything else the encoding is big endian (see Section 2.3.8 of |
| 1144 | * _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] | 1145 | * |
Janos Follath | 1f01318 | 2021-06-08 15:30:48 +0100 | [diff] [blame] | 1146 | * In both cases leading zeroes are allowed as long as the length in bytes does |
| 1147 | * not exceed the byte length of the group order. |
| 1148 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1149 | * For information regarding how the group is determined, consult the |
| 1150 | * documentation #PSA_PAKE_PRIMITIVE. |
| 1151 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | #define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1153 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1154 | /** The type of the data structure for PAKE cipher suites. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1155 | * |
| 1156 | * This is an implementation-defined \c struct. Applications should not |
| 1157 | * make any assumptions about the content of this structure. |
| 1158 | * Implementation details can change in future versions without notice. |
| 1159 | */ |
| 1160 | typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t; |
| 1161 | |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1162 | /** Return an initial value for a PAKE cipher suite object. |
| 1163 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void); |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1165 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1166 | /** Retrieve the PAKE algorithm from a PAKE cipher suite. |
| 1167 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1168 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1169 | * |
| 1170 | * \return The PAKE algorithm stored in the cipher suite structure. |
| 1171 | */ |
| 1172 | static psa_algorithm_t psa_pake_cs_get_algorithm( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | const psa_pake_cipher_suite_t *cipher_suite); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1174 | |
| 1175 | /** Declare the PAKE algorithm for the cipher suite. |
| 1176 | * |
| 1177 | * This function overwrites any PAKE algorithm |
| 1178 | * previously set in \p cipher_suite. |
| 1179 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1180 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1181 | * \param algorithm The PAKE algorithm to write. |
| 1182 | * (`PSA_ALG_XXX` values of type ::psa_algorithm_t |
| 1183 | * such that #PSA_ALG_IS_PAKE(\c alg) is true.) |
| 1184 | * If this is 0, the PAKE algorithm in |
| 1185 | * \p cipher_suite becomes unspecified. |
| 1186 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite, |
| 1188 | psa_algorithm_t algorithm); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1189 | |
| 1190 | /** Retrieve the primitive from a PAKE cipher suite. |
| 1191 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1192 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1193 | * |
| 1194 | * \return The primitive stored in the cipher suite structure. |
| 1195 | */ |
| 1196 | static psa_pake_primitive_t psa_pake_cs_get_primitive( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | const psa_pake_cipher_suite_t *cipher_suite); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1198 | |
| 1199 | /** Declare the primitive for a PAKE cipher suite. |
| 1200 | * |
| 1201 | * This function overwrites any primitive previously set in \p cipher_suite. |
| 1202 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1203 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1204 | * \param primitive The primitive to write. If this is 0, the |
| 1205 | * primitive type in \p cipher_suite becomes |
| 1206 | * unspecified. |
| 1207 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite, |
| 1209 | psa_pake_primitive_t primitive); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1210 | |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1211 | /** Retrieve the PAKE family from a PAKE cipher suite. |
| 1212 | * |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1213 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1214 | * |
| 1215 | * \return The PAKE family stored in the cipher suite structure. |
| 1216 | */ |
| 1217 | static psa_pake_family_t psa_pake_cs_get_family( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | const psa_pake_cipher_suite_t *cipher_suite); |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1219 | |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1220 | /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite. |
| 1221 | * |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1222 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1223 | * |
| 1224 | * \return The PAKE primitive bit-size stored in the cipher suite structure. |
| 1225 | */ |
| 1226 | static uint16_t psa_pake_cs_get_bits( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | const psa_pake_cipher_suite_t *cipher_suite); |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1228 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1229 | /** Retrieve the hash algorithm from a PAKE cipher suite. |
| 1230 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1231 | * \param[in] cipher_suite The cipher suite structure to query. |
| 1232 | * |
| 1233 | * \return The hash algorithm stored in the cipher suite structure. The return |
| 1234 | * value is 0 if the PAKE is not parametrised by a hash algorithm or if |
| 1235 | * the hash algorithm is not set. |
| 1236 | */ |
| 1237 | static psa_algorithm_t psa_pake_cs_get_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1238 | const psa_pake_cipher_suite_t *cipher_suite); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1239 | |
| 1240 | /** Declare the hash algorithm for a PAKE cipher suite. |
| 1241 | * |
| 1242 | * This function overwrites any hash algorithm |
| 1243 | * previously set in \p cipher_suite. |
| 1244 | * |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1245 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1246 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1247 | * for more information. |
| 1248 | * |
| 1249 | * \param[out] cipher_suite The cipher suite structure to write to. |
| 1250 | * \param hash The hash involved in the cipher suite. |
| 1251 | * (`PSA_ALG_XXX` values of type ::psa_algorithm_t |
| 1252 | * such that #PSA_ALG_IS_HASH(\c alg) is true.) |
| 1253 | * If this is 0, the hash algorithm in |
| 1254 | * \p cipher_suite becomes unspecified. |
| 1255 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite, |
| 1257 | psa_algorithm_t hash); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1258 | |
| 1259 | /** The type of the state data structure for PAKE operations. |
| 1260 | * |
| 1261 | * Before calling any function on a PAKE operation object, the application |
| 1262 | * must initialize it by any of the following means: |
| 1263 | * - Set the structure to all-bits-zero, for example: |
| 1264 | * \code |
| 1265 | * psa_pake_operation_t operation; |
| 1266 | * memset(&operation, 0, sizeof(operation)); |
| 1267 | * \endcode |
| 1268 | * - Initialize the structure to logical zero values, for example: |
| 1269 | * \code |
| 1270 | * psa_pake_operation_t operation = {0}; |
| 1271 | * \endcode |
| 1272 | * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT, |
| 1273 | * for example: |
| 1274 | * \code |
| 1275 | * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT; |
| 1276 | * \endcode |
| 1277 | * - Assign the result of the function psa_pake_operation_init() |
| 1278 | * to the structure, for example: |
| 1279 | * \code |
| 1280 | * psa_pake_operation_t operation; |
| 1281 | * operation = psa_pake_operation_init(); |
| 1282 | * \endcode |
| 1283 | * |
| 1284 | * This is an implementation-defined \c struct. Applications should not |
| 1285 | * make any assumptions about the content of this structure. |
| 1286 | * Implementation details can change in future versions without notice. */ |
| 1287 | typedef struct psa_pake_operation_s psa_pake_operation_t; |
| 1288 | |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 1289 | /** Return an initial value for a PAKE operation object. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1290 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | static psa_pake_operation_t psa_pake_operation_init(void); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1292 | |
| 1293 | /** Set the session information for a password-authenticated key exchange. |
| 1294 | * |
| 1295 | * The sequence of operations to set up a password-authenticated key exchange |
| 1296 | * is as follows: |
| 1297 | * -# Allocate an operation object which will be passed to all the functions |
| 1298 | * listed here. |
| 1299 | * -# Initialize the operation object with one of the methods described in the |
| 1300 | * documentation for #psa_pake_operation_t, e.g. |
| 1301 | * #PSA_PAKE_OPERATION_INIT. |
| 1302 | * -# Call psa_pake_setup() to specify the cipher suite. |
| 1303 | * -# Call \c psa_pake_set_xxx() functions on the operation to complete the |
| 1304 | * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs |
| 1305 | * to be called depends on the algorithm in use. |
| 1306 | * |
| 1307 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1308 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1309 | * for more information. |
| 1310 | * |
| 1311 | * A typical sequence of calls to perform a password-authenticated key |
| 1312 | * exchange: |
| 1313 | * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the |
| 1314 | * key share that needs to be sent to the peer. |
| 1315 | * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide |
| 1316 | * the key share that was received from the peer. |
| 1317 | * -# Depending on the algorithm additional calls to psa_pake_output() and |
| 1318 | * psa_pake_input() might be necessary. |
| 1319 | * -# Call psa_pake_get_implicit_key() for accessing the shared secret. |
| 1320 | * |
| 1321 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1322 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1323 | * for more information. |
| 1324 | * |
| 1325 | * If an error occurs at any step after a call to psa_pake_setup(), |
| 1326 | * the operation will need to be reset by a call to psa_pake_abort(). The |
| 1327 | * application may call psa_pake_abort() at any time after the operation |
| 1328 | * has been initialized. |
| 1329 | * |
| 1330 | * After a successful call to psa_pake_setup(), the application must |
| 1331 | * eventually terminate the operation. The following events terminate an |
| 1332 | * operation: |
| 1333 | * - A call to psa_pake_abort(). |
| 1334 | * - A successful call to psa_pake_get_implicit_key(). |
| 1335 | * |
| 1336 | * \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] | 1337 | * been initialized but not set up yet. |
Neil Armstrong | 47e700e | 2022-05-20 10:16:41 +0200 | [diff] [blame] | 1338 | * \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] | 1339 | * characterizes a PAKE algorithm and determines |
| 1340 | * the algorithm as well.) |
| 1341 | * |
| 1342 | * \retval #PSA_SUCCESS |
| 1343 | * Success. |
Neil Armstrong | 4721a6f | 2022-05-20 10:53:00 +0200 | [diff] [blame] | 1344 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1345 | * The algorithm in \p cipher_suite is not a PAKE algorithm, or the |
| 1346 | * PAKE primitive in \p cipher_suite is not compatible with the |
| 1347 | * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid |
| 1348 | * or not compatible with the PAKE algorithm and primitive. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1349 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Neil Armstrong | 4721a6f | 2022-05-20 10:53:00 +0200 | [diff] [blame] | 1350 | * The algorithm in \p cipher_suite is not a supported PAKE algorithm, |
| 1351 | * or the PAKE primitive in \p cipher_suite is not supported or not |
| 1352 | * compatible with the PAKE algorithm, or the hash algorithm in |
| 1353 | * \p cipher_suite is not supported or not compatible with the PAKE |
| 1354 | * algorithm and primitive. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1355 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1356 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1357 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1358 | * The operation state is not valid, or |
| 1359 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1360 | * It is implementation-dependent whether a failure to initialize |
| 1361 | * results in this error code. |
| 1362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | psa_status_t psa_pake_setup(psa_pake_operation_t *operation, |
| 1364 | const psa_pake_cipher_suite_t *cipher_suite); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1365 | |
| 1366 | /** Set the password for a password-authenticated key exchange from key ID. |
| 1367 | * |
| 1368 | * 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] | 1369 | * is already present in the key store. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1370 | * |
| 1371 | * \param[in,out] operation The operation object to set the password for. It |
| 1372 | * must have been set up by psa_pake_setup() and |
| 1373 | * not yet in use (neither psa_pake_output() nor |
| 1374 | * psa_pake_input() has been called yet). It must |
| 1375 | * be on operation for which the password hasn't |
Janos Follath | 52f9efa | 2021-05-27 08:40:16 +0100 | [diff] [blame] | 1376 | * been set yet (psa_pake_set_password_key() |
Janos Follath | 559f05e | 2021-05-26 15:44:30 +0100 | [diff] [blame] | 1377 | * hasn't been called yet). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1378 | * \param password Identifier of the key holding the password or a |
| 1379 | * value derived from the password (eg. by a |
| 1380 | * memory-hard function). It must remain valid |
| 1381 | * until the operation terminates. It must be of |
| 1382 | * type #PSA_KEY_TYPE_PASSWORD or |
| 1383 | * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow |
| 1384 | * the usage #PSA_KEY_USAGE_DERIVE. |
| 1385 | * |
| 1386 | * \retval #PSA_SUCCESS |
| 1387 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1388 | * \retval #PSA_ERROR_INVALID_HANDLE |
Neil Armstrong | 71cae61 | 2022-05-20 11:00:49 +0200 | [diff] [blame] | 1389 | * \p password is not a valid key identifier. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1390 | * \retval #PSA_ERROR_NOT_PERMITTED |
Neil Armstrong | 71cae61 | 2022-05-20 11:00:49 +0200 | [diff] [blame] | 1391 | * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not |
| 1392 | * permit the \p operation's algorithm. |
| 1393 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1394 | * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or |
| 1395 | * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with |
| 1396 | * the \p operation's cipher suite. |
| 1397 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1398 | * The key type or key size of \p password is not supported with the |
| 1399 | * \p operation's cipher suite. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1400 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1401 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 1402 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 1403 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1404 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1405 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1406 | * The operation state is not valid (it must have been set up.), or |
| 1407 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1408 | * It is implementation-dependent whether a failure to initialize |
| 1409 | * results in this error code. |
| 1410 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation, |
| 1412 | mbedtls_svc_key_id_t password); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1413 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1414 | /** Set the user ID for a password-authenticated key exchange. |
| 1415 | * |
| 1416 | * Call this function to set the user ID. For PAKE algorithms that associate a |
| 1417 | * user identifier with each side of the session you need to call |
| 1418 | * psa_pake_set_peer() as well. For PAKE algorithms that associate a single |
| 1419 | * user identifier with the session, call psa_pake_set_user() only. |
| 1420 | * |
| 1421 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1422 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1423 | * for more information. |
| 1424 | * |
| 1425 | * \param[in,out] operation The operation object to set the user ID for. It |
| 1426 | * must have been set up by psa_pake_setup() and |
| 1427 | * not yet in use (neither psa_pake_output() nor |
| 1428 | * psa_pake_input() has been called yet). It must |
| 1429 | * be on operation for which the user ID hasn't |
| 1430 | * been set (psa_pake_set_user() hasn't been |
| 1431 | * called yet). |
| 1432 | * \param[in] user_id The user ID to authenticate with. |
| 1433 | * \param user_id_len Size of the \p user_id buffer in bytes. |
| 1434 | * |
| 1435 | * \retval #PSA_SUCCESS |
| 1436 | * Success. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1437 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Neil Armstrong | 3585168 | 2022-05-20 11:02:37 +0200 | [diff] [blame] | 1438 | * \p user_id is not valid for the \p operation's algorithm and cipher |
| 1439 | * suite. |
| 1440 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1441 | * The value of \p user_id is not supported by the implementation. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1442 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1443 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1444 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1445 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1446 | * The operation state is not valid, or |
| 1447 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1448 | * It is implementation-dependent whether a failure to initialize |
| 1449 | * results in this error code. |
| 1450 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_status_t psa_pake_set_user(psa_pake_operation_t *operation, |
| 1452 | const uint8_t *user_id, |
| 1453 | size_t user_id_len); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1454 | |
| 1455 | /** Set the peer ID for a password-authenticated key exchange. |
| 1456 | * |
| 1457 | * Call this function in addition to psa_pake_set_user() for PAKE algorithms |
| 1458 | * that associate a user identifier with each side of the session. For PAKE |
| 1459 | * algorithms that associate a single user identifier with the session, call |
| 1460 | * psa_pake_set_user() only. |
| 1461 | * |
| 1462 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1463 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1464 | * for more information. |
| 1465 | * |
| 1466 | * \param[in,out] operation The operation object to set the peer ID for. It |
| 1467 | * must have been set up by psa_pake_setup() and |
| 1468 | * not yet in use (neither psa_pake_output() nor |
| 1469 | * psa_pake_input() has been called yet). It must |
| 1470 | * be on operation for which the peer ID hasn't |
| 1471 | * been set (psa_pake_set_peer() hasn't been |
| 1472 | * called yet). |
| 1473 | * \param[in] peer_id The peer's ID to authenticate. |
| 1474 | * \param peer_id_len Size of the \p peer_id buffer in bytes. |
| 1475 | * |
| 1476 | * \retval #PSA_SUCCESS |
| 1477 | * Success. |
Neil Armstrong | 16ff788 | 2022-05-20 11:04:20 +0200 | [diff] [blame] | 1478 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1479 | * \p user_id is not valid for the \p operation's algorithm and cipher |
| 1480 | * suite. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1481 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1482 | * The algorithm doesn't associate a second identity with the session. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1483 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1484 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1485 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1486 | * \retval #PSA_ERROR_BAD_STATE |
Neil Armstrong | 0d24575 | 2022-05-20 11:35:40 +0200 | [diff] [blame] | 1487 | * Calling psa_pake_set_peer() is invalid with the \p operation's |
| 1488 | * algorithm, the operation state is not valid, or the library has not |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1489 | * been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1490 | * It is implementation-dependent whether a failure to initialize |
| 1491 | * results in this error code. |
| 1492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1493 | psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation, |
| 1494 | const uint8_t *peer_id, |
| 1495 | size_t peer_id_len); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1496 | |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1497 | /** Set the application role for a password-authenticated key exchange. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1498 | * |
| 1499 | * Not all PAKE algorithms need to differentiate the communicating entities. |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1500 | * It is optional to call this function for PAKEs that don't require a role |
| 1501 | * to be specified. For such PAKEs the application role parameter is ignored, |
| 1502 | * or #PSA_PAKE_ROLE_NONE can be passed as \c role. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1503 | * |
| 1504 | * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX` |
| 1505 | * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) |
| 1506 | * for more information. |
| 1507 | * |
Neil Armstrong | ef15751 | 2022-05-25 11:49:45 +0200 | [diff] [blame] | 1508 | * \param[in,out] operation The operation object to specify the |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1509 | * application's role for. It must have been set up |
| 1510 | * by psa_pake_setup() and not yet in use (neither |
| 1511 | * psa_pake_output() nor psa_pake_input() has been |
| 1512 | * called yet). It must be on operation for which |
| 1513 | * the application's role hasn't been specified |
| 1514 | * (psa_pake_set_role() hasn't been called yet). |
| 1515 | * \param role A value of type ::psa_pake_role_t indicating the |
| 1516 | * application's role in the PAKE the algorithm |
| 1517 | * that is being set up. For more information see |
| 1518 | * the documentation of \c PSA_PAKE_ROLE_XXX |
| 1519 | * constants. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1520 | * |
| 1521 | * \retval #PSA_SUCCESS |
| 1522 | * Success. |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1523 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1524 | * The \p role is not a valid PAKE role in the \p operation’s algorithm. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1525 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Neil Armstrong | 2a6dd9c | 2022-05-20 11:17:10 +0200 | [diff] [blame] | 1526 | * The \p role for this algorithm is not supported or is not valid. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1527 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1528 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1529 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1530 | * The operation state is not valid, or |
| 1531 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1532 | * It is implementation-dependent whether a failure to initialize |
| 1533 | * results in this error code. |
| 1534 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | psa_status_t psa_pake_set_role(psa_pake_operation_t *operation, |
| 1536 | psa_pake_role_t role); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1537 | |
| 1538 | /** Get output for a step of a password-authenticated key exchange. |
| 1539 | * |
| 1540 | * Depending on the algorithm being executed, you might need to call this |
| 1541 | * function several times or you might not need to call this at all. |
| 1542 | * |
| 1543 | * The exact sequence of calls to perform a password-authenticated key |
| 1544 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1545 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1546 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1547 | * information. |
| 1548 | * |
| 1549 | * If this function returns an error status, the operation enters an error |
| 1550 | * state and must be aborted by calling psa_pake_abort(). |
| 1551 | * |
| 1552 | * \param[in,out] operation Active PAKE operation. |
| 1553 | * \param step The step of the algorithm for which the output is |
| 1554 | * requested. |
| 1555 | * \param[out] output Buffer where the output is to be written in the |
| 1556 | * format appropriate for this \p step. Refer to |
| 1557 | * the documentation of the individual |
| 1558 | * \c PSA_PAKE_STEP_XXX constants for more |
| 1559 | * information. |
| 1560 | * \param output_size Size of the \p output buffer in bytes. This must |
Neil Armstrong | 7bc71e9 | 2022-05-20 10:36:14 +0200 | [diff] [blame] | 1561 | * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p |
| 1562 | * primitive, \p step) where \p alg and |
| 1563 | * \p primitive are the PAKE algorithm and primitive |
| 1564 | * in the operation's cipher suite, and \p step is |
| 1565 | * the output step. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1566 | * |
| 1567 | * \param[out] output_length On success, the number of bytes of the returned |
| 1568 | * output. |
| 1569 | * |
| 1570 | * \retval #PSA_SUCCESS |
| 1571 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1572 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1573 | * The size of the \p output buffer is too small. |
Neil Armstrong | 664077e | 2022-05-20 11:24:41 +0200 | [diff] [blame] | 1574 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1575 | * \p step is not compatible with the operation's algorithm. |
| 1576 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1577 | * \p step is not supported with the operation's algorithm. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1578 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription |
| 1579 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1580 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1581 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 1582 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 1583 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1584 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1585 | * \retval #PSA_ERROR_BAD_STATE |
Neil Armstrong | e9b4581 | 2022-05-20 11:39:09 +0200 | [diff] [blame] | 1586 | * The operation state is not valid (it must be active, and fully set |
| 1587 | * up, and this call must conform to the algorithm's requirements |
| 1588 | * for ordering of input and output steps), or |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1589 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1590 | * It is implementation-dependent whether a failure to initialize |
| 1591 | * results in this error code. |
| 1592 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | psa_status_t psa_pake_output(psa_pake_operation_t *operation, |
| 1594 | psa_pake_step_t step, |
| 1595 | uint8_t *output, |
| 1596 | size_t output_size, |
| 1597 | size_t *output_length); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1598 | |
| 1599 | /** Provide input for a step of a password-authenticated key exchange. |
| 1600 | * |
| 1601 | * Depending on the algorithm being executed, you might need to call this |
| 1602 | * function several times or you might not need to call this at all. |
| 1603 | * |
| 1604 | * The exact sequence of calls to perform a password-authenticated key |
| 1605 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1606 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1607 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1608 | * information. |
| 1609 | * |
| 1610 | * If this function returns an error status, the operation enters an error |
| 1611 | * state and must be aborted by calling psa_pake_abort(). |
| 1612 | * |
| 1613 | * \param[in,out] operation Active PAKE operation. |
| 1614 | * \param step The step for which the input is provided. |
Neil Armstrong | 799106b | 2022-05-20 10:18:53 +0200 | [diff] [blame] | 1615 | * \param[in] input Buffer containing the input in the format |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1616 | * appropriate for this \p step. Refer to the |
| 1617 | * documentation of the individual |
| 1618 | * \c PSA_PAKE_STEP_XXX constants for more |
| 1619 | * information. |
Neil Armstrong | 799106b | 2022-05-20 10:18:53 +0200 | [diff] [blame] | 1620 | * \param input_length Size of the \p input buffer in bytes. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1621 | * |
| 1622 | * \retval #PSA_SUCCESS |
| 1623 | * Success. |
Neil Armstrong | 407b27b | 2022-05-20 11:28:23 +0200 | [diff] [blame] | 1624 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1625 | * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step. |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1626 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Neil Armstrong | 407b27b | 2022-05-20 11:28:23 +0200 | [diff] [blame] | 1627 | * \p is not compatible with the \p operation’s algorithm, or the |
| 1628 | * \p input is not valid for the \p operation's algorithm, cipher suite |
| 1629 | * or \p step. |
| 1630 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1631 | * \p step p is not supported with the \p operation's algorithm, or the |
| 1632 | * \p input is not supported for the \p operation's algorithm, cipher |
| 1633 | * suite or \p step. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1634 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1635 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1636 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 1637 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 1638 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1639 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1640 | * \retval #PSA_ERROR_BAD_STATE |
Neil Armstrong | e9b4581 | 2022-05-20 11:39:09 +0200 | [diff] [blame] | 1641 | * The operation state is not valid (it must be active, and fully set |
| 1642 | * up, and this call must conform to the algorithm's requirements |
| 1643 | * for ordering of input and output steps), or |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1644 | * the library has not been previously initialized by psa_crypto_init(). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1645 | * It is implementation-dependent whether a failure to initialize |
| 1646 | * results in this error code. |
| 1647 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | psa_status_t psa_pake_input(psa_pake_operation_t *operation, |
| 1649 | psa_pake_step_t step, |
| 1650 | const uint8_t *input, |
| 1651 | size_t input_length); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1652 | |
| 1653 | /** Get implicitly confirmed shared secret from a PAKE. |
| 1654 | * |
| 1655 | * At this point there is a cryptographic guarantee that only the authenticated |
| 1656 | * 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] | 1657 | * 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] | 1658 | * |
Janos Follath | b4db90f | 2021-06-03 13:17:09 +0100 | [diff] [blame] | 1659 | * That is, the authentication is only implicit. Since the peer is not |
| 1660 | * authenticated yet, no action should be taken yet that assumes that the peer |
| 1661 | * is who it claims to be. For example, do not access restricted files on the |
| 1662 | * peer's behalf until an explicit authentication has succeeded. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1663 | * |
| 1664 | * This function can be called after the key exchange phase of the operation |
| 1665 | * has completed. It imports the shared secret output of the PAKE into the |
| 1666 | * provided derivation operation. The input step |
| 1667 | * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key |
| 1668 | * material in the key derivation operation. |
| 1669 | * |
| 1670 | * The exact sequence of calls to perform a password-authenticated key |
| 1671 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 1672 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 1673 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 1674 | * information. |
| 1675 | * |
| 1676 | * When this function returns successfully, \p operation becomes inactive. |
| 1677 | * If this function returns an error status, both \p operation |
| 1678 | * and \p key_derivation operations enter an error state and must be aborted by |
| 1679 | * calling psa_pake_abort() and psa_key_derivation_abort() respectively. |
| 1680 | * |
| 1681 | * \param[in,out] operation Active PAKE operation. |
| 1682 | * \param[out] output A key derivation operation that is ready |
| 1683 | * for an input step of type |
| 1684 | * #PSA_KEY_DERIVATION_INPUT_SECRET. |
| 1685 | * |
| 1686 | * \retval #PSA_SUCCESS |
| 1687 | * Success. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1688 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Neil Armstrong | 97d74b8 | 2022-05-20 11:30:31 +0200 | [diff] [blame] | 1689 | * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the |
| 1690 | * algorithm in the \p output key derivation operation. |
| 1691 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1692 | * Input from a PAKE is not supported by the algorithm in the \p output |
| 1693 | * key derivation operation. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1694 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1695 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1696 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 1697 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 1698 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1699 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1700 | * \retval #PSA_ERROR_BAD_STATE |
Andrzej Kurek | f7c1f74 | 2022-02-03 11:30:54 -0500 | [diff] [blame] | 1701 | * The PAKE operation state is not valid (it must be active, but beyond |
| 1702 | * that validity is specific to the algorithm), or |
| 1703 | * the library has not been previously initialized by psa_crypto_init(), |
| 1704 | * or the state of \p output is not valid for |
| 1705 | * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the |
| 1706 | * step is out of order or the application has done this step already |
| 1707 | * and it may not be repeated. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1708 | * It is implementation-dependent whether a failure to initialize |
| 1709 | * results in this error code. |
| 1710 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 1712 | psa_key_derivation_operation_t *output); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1713 | |
Neil Armstrong | 0c8ef93 | 2022-05-20 10:23:51 +0200 | [diff] [blame] | 1714 | /** Abort a PAKE operation. |
| 1715 | * |
| 1716 | * Aborting an operation frees all associated resources except for the \c |
| 1717 | * operation structure itself. Once aborted, the operation object can be reused |
| 1718 | * for another operation by calling psa_pake_setup() again. |
| 1719 | * |
| 1720 | * This function may be called at any time after the operation |
| 1721 | * object has been initialized as described in #psa_pake_operation_t. |
| 1722 | * |
| 1723 | * In particular, calling psa_pake_abort() after the operation has been |
| 1724 | * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key() |
| 1725 | * is safe and has no effect. |
| 1726 | * |
| 1727 | * \param[in,out] operation The operation to abort. |
| 1728 | * |
| 1729 | * \retval #PSA_SUCCESS |
Neil Armstrong | 59fa8ee | 2022-05-20 11:31:04 +0200 | [diff] [blame] | 1730 | * Success. |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame^] | 1731 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription |
| 1732 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Neil Armstrong | 0c8ef93 | 2022-05-20 10:23:51 +0200 | [diff] [blame] | 1733 | * \retval #PSA_ERROR_BAD_STATE |
| 1734 | * The library has not been previously initialized by psa_crypto_init(). |
| 1735 | * It is implementation-dependent whether a failure to initialize |
| 1736 | * results in this error code. |
| 1737 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | psa_status_t psa_pake_abort(psa_pake_operation_t *operation); |
Neil Armstrong | 0c8ef93 | 2022-05-20 10:23:51 +0200 | [diff] [blame] | 1739 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1740 | /**@}*/ |
| 1741 | |
| 1742 | /** A sufficient output buffer size for psa_pake_output(). |
| 1743 | * |
| 1744 | * If the size of the output buffer is at least this large, it is guaranteed |
| 1745 | * that psa_pake_output() will not fail due to an insufficient output buffer |
| 1746 | * size. The actual size of the output might be smaller in any given call. |
| 1747 | * |
| 1748 | * See also #PSA_PAKE_OUTPUT_MAX_SIZE |
| 1749 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 1750 | * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1751 | * #PSA_ALG_IS_PAKE(\p alg) is true). |
| 1752 | * \param primitive A primitive of type ::psa_pake_primitive_t that is |
| 1753 | * compatible with algorithm \p alg. |
| 1754 | * \param output_step A value of type ::psa_pake_step_t that is valid for the |
| 1755 | * algorithm \p alg. |
| 1756 | * \return A sufficient output buffer size for the specified |
Neil Armstrong | cd974d5 | 2022-05-20 10:30:12 +0200 | [diff] [blame] | 1757 | * PAKE algorithm, primitive, and output step. If the |
| 1758 | * PAKE algorithm, primitive, or output step is not |
| 1759 | * recognized, or the parameters are incompatible, |
| 1760 | * return 0. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1761 | */ |
Neil Armstrong | 7aaa34a | 2022-06-08 14:05:02 +0200 | [diff] [blame] | 1762 | #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1763 | (alg == PSA_ALG_JPAKE && \ |
| 1764 | primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \ |
| 1765 | PSA_ECC_FAMILY_SECP_R1, 256) ? \ |
| 1766 | ( \ |
| 1767 | output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \ |
| 1768 | output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \ |
| 1769 | 32 \ |
| 1770 | ) : \ |
| 1771 | 0) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1772 | |
| 1773 | /** A sufficient input buffer size for psa_pake_input(). |
| 1774 | * |
Janos Follath | b4db90f | 2021-06-03 13:17:09 +0100 | [diff] [blame] | 1775 | * The value returned by this macro is guaranteed to be large enough for any |
| 1776 | * valid input to psa_pake_input() in an operation with the specified |
| 1777 | * parameters. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1778 | * |
| 1779 | * See also #PSA_PAKE_INPUT_MAX_SIZE |
| 1780 | * |
Janos Follath | 46c0237 | 2021-06-08 15:22:51 +0100 | [diff] [blame] | 1781 | * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1782 | * #PSA_ALG_IS_PAKE(\p alg) is true). |
| 1783 | * \param primitive A primitive of type ::psa_pake_primitive_t that is |
| 1784 | * compatible with algorithm \p alg. |
Janos Follath | ec83eb6 | 2021-05-27 08:41:59 +0100 | [diff] [blame] | 1785 | * \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] | 1786 | * algorithm \p alg. |
Janos Follath | 38d29db | 2021-06-03 13:14:42 +0100 | [diff] [blame] | 1787 | * \return A sufficient input buffer size for the specified |
| 1788 | * input, cipher suite and algorithm. If the cipher suite, |
| 1789 | * the input type or PAKE algorithm is not recognized, or |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1790 | * the parameters are incompatible, return 0. |
| 1791 | */ |
Neil Armstrong | 7aaa34a | 2022-06-08 14:05:02 +0200 | [diff] [blame] | 1792 | #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | (alg == PSA_ALG_JPAKE && \ |
| 1794 | primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \ |
| 1795 | PSA_ECC_FAMILY_SECP_R1, 256) ? \ |
| 1796 | ( \ |
| 1797 | input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \ |
| 1798 | input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \ |
| 1799 | 32 \ |
| 1800 | ) : \ |
| 1801 | 0) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1802 | |
Neil Armstrong | 2056ce5 | 2022-05-25 11:38:15 +0200 | [diff] [blame] | 1803 | /** Output buffer size for psa_pake_output() for any of the supported PAKE |
| 1804 | * algorithm and primitive suites and output step. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1805 | * |
| 1806 | * This macro must expand to a compile-time constant integer. |
| 1807 | * |
Neil Armstrong | 2056ce5 | 2022-05-25 11:38:15 +0200 | [diff] [blame] | 1808 | * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p step). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1809 | */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1810 | #define PSA_PAKE_OUTPUT_MAX_SIZE 65 |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1811 | |
Neil Armstrong | 2056ce5 | 2022-05-25 11:38:15 +0200 | [diff] [blame] | 1812 | /** Input buffer size for psa_pake_input() for any of the supported PAKE |
| 1813 | * algorithm and primitive suites and input step. |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1814 | * |
| 1815 | * This macro must expand to a compile-time constant integer. |
| 1816 | * |
Neil Armstrong | 2056ce5 | 2022-05-25 11:38:15 +0200 | [diff] [blame] | 1817 | * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p step). |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1818 | */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1819 | #define PSA_PAKE_INPUT_MAX_SIZE 65 |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1820 | |
Neil Armstrong | fb99302 | 2022-05-20 10:08:58 +0200 | [diff] [blame] | 1821 | /** Returns a suitable initializer for a PAKE cipher suite object of type |
| 1822 | * psa_pake_cipher_suite_t. |
| 1823 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | #define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE } |
Neil Armstrong | fb99302 | 2022-05-20 10:08:58 +0200 | [diff] [blame] | 1825 | |
Neil Armstrong | 0151c55 | 2022-05-20 10:13:53 +0200 | [diff] [blame] | 1826 | /** Returns a suitable initializer for a PAKE operation object of type |
| 1827 | * psa_pake_operation_t. |
| 1828 | */ |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1829 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | #define PSA_PAKE_OPERATION_INIT { PSA_ALG_NONE, 0, 0, 0, 0, \ |
| 1831 | NULL, 0, \ |
| 1832 | PSA_PAKE_ROLE_NONE, { 0 }, 0, 0, \ |
| 1833 | { .dummy = 0 } } |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1834 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1835 | #define PSA_PAKE_OPERATION_INIT { PSA_ALG_NONE, 0, 0, { 0 } } |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1836 | #endif |
Neil Armstrong | 0151c55 | 2022-05-20 10:13:53 +0200 | [diff] [blame] | 1837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | struct psa_pake_cipher_suite_s { |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1839 | psa_algorithm_t algorithm; |
| 1840 | psa_pake_primitive_type_t type; |
| 1841 | psa_pake_family_t family; |
| 1842 | uint16_t bits; |
| 1843 | psa_algorithm_t hash; |
| 1844 | }; |
| 1845 | |
| 1846 | static inline psa_algorithm_t psa_pake_cs_get_algorithm( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | const psa_pake_cipher_suite_t *cipher_suite) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1848 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | return cipher_suite->algorithm; |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | static inline void psa_pake_cs_set_algorithm( |
| 1853 | psa_pake_cipher_suite_t *cipher_suite, |
| 1854 | psa_algorithm_t algorithm) |
| 1855 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | if (!PSA_ALG_IS_PAKE(algorithm)) { |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1857 | cipher_suite->algorithm = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | } else { |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1859 | cipher_suite->algorithm = algorithm; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | } |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | static inline psa_pake_primitive_t psa_pake_cs_get_primitive( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | const psa_pake_cipher_suite_t *cipher_suite) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1865 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1866 | return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family, |
| 1867 | cipher_suite->bits); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static inline void psa_pake_cs_set_primitive( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | psa_pake_cipher_suite_t *cipher_suite, |
| 1872 | psa_pake_primitive_t primitive) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1873 | { |
| 1874 | cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24); |
| 1875 | cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16)); |
| 1876 | cipher_suite->bits = (uint16_t) (0xFFFF & primitive); |
| 1877 | } |
| 1878 | |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1879 | static inline psa_pake_family_t psa_pake_cs_get_family( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | const psa_pake_cipher_suite_t *cipher_suite) |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1881 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | return cipher_suite->family; |
Neil Armstrong | ff9cac7 | 2022-05-20 10:25:15 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1885 | static inline uint16_t psa_pake_cs_get_bits( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | const psa_pake_cipher_suite_t *cipher_suite) |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1887 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | return cipher_suite->bits; |
Neil Armstrong | d5a4825 | 2022-05-20 10:26:36 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1891 | static inline psa_algorithm_t psa_pake_cs_get_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | const psa_pake_cipher_suite_t *cipher_suite) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1893 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | return cipher_suite->hash; |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1897 | static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite, |
| 1898 | psa_algorithm_t hash) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1899 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | if (!PSA_ALG_IS_HASH(hash)) { |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1901 | cipher_suite->hash = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | } else { |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1903 | cipher_suite->hash = hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | } |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 1907 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1908 | #include <mbedtls/ecjpake.h> |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1909 | /* Note: the format for mbedtls_ecjpake_read/write function has an extra |
| 1910 | * length byte for each step, plus an extra 3 bytes for ECParameters in the |
| 1911 | * server's 2nd round. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | #define MBEDTLS_PSA_PAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2) |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1913 | #endif |
| 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | struct psa_pake_operation_s { |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1916 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 1917 | unsigned int MBEDTLS_PRIVATE(state); |
| 1918 | unsigned int MBEDTLS_PRIVATE(sequence); |
| 1919 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) |
| 1920 | unsigned int MBEDTLS_PRIVATE(input_step); |
| 1921 | unsigned int MBEDTLS_PRIVATE(output_step); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | uint8_t *MBEDTLS_PRIVATE(password); |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 1923 | size_t MBEDTLS_PRIVATE(password_len); |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1924 | psa_pake_role_t MBEDTLS_PRIVATE(role); |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 1925 | uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_PAKE_BUFFER_SIZE]); |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1926 | size_t MBEDTLS_PRIVATE(buffer_length); |
| 1927 | size_t MBEDTLS_PRIVATE(buffer_offset); |
| 1928 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | union { |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 1930 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1931 | mbedtls_ecjpake_context ecjpake; |
| 1932 | #endif |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1933 | /* Make the union non-empty even with no supported algorithms. */ |
| 1934 | uint8_t dummy; |
Neil Armstrong | 35269d9 | 2022-05-25 11:26:31 +0200 | [diff] [blame] | 1935 | } MBEDTLS_PRIVATE(ctx); |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1936 | }; |
| 1937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void) |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1939 | { |
| 1940 | const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | return v; |
Neil Armstrong | 5ff6a7f | 2022-05-20 10:12:01 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | static inline struct psa_pake_operation_s psa_pake_operation_init(void) |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1945 | { |
| 1946 | const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | return v; |
Janos Follath | 702cf09 | 2021-05-26 12:58:23 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1950 | #ifdef __cplusplus |
| 1951 | } |
| 1952 | #endif |
| 1953 | |
| 1954 | #endif /* PSA_CRYPTO_EXTRA_H */ |