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