blob: 9bc68f6a268e18c71a7be22ab40a782729b7b43d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto_extra.h
3 *
4 * \brief PSA cryptography module: Mbed TLS vendor extensions
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
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 Peskinee59236f2018-01-27 23:32:46 +010010 */
11/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020012 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000013 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +010014 */
15
16#ifndef PSA_CRYPTO_EXTRA_H
17#define PSA_CRYPTO_EXTRA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020018#include "mbedtls/private_access.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010019
Gilles Peskine09c02ee2021-11-25 20:30:47 +010020#include "crypto_types.h"
Gilles Peskine7a894f22019-11-26 16:06:46 +010021#include "crypto_compat.h"
22
Gilles Peskinee59236f2018-01-27 23:32:46 +010023#ifdef __cplusplus
24extern "C" {
25#endif
26
Netanel Gonen2bcd3122018-11-19 11:53:02 +020027/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020028#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020029
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020030/* See mbedtls_config.h for definition */
Steven Cooreman863470a2021-02-15 14:03:19 +010031#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
32#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
Steven Cooreman1f968fd2021-02-15 14:00:24 +010033#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000034
Valerio Settice849212024-08-29 15:02:47 +020035/* If the size of static key slots is not explicitly defined by the user, then
36 * set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE and
37 * PSA_CIPHER_MAX_KEY_LENGTH.
38 * See mbedtls_config.h for the definition. */
39#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)
40#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE \
41 ((PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > PSA_CIPHER_MAX_KEY_LENGTH) ? \
42 PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE : PSA_CIPHER_MAX_KEY_LENGTH)
43#endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/
44
Valerio Setti0c4a74b2024-09-26 17:26:02 +020045/* Define the size of the each key slot buffer. */
46#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
47#define MBEDTLS_PSA_KEY_BUFFER_MAX_SIZE MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
48#else
49#define MBEDTLS_PSA_KEY_BUFFER_MAX_SIZE SIZE_MAX
50#endif
51
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020052/** \addtogroup attributes
53 * @{
54 */
55
56/** \brief Declare the enrollment algorithm for a key.
57 *
58 * An operation on a key may indifferently use the algorithm set with
59 * psa_set_key_algorithm() or with this function.
60 *
61 * \param[out] attributes The attribute structure to write to.
62 * \param alg2 A second algorithm that the key may be used
63 * for, in addition to the algorithm set with
64 * psa_set_key_algorithm().
65 *
66 * \warning Setting an enrollment algorithm is not recommended, because
67 * using the same key with different algorithms can allow some
68 * attacks based on arithmetic relations between different
69 * computations made with the same key, or can escalate harmless
70 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020071 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020072 * verified that the usage of the key with multiple algorithms
73 * is safe.
74 */
75static inline void psa_set_key_enrollment_algorithm(
76 psa_key_attributes_t *attributes,
77 psa_algorithm_t alg2)
78{
Gilles Peskine2f107ae2024-02-28 01:26:46 +010079 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020080}
81
82/** Retrieve the enrollment algorithm policy from key attributes.
83 *
84 * \param[in] attributes The key attribute structure to query.
85 *
86 * \return The enrollment algorithm stored in the attribute structure.
87 */
88static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
89 const psa_key_attributes_t *attributes)
90{
Gilles Peskine2f107ae2024-02-28 01:26:46 +010091 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020092}
93
Gilles Peskinec8000c02019-08-02 20:15:51 +020094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
95
96/** Retrieve the slot number where a key is stored.
97 *
98 * A slot number is only defined for keys that are stored in a secure
99 * element.
100 *
101 * This information is only useful if the secure element is not entirely
102 * managed through the PSA Cryptography API. It is up to the secure
103 * element driver to decide how PSA slot numbers map to any other interface
104 * that the secure element may have.
105 *
106 * \param[in] attributes The key attribute structure to query.
107 * \param[out] slot_number On success, the slot number containing the key.
108 *
109 * \retval #PSA_SUCCESS
110 * The key is located in a secure element, and \p *slot_number
111 * indicates the slot number that contains it.
112 * \retval #PSA_ERROR_NOT_PERMITTED
113 * The caller is not permitted to query the slot number.
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200114 * Mbed TLS currently does not return this error.
Gilles Peskinec8000c02019-08-02 20:15:51 +0200115 * \retval #PSA_ERROR_INVALID_ARGUMENT
116 * The key is not located in a secure element.
117 */
118psa_status_t psa_get_key_slot_number(
119 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 psa_key_slot_number_t *slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200121
122/** Choose the slot number where a key is stored.
123 *
124 * This function declares a slot number in the specified attribute
125 * structure.
126 *
127 * A slot number is only meaningful for keys that are stored in a secure
128 * element. It is up to the secure element driver to decide how PSA slot
129 * numbers map to any other interface that the secure element may have.
130 *
131 * \note Setting a slot number in key attributes for a key creation can
132 * cause the following errors when creating the key:
133 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
134 * not support choosing a specific slot number.
135 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
136 * choose slot numbers in general or to choose this specific slot.
137 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
138 * valid in general or not valid for this specific key.
139 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
140 * selected slot.
141 *
142 * \param[out] attributes The attribute structure to write to.
143 * \param slot_number The slot number to set.
144 */
145static inline void psa_set_key_slot_number(
146 psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 psa_key_slot_number_t slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +0200148{
Gilles Peskine972539c2024-02-28 01:49:45 +0100149 attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100150 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200151}
152
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200153/** Remove the slot number attribute from a key attribute structure.
154 *
155 * This function undoes the action of psa_set_key_slot_number().
156 *
157 * \param[out] attributes The attribute structure to write to.
158 */
159static inline void psa_clear_key_slot_number(
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 psa_key_attributes_t *attributes)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200161{
Gilles Peskine972539c2024-02-28 01:49:45 +0100162 attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200163}
164
Gilles Peskined7729582019-08-05 15:55:54 +0200165/** Register a key that is already present in a secure element.
166 *
167 * The key must be located in a secure element designated by the
168 * lifetime field in \p attributes, in the slot set with
169 * psa_set_key_slot_number() in the attribute structure.
170 * This function makes the key available through the key identifier
171 * specified in \p attributes.
172 *
173 * \param[in] attributes The attributes of the existing key.
Gilles Peskined72ad732024-06-13 16:06:45 +0200174 * - The lifetime must be a persistent lifetime
175 * in a secure element. Volatile lifetimes are
176 * not currently supported.
177 * - The key identifier must be in the valid
178 * range for persistent keys.
179 * - The key type and size must be specified and
180 * must be consistent with the key material
181 * in the secure element.
Gilles Peskined7729582019-08-05 15:55:54 +0200182 *
183 * \retval #PSA_SUCCESS
184 * The key was successfully registered.
185 * Note that depending on the design of the driver, this may or may
186 * not guarantee that a key actually exists in the designated slot
187 * and is compatible with the specified attributes.
188 * \retval #PSA_ERROR_ALREADY_EXISTS
189 * There is already a key with the identifier specified in
190 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200191 * \retval #PSA_ERROR_NOT_SUPPORTED
192 * The secure element driver for the specified lifetime does not
193 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200194 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200195 * The identifier in \p attributes is invalid, namely the identifier is
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500196 * not in the user range, or
Gilles Peskined7729582019-08-05 15:55:54 +0200197 * \p attributes specifies a lifetime which is not located
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500198 * in a secure element, or no slot number is specified in \p attributes,
Gilles Peskined7729582019-08-05 15:55:54 +0200199 * or the specified slot number is not valid.
200 * \retval #PSA_ERROR_NOT_PERMITTED
201 * The caller is not authorized to register the specified key slot.
Gilles Peskineed733552023-02-14 19:21:09 +0100202 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
203 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
205 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
206 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
207 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskined7729582019-08-05 15:55:54 +0200208 * \retval #PSA_ERROR_BAD_STATE
209 * The library has not been previously initialized by psa_crypto_init().
210 * It is implementation-dependent whether a failure to initialize
211 * results in this error code.
212 */
213psa_status_t mbedtls_psa_register_se_key(
214 const psa_key_attributes_t *attributes);
215
Gilles Peskinec8000c02019-08-02 20:15:51 +0200216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
217
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200218/**@}*/
219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220/**
221 * \brief Library deinitialization.
222 *
223 * This function clears all data associated with the PSA layer,
224 * including the whole key store.
Ryan Everett16abd592024-01-24 17:37:46 +0000225 * This function is not thread safe, it wipes every key slot regardless of
226 * state and reader count. It should only be called when no slot is in use.
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 *
228 * This is an Mbed TLS extension.
229 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100231
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200232/** \brief Statistics about
233 * resource consumption related to the PSA keystore.
234 *
235 * \note The content of this structure is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200236 * of Mbed TLS and may change arbitrarily from version to version.
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200239 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200240 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200241 /** Number of slots containing key material for a key which is in
242 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200243 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200244 /** Number of slots containing a reference to a key in a
245 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200246 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200247 /** Number of slots which are occupied, but do not contain
248 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200249 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200250 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200251 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200252 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200253 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100254 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200255 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200256 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200257 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200258 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200259 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200260} mbedtls_psa_stats_t;
261
262/** \brief Get statistics about
263 * resource consumption related to the PSA keystore.
264 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200265 * \note When Mbed TLS is built as part of a service, with isolation
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200266 * between the application and the keystore, the service may or
267 * may not expose this function.
268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200270
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200271/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100272 * \brief Inject an initial entropy seed for the random generator into
273 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100274 *
275 * This function injects data to be used as a seed for the random generator
276 * used by the PSA Crypto implementation. On devices that lack a trusted
277 * entropy source (preferably a hardware random number generator),
278 * the Mbed PSA Crypto implementation uses this value to seed its
279 * random generator.
280 *
281 * On devices without a trusted entropy source, this function must be
282 * called exactly once in the lifetime of the device. On devices with
283 * a trusted entropy source, calling this function is optional.
284 * In all cases, this function may only be called before calling any
285 * other function in the PSA Crypto API, including psa_crypto_init().
286 *
287 * When this function returns successfully, it populates a file in
288 * persistent storage. Once the file has been created, this function
289 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100290 *
291 * If any error occurs, this function does not change the system state.
292 * You can call this function again after correcting the reason for the
293 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200294 *
295 * \warning This function **can** fail! Callers MUST check the return status.
296 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100297 * \warning If you use this function, you should use it as part of a
298 * factory provisioning process. The value of the injected seed
299 * is critical to the security of the device. It must be
300 * *secret*, *unpredictable* and (statistically) *unique per device*.
301 * You should be generate it randomly using a cryptographically
302 * secure random generator seeded from trusted entropy sources.
303 * You should transmit it securely to the device and ensure
304 * that its value is not leaked or stored anywhere beyond the
305 * needs of transmitting it from the point of generation to
306 * the call of this function, and erase all copies of the value
307 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200308 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100309 * This is an Mbed TLS extension.
310 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200311 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100312 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
313 * Note that you must provide compatible implementations of
314 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200315 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200316 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200317 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200318 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200319 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000320 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
321 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
322 * in `library/entropy_poll.h` in the Mbed TLS source
323 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200324 * It must be less or equal to
325 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200326 *
327 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100328 * The seed value was injected successfully. The random generator
329 * of the PSA Crypto implementation is now ready for use.
330 * You may now call psa_crypto_init() and use the PSA Crypto
331 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200332 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100333 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200334 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100335 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200336 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100337 * The library has already been initialized. It is no longer
338 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200339 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100340psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200341 size_t seed_size);
342
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200343/** \addtogroup crypto_types
344 * @{
345 */
346
Gilles Peskinea1302192019-05-16 13:58:24 +0200347/** DSA public key.
348 *
349 * The import and export format is the
350 * representation of the public key `y = g^x mod p` as a big-endian byte
351 * string. The length of the byte string is the length of the base prime `p`
352 * in bytes.
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200355
356/** DSA key pair (private and public key).
357 *
358 * The import and export format is the
359 * representation of the private key `x` as a big-endian byte string. The
360 * length of the byte string is the private key size in bytes (leading zeroes
361 * are not stripped).
362 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800363 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200364 * FIPS 186-4 §B.1.2: interpret the byte string as integer
365 * in big-endian order. Discard it if it is not in the range
366 * [0, *N* - 2] where *N* is the boundary of the private key domain
367 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
368 * or the order of the curve's base point for ECC).
369 * Add 1 to the resulting integer and use this as the private key *x*.
370 *
371 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200373
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100374/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200375#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200376 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200379/** DSA signature with hashing.
380 *
381 * This is the signature scheme defined by FIPS 186-4,
382 * with a random per-message secret number (*k*).
383 *
384 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
385 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
386 * This includes #PSA_ALG_ANY_HASH
387 * when specifying the algorithm in a usage policy.
388 *
389 * \return The corresponding DSA signature algorithm.
390 * \return Unspecified if \p hash_alg is not a supported
391 * hash algorithm.
392 */
393#define PSA_ALG_DSA(hash_alg) \
394 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100395#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100396#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200397/** Deterministic DSA signature with hashing.
398 *
399 * This is the deterministic variant defined by RFC 6979 of
400 * the signature scheme defined by FIPS 186-4.
401 *
402 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
403 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
404 * This includes #PSA_ALG_ANY_HASH
405 * when specifying the algorithm in a usage policy.
406 *
407 * \return The corresponding DSA signature algorithm.
408 * \return Unspecified if \p hash_alg is not a supported
409 * hash algorithm.
410 */
411#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
412 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
413#define PSA_ALG_IS_DSA(alg) \
414 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
415 PSA_ALG_DSA_BASE)
416#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
417 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
418#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
419 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
420#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
421 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
422
423
424/* We need to expand the sample definition of this macro from
425 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100426#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
427#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
428 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200429
430/**@}*/
431
Gilles Peskine24f10f82019-05-16 12:18:32 +0200432/** \addtogroup attributes
433 * @{
434 */
435
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100436/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100437#define PSA_PAKE_OPERATION_STAGE_SETUP 0
438#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
439#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200440
Gilles Peskine24f10f82019-05-16 12:18:32 +0200441/**@}*/
442
Gilles Peskine5055b232019-12-12 17:49:31 +0100443
Gilles Peskineb8af2282020-11-13 18:00:34 +0100444/** \defgroup psa_external_rng External random generator
445 * @{
446 */
447
448#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
449/** External random generator function, implemented by the platform.
450 *
451 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
452 * this function replaces Mbed TLS's entropy and DRBG modules for all
453 * random generation triggered via PSA crypto interfaces.
454 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100455 * \note This random generator must deliver random numbers with cryptographic
456 * quality and high performance. It must supply unpredictable numbers
457 * with a uniform distribution. The implementation of this function
458 * is responsible for ensuring that the random generator is seeded
459 * with sufficient entropy. If you have a hardware TRNG which is slow
460 * or delivers non-uniform output, declare it as an entropy source
461 * with mbedtls_entropy_add_source() instead of enabling this option.
462 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100463 * \param[in,out] context Pointer to the random generator context.
464 * This is all-bits-zero on the first call
465 * and preserved between successive calls.
466 * \param[out] output Output buffer. On success, this buffer
467 * contains random data with a uniform
468 * distribution.
469 * \param output_size The size of the \p output buffer in bytes.
470 * \param[out] output_length On success, set this value to \p output_size.
471 *
472 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100473 * Success. The output buffer contains \p output_size bytes of
474 * cryptographic-quality random data, and \c *output_length is
475 * set to \p output_size.
476 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
477 * The random generator requires extra entropy and there is no
478 * way to obtain entropy under current environment conditions.
479 * This error should not happen under normal circumstances since
480 * this function is responsible for obtaining as much entropy as
481 * it needs. However implementations of this function may return
482 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
483 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100484 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100485 * A failure of the random generator hardware that isn't covered
486 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100487 */
488psa_status_t mbedtls_psa_external_get_random(
489 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100491#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
492
493/**@}*/
494
Steven Cooreman6801f082021-02-19 17:21:22 +0100495/** \defgroup psa_builtin_keys Built-in keys
496 * @{
497 */
498
499/** The minimum value for a key identifier that is built into the
500 * implementation.
501 *
502 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
503 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
504 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
505 * with any other set of implementation-chosen key identifiers.
506 *
Gilles Peskine543909d2024-06-20 22:10:08 +0200507 * This value is part of the library's API since changing it would invalidate
Steven Cooreman6801f082021-02-19 17:21:22 +0100508 * the values of built-in key identifiers in applications.
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100511
512/** The maximum value for a key identifier that is built into the
513 * implementation.
514 *
515 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
516 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100518
519/** A slot number identifying a key in a driver.
520 *
521 * Values of this type are used to identify built-in keys.
522 */
523typedef uint64_t psa_drv_slot_number_t;
524
525#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
526/** Test whether a key identifier belongs to the builtin key range.
527 *
528 * \param key_id Key identifier to test.
529 *
530 * \retval 1
531 * The key identifier is a builtin key identifier.
532 * \retval 0
533 * The key identifier is not a builtin key identifier.
534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100536{
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
538 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100539}
540
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200541/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100542 *
543 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100544 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100545 * as part of a platform's system image.
546 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100547 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100548 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
549 *
550 * In a multi-application configuration
551 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
552 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
553 * is allowed to use the given key.
554 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100555 * \param key_id The key ID for which to retrieve the
556 * location and slot attributes.
557 * \param[out] lifetime On success, the lifetime associated with the key
558 * corresponding to \p key_id. Lifetime is a
559 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200560 * and with what persistence level the key is
561 * intended to be used. If the platform
562 * implementation does not contain specific
563 * information about the intended key persistence
564 * level, the persistence level may be reported as
565 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100566 * \param[out] slot_number On success, the slot number known to the driver
567 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200568 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100569 * requested built-in key.
570 *
571 * \retval #PSA_SUCCESS
572 * The requested key identifier designates a built-in key.
573 * In a multi-application configuration, the requested owner
574 * is allowed to access it.
575 * \retval #PSA_ERROR_DOES_NOT_EXIST
576 * The requested key identifier is not a built-in key which is known
577 * to this function. If a key exists in the key storage with this
578 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100579 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100580 * Any other error is propagated to the function that requested the key.
581 * Common errors include:
582 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
583 * is not allowed to access it.
584 */
585psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100586 mbedtls_svc_key_id_t key_id,
587 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100589#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
590
591/** @} */
592
Janos Follath702cf092021-05-26 12:58:23 +0100593/** \addtogroup crypto_types
594 * @{
595 */
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100598
599/** Whether the specified algorithm is a password-authenticated key exchange.
600 *
601 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
602 *
603 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
604 * algorithm, 0 otherwise.
605 * This macro may return either 0 or 1 if \p alg is not a supported
606 * algorithm identifier.
607 */
608#define PSA_ALG_IS_PAKE(alg) \
609 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
610
611/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
612 *
613 * This is J-PAKE as defined by RFC 8236, instantiated with the following
614 * parameters:
615 *
616 * - The group can be either an elliptic curve or defined over a finite field.
617 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
618 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100619 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100620 *
Janos Follath46c02372021-06-08 15:22:51 +0100621 * To select these parameters and set up the cipher suite, call these functions
622 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100623 *
624 * \code
625 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
626 * psa_pake_cs_set_primitive(cipher_suite,
627 * PSA_PAKE_PRIMITIVE(type, family, bits));
628 * psa_pake_cs_set_hash(cipher_suite, hash);
629 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100630 *
631 * For more information on how to set a specific curve or field, refer to the
632 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
633 *
634 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100635 *
636 * \code
637 * psa_pake_setup(operation, cipher_suite);
638 * psa_pake_set_user(operation, ...);
639 * psa_pake_set_peer(operation, ...);
640 * psa_pake_set_password_key(operation, ...);
641 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100642 *
Neil Armstrong16145372022-05-20 10:42:36 +0200643 * The password is provided as a key. This can be the password text itself,
644 * in an agreed character encoding, or some value derived from the password
645 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100646 *
Neil Armstrong16145372022-05-20 10:42:36 +0200647 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100648 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
649 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
650 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200651 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100652 * of the reduction is 0.)
653 *
654 * The key exchange flow for J-PAKE is as follows:
655 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100656 * \code
657 * // Get g1
658 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
659 * // Get the ZKP public key for x1
660 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
661 * // Get the ZKP proof for x1
662 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
663 * // Get g2
664 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
665 * // Get the ZKP public key for x2
666 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
667 * // Get the ZKP proof for x2
668 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
669 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100670 * -# To provide the first round data received from the peer to the operation,
671 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100672 * \code
673 * // Set g3
674 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
675 * // Set the ZKP public key for x3
676 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
677 * // Set the ZKP proof for x3
678 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
679 * // Set g4
680 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
681 * // Set the ZKP public key for x4
682 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
683 * // Set the ZKP proof for x4
684 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
685 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100686 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100687 * \code
688 * // Get A
689 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
690 * // Get ZKP public key for x2*s
691 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
692 * // Get ZKP proof for x2*s
693 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
694 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100695 * -# To provide the second round data received from the peer to the operation,
696 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100697 * \code
698 * // Set B
699 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
700 * // Set ZKP public key for x4*s
701 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
702 * // Set ZKP proof for x4*s
703 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
704 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100705 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100706 * \code
707 * // Get Ka=Kb=K
708 * psa_pake_get_implicit_key()
709 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100710 *
711 * For more information consult the documentation of the individual
712 * \c PSA_PAKE_STEP_XXX constants.
713 *
714 * At this point there is a cryptographic guarantee that only the authenticated
715 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100716 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100717 *
718 * That is, the authentication is only implicit (the peer is not authenticated
719 * at this point, and no action should be taken that assume that they are - like
720 * for example accessing restricted files).
721 *
722 * To make the authentication explicit there are various methods, see Section 5
723 * of RFC 8236 for two examples.
724 *
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100727
728/** @} */
729
730/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100731 *
732 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
733 * the official PSA Crypto API yet.
734 *
735 * \note The content of this section is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200736 * of Mbed TLS and may change arbitrarily from version to version.
Janos Follath7d69b3a2021-05-26 13:10:56 +0100737 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
738 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100739 * @{
740 */
741
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200742/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100743 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200744 * Encodes the application's role in the algorithm is being executed. For more
745 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
746 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100747 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200748typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100749
750/** Encoding of input and output indicators for PAKE.
751 *
752 * Some PAKE algorithms need to exchange more data than just a single key share.
753 * This type is for encoding additional input and output data for such
754 * algorithms.
755 */
756typedef uint8_t psa_pake_step_t;
757
758/** Encoding of the type of the PAKE's primitive.
759 *
760 * Values defined by this standard will never be in the range 0x80-0xff.
761 * Vendors who define additional types must use an encoding in this range.
762 *
763 * For more information see the documentation of individual
764 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
765 */
766typedef uint8_t psa_pake_primitive_type_t;
767
768/** \brief Encoding of the family of the primitive associated with the PAKE.
769 *
770 * For more information see the documentation of individual
771 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
772 */
773typedef uint8_t psa_pake_family_t;
774
775/** \brief Encoding of the primitive associated with the PAKE.
776 *
777 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
778 */
779typedef uint32_t psa_pake_primitive_t;
780
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200781/** A value to indicate no role in a PAKE algorithm.
782 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
783 * algorithms which do not assign roles.
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200786
Janos Follath702cf092021-05-26 12:58:23 +0100787/** The first peer in a balanced PAKE.
788 *
789 * Although balanced PAKE algorithms are symmetric, some of them needs an
790 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200791 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100792 * accepted.
793 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100795
796/** The second peer in a balanced PAKE.
797 *
798 * Although balanced PAKE algorithms are symmetric, some of them needs an
799 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200800 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100801 * accepted.
802 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100804
805/** The client in an augmented PAKE.
806 *
807 * Augmented PAKE algorithms need to differentiate between client and server.
808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +0100810
811/** The server in an augmented PAKE.
812 *
813 * Augmented PAKE algorithms need to differentiate between client and server.
814 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +0100816
817/** The PAKE primitive type indicating the use of elliptic curves.
818 *
819 * The values of the \c family and \c bits fields of the cipher suite identify a
820 * specific elliptic curve, using the same mapping that is used for ECC
821 * (::psa_ecc_family_t) keys.
822 *
823 * (Here \c family means the value returned by psa_pake_cs_get_family() and
824 * \c bits means the value returned by psa_pake_cs_get_bits().)
825 *
826 * Input and output during the operation can involve group elements and scalar
827 * values:
828 * -# The format for group elements is the same as for public keys on the
829 * specific curve would be. For more information, consult the documentation of
830 * psa_export_public_key().
831 * -# The format for scalars is the same as for private keys on the specific
832 * curve would be. For more information, consult the documentation of
833 * psa_export_key().
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100836
837/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
838 *
839 * The values of the \c family and \c bits fields of the cipher suite identify
840 * a specific Diffie-Hellman group, using the same mapping that is used for
841 * Diffie-Hellman (::psa_dh_family_t) keys.
842 *
843 * (Here \c family means the value returned by psa_pake_cs_get_family() and
844 * \c bits means the value returned by psa_pake_cs_get_bits().)
845 *
846 * Input and output during the operation can involve group elements and scalar
847 * values:
848 * -# The format for group elements is the same as for public keys on the
849 * specific group would be. For more information, consult the documentation of
850 * psa_export_public_key().
851 * -# The format for scalars is the same as for private keys on the specific
852 * group would be. For more information, consult the documentation of
853 * psa_export_key().
854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100856
857/** Construct a PAKE primitive from type, family and bit-size.
858 *
859 * \param pake_type The type of the primitive
860 * (value of type ::psa_pake_primitive_type_t).
861 * \param pake_family The family of the primitive
862 * (the type and interpretation of this parameter depends
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500863 * on \p pake_type, for more information consult the
Janos Follath702cf092021-05-26 12:58:23 +0100864 * documentation of individual ::psa_pake_primitive_type_t
865 * constants).
866 * \param pake_bits The bit-size of the primitive
867 * (Value of type \c size_t. The interpretation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500868 * of this parameter depends on \p pake_family, for more
Janos Follath702cf092021-05-26 12:58:23 +0100869 * information consult the documentation of individual
870 * ::psa_pake_primitive_type_t constants).
871 *
872 * \return The constructed primitive value of type ::psa_pake_primitive_t.
873 * Return 0 if the requested primitive can't be encoded as
874 * ::psa_pake_primitive_t.
875 */
876#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
877 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
878 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +0100880
881/** The key share being sent to or received from the peer.
882 *
883 * The format for both input and output at this step is the same as for public
884 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
885 * be.
886 *
887 * For more information on the format, consult the documentation of
888 * psa_export_public_key().
889 *
890 * For information regarding how the group is determined, consult the
891 * documentation #PSA_PAKE_PRIMITIVE.
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100894
895/** A Schnorr NIZKP public key.
896 *
Janos Follath55dd5dc2021-06-03 15:51:09 +0100897 * This is the ephemeral public key in the Schnorr Non-Interactive
898 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
899 *
Janos Follath702cf092021-05-26 12:58:23 +0100900 * The format for both input and output at this step is the same as for public
901 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
902 * be.
903 *
904 * For more information on the format, consult the documentation of
905 * psa_export_public_key().
906 *
907 * For information regarding how the group is determined, consult the
908 * documentation #PSA_PAKE_PRIMITIVE.
909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100911
912/** A Schnorr NIZKP proof.
913 *
Janos Follath55dd5dc2021-06-03 15:51:09 +0100914 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
915 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +0100916 *
Janos Follath1f013182021-06-08 15:30:48 +0100917 * Both for input and output, the value at this step is an integer less than
918 * the order of the group selected in the cipher suite. The format depends on
919 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +0100920 *
Janos Follath1f013182021-06-08 15:30:48 +0100921 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +0100922 * - For everything else the encoding is big endian (see Section 2.3.8 of
923 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +0100924 *
Janos Follath1f013182021-06-08 15:30:48 +0100925 * In both cases leading zeroes are allowed as long as the length in bytes does
926 * not exceed the byte length of the group order.
927 *
Janos Follath702cf092021-05-26 12:58:23 +0100928 * For information regarding how the group is determined, consult the
929 * documentation #PSA_PAKE_PRIMITIVE.
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +0100932
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800933/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +0100934 *
935 * This is an implementation-defined \c struct. Applications should not
936 * make any assumptions about the content of this structure.
937 * Implementation details can change in future versions without notice.
938 */
939typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
940
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +0200941/** Return an initial value for a PAKE cipher suite object.
942 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +0200944
Janos Follath702cf092021-05-26 12:58:23 +0100945/** Retrieve the PAKE algorithm from a PAKE cipher suite.
946 *
Janos Follath702cf092021-05-26 12:58:23 +0100947 * \param[in] cipher_suite The cipher suite structure to query.
948 *
949 * \return The PAKE algorithm stored in the cipher suite structure.
950 */
951static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +0100953
954/** Declare the PAKE algorithm for the cipher suite.
955 *
956 * This function overwrites any PAKE algorithm
957 * previously set in \p cipher_suite.
958 *
Janos Follath702cf092021-05-26 12:58:23 +0100959 * \param[out] cipher_suite The cipher suite structure to write to.
960 * \param algorithm The PAKE algorithm to write.
961 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
962 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
963 * If this is 0, the PAKE algorithm in
964 * \p cipher_suite becomes unspecified.
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
967 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +0100968
969/** Retrieve the primitive from a PAKE cipher suite.
970 *
Janos Follath702cf092021-05-26 12:58:23 +0100971 * \param[in] cipher_suite The cipher suite structure to query.
972 *
973 * \return The primitive stored in the cipher suite structure.
974 */
975static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +0100977
978/** Declare the primitive for a PAKE cipher suite.
979 *
980 * This function overwrites any primitive previously set in \p cipher_suite.
981 *
Janos Follath702cf092021-05-26 12:58:23 +0100982 * \param[out] cipher_suite The cipher suite structure to write to.
983 * \param primitive The primitive to write. If this is 0, the
984 * primitive type in \p cipher_suite becomes
985 * unspecified.
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
988 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +0100989
Neil Armstrongff9cac72022-05-20 10:25:15 +0200990/** Retrieve the PAKE family from a PAKE cipher suite.
991 *
Neil Armstrongff9cac72022-05-20 10:25:15 +0200992 * \param[in] cipher_suite The cipher suite structure to query.
993 *
994 * \return The PAKE family stored in the cipher suite structure.
995 */
996static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +0200998
Neil Armstrongd5a48252022-05-20 10:26:36 +0200999/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1000 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001001 * \param[in] cipher_suite The cipher suite structure to query.
1002 *
1003 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1004 */
1005static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001007
Janos Follath702cf092021-05-26 12:58:23 +01001008/** Retrieve the hash algorithm from a PAKE cipher suite.
1009 *
Janos Follath702cf092021-05-26 12:58:23 +01001010 * \param[in] cipher_suite The cipher suite structure to query.
1011 *
1012 * \return The hash algorithm stored in the cipher suite structure. The return
1013 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1014 * the hash algorithm is not set.
1015 */
1016static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001018
1019/** Declare the hash algorithm for a PAKE cipher suite.
1020 *
1021 * This function overwrites any hash algorithm
1022 * previously set in \p cipher_suite.
1023 *
Janos Follath702cf092021-05-26 12:58:23 +01001024 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1025 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1026 * for more information.
1027 *
1028 * \param[out] cipher_suite The cipher suite structure to write to.
1029 * \param hash The hash involved in the cipher suite.
1030 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1031 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1032 * If this is 0, the hash algorithm in
1033 * \p cipher_suite becomes unspecified.
1034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1036 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001037
1038/** The type of the state data structure for PAKE operations.
1039 *
1040 * Before calling any function on a PAKE operation object, the application
1041 * must initialize it by any of the following means:
1042 * - Set the structure to all-bits-zero, for example:
1043 * \code
1044 * psa_pake_operation_t operation;
1045 * memset(&operation, 0, sizeof(operation));
1046 * \endcode
1047 * - Initialize the structure to logical zero values, for example:
1048 * \code
1049 * psa_pake_operation_t operation = {0};
1050 * \endcode
1051 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1052 * for example:
1053 * \code
1054 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1055 * \endcode
1056 * - Assign the result of the function psa_pake_operation_init()
1057 * to the structure, for example:
1058 * \code
1059 * psa_pake_operation_t operation;
1060 * operation = psa_pake_operation_init();
1061 * \endcode
1062 *
1063 * This is an implementation-defined \c struct. Applications should not
1064 * make any assumptions about the content of this structure.
1065 * Implementation details can change in future versions without notice. */
1066typedef struct psa_pake_operation_s psa_pake_operation_t;
1067
Przemek Stekiel51eac532022-12-07 11:04:51 +01001068/** The type of input values for PAKE operations. */
1069typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1070
Przemek Stekielb09c4872023-01-17 12:05:38 +01001071/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001072typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1073
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001074/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001077
Przemek Stekielc0e62502023-03-14 11:49:36 +01001078/** Get the length of the password in bytes from given inputs.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001079 *
1080 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001081 * \param[out] password_len Password length.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001082 *
1083 * \retval #PSA_SUCCESS
1084 * Success.
1085 * \retval #PSA_ERROR_BAD_STATE
1086 * Password hasn't been set yet.
1087 */
1088psa_status_t psa_crypto_driver_pake_get_password_len(
1089 const psa_crypto_driver_pake_inputs_t *inputs,
1090 size_t *password_len);
1091
1092/** Get the password from given inputs.
1093 *
1094 * \param[in] inputs Operation inputs.
1095 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001096 * \param buffer_size Size of the return buffer in bytes.
1097 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001098 *
1099 * \retval #PSA_SUCCESS
1100 * Success.
1101 * \retval #PSA_ERROR_BAD_STATE
1102 * Password hasn't been set yet.
1103 */
1104psa_status_t psa_crypto_driver_pake_get_password(
1105 const psa_crypto_driver_pake_inputs_t *inputs,
1106 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1107
Przemek Stekielc0e62502023-03-14 11:49:36 +01001108/** Get the length of the user id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001109 *
1110 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001111 * \param[out] user_len User id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001112 *
1113 * \retval #PSA_SUCCESS
1114 * Success.
1115 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001116 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001117 */
1118psa_status_t psa_crypto_driver_pake_get_user_len(
1119 const psa_crypto_driver_pake_inputs_t *inputs,
1120 size_t *user_len);
1121
Przemek Stekielc0e62502023-03-14 11:49:36 +01001122/** Get the length of the peer id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001123 *
1124 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001125 * \param[out] peer_len Peer id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001126 *
1127 * \retval #PSA_SUCCESS
1128 * Success.
1129 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001130 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001131 */
1132psa_status_t psa_crypto_driver_pake_get_peer_len(
1133 const psa_crypto_driver_pake_inputs_t *inputs,
1134 size_t *peer_len);
1135
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001136/** Get the user id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001137 *
1138 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001139 * \param[out] user_id User id.
1140 * \param user_id_size Size of \p user_id in bytes.
1141 * \param[out] user_id_len Size of the user id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001142 *
1143 * \retval #PSA_SUCCESS
1144 * Success.
1145 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001146 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001147 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001148 * The size of the \p user_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001149 */
1150psa_status_t psa_crypto_driver_pake_get_user(
1151 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001152 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001153
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001154/** Get the peer id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001155 *
1156 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001157 * \param[out] peer_id Peer id.
1158 * \param peer_id_size Size of \p peer_id in bytes.
1159 * \param[out] peer_id_length Size of the peer id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001160 *
1161 * \retval #PSA_SUCCESS
1162 * Success.
1163 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001164 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001165 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001166 * The size of the \p peer_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001167 */
1168psa_status_t psa_crypto_driver_pake_get_peer(
1169 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001170 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001171
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001172/** Get the cipher suite from given inputs.
1173 *
1174 * \param[in] inputs Operation inputs.
1175 * \param[out] cipher_suite Return buffer for role.
1176 *
1177 * \retval #PSA_SUCCESS
1178 * Success.
1179 * \retval #PSA_ERROR_BAD_STATE
1180 * Cipher_suite hasn't been set yet.
1181 */
1182psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1183 const psa_crypto_driver_pake_inputs_t *inputs,
1184 psa_pake_cipher_suite_t *cipher_suite);
1185
Janos Follath702cf092021-05-26 12:58:23 +01001186/** Set the session information for a password-authenticated key exchange.
1187 *
1188 * The sequence of operations to set up a password-authenticated key exchange
1189 * is as follows:
1190 * -# Allocate an operation object which will be passed to all the functions
1191 * listed here.
1192 * -# Initialize the operation object with one of the methods described in the
1193 * documentation for #psa_pake_operation_t, e.g.
1194 * #PSA_PAKE_OPERATION_INIT.
1195 * -# Call psa_pake_setup() to specify the cipher suite.
1196 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1197 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1198 * to be called depends on the algorithm in use.
1199 *
1200 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1201 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1202 * for more information.
1203 *
1204 * A typical sequence of calls to perform a password-authenticated key
1205 * exchange:
1206 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1207 * key share that needs to be sent to the peer.
1208 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1209 * the key share that was received from the peer.
1210 * -# Depending on the algorithm additional calls to psa_pake_output() and
1211 * psa_pake_input() might be necessary.
1212 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1213 *
1214 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1215 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1216 * for more information.
1217 *
1218 * If an error occurs at any step after a call to psa_pake_setup(),
1219 * the operation will need to be reset by a call to psa_pake_abort(). The
1220 * application may call psa_pake_abort() at any time after the operation
1221 * has been initialized.
1222 *
1223 * After a successful call to psa_pake_setup(), the application must
1224 * eventually terminate the operation. The following events terminate an
1225 * operation:
1226 * - A call to psa_pake_abort().
1227 * - A successful call to psa_pake_get_implicit_key().
1228 *
1229 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001230 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001231 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001232 * characterizes a PAKE algorithm and determines
1233 * the algorithm as well.)
1234 *
1235 * \retval #PSA_SUCCESS
1236 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001237 * \retval #PSA_ERROR_INVALID_ARGUMENT
1238 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1239 * PAKE primitive in \p cipher_suite is not compatible with the
1240 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1241 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001242 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001243 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1244 * or the PAKE primitive in \p cipher_suite is not supported or not
1245 * compatible with the PAKE algorithm, or the hash algorithm in
1246 * \p cipher_suite is not supported or not compatible with the PAKE
1247 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1249 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001250 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001251 * The operation state is not valid, or
1252 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001253 * It is implementation-dependent whether a failure to initialize
1254 * results in this error code.
1255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1257 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001258
1259/** Set the password for a password-authenticated key exchange from key ID.
1260 *
1261 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001262 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001263 *
1264 * \param[in,out] operation The operation object to set the password for. It
1265 * must have been set up by psa_pake_setup() and
1266 * not yet in use (neither psa_pake_output() nor
1267 * psa_pake_input() has been called yet). It must
1268 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001269 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001270 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001271 * \param password Identifier of the key holding the password or a
1272 * value derived from the password (eg. by a
1273 * memory-hard function). It must remain valid
1274 * until the operation terminates. It must be of
1275 * type #PSA_KEY_TYPE_PASSWORD or
1276 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1277 * the usage #PSA_KEY_USAGE_DERIVE.
1278 *
1279 * \retval #PSA_SUCCESS
1280 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001281 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001282 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001283 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001284 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1285 * permit the \p operation's algorithm.
1286 * \retval #PSA_ERROR_INVALID_ARGUMENT
1287 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1288 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1289 * the \p operation's cipher suite.
1290 * \retval #PSA_ERROR_NOT_SUPPORTED
1291 * The key type or key size of \p password is not supported with the
1292 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1294 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1295 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1296 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1297 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001298 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001299 * The operation state is not valid (it must have been set up.), or
1300 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001301 * It is implementation-dependent whether a failure to initialize
1302 * results in this error code.
1303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1305 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001306
Janos Follath702cf092021-05-26 12:58:23 +01001307/** Set the user ID for a password-authenticated key exchange.
1308 *
1309 * Call this function to set the user ID. For PAKE algorithms that associate a
1310 * user identifier with each side of the session you need to call
1311 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1312 * user identifier with the session, call psa_pake_set_user() only.
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 * \param[in,out] operation The operation object to set the user ID for. It
1319 * must have been set up by psa_pake_setup() and
1320 * not yet in use (neither psa_pake_output() nor
1321 * psa_pake_input() has been called yet). It must
1322 * be on operation for which the user ID hasn't
1323 * been set (psa_pake_set_user() hasn't been
1324 * called yet).
1325 * \param[in] user_id The user ID to authenticate with.
1326 * \param user_id_len Size of the \p user_id buffer in bytes.
1327 *
1328 * \retval #PSA_SUCCESS
1329 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001330 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001331 * \p user_id is not valid for the \p operation's algorithm and cipher
1332 * suite.
1333 * \retval #PSA_ERROR_NOT_SUPPORTED
1334 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1337 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001338 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001339 * The operation state is not valid, or
1340 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001341 * It is implementation-dependent whether a failure to initialize
1342 * results in this error code.
1343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1345 const uint8_t *user_id,
1346 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001347
1348/** Set the peer ID for a password-authenticated key exchange.
1349 *
1350 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1351 * that associate a user identifier with each side of the session. For PAKE
1352 * algorithms that associate a single user identifier with the session, call
1353 * psa_pake_set_user() only.
1354 *
1355 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1356 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1357 * for more information.
1358 *
1359 * \param[in,out] operation The operation object to set the peer ID for. It
1360 * must have been set up by psa_pake_setup() and
1361 * not yet in use (neither psa_pake_output() nor
1362 * psa_pake_input() has been called yet). It must
1363 * be on operation for which the peer ID hasn't
1364 * been set (psa_pake_set_peer() hasn't been
1365 * called yet).
1366 * \param[in] peer_id The peer's ID to authenticate.
1367 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1368 *
1369 * \retval #PSA_SUCCESS
1370 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001371 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001372 * \p peer_id is not valid for the \p operation's algorithm and cipher
Neil Armstrong16ff7882022-05-20 11:04:20 +02001373 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001374 * \retval #PSA_ERROR_NOT_SUPPORTED
1375 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1378 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001379 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001380 * Calling psa_pake_set_peer() is invalid with the \p operation's
1381 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001382 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001383 * It is implementation-dependent whether a failure to initialize
1384 * results in this error code.
1385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1387 const uint8_t *peer_id,
1388 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001389
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001390/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001391 *
1392 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001393 * It is optional to call this function for PAKEs that don't require a role
1394 * to be specified. For such PAKEs the application role parameter is ignored,
1395 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001396 *
1397 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1398 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1399 * for more information.
1400 *
Neil Armstrongef157512022-05-25 11:49:45 +02001401 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001402 * application's role for. It must have been set up
1403 * by psa_pake_setup() and not yet in use (neither
1404 * psa_pake_output() nor psa_pake_input() has been
1405 * called yet). It must be on operation for which
1406 * the application's role hasn't been specified
1407 * (psa_pake_set_role() hasn't been called yet).
1408 * \param role A value of type ::psa_pake_role_t indicating the
1409 * application's role in the PAKE the algorithm
1410 * that is being set up. For more information see
1411 * the documentation of \c PSA_PAKE_ROLE_XXX
1412 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001413 *
1414 * \retval #PSA_SUCCESS
1415 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001416 * \retval #PSA_ERROR_INVALID_ARGUMENT
1417 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001418 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001419 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1421 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001422 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001423 * The operation state is not valid, or
1424 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001425 * It is implementation-dependent whether a failure to initialize
1426 * results in this error code.
1427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1429 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001430
1431/** Get output for a step of a password-authenticated key exchange.
1432 *
1433 * Depending on the algorithm being executed, you might need to call this
1434 * function several times or you might not need to call this at all.
1435 *
1436 * The exact sequence of calls to perform a password-authenticated key
1437 * exchange depends on the algorithm in use. Refer to the documentation of
1438 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1439 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1440 * information.
1441 *
1442 * If this function returns an error status, the operation enters an error
1443 * state and must be aborted by calling psa_pake_abort().
1444 *
1445 * \param[in,out] operation Active PAKE operation.
1446 * \param step The step of the algorithm for which the output is
1447 * requested.
1448 * \param[out] output Buffer where the output is to be written in the
1449 * format appropriate for this \p step. Refer to
1450 * the documentation of the individual
1451 * \c PSA_PAKE_STEP_XXX constants for more
1452 * information.
1453 * \param output_size Size of the \p output buffer in bytes. This must
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001454 * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1455 * primitive, \p output_step) where \c alg and
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001456 * \p primitive are the PAKE algorithm and primitive
1457 * in the operation's cipher suite, and \p step is
1458 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001459 *
1460 * \param[out] output_length On success, the number of bytes of the returned
1461 * output.
1462 *
1463 * \retval #PSA_SUCCESS
1464 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001465 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1466 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001467 * \retval #PSA_ERROR_INVALID_ARGUMENT
1468 * \p step is not compatible with the operation's algorithm.
1469 * \retval #PSA_ERROR_NOT_SUPPORTED
1470 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001471 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1474 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1475 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1476 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1477 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001478 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001479 * The operation state is not valid (it must be active, and fully set
1480 * up, and this call must conform to the algorithm's requirements
1481 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001482 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001483 * It is implementation-dependent whether a failure to initialize
1484 * results in this error code.
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1487 psa_pake_step_t step,
1488 uint8_t *output,
1489 size_t output_size,
1490 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001491
1492/** Provide input for a step of a password-authenticated key exchange.
1493 *
1494 * Depending on the algorithm being executed, you might need to call this
1495 * function several times or you might not need to call this at all.
1496 *
1497 * The exact sequence of calls to perform a password-authenticated key
1498 * exchange depends on the algorithm in use. Refer to the documentation of
1499 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1500 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1501 * information.
1502 *
1503 * If this function returns an error status, the operation enters an error
1504 * state and must be aborted by calling psa_pake_abort().
1505 *
1506 * \param[in,out] operation Active PAKE operation.
1507 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001508 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001509 * appropriate for this \p step. Refer to the
1510 * documentation of the individual
1511 * \c PSA_PAKE_STEP_XXX constants for more
1512 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001513 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001514 *
1515 * \retval #PSA_SUCCESS
1516 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001517 * \retval #PSA_ERROR_INVALID_SIGNATURE
1518 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001519 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001520 * \p input_length is not compatible with the \p operation’s algorithm,
1521 * or the \p input is not valid for the \p operation's algorithm,
1522 * cipher suite or \p step.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001523 * \retval #PSA_ERROR_NOT_SUPPORTED
1524 * \p step p is not supported with the \p operation's algorithm, or the
1525 * \p input is not supported for the \p operation's algorithm, cipher
1526 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1529 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1530 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1531 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1532 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001533 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001534 * The operation state is not valid (it must be active, and fully set
1535 * up, and this call must conform to the algorithm's requirements
1536 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001537 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001538 * It is implementation-dependent whether a failure to initialize
1539 * results in this error code.
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1542 psa_pake_step_t step,
1543 const uint8_t *input,
1544 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001545
1546/** Get implicitly confirmed shared secret from a PAKE.
1547 *
1548 * At this point there is a cryptographic guarantee that only the authenticated
1549 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001550 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001551 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001552 * That is, the authentication is only implicit. Since the peer is not
1553 * authenticated yet, no action should be taken yet that assumes that the peer
1554 * is who it claims to be. For example, do not access restricted files on the
1555 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001556 *
1557 * This function can be called after the key exchange phase of the operation
1558 * has completed. It imports the shared secret output of the PAKE into the
1559 * provided derivation operation. The input step
1560 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1561 * material in the key derivation operation.
1562 *
1563 * The exact sequence of calls to perform a password-authenticated key
1564 * exchange depends on the algorithm in use. Refer to the documentation of
1565 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1566 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1567 * information.
1568 *
1569 * When this function returns successfully, \p operation becomes inactive.
1570 * If this function returns an error status, both \p operation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001571 * and \c key_derivation operations enter an error state and must be aborted by
Janos Follath702cf092021-05-26 12:58:23 +01001572 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1573 *
1574 * \param[in,out] operation Active PAKE operation.
1575 * \param[out] output A key derivation operation that is ready
1576 * for an input step of type
1577 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1578 *
1579 * \retval #PSA_SUCCESS
1580 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001581 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001582 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1583 * algorithm in the \p output key derivation operation.
1584 * \retval #PSA_ERROR_NOT_SUPPORTED
1585 * Input from a PAKE is not supported by the algorithm in the \p output
1586 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001587 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1589 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1590 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1591 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1592 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001593 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001594 * The PAKE operation state is not valid (it must be active, but beyond
1595 * that validity is specific to the algorithm), or
1596 * the library has not been previously initialized by psa_crypto_init(),
1597 * or the state of \p output is not valid for
1598 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1599 * step is out of order or the application has done this step already
1600 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001601 * It is implementation-dependent whether a failure to initialize
1602 * results in this error code.
1603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1605 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001606
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001607/** Abort a PAKE operation.
1608 *
1609 * Aborting an operation frees all associated resources except for the \c
1610 * operation structure itself. Once aborted, the operation object can be reused
1611 * for another operation by calling psa_pake_setup() again.
1612 *
1613 * This function may be called at any time after the operation
1614 * object has been initialized as described in #psa_pake_operation_t.
1615 *
1616 * In particular, calling psa_pake_abort() after the operation has been
1617 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1618 * is safe and has no effect.
1619 *
1620 * \param[in,out] operation The operation to abort.
1621 *
1622 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001623 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1625 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001626 * \retval #PSA_ERROR_BAD_STATE
1627 * The library has not been previously initialized by psa_crypto_init().
1628 * It is implementation-dependent whether a failure to initialize
1629 * results in this error code.
1630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001631psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001632
Janos Follath702cf092021-05-26 12:58:23 +01001633/**@}*/
1634
1635/** A sufficient output buffer size for psa_pake_output().
1636 *
1637 * If the size of the output buffer is at least this large, it is guaranteed
1638 * that psa_pake_output() will not fail due to an insufficient output buffer
1639 * size. The actual size of the output might be smaller in any given call.
1640 *
1641 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1642 *
Janos Follath46c02372021-06-08 15:22:51 +01001643 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001644 * #PSA_ALG_IS_PAKE(\p alg) is true).
1645 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1646 * compatible with algorithm \p alg.
1647 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1648 * algorithm \p alg.
1649 * \return A sufficient output buffer size for the specified
Neil Armstrongcd974d52022-05-20 10:30:12 +02001650 * PAKE algorithm, primitive, and output step. If the
1651 * PAKE algorithm, primitive, or output step is not
1652 * recognized, or the parameters are incompatible,
1653 * return 0.
Janos Follath702cf092021-05-26 12:58:23 +01001654 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001655#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 (alg == PSA_ALG_JPAKE && \
1657 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1658 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1659 ( \
1660 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1661 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1662 32 \
1663 ) : \
1664 0)
Janos Follath702cf092021-05-26 12:58:23 +01001665
1666/** A sufficient input buffer size for psa_pake_input().
1667 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001668 * The value returned by this macro is guaranteed to be large enough for any
1669 * valid input to psa_pake_input() in an operation with the specified
1670 * parameters.
Janos Follath702cf092021-05-26 12:58:23 +01001671 *
1672 * See also #PSA_PAKE_INPUT_MAX_SIZE
1673 *
Janos Follath46c02372021-06-08 15:22:51 +01001674 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001675 * #PSA_ALG_IS_PAKE(\p alg) is true).
1676 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1677 * compatible with algorithm \p alg.
Janos Follathec83eb62021-05-27 08:41:59 +01001678 * \param input_step A value of type ::psa_pake_step_t that is valid for the
Janos Follath702cf092021-05-26 12:58:23 +01001679 * algorithm \p alg.
Janos Follath38d29db2021-06-03 13:14:42 +01001680 * \return A sufficient input buffer size for the specified
1681 * input, cipher suite and algorithm. If the cipher suite,
1682 * the input type or PAKE algorithm is not recognized, or
Janos Follath702cf092021-05-26 12:58:23 +01001683 * the parameters are incompatible, return 0.
1684 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001685#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 (alg == PSA_ALG_JPAKE && \
1687 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1688 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1689 ( \
1690 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1691 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1692 32 \
1693 ) : \
1694 0)
Janos Follath702cf092021-05-26 12:58:23 +01001695
Neil Armstrong2056ce52022-05-25 11:38:15 +02001696/** Output buffer size for psa_pake_output() for any of the supported PAKE
1697 * algorithm and primitive suites and output step.
Janos Follath702cf092021-05-26 12:58:23 +01001698 *
1699 * This macro must expand to a compile-time constant integer.
1700 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001701 * The value of this macro must be at least as large as the largest value
1702 * returned by PSA_PAKE_OUTPUT_SIZE()
1703 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001704 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001705 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001706#define PSA_PAKE_OUTPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001707
Neil Armstrong2056ce52022-05-25 11:38:15 +02001708/** Input buffer size for psa_pake_input() for any of the supported PAKE
1709 * algorithm and primitive suites and input step.
Janos Follath702cf092021-05-26 12:58:23 +01001710 *
1711 * This macro must expand to a compile-time constant integer.
1712 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001713 * The value of this macro must be at least as large as the largest value
1714 * returned by PSA_PAKE_INPUT_SIZE()
1715 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001716 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001717 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001718#define PSA_PAKE_INPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001719
Neil Armstrongfb993022022-05-20 10:08:58 +02001720/** Returns a suitable initializer for a PAKE cipher suite object of type
1721 * psa_pake_cipher_suite_t.
1722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
Neil Armstrongfb993022022-05-20 10:08:58 +02001724
Neil Armstrong0151c552022-05-20 10:13:53 +02001725/** Returns a suitable initializer for a PAKE operation object of type
1726 * psa_pake_operation_t.
1727 */
Antonio de Angelis4380a332024-02-02 14:21:24 +00001728#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1729#define PSA_PAKE_OPERATION_INIT { 0 }
1730#else
Przemek Stekiel656b2592023-03-22 13:15:33 +01001731#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001732 { 0 }, { { 0 } } }
Antonio de Angelis4380a332024-02-02 14:21:24 +00001733#endif
Neil Armstrong0151c552022-05-20 10:13:53 +02001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735struct psa_pake_cipher_suite_s {
Janos Follath702cf092021-05-26 12:58:23 +01001736 psa_algorithm_t algorithm;
1737 psa_pake_primitive_type_t type;
1738 psa_pake_family_t family;
1739 uint16_t bits;
1740 psa_algorithm_t hash;
1741};
1742
1743static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001745{
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001747}
1748
1749static inline void psa_pake_cs_set_algorithm(
1750 psa_pake_cipher_suite_t *cipher_suite,
1751 psa_algorithm_t algorithm)
1752{
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001754 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001756 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 }
Janos Follath702cf092021-05-26 12:58:23 +01001758}
1759
1760static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001762{
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1764 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001765}
1766
1767static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 psa_pake_cipher_suite_t *cipher_suite,
1769 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01001770{
1771 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1772 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1773 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1774}
1775
Neil Armstrongff9cac72022-05-20 10:25:15 +02001776static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02001778{
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02001780}
1781
Neil Armstrongd5a48252022-05-20 10:26:36 +02001782static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02001784{
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02001786}
1787
Janos Follath702cf092021-05-26 12:58:23 +01001788static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001790{
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01001792}
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1795 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01001796{
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01001798 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001800 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 }
Janos Follath702cf092021-05-26 12:58:23 +01001802}
1803
Przemek Stekiel51eac532022-12-07 11:04:51 +01001804struct psa_crypto_driver_pake_inputs_s {
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 uint8_t *MBEDTLS_PRIVATE(password);
Przemek Stekiel152ae072022-11-17 13:24:36 +01001806 size_t MBEDTLS_PRIVATE(password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01001807 uint8_t *MBEDTLS_PRIVATE(user);
1808 size_t MBEDTLS_PRIVATE(user_len);
1809 uint8_t *MBEDTLS_PRIVATE(peer);
1810 size_t MBEDTLS_PRIVATE(peer_len);
Przemek Stekiel9dd24402023-01-26 15:06:09 +01001811 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001812 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
1813};
1814
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001815typedef enum psa_crypto_driver_pake_step {
Przemek Stekielb09c4872023-01-17 12:05:38 +01001816 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1817 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1818 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1819 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1820 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1821 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1822 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1823 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1824 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1825 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1826 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1827 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1828 PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001829} psa_crypto_driver_pake_step_t;
Przemek Stekiel57980032023-01-09 15:07:26 +01001830
David Horstmanne7f21e62023-05-12 18:17:21 +01001831typedef enum psa_jpake_round {
David Horstmann5da95602023-06-08 15:37:12 +01001832 PSA_JPAKE_FIRST = 0,
1833 PSA_JPAKE_SECOND = 1,
1834 PSA_JPAKE_FINISHED = 2
David Horstmanne7f21e62023-05-12 18:17:21 +01001835} psa_jpake_round_t;
1836
1837typedef enum psa_jpake_io_mode {
David Horstmann5da95602023-06-08 15:37:12 +01001838 PSA_JPAKE_INPUT = 0,
1839 PSA_JPAKE_OUTPUT = 1
David Horstmanne7f21e62023-05-12 18:17:21 +01001840} psa_jpake_io_mode_t;
Przemek Stekielb09c4872023-01-17 12:05:38 +01001841
Przemek Stekiele12ed362022-12-21 12:54:46 +01001842struct psa_jpake_computation_stage_s {
David Horstmanne7f21e62023-05-12 18:17:21 +01001843 /* The J-PAKE round we are currently on */
1844 psa_jpake_round_t MBEDTLS_PRIVATE(round);
1845 /* The 'mode' we are currently in (inputting or outputting) */
David Horstmann024e5c52023-06-14 15:48:21 +01001846 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
David Horstmann279d2272023-06-14 17:13:56 +01001847 /* The number of completed inputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001848 uint8_t MBEDTLS_PRIVATE(inputs);
David Horstmann279d2272023-06-14 17:13:56 +01001849 /* The number of completed outputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001850 uint8_t MBEDTLS_PRIVATE(outputs);
1851 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1852 psa_pake_step_t MBEDTLS_PRIVATE(step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001853};
1854
David Horstmann5dbe17d2023-06-27 10:30:28 +01001855#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1856 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1857#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1858 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
David Horstmanne7f21e62023-05-12 18:17:21 +01001859
Janos Follath702cf092021-05-26 12:58:23 +01001860struct psa_pake_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +00001861#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1862 mbedtls_psa_client_handle_t handle;
1863#else
Przemek Stekield91bcb72022-11-22 14:00:51 +01001864 /** Unique ID indicating which driver got assigned to do the
1865 * operation. Since driver contexts are driver-specific, swapping
1866 * drivers halfway through the operation is not supported.
1867 * ID values are auto-generated in psa_crypto_driver_wrappers.h
1868 * ID value zero means the context is not valid or not assigned to
1869 * any driver (i.e. none of the driver contexts are active). */
1870 unsigned int MBEDTLS_PRIVATE(id);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001871 /* Algorithm of the PAKE operation */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001872 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Przemek Stekiel656b2592023-03-22 13:15:33 +01001873 /* A primitive of type compatible with algorithm */
1874 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001875 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1876 * or computing. */
Przemek Stekiel51eac532022-12-07 11:04:51 +01001877 uint8_t MBEDTLS_PRIVATE(stage);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001878 /* Holds computation stage of the PAKE algorithms. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 union {
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001880 uint8_t MBEDTLS_PRIVATE(dummy);
Przemek Stekiel4aa99402023-02-27 13:00:57 +01001881#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekieldde6a912023-01-26 08:46:37 +01001882 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
Neil Armstrong35269d92022-05-25 11:26:31 +02001883#endif
Przemek Stekieldde6a912023-01-26 08:46:37 +01001884 } MBEDTLS_PRIVATE(computation_stage);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001885 union {
Przemek Stekiel51eac532022-12-07 11:04:51 +01001886 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
Przemek Stekielac067d72023-01-26 16:31:03 +01001887 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001888 } MBEDTLS_PRIVATE(data);
Antonio de Angelis4380a332024-02-02 14:21:24 +00001889#endif
Janos Follath702cf092021-05-26 12:58:23 +01001890};
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001893{
1894 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001896}
1897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01001899{
1900 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return v;
Janos Follath702cf092021-05-26 12:58:23 +01001902}
1903
Gilles Peskinee59236f2018-01-27 23:32:46 +01001904#ifdef __cplusplus
1905}
1906#endif
1907
1908#endif /* PSA_CRYPTO_EXTRA_H */