blob: c67345bd2e58cc07bba28df00f6aa2f2719b3e24 [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
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020035/** \addtogroup attributes
36 * @{
37 */
38
39/** \brief Declare the enrollment algorithm for a key.
40 *
41 * An operation on a key may indifferently use the algorithm set with
42 * psa_set_key_algorithm() or with this function.
43 *
44 * \param[out] attributes The attribute structure to write to.
45 * \param alg2 A second algorithm that the key may be used
46 * for, in addition to the algorithm set with
47 * psa_set_key_algorithm().
48 *
49 * \warning Setting an enrollment algorithm is not recommended, because
50 * using the same key with different algorithms can allow some
51 * attacks based on arithmetic relations between different
52 * computations made with the same key, or can escalate harmless
53 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020054 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020055 * verified that the usage of the key with multiple algorithms
56 * is safe.
57 */
58static inline void psa_set_key_enrollment_algorithm(
59 psa_key_attributes_t *attributes,
60 psa_algorithm_t alg2)
61{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020062 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020063}
64
65/** Retrieve the enrollment algorithm policy from key attributes.
66 *
67 * \param[in] attributes The key attribute structure to query.
68 *
69 * \return The enrollment algorithm stored in the attribute structure.
70 */
71static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
72 const psa_key_attributes_t *attributes)
73{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020075}
76
Gilles Peskinec8000c02019-08-02 20:15:51 +020077#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
78
79/** Retrieve the slot number where a key is stored.
80 *
81 * A slot number is only defined for keys that are stored in a secure
82 * element.
83 *
84 * This information is only useful if the secure element is not entirely
85 * managed through the PSA Cryptography API. It is up to the secure
86 * element driver to decide how PSA slot numbers map to any other interface
87 * that the secure element may have.
88 *
89 * \param[in] attributes The key attribute structure to query.
90 * \param[out] slot_number On success, the slot number containing the key.
91 *
92 * \retval #PSA_SUCCESS
93 * The key is located in a secure element, and \p *slot_number
94 * indicates the slot number that contains it.
95 * \retval #PSA_ERROR_NOT_PERMITTED
96 * The caller is not permitted to query the slot number.
Fredrik Hessecc207bc2021-09-28 21:06:08 +020097 * Mbed TLS currently does not return this error.
Gilles Peskinec8000c02019-08-02 20:15:51 +020098 * \retval #PSA_ERROR_INVALID_ARGUMENT
99 * The key is not located in a secure element.
100 */
101psa_status_t psa_get_key_slot_number(
102 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 psa_key_slot_number_t *slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200104
105/** Choose the slot number where a key is stored.
106 *
107 * This function declares a slot number in the specified attribute
108 * structure.
109 *
110 * A slot number is only meaningful for keys that are stored in a secure
111 * element. It is up to the secure element driver to decide how PSA slot
112 * numbers map to any other interface that the secure element may have.
113 *
114 * \note Setting a slot number in key attributes for a key creation can
115 * cause the following errors when creating the key:
116 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
117 * not support choosing a specific slot number.
118 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
119 * choose slot numbers in general or to choose this specific slot.
120 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
121 * valid in general or not valid for this specific key.
122 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
123 * selected slot.
124 *
125 * \param[out] attributes The attribute structure to write to.
126 * \param slot_number The slot number to set.
127 */
128static inline void psa_set_key_slot_number(
129 psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 psa_key_slot_number_t slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +0200131{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200132 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
133 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200134}
135
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200136/** Remove the slot number attribute from a key attribute structure.
137 *
138 * This function undoes the action of psa_set_key_slot_number().
139 *
140 * \param[out] attributes The attribute structure to write to.
141 */
142static inline void psa_clear_key_slot_number(
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 psa_key_attributes_t *attributes)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200144{
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &=
146 ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200147}
148
Gilles Peskined7729582019-08-05 15:55:54 +0200149/** Register a key that is already present in a secure element.
150 *
151 * The key must be located in a secure element designated by the
152 * lifetime field in \p attributes, in the slot set with
153 * psa_set_key_slot_number() in the attribute structure.
154 * This function makes the key available through the key identifier
155 * specified in \p attributes.
156 *
157 * \param[in] attributes The attributes of the existing key.
158 *
159 * \retval #PSA_SUCCESS
160 * The key was successfully registered.
161 * Note that depending on the design of the driver, this may or may
162 * not guarantee that a key actually exists in the designated slot
163 * and is compatible with the specified attributes.
164 * \retval #PSA_ERROR_ALREADY_EXISTS
165 * There is already a key with the identifier specified in
166 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200167 * \retval #PSA_ERROR_NOT_SUPPORTED
168 * The secure element driver for the specified lifetime does not
169 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200170 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200171 * The identifier in \p attributes is invalid, namely the identifier is
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500172 * not in the user range, or
Gilles Peskined7729582019-08-05 15:55:54 +0200173 * \p attributes specifies a lifetime which is not located
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500174 * in a secure element, or no slot number is specified in \p attributes,
Gilles Peskined7729582019-08-05 15:55:54 +0200175 * or the specified slot number is not valid.
176 * \retval #PSA_ERROR_NOT_PERMITTED
177 * The caller is not authorized to register the specified key slot.
Gilles Peskineed733552023-02-14 19:21:09 +0100178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
179 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
180 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
181 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
182 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
183 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskined7729582019-08-05 15:55:54 +0200184 * \retval #PSA_ERROR_BAD_STATE
185 * The library has not been previously initialized by psa_crypto_init().
186 * It is implementation-dependent whether a failure to initialize
187 * results in this error code.
188 */
189psa_status_t mbedtls_psa_register_se_key(
190 const psa_key_attributes_t *attributes);
191
Gilles Peskinec8000c02019-08-02 20:15:51 +0200192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
193
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200194/**@}*/
195
Gilles Peskinee59236f2018-01-27 23:32:46 +0100196/**
197 * \brief Library deinitialization.
198 *
199 * This function clears all data associated with the PSA layer,
200 * including the whole key store.
201 *
202 * This is an Mbed TLS extension.
203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100205
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200206/** \brief Statistics about
207 * resource consumption related to the PSA keystore.
208 *
209 * \note The content of this structure is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200210 * of Mbed TLS and may change arbitrarily from version to version.
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200213 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200214 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200215 /** Number of slots containing key material for a key which is in
216 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200217 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200218 /** Number of slots containing a reference to a key in a
219 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200220 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200221 /** Number of slots which are occupied, but do not contain
222 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200223 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200224 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200225 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200226 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200227 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100228 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200229 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200230 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200231 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200232 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200233 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200234} mbedtls_psa_stats_t;
235
236/** \brief Get statistics about
237 * resource consumption related to the PSA keystore.
238 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200239 * \note When Mbed TLS is built as part of a service, with isolation
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200240 * between the application and the keystore, the service may or
241 * may not expose this function.
242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200244
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200245/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100246 * \brief Inject an initial entropy seed for the random generator into
247 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100248 *
249 * This function injects data to be used as a seed for the random generator
250 * used by the PSA Crypto implementation. On devices that lack a trusted
251 * entropy source (preferably a hardware random number generator),
252 * the Mbed PSA Crypto implementation uses this value to seed its
253 * random generator.
254 *
255 * On devices without a trusted entropy source, this function must be
256 * called exactly once in the lifetime of the device. On devices with
257 * a trusted entropy source, calling this function is optional.
258 * In all cases, this function may only be called before calling any
259 * other function in the PSA Crypto API, including psa_crypto_init().
260 *
261 * When this function returns successfully, it populates a file in
262 * persistent storage. Once the file has been created, this function
263 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100264 *
265 * If any error occurs, this function does not change the system state.
266 * You can call this function again after correcting the reason for the
267 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200268 *
269 * \warning This function **can** fail! Callers MUST check the return status.
270 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100271 * \warning If you use this function, you should use it as part of a
272 * factory provisioning process. The value of the injected seed
273 * is critical to the security of the device. It must be
274 * *secret*, *unpredictable* and (statistically) *unique per device*.
275 * You should be generate it randomly using a cryptographically
276 * secure random generator seeded from trusted entropy sources.
277 * You should transmit it securely to the device and ensure
278 * that its value is not leaked or stored anywhere beyond the
279 * needs of transmitting it from the point of generation to
280 * the call of this function, and erase all copies of the value
281 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200282 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100283 * This is an Mbed TLS extension.
284 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200285 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100286 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
287 * Note that you must provide compatible implementations of
288 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200289 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200290 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200291 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200292 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200293 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000294 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
295 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
296 * in `library/entropy_poll.h` in the Mbed TLS source
297 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200298 * It must be less or equal to
299 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200300 *
301 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100302 * The seed value was injected successfully. The random generator
303 * of the PSA Crypto implementation is now ready for use.
304 * You may now call psa_crypto_init() and use the PSA Crypto
305 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200306 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100307 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200308 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100309 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200310 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100311 * The library has already been initialized. It is no longer
312 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200313 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100314psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200315 size_t seed_size);
316
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200317/** \addtogroup crypto_types
318 * @{
319 */
320
Gilles Peskinea1302192019-05-16 13:58:24 +0200321/** DSA public key.
322 *
323 * The import and export format is the
324 * representation of the public key `y = g^x mod p` as a big-endian byte
325 * string. The length of the byte string is the length of the base prime `p`
326 * in bytes.
327 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200329
330/** DSA key pair (private and public key).
331 *
332 * The import and export format is the
333 * representation of the private key `x` as a big-endian byte string. The
334 * length of the byte string is the private key size in bytes (leading zeroes
335 * are not stripped).
336 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800337 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200338 * FIPS 186-4 §B.1.2: interpret the byte string as integer
339 * in big-endian order. Discard it if it is not in the range
340 * [0, *N* - 2] where *N* is the boundary of the private key domain
341 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
342 * or the order of the curve's base point for ECC).
343 * Add 1 to the resulting integer and use this as the private key *x*.
344 *
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200347
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100348/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200349#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200350 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200353/** DSA signature with hashing.
354 *
355 * This is the signature scheme defined by FIPS 186-4,
356 * with a random per-message secret number (*k*).
357 *
358 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
359 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
360 * This includes #PSA_ALG_ANY_HASH
361 * when specifying the algorithm in a usage policy.
362 *
363 * \return The corresponding DSA signature algorithm.
364 * \return Unspecified if \p hash_alg is not a supported
365 * hash algorithm.
366 */
367#define PSA_ALG_DSA(hash_alg) \
368 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100369#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100370#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200371/** Deterministic DSA signature with hashing.
372 *
373 * This is the deterministic variant defined by RFC 6979 of
374 * the signature scheme defined by FIPS 186-4.
375 *
376 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
377 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
378 * This includes #PSA_ALG_ANY_HASH
379 * when specifying the algorithm in a usage policy.
380 *
381 * \return The corresponding DSA signature algorithm.
382 * \return Unspecified if \p hash_alg is not a supported
383 * hash algorithm.
384 */
385#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
386 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
387#define PSA_ALG_IS_DSA(alg) \
388 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
389 PSA_ALG_DSA_BASE)
390#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
391 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
392#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
393 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
394#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
395 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
396
397
398/* We need to expand the sample definition of this macro from
399 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100400#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
401#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
402 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200403
404/**@}*/
405
Gilles Peskine24f10f82019-05-16 12:18:32 +0200406/** \addtogroup attributes
407 * @{
408 */
409
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200410/** Custom Diffie-Hellman group.
411 *
Paul Elliott75e27032020-06-03 15:17:39 +0100412 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
413 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200414 * from domain parameters set by psa_set_key_domain_parameters().
415 */
Paul Elliott75e27032020-06-03 15:17:39 +0100416#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200417
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100418/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100419#define PSA_PAKE_OPERATION_STAGE_SETUP 0
420#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
421#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200422
Gilles Peskine24f10f82019-05-16 12:18:32 +0200423/**
424 * \brief Set domain parameters for a key.
425 *
426 * Some key types require additional domain parameters in addition to
427 * the key type identifier and the key size. Use this function instead
428 * of psa_set_key_type() when you need to specify domain parameters.
429 *
430 * The format for the required domain parameters varies based on the key type.
Gilles Peskine5ad95392023-12-19 12:22:46 +0100431 * Mbed TLS supports the following key type with domain parameters:
Gilles Peskine24f10f82019-05-16 12:18:32 +0200432 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200433 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200434 * the domain parameter data consists of the public exponent,
435 * represented as a big-endian integer with no leading zeros.
436 * This information is used when generating an RSA key pair.
437 * When importing a key, the public exponent is read from the imported
438 * key data and the exponent recorded in the attribute structure is ignored.
439 * As an exception, the public exponent 65537 is represented by an empty
440 * byte string.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200441 *
442 * \note This function may allocate memory or other resources.
443 * Once you have called this function on an attribute structure,
444 * you must call psa_reset_key_attributes() to free these resources.
445 *
446 * \note This is an experimental extension to the interface. It may change
447 * in future versions of the library.
448 *
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100449 * \note Due to an implementation limitation, domain parameters are ignored
450 * for keys that are managed by a driver.
451 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200452 * \param[in,out] attributes Attribute structure where the specified domain
453 * parameters will be stored.
454 * If this function fails, the content of
455 * \p attributes is not modified.
456 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
457 * \param[in] data Buffer containing the key domain parameters.
458 * The content of this buffer is interpreted
459 * according to \p type as described above.
460 * \param data_length Size of the \p data buffer in bytes.
461 *
Gilles Peskineed733552023-02-14 19:21:09 +0100462 * \retval #PSA_SUCCESS \emptydescription
463 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
464 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
465 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200466 */
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100467#if !defined(PSA_SET_KEY_DOMAIN_PARAMETERS)
468#define PSA_SET_KEY_DOMAIN_PARAMETERS
Gilles Peskine24f10f82019-05-16 12:18:32 +0200469psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
470 psa_key_type_t type,
471 const uint8_t *data,
472 size_t data_length);
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100473#endif /* PSA_SET_KEY_DOMAIN_PARAMETERS */
Gilles Peskine24f10f82019-05-16 12:18:32 +0200474
475/**
476 * \brief Get domain parameters for a key.
477 *
478 * Get the domain parameters for a key with this function, if any. The format
479 * of the domain parameters written to \p data is specified in the
480 * documentation for psa_set_key_domain_parameters().
481 *
482 * \note This is an experimental extension to the interface. It may change
483 * in future versions of the library.
484 *
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100485 * \note Due to an implementation limitation, domain parameters are not
486 * supported with keys that are managed by a driver.
487 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200488 * \param[in] attributes The key attribute structure to query.
489 * \param[out] data On success, the key domain parameters.
490 * \param data_size Size of the \p data buffer in bytes.
491 * The buffer is guaranteed to be large
492 * enough if its size in bytes is at least
493 * the value given by
494 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
495 * \param[out] data_length On success, the number of bytes
496 * that make up the key domain parameters data.
497 *
Gilles Peskineed733552023-02-14 19:21:09 +0100498 * \retval #PSA_SUCCESS \emptydescription
499 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100500 * \retval #PSA_ERROR_NOT_SUPPORTED
501 * The key is managed by a driver.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200502 */
503psa_status_t psa_get_key_domain_parameters(
504 const psa_key_attributes_t *attributes,
505 uint8_t *data,
506 size_t data_size,
507 size_t *data_length);
508
509/** Safe output buffer size for psa_get_key_domain_parameters().
510 *
511 * This macro returns a compile-time constant if its arguments are
512 * compile-time constants.
513 *
514 * \warning This function may call its arguments multiple times or
515 * zero times, so you should not pass arguments that contain
516 * side effects.
517 *
518 * \note This is an experimental extension to the interface. It may change
519 * in future versions of the library.
520 *
521 * \param key_type A supported key type.
522 * \param key_bits The size of the key in bits.
523 *
524 * \return If the parameters are valid and supported, return
525 * a buffer size in bytes that guarantees that
526 * psa_get_key_domain_parameters() will not fail with
527 * #PSA_ERROR_BUFFER_TOO_SMALL.
528 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200529 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200530 * sensible size or 0.
531 * If the parameters are not valid, the
532 * return value is unspecified.
533 */
534#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
535 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
536 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
537 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
538 0)
539#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
540 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
541#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
542 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
543
544/**@}*/
545
Gilles Peskine5055b232019-12-12 17:49:31 +0100546
Gilles Peskineb8af2282020-11-13 18:00:34 +0100547/** \defgroup psa_external_rng External random generator
548 * @{
549 */
550
551#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
552/** External random generator function, implemented by the platform.
553 *
554 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
555 * this function replaces Mbed TLS's entropy and DRBG modules for all
556 * random generation triggered via PSA crypto interfaces.
557 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100558 * \note This random generator must deliver random numbers with cryptographic
559 * quality and high performance. It must supply unpredictable numbers
560 * with a uniform distribution. The implementation of this function
561 * is responsible for ensuring that the random generator is seeded
562 * with sufficient entropy. If you have a hardware TRNG which is slow
563 * or delivers non-uniform output, declare it as an entropy source
564 * with mbedtls_entropy_add_source() instead of enabling this option.
565 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100566 * \param[in,out] context Pointer to the random generator context.
567 * This is all-bits-zero on the first call
568 * and preserved between successive calls.
569 * \param[out] output Output buffer. On success, this buffer
570 * contains random data with a uniform
571 * distribution.
572 * \param output_size The size of the \p output buffer in bytes.
573 * \param[out] output_length On success, set this value to \p output_size.
574 *
575 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100576 * Success. The output buffer contains \p output_size bytes of
577 * cryptographic-quality random data, and \c *output_length is
578 * set to \p output_size.
579 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
580 * The random generator requires extra entropy and there is no
581 * way to obtain entropy under current environment conditions.
582 * This error should not happen under normal circumstances since
583 * this function is responsible for obtaining as much entropy as
584 * it needs. However implementations of this function may return
585 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
586 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100587 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100588 * A failure of the random generator hardware that isn't covered
589 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100590 */
591psa_status_t mbedtls_psa_external_get_random(
592 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100594#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
595
596/**@}*/
597
Steven Cooreman6801f082021-02-19 17:21:22 +0100598/** \defgroup psa_builtin_keys Built-in keys
599 * @{
600 */
601
602/** The minimum value for a key identifier that is built into the
603 * implementation.
604 *
605 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
606 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
607 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
608 * with any other set of implementation-chosen key identifiers.
609 *
610 * This value is part of the library's ABI since changing it would invalidate
611 * the values of built-in key identifiers in applications.
612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100614
615/** The maximum value for a key identifier that is built into the
616 * implementation.
617 *
618 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
619 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100621
622/** A slot number identifying a key in a driver.
623 *
624 * Values of this type are used to identify built-in keys.
625 */
626typedef uint64_t psa_drv_slot_number_t;
627
628#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
629/** Test whether a key identifier belongs to the builtin key range.
630 *
631 * \param key_id Key identifier to test.
632 *
633 * \retval 1
634 * The key identifier is a builtin key identifier.
635 * \retval 0
636 * The key identifier is not a builtin key identifier.
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100639{
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
641 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100642}
643
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200644/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100645 *
646 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100647 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100648 * as part of a platform's system image.
649 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100650 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100651 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
652 *
653 * In a multi-application configuration
654 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
655 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
656 * is allowed to use the given key.
657 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100658 * \param key_id The key ID for which to retrieve the
659 * location and slot attributes.
660 * \param[out] lifetime On success, the lifetime associated with the key
661 * corresponding to \p key_id. Lifetime is a
662 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200663 * and with what persistence level the key is
664 * intended to be used. If the platform
665 * implementation does not contain specific
666 * information about the intended key persistence
667 * level, the persistence level may be reported as
668 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100669 * \param[out] slot_number On success, the slot number known to the driver
670 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200671 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100672 * requested built-in key.
673 *
674 * \retval #PSA_SUCCESS
675 * The requested key identifier designates a built-in key.
676 * In a multi-application configuration, the requested owner
677 * is allowed to access it.
678 * \retval #PSA_ERROR_DOES_NOT_EXIST
679 * The requested key identifier is not a built-in key which is known
680 * to this function. If a key exists in the key storage with this
681 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100682 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100683 * Any other error is propagated to the function that requested the key.
684 * Common errors include:
685 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
686 * is not allowed to access it.
687 */
688psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100689 mbedtls_svc_key_id_t key_id,
690 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100692#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
693
694/** @} */
695
Janos Follath702cf092021-05-26 12:58:23 +0100696/** \addtogroup crypto_types
697 * @{
698 */
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100701
702/** Whether the specified algorithm is a password-authenticated key exchange.
703 *
704 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
705 *
706 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
707 * algorithm, 0 otherwise.
708 * This macro may return either 0 or 1 if \p alg is not a supported
709 * algorithm identifier.
710 */
711#define PSA_ALG_IS_PAKE(alg) \
712 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
713
714/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
715 *
716 * This is J-PAKE as defined by RFC 8236, instantiated with the following
717 * parameters:
718 *
719 * - The group can be either an elliptic curve or defined over a finite field.
720 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
721 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100722 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100723 *
Janos Follath46c02372021-06-08 15:22:51 +0100724 * To select these parameters and set up the cipher suite, call these functions
725 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100726 *
727 * \code
728 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
729 * psa_pake_cs_set_primitive(cipher_suite,
730 * PSA_PAKE_PRIMITIVE(type, family, bits));
731 * psa_pake_cs_set_hash(cipher_suite, hash);
732 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100733 *
734 * For more information on how to set a specific curve or field, refer to the
735 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
736 *
737 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100738 *
739 * \code
740 * psa_pake_setup(operation, cipher_suite);
741 * psa_pake_set_user(operation, ...);
742 * psa_pake_set_peer(operation, ...);
743 * psa_pake_set_password_key(operation, ...);
744 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100745 *
Neil Armstrong16145372022-05-20 10:42:36 +0200746 * The password is provided as a key. This can be the password text itself,
747 * in an agreed character encoding, or some value derived from the password
748 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100749 *
Neil Armstrong16145372022-05-20 10:42:36 +0200750 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100751 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
752 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
753 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200754 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100755 * of the reduction is 0.)
756 *
757 * The key exchange flow for J-PAKE is as follows:
758 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100759 * \code
760 * // Get g1
761 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
762 * // Get the ZKP public key for x1
763 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
764 * // Get the ZKP proof for x1
765 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
766 * // Get g2
767 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
768 * // Get the ZKP public key for x2
769 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
770 * // Get the ZKP proof for x2
771 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
772 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100773 * -# To provide the first round data received from the peer to the operation,
774 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100775 * \code
776 * // Set g3
777 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
778 * // Set the ZKP public key for x3
779 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
780 * // Set the ZKP proof for x3
781 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
782 * // Set g4
783 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
784 * // Set the ZKP public key for x4
785 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
786 * // Set the ZKP proof for x4
787 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
788 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100789 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100790 * \code
791 * // Get A
792 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
793 * // Get ZKP public key for x2*s
794 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
795 * // Get ZKP proof for x2*s
796 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
797 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100798 * -# To provide the second round data received from the peer to the operation,
799 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100800 * \code
801 * // Set B
802 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
803 * // Set ZKP public key for x4*s
804 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
805 * // Set ZKP proof for x4*s
806 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
807 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100808 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100809 * \code
810 * // Get Ka=Kb=K
811 * psa_pake_get_implicit_key()
812 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100813 *
814 * For more information consult the documentation of the individual
815 * \c PSA_PAKE_STEP_XXX constants.
816 *
817 * At this point there is a cryptographic guarantee that only the authenticated
818 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100819 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100820 *
821 * That is, the authentication is only implicit (the peer is not authenticated
822 * at this point, and no action should be taken that assume that they are - like
823 * for example accessing restricted files).
824 *
825 * To make the authentication explicit there are various methods, see Section 5
826 * of RFC 8236 for two examples.
827 *
828 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100830
831/** @} */
832
833/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100834 *
835 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
836 * the official PSA Crypto API yet.
837 *
838 * \note The content of this section is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200839 * of Mbed TLS and may change arbitrarily from version to version.
Janos Follath7d69b3a2021-05-26 13:10:56 +0100840 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
841 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100842 * @{
843 */
844
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200845/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100846 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200847 * Encodes the application's role in the algorithm is being executed. For more
848 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
849 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100850 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200851typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100852
853/** Encoding of input and output indicators for PAKE.
854 *
855 * Some PAKE algorithms need to exchange more data than just a single key share.
856 * This type is for encoding additional input and output data for such
857 * algorithms.
858 */
859typedef uint8_t psa_pake_step_t;
860
861/** Encoding of the type of the PAKE's primitive.
862 *
863 * Values defined by this standard will never be in the range 0x80-0xff.
864 * Vendors who define additional types must use an encoding in this range.
865 *
866 * For more information see the documentation of individual
867 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
868 */
869typedef uint8_t psa_pake_primitive_type_t;
870
871/** \brief Encoding of the family of the primitive associated with the PAKE.
872 *
873 * For more information see the documentation of individual
874 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
875 */
876typedef uint8_t psa_pake_family_t;
877
878/** \brief Encoding of the primitive associated with the PAKE.
879 *
880 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
881 */
882typedef uint32_t psa_pake_primitive_t;
883
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200884/** A value to indicate no role in a PAKE algorithm.
885 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
886 * algorithms which do not assign roles.
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200889
Janos Follath702cf092021-05-26 12:58:23 +0100890/** The first peer in a balanced PAKE.
891 *
892 * Although balanced PAKE algorithms are symmetric, some of them needs an
893 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200894 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100895 * accepted.
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100898
899/** The second peer in a balanced PAKE.
900 *
901 * Although balanced PAKE algorithms are symmetric, some of them needs an
902 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200903 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100904 * accepted.
905 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100907
908/** The client in an augmented PAKE.
909 *
910 * Augmented PAKE algorithms need to differentiate between client and server.
911 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +0100913
914/** The server in an augmented PAKE.
915 *
916 * Augmented PAKE algorithms need to differentiate between client and server.
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +0100919
920/** The PAKE primitive type indicating the use of elliptic curves.
921 *
922 * The values of the \c family and \c bits fields of the cipher suite identify a
923 * specific elliptic curve, using the same mapping that is used for ECC
924 * (::psa_ecc_family_t) keys.
925 *
926 * (Here \c family means the value returned by psa_pake_cs_get_family() and
927 * \c bits means the value returned by psa_pake_cs_get_bits().)
928 *
929 * Input and output during the operation can involve group elements and scalar
930 * values:
931 * -# The format for group elements is the same as for public keys on the
932 * specific curve would be. For more information, consult the documentation of
933 * psa_export_public_key().
934 * -# The format for scalars is the same as for private keys on the specific
935 * curve would be. For more information, consult the documentation of
936 * psa_export_key().
937 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100939
940/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
941 *
942 * The values of the \c family and \c bits fields of the cipher suite identify
943 * a specific Diffie-Hellman group, using the same mapping that is used for
944 * Diffie-Hellman (::psa_dh_family_t) keys.
945 *
946 * (Here \c family means the value returned by psa_pake_cs_get_family() and
947 * \c bits means the value returned by psa_pake_cs_get_bits().)
948 *
949 * Input and output during the operation can involve group elements and scalar
950 * values:
951 * -# The format for group elements is the same as for public keys on the
952 * specific group would be. For more information, consult the documentation of
953 * psa_export_public_key().
954 * -# The format for scalars is the same as for private keys on the specific
955 * group would be. For more information, consult the documentation of
956 * psa_export_key().
957 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100959
960/** Construct a PAKE primitive from type, family and bit-size.
961 *
962 * \param pake_type The type of the primitive
963 * (value of type ::psa_pake_primitive_type_t).
964 * \param pake_family The family of the primitive
965 * (the type and interpretation of this parameter depends
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500966 * on \p pake_type, for more information consult the
Janos Follath702cf092021-05-26 12:58:23 +0100967 * documentation of individual ::psa_pake_primitive_type_t
968 * constants).
969 * \param pake_bits The bit-size of the primitive
970 * (Value of type \c size_t. The interpretation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500971 * of this parameter depends on \p pake_family, for more
Janos Follath702cf092021-05-26 12:58:23 +0100972 * information consult the documentation of individual
973 * ::psa_pake_primitive_type_t constants).
974 *
975 * \return The constructed primitive value of type ::psa_pake_primitive_t.
976 * Return 0 if the requested primitive can't be encoded as
977 * ::psa_pake_primitive_t.
978 */
979#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
980 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
981 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +0100983
984/** The key share being sent to or received from the peer.
985 *
986 * The format for both input and output at this step is the same as for public
987 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
988 * be.
989 *
990 * For more information on the format, consult the documentation of
991 * psa_export_public_key().
992 *
993 * For information regarding how the group is determined, consult the
994 * documentation #PSA_PAKE_PRIMITIVE.
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100997
998/** A Schnorr NIZKP public key.
999 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001000 * This is the ephemeral public key in the Schnorr Non-Interactive
1001 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1002 *
Janos Follath702cf092021-05-26 12:58:23 +01001003 * The format for both input and output at this step is the same as for public
1004 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1005 * be.
1006 *
1007 * For more information on the format, consult the documentation of
1008 * psa_export_public_key().
1009 *
1010 * For information regarding how the group is determined, consult the
1011 * documentation #PSA_PAKE_PRIMITIVE.
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001014
1015/** A Schnorr NIZKP proof.
1016 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001017 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1018 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +01001019 *
Janos Follath1f013182021-06-08 15:30:48 +01001020 * Both for input and output, the value at this step is an integer less than
1021 * the order of the group selected in the cipher suite. The format depends on
1022 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +01001023 *
Janos Follath1f013182021-06-08 15:30:48 +01001024 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +01001025 * - For everything else the encoding is big endian (see Section 2.3.8 of
1026 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +01001027 *
Janos Follath1f013182021-06-08 15:30:48 +01001028 * In both cases leading zeroes are allowed as long as the length in bytes does
1029 * not exceed the byte length of the group order.
1030 *
Janos Follath702cf092021-05-26 12:58:23 +01001031 * For information regarding how the group is determined, consult the
1032 * documentation #PSA_PAKE_PRIMITIVE.
1033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +01001035
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001036/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +01001037 *
1038 * This is an implementation-defined \c struct. Applications should not
1039 * make any assumptions about the content of this structure.
1040 * Implementation details can change in future versions without notice.
1041 */
1042typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1043
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001044/** Return an initial value for a PAKE cipher suite object.
1045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001047
Janos Follath702cf092021-05-26 12:58:23 +01001048/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1049 *
Janos Follath702cf092021-05-26 12:58:23 +01001050 * \param[in] cipher_suite The cipher suite structure to query.
1051 *
1052 * \return The PAKE algorithm stored in the cipher suite structure.
1053 */
1054static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001056
1057/** Declare the PAKE algorithm for the cipher suite.
1058 *
1059 * This function overwrites any PAKE algorithm
1060 * previously set in \p cipher_suite.
1061 *
Janos Follath702cf092021-05-26 12:58:23 +01001062 * \param[out] cipher_suite The cipher suite structure to write to.
1063 * \param algorithm The PAKE algorithm to write.
1064 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1065 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1066 * If this is 0, the PAKE algorithm in
1067 * \p cipher_suite becomes unspecified.
1068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1070 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +01001071
1072/** Retrieve the primitive from a PAKE cipher suite.
1073 *
Janos Follath702cf092021-05-26 12:58:23 +01001074 * \param[in] cipher_suite The cipher suite structure to query.
1075 *
1076 * \return The primitive stored in the cipher suite structure.
1077 */
1078static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001080
1081/** Declare the primitive for a PAKE cipher suite.
1082 *
1083 * This function overwrites any primitive previously set in \p cipher_suite.
1084 *
Janos Follath702cf092021-05-26 12:58:23 +01001085 * \param[out] cipher_suite The cipher suite structure to write to.
1086 * \param primitive The primitive to write. If this is 0, the
1087 * primitive type in \p cipher_suite becomes
1088 * unspecified.
1089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1091 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +01001092
Neil Armstrongff9cac72022-05-20 10:25:15 +02001093/** Retrieve the PAKE family from a PAKE cipher suite.
1094 *
Neil Armstrongff9cac72022-05-20 10:25:15 +02001095 * \param[in] cipher_suite The cipher suite structure to query.
1096 *
1097 * \return The PAKE family stored in the cipher suite structure.
1098 */
1099static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +02001101
Neil Armstrongd5a48252022-05-20 10:26:36 +02001102/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1103 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001104 * \param[in] cipher_suite The cipher suite structure to query.
1105 *
1106 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1107 */
1108static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001110
Janos Follath702cf092021-05-26 12:58:23 +01001111/** Retrieve the hash algorithm from a PAKE cipher suite.
1112 *
Janos Follath702cf092021-05-26 12:58:23 +01001113 * \param[in] cipher_suite The cipher suite structure to query.
1114 *
1115 * \return The hash algorithm stored in the cipher suite structure. The return
1116 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1117 * the hash algorithm is not set.
1118 */
1119static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001121
1122/** Declare the hash algorithm for a PAKE cipher suite.
1123 *
1124 * This function overwrites any hash algorithm
1125 * previously set in \p cipher_suite.
1126 *
Janos Follath702cf092021-05-26 12:58:23 +01001127 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1128 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1129 * for more information.
1130 *
1131 * \param[out] cipher_suite The cipher suite structure to write to.
1132 * \param hash The hash involved in the cipher suite.
1133 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1134 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1135 * If this is 0, the hash algorithm in
1136 * \p cipher_suite becomes unspecified.
1137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1139 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001140
1141/** The type of the state data structure for PAKE operations.
1142 *
1143 * Before calling any function on a PAKE operation object, the application
1144 * must initialize it by any of the following means:
1145 * - Set the structure to all-bits-zero, for example:
1146 * \code
1147 * psa_pake_operation_t operation;
1148 * memset(&operation, 0, sizeof(operation));
1149 * \endcode
1150 * - Initialize the structure to logical zero values, for example:
1151 * \code
1152 * psa_pake_operation_t operation = {0};
1153 * \endcode
1154 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1155 * for example:
1156 * \code
1157 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1158 * \endcode
1159 * - Assign the result of the function psa_pake_operation_init()
1160 * to the structure, for example:
1161 * \code
1162 * psa_pake_operation_t operation;
1163 * operation = psa_pake_operation_init();
1164 * \endcode
1165 *
1166 * This is an implementation-defined \c struct. Applications should not
1167 * make any assumptions about the content of this structure.
1168 * Implementation details can change in future versions without notice. */
1169typedef struct psa_pake_operation_s psa_pake_operation_t;
1170
Przemek Stekiel51eac532022-12-07 11:04:51 +01001171/** The type of input values for PAKE operations. */
1172typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1173
Przemek Stekielb09c4872023-01-17 12:05:38 +01001174/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001175typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1176
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001177/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001180
Przemek Stekielc0e62502023-03-14 11:49:36 +01001181/** Get the length of the password in bytes from given inputs.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001182 *
1183 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001184 * \param[out] password_len Password length.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001185 *
1186 * \retval #PSA_SUCCESS
1187 * Success.
1188 * \retval #PSA_ERROR_BAD_STATE
1189 * Password hasn't been set yet.
1190 */
1191psa_status_t psa_crypto_driver_pake_get_password_len(
1192 const psa_crypto_driver_pake_inputs_t *inputs,
1193 size_t *password_len);
1194
1195/** Get the password from given inputs.
1196 *
1197 * \param[in] inputs Operation inputs.
1198 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001199 * \param buffer_size Size of the return buffer in bytes.
1200 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001201 *
1202 * \retval #PSA_SUCCESS
1203 * Success.
1204 * \retval #PSA_ERROR_BAD_STATE
1205 * Password hasn't been set yet.
1206 */
1207psa_status_t psa_crypto_driver_pake_get_password(
1208 const psa_crypto_driver_pake_inputs_t *inputs,
1209 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1210
Przemek Stekielc0e62502023-03-14 11:49:36 +01001211/** Get the length of the user id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001212 *
1213 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001214 * \param[out] user_len User id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001215 *
1216 * \retval #PSA_SUCCESS
1217 * Success.
1218 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001219 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001220 */
1221psa_status_t psa_crypto_driver_pake_get_user_len(
1222 const psa_crypto_driver_pake_inputs_t *inputs,
1223 size_t *user_len);
1224
Przemek Stekielc0e62502023-03-14 11:49:36 +01001225/** Get the length of the peer id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001226 *
1227 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001228 * \param[out] peer_len Peer id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001229 *
1230 * \retval #PSA_SUCCESS
1231 * Success.
1232 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001233 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001234 */
1235psa_status_t psa_crypto_driver_pake_get_peer_len(
1236 const psa_crypto_driver_pake_inputs_t *inputs,
1237 size_t *peer_len);
1238
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001239/** Get the user id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001240 *
1241 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001242 * \param[out] user_id User id.
1243 * \param user_id_size Size of \p user_id in bytes.
1244 * \param[out] user_id_len Size of the user id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001245 *
1246 * \retval #PSA_SUCCESS
1247 * Success.
1248 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001249 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001250 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001251 * The size of the \p user_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001252 */
1253psa_status_t psa_crypto_driver_pake_get_user(
1254 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001255 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001256
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001257/** Get the peer id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001258 *
1259 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001260 * \param[out] peer_id Peer id.
1261 * \param peer_id_size Size of \p peer_id in bytes.
1262 * \param[out] peer_id_length Size of the peer id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001263 *
1264 * \retval #PSA_SUCCESS
1265 * Success.
1266 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001267 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001268 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001269 * The size of the \p peer_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001270 */
1271psa_status_t psa_crypto_driver_pake_get_peer(
1272 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001273 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001274
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001275/** Get the cipher suite from given inputs.
1276 *
1277 * \param[in] inputs Operation inputs.
1278 * \param[out] cipher_suite Return buffer for role.
1279 *
1280 * \retval #PSA_SUCCESS
1281 * Success.
1282 * \retval #PSA_ERROR_BAD_STATE
1283 * Cipher_suite hasn't been set yet.
1284 */
1285psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1286 const psa_crypto_driver_pake_inputs_t *inputs,
1287 psa_pake_cipher_suite_t *cipher_suite);
1288
Janos Follath702cf092021-05-26 12:58:23 +01001289/** Set the session information for a password-authenticated key exchange.
1290 *
1291 * The sequence of operations to set up a password-authenticated key exchange
1292 * is as follows:
1293 * -# Allocate an operation object which will be passed to all the functions
1294 * listed here.
1295 * -# Initialize the operation object with one of the methods described in the
1296 * documentation for #psa_pake_operation_t, e.g.
1297 * #PSA_PAKE_OPERATION_INIT.
1298 * -# Call psa_pake_setup() to specify the cipher suite.
1299 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1300 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1301 * to be called depends on the algorithm in use.
1302 *
1303 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1304 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1305 * for more information.
1306 *
1307 * A typical sequence of calls to perform a password-authenticated key
1308 * exchange:
1309 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1310 * key share that needs to be sent to the peer.
1311 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1312 * the key share that was received from the peer.
1313 * -# Depending on the algorithm additional calls to psa_pake_output() and
1314 * psa_pake_input() might be necessary.
1315 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1316 *
1317 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1318 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1319 * for more information.
1320 *
1321 * If an error occurs at any step after a call to psa_pake_setup(),
1322 * the operation will need to be reset by a call to psa_pake_abort(). The
1323 * application may call psa_pake_abort() at any time after the operation
1324 * has been initialized.
1325 *
1326 * After a successful call to psa_pake_setup(), the application must
1327 * eventually terminate the operation. The following events terminate an
1328 * operation:
1329 * - A call to psa_pake_abort().
1330 * - A successful call to psa_pake_get_implicit_key().
1331 *
1332 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001333 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001334 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001335 * characterizes a PAKE algorithm and determines
1336 * the algorithm as well.)
1337 *
1338 * \retval #PSA_SUCCESS
1339 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001340 * \retval #PSA_ERROR_INVALID_ARGUMENT
1341 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1342 * PAKE primitive in \p cipher_suite is not compatible with the
1343 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1344 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001345 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001346 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1347 * or the PAKE primitive in \p cipher_suite is not supported or not
1348 * compatible with the PAKE algorithm, or the hash algorithm in
1349 * \p cipher_suite is not supported or not compatible with the PAKE
1350 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1352 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001353 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001354 * The operation state is not valid, or
1355 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001356 * It is implementation-dependent whether a failure to initialize
1357 * results in this error code.
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1360 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001361
1362/** Set the password for a password-authenticated key exchange from key ID.
1363 *
1364 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001365 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001366 *
1367 * \param[in,out] operation The operation object to set the password for. It
1368 * must have been set up by psa_pake_setup() and
1369 * not yet in use (neither psa_pake_output() nor
1370 * psa_pake_input() has been called yet). It must
1371 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001372 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001373 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001374 * \param password Identifier of the key holding the password or a
1375 * value derived from the password (eg. by a
1376 * memory-hard function). It must remain valid
1377 * until the operation terminates. It must be of
1378 * type #PSA_KEY_TYPE_PASSWORD or
1379 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1380 * the usage #PSA_KEY_USAGE_DERIVE.
1381 *
1382 * \retval #PSA_SUCCESS
1383 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001384 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001385 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001386 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001387 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1388 * permit the \p operation's algorithm.
1389 * \retval #PSA_ERROR_INVALID_ARGUMENT
1390 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1391 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1392 * the \p operation's cipher suite.
1393 * \retval #PSA_ERROR_NOT_SUPPORTED
1394 * The key type or key size of \p password is not supported with the
1395 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1397 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1398 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1399 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1400 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001401 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001402 * The operation state is not valid (it must have been set up.), or
1403 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001404 * It is implementation-dependent whether a failure to initialize
1405 * results in this error code.
1406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1408 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001409
Janos Follath702cf092021-05-26 12:58:23 +01001410/** Set the user ID for a password-authenticated key exchange.
1411 *
1412 * Call this function to set the user ID. For PAKE algorithms that associate a
1413 * user identifier with each side of the session you need to call
1414 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1415 * user identifier with the session, call psa_pake_set_user() only.
1416 *
1417 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1418 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1419 * for more information.
1420 *
1421 * \param[in,out] operation The operation object to set the user ID for. It
1422 * must have been set up by psa_pake_setup() and
1423 * not yet in use (neither psa_pake_output() nor
1424 * psa_pake_input() has been called yet). It must
1425 * be on operation for which the user ID hasn't
1426 * been set (psa_pake_set_user() hasn't been
1427 * called yet).
1428 * \param[in] user_id The user ID to authenticate with.
1429 * \param user_id_len Size of the \p user_id buffer in bytes.
1430 *
1431 * \retval #PSA_SUCCESS
1432 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001433 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001434 * \p user_id is not valid for the \p operation's algorithm and cipher
1435 * suite.
1436 * \retval #PSA_ERROR_NOT_SUPPORTED
1437 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1439 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1440 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001441 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001442 * The operation state is not valid, or
1443 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001444 * It is implementation-dependent whether a failure to initialize
1445 * results in this error code.
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1448 const uint8_t *user_id,
1449 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001450
1451/** Set the peer ID for a password-authenticated key exchange.
1452 *
1453 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1454 * that associate a user identifier with each side of the session. For PAKE
1455 * algorithms that associate a single user identifier with the session, call
1456 * psa_pake_set_user() only.
1457 *
1458 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1459 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1460 * for more information.
1461 *
1462 * \param[in,out] operation The operation object to set the peer ID for. It
1463 * must have been set up by psa_pake_setup() and
1464 * not yet in use (neither psa_pake_output() nor
1465 * psa_pake_input() has been called yet). It must
1466 * be on operation for which the peer ID hasn't
1467 * been set (psa_pake_set_peer() hasn't been
1468 * called yet).
1469 * \param[in] peer_id The peer's ID to authenticate.
1470 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1471 *
1472 * \retval #PSA_SUCCESS
1473 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001474 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001475 * \p peer_id is not valid for the \p operation's algorithm and cipher
Neil Armstrong16ff7882022-05-20 11:04:20 +02001476 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001477 * \retval #PSA_ERROR_NOT_SUPPORTED
1478 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1481 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001482 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001483 * Calling psa_pake_set_peer() is invalid with the \p operation's
1484 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001485 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001486 * It is implementation-dependent whether a failure to initialize
1487 * results in this error code.
1488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1490 const uint8_t *peer_id,
1491 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001492
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001493/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001494 *
1495 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001496 * It is optional to call this function for PAKEs that don't require a role
1497 * to be specified. For such PAKEs the application role parameter is ignored,
1498 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001499 *
1500 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1501 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1502 * for more information.
1503 *
Neil Armstrongef157512022-05-25 11:49:45 +02001504 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001505 * application's role for. It must have been set up
1506 * by psa_pake_setup() and not yet in use (neither
1507 * psa_pake_output() nor psa_pake_input() has been
1508 * called yet). It must be on operation for which
1509 * the application's role hasn't been specified
1510 * (psa_pake_set_role() hasn't been called yet).
1511 * \param role A value of type ::psa_pake_role_t indicating the
1512 * application's role in the PAKE the algorithm
1513 * that is being set up. For more information see
1514 * the documentation of \c PSA_PAKE_ROLE_XXX
1515 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001516 *
1517 * \retval #PSA_SUCCESS
1518 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001519 * \retval #PSA_ERROR_INVALID_ARGUMENT
1520 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001521 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001522 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001523 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1524 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001525 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001526 * The operation state is not valid, or
1527 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001528 * It is implementation-dependent whether a failure to initialize
1529 * results in this error code.
1530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1532 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001533
1534/** Get output for a step of a password-authenticated key exchange.
1535 *
1536 * Depending on the algorithm being executed, you might need to call this
1537 * function several times or you might not need to call this at all.
1538 *
1539 * The exact sequence of calls to perform a password-authenticated key
1540 * exchange depends on the algorithm in use. Refer to the documentation of
1541 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1542 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1543 * information.
1544 *
1545 * If this function returns an error status, the operation enters an error
1546 * state and must be aborted by calling psa_pake_abort().
1547 *
1548 * \param[in,out] operation Active PAKE operation.
1549 * \param step The step of the algorithm for which the output is
1550 * requested.
1551 * \param[out] output Buffer where the output is to be written in the
1552 * format appropriate for this \p step. Refer to
1553 * the documentation of the individual
1554 * \c PSA_PAKE_STEP_XXX constants for more
1555 * information.
1556 * \param output_size Size of the \p output buffer in bytes. This must
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001557 * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1558 * primitive, \p output_step) where \c alg and
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001559 * \p primitive are the PAKE algorithm and primitive
1560 * in the operation's cipher suite, and \p step is
1561 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001562 *
1563 * \param[out] output_length On success, the number of bytes of the returned
1564 * output.
1565 *
1566 * \retval #PSA_SUCCESS
1567 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001568 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1569 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001570 * \retval #PSA_ERROR_INVALID_ARGUMENT
1571 * \p step is not compatible with the operation's algorithm.
1572 * \retval #PSA_ERROR_NOT_SUPPORTED
1573 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001574 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1577 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1578 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1579 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1580 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001581 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001582 * The operation state is not valid (it must be active, and fully set
1583 * up, and this call must conform to the algorithm's requirements
1584 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001585 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001586 * It is implementation-dependent whether a failure to initialize
1587 * results in this error code.
1588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1590 psa_pake_step_t step,
1591 uint8_t *output,
1592 size_t output_size,
1593 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001594
1595/** Provide input for a step of a password-authenticated key exchange.
1596 *
1597 * Depending on the algorithm being executed, you might need to call this
1598 * function several times or you might not need to call this at all.
1599 *
1600 * The exact sequence of calls to perform a password-authenticated key
1601 * exchange depends on the algorithm in use. Refer to the documentation of
1602 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1603 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1604 * information.
1605 *
1606 * If this function returns an error status, the operation enters an error
1607 * state and must be aborted by calling psa_pake_abort().
1608 *
1609 * \param[in,out] operation Active PAKE operation.
1610 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001611 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001612 * appropriate for this \p step. Refer to the
1613 * documentation of the individual
1614 * \c PSA_PAKE_STEP_XXX constants for more
1615 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001616 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001617 *
1618 * \retval #PSA_SUCCESS
1619 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001620 * \retval #PSA_ERROR_INVALID_SIGNATURE
1621 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001622 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001623 * \p input_length is not compatible with the \p operation’s algorithm,
1624 * or the \p input is not valid for the \p operation's algorithm,
1625 * cipher suite or \p step.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001626 * \retval #PSA_ERROR_NOT_SUPPORTED
1627 * \p step p is not supported with the \p operation's algorithm, or the
1628 * \p input is not supported for the \p operation's algorithm, cipher
1629 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1631 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1632 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1633 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1634 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1635 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001636 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001637 * The operation state is not valid (it must be active, and fully set
1638 * up, and this call must conform to the algorithm's requirements
1639 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001640 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001641 * It is implementation-dependent whether a failure to initialize
1642 * results in this error code.
1643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1645 psa_pake_step_t step,
1646 const uint8_t *input,
1647 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001648
1649/** Get implicitly confirmed shared secret from a PAKE.
1650 *
1651 * At this point there is a cryptographic guarantee that only the authenticated
1652 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001653 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001654 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001655 * That is, the authentication is only implicit. Since the peer is not
1656 * authenticated yet, no action should be taken yet that assumes that the peer
1657 * is who it claims to be. For example, do not access restricted files on the
1658 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001659 *
1660 * This function can be called after the key exchange phase of the operation
1661 * has completed. It imports the shared secret output of the PAKE into the
1662 * provided derivation operation. The input step
1663 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1664 * material in the key derivation operation.
1665 *
1666 * The exact sequence of calls to perform a password-authenticated key
1667 * exchange depends on the algorithm in use. Refer to the documentation of
1668 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1669 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1670 * information.
1671 *
1672 * When this function returns successfully, \p operation becomes inactive.
1673 * If this function returns an error status, both \p operation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001674 * and \c key_derivation operations enter an error state and must be aborted by
Janos Follath702cf092021-05-26 12:58:23 +01001675 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1676 *
1677 * \param[in,out] operation Active PAKE operation.
1678 * \param[out] output A key derivation operation that is ready
1679 * for an input step of type
1680 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1681 *
1682 * \retval #PSA_SUCCESS
1683 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001684 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001685 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1686 * algorithm in the \p output key derivation operation.
1687 * \retval #PSA_ERROR_NOT_SUPPORTED
1688 * Input from a PAKE is not supported by the algorithm in the \p output
1689 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1692 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1693 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1694 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1695 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001696 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001697 * The PAKE operation state is not valid (it must be active, but beyond
1698 * that validity is specific to the algorithm), or
1699 * the library has not been previously initialized by psa_crypto_init(),
1700 * or the state of \p output is not valid for
1701 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1702 * step is out of order or the application has done this step already
1703 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001704 * It is implementation-dependent whether a failure to initialize
1705 * results in this error code.
1706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1708 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001709
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001710/** Abort a PAKE operation.
1711 *
1712 * Aborting an operation frees all associated resources except for the \c
1713 * operation structure itself. Once aborted, the operation object can be reused
1714 * for another operation by calling psa_pake_setup() again.
1715 *
1716 * This function may be called at any time after the operation
1717 * object has been initialized as described in #psa_pake_operation_t.
1718 *
1719 * In particular, calling psa_pake_abort() after the operation has been
1720 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1721 * is safe and has no effect.
1722 *
1723 * \param[in,out] operation The operation to abort.
1724 *
1725 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001726 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001727 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1728 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001729 * \retval #PSA_ERROR_BAD_STATE
1730 * The library has not been previously initialized by psa_crypto_init().
1731 * It is implementation-dependent whether a failure to initialize
1732 * results in this error code.
1733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001735
Janos Follath702cf092021-05-26 12:58:23 +01001736/**@}*/
1737
1738/** A sufficient output buffer size for psa_pake_output().
1739 *
1740 * If the size of the output buffer is at least this large, it is guaranteed
1741 * that psa_pake_output() will not fail due to an insufficient output buffer
1742 * size. The actual size of the output might be smaller in any given call.
1743 *
1744 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1745 *
Janos Follath46c02372021-06-08 15:22:51 +01001746 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001747 * #PSA_ALG_IS_PAKE(\p alg) is true).
1748 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1749 * compatible with algorithm \p alg.
1750 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1751 * algorithm \p alg.
1752 * \return A sufficient output buffer size for the specified
Neil Armstrongcd974d52022-05-20 10:30:12 +02001753 * PAKE algorithm, primitive, and output step. If the
1754 * PAKE algorithm, primitive, or output step is not
1755 * recognized, or the parameters are incompatible,
1756 * return 0.
Janos Follath702cf092021-05-26 12:58:23 +01001757 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001758#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 (alg == PSA_ALG_JPAKE && \
1760 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1761 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1762 ( \
1763 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1764 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1765 32 \
1766 ) : \
1767 0)
Janos Follath702cf092021-05-26 12:58:23 +01001768
1769/** A sufficient input buffer size for psa_pake_input().
1770 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001771 * The value returned by this macro is guaranteed to be large enough for any
1772 * valid input to psa_pake_input() in an operation with the specified
1773 * parameters.
Janos Follath702cf092021-05-26 12:58:23 +01001774 *
1775 * See also #PSA_PAKE_INPUT_MAX_SIZE
1776 *
Janos Follath46c02372021-06-08 15:22:51 +01001777 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001778 * #PSA_ALG_IS_PAKE(\p alg) is true).
1779 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1780 * compatible with algorithm \p alg.
Janos Follathec83eb62021-05-27 08:41:59 +01001781 * \param input_step A value of type ::psa_pake_step_t that is valid for the
Janos Follath702cf092021-05-26 12:58:23 +01001782 * algorithm \p alg.
Janos Follath38d29db2021-06-03 13:14:42 +01001783 * \return A sufficient input buffer size for the specified
1784 * input, cipher suite and algorithm. If the cipher suite,
1785 * the input type or PAKE algorithm is not recognized, or
Janos Follath702cf092021-05-26 12:58:23 +01001786 * the parameters are incompatible, return 0.
1787 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001788#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 (alg == PSA_ALG_JPAKE && \
1790 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1791 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1792 ( \
1793 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1794 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1795 32 \
1796 ) : \
1797 0)
Janos Follath702cf092021-05-26 12:58:23 +01001798
Neil Armstrong2056ce52022-05-25 11:38:15 +02001799/** Output buffer size for psa_pake_output() for any of the supported PAKE
1800 * algorithm and primitive suites and output step.
Janos Follath702cf092021-05-26 12:58:23 +01001801 *
1802 * This macro must expand to a compile-time constant integer.
1803 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001804 * The value of this macro must be at least as large as the largest value
1805 * returned by PSA_PAKE_OUTPUT_SIZE()
1806 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001807 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001808 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001809#define PSA_PAKE_OUTPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001810
Neil Armstrong2056ce52022-05-25 11:38:15 +02001811/** Input buffer size for psa_pake_input() for any of the supported PAKE
1812 * algorithm and primitive suites and input step.
Janos Follath702cf092021-05-26 12:58:23 +01001813 *
1814 * This macro must expand to a compile-time constant integer.
1815 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001816 * The value of this macro must be at least as large as the largest value
1817 * returned by PSA_PAKE_INPUT_SIZE()
1818 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001819 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001820 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001821#define PSA_PAKE_INPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001822
Neil Armstrongfb993022022-05-20 10:08:58 +02001823/** Returns a suitable initializer for a PAKE cipher suite object of type
1824 * psa_pake_cipher_suite_t.
1825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
Neil Armstrongfb993022022-05-20 10:08:58 +02001827
Neil Armstrong0151c552022-05-20 10:13:53 +02001828/** Returns a suitable initializer for a PAKE operation object of type
1829 * psa_pake_operation_t.
1830 */
Antonio de Angelis4380a332024-02-02 14:21:24 +00001831#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1832#define PSA_PAKE_OPERATION_INIT { 0 }
1833#else
Przemek Stekiel656b2592023-03-22 13:15:33 +01001834#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001835 { 0 }, { { 0 } } }
Antonio de Angelis4380a332024-02-02 14:21:24 +00001836#endif
Neil Armstrong0151c552022-05-20 10:13:53 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838struct psa_pake_cipher_suite_s {
Janos Follath702cf092021-05-26 12:58:23 +01001839 psa_algorithm_t algorithm;
1840 psa_pake_primitive_type_t type;
1841 psa_pake_family_t family;
1842 uint16_t bits;
1843 psa_algorithm_t hash;
1844};
1845
1846static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001848{
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001850}
1851
1852static inline void psa_pake_cs_set_algorithm(
1853 psa_pake_cipher_suite_t *cipher_suite,
1854 psa_algorithm_t algorithm)
1855{
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001857 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001859 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 }
Janos Follath702cf092021-05-26 12:58:23 +01001861}
1862
1863static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001865{
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1867 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001868}
1869
1870static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 psa_pake_cipher_suite_t *cipher_suite,
1872 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01001873{
1874 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1875 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1876 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1877}
1878
Neil Armstrongff9cac72022-05-20 10:25:15 +02001879static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02001881{
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02001883}
1884
Neil Armstrongd5a48252022-05-20 10:26:36 +02001885static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02001887{
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02001889}
1890
Janos Follath702cf092021-05-26 12:58:23 +01001891static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001893{
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01001895}
1896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1898 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01001899{
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01001901 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001903 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
Janos Follath702cf092021-05-26 12:58:23 +01001905}
1906
Przemek Stekiel51eac532022-12-07 11:04:51 +01001907struct psa_crypto_driver_pake_inputs_s {
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 uint8_t *MBEDTLS_PRIVATE(password);
Przemek Stekiel152ae072022-11-17 13:24:36 +01001909 size_t MBEDTLS_PRIVATE(password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01001910 uint8_t *MBEDTLS_PRIVATE(user);
1911 size_t MBEDTLS_PRIVATE(user_len);
1912 uint8_t *MBEDTLS_PRIVATE(peer);
1913 size_t MBEDTLS_PRIVATE(peer_len);
Przemek Stekiel9dd24402023-01-26 15:06:09 +01001914 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001915 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
1916};
1917
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001918typedef enum psa_crypto_driver_pake_step {
Przemek Stekielb09c4872023-01-17 12:05:38 +01001919 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1920 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1921 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1922 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1923 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1924 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1925 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1926 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1927 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1928 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1929 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1930 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1931 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 +01001932} psa_crypto_driver_pake_step_t;
Przemek Stekiel57980032023-01-09 15:07:26 +01001933
David Horstmanne7f21e62023-05-12 18:17:21 +01001934typedef enum psa_jpake_round {
David Horstmann5da95602023-06-08 15:37:12 +01001935 PSA_JPAKE_FIRST = 0,
1936 PSA_JPAKE_SECOND = 1,
1937 PSA_JPAKE_FINISHED = 2
David Horstmanne7f21e62023-05-12 18:17:21 +01001938} psa_jpake_round_t;
1939
1940typedef enum psa_jpake_io_mode {
David Horstmann5da95602023-06-08 15:37:12 +01001941 PSA_JPAKE_INPUT = 0,
1942 PSA_JPAKE_OUTPUT = 1
David Horstmanne7f21e62023-05-12 18:17:21 +01001943} psa_jpake_io_mode_t;
Przemek Stekielb09c4872023-01-17 12:05:38 +01001944
Przemek Stekiele12ed362022-12-21 12:54:46 +01001945struct psa_jpake_computation_stage_s {
David Horstmanne7f21e62023-05-12 18:17:21 +01001946 /* The J-PAKE round we are currently on */
1947 psa_jpake_round_t MBEDTLS_PRIVATE(round);
1948 /* The 'mode' we are currently in (inputting or outputting) */
David Horstmann024e5c52023-06-14 15:48:21 +01001949 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
David Horstmann279d2272023-06-14 17:13:56 +01001950 /* The number of completed inputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001951 uint8_t MBEDTLS_PRIVATE(inputs);
David Horstmann279d2272023-06-14 17:13:56 +01001952 /* The number of completed outputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001953 uint8_t MBEDTLS_PRIVATE(outputs);
1954 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1955 psa_pake_step_t MBEDTLS_PRIVATE(step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001956};
1957
David Horstmann5dbe17d2023-06-27 10:30:28 +01001958#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1959 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1960#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1961 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
David Horstmanne7f21e62023-05-12 18:17:21 +01001962
Janos Follath702cf092021-05-26 12:58:23 +01001963struct psa_pake_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +00001964#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1965 mbedtls_psa_client_handle_t handle;
1966#else
Przemek Stekield91bcb72022-11-22 14:00:51 +01001967 /** Unique ID indicating which driver got assigned to do the
1968 * operation. Since driver contexts are driver-specific, swapping
1969 * drivers halfway through the operation is not supported.
1970 * ID values are auto-generated in psa_crypto_driver_wrappers.h
1971 * ID value zero means the context is not valid or not assigned to
1972 * any driver (i.e. none of the driver contexts are active). */
1973 unsigned int MBEDTLS_PRIVATE(id);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001974 /* Algorithm of the PAKE operation */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001975 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Przemek Stekiel656b2592023-03-22 13:15:33 +01001976 /* A primitive of type compatible with algorithm */
1977 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001978 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1979 * or computing. */
Przemek Stekiel51eac532022-12-07 11:04:51 +01001980 uint8_t MBEDTLS_PRIVATE(stage);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001981 /* Holds computation stage of the PAKE algorithms. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 union {
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001983 uint8_t MBEDTLS_PRIVATE(dummy);
Przemek Stekiel4aa99402023-02-27 13:00:57 +01001984#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekieldde6a912023-01-26 08:46:37 +01001985 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
Neil Armstrong35269d92022-05-25 11:26:31 +02001986#endif
Przemek Stekieldde6a912023-01-26 08:46:37 +01001987 } MBEDTLS_PRIVATE(computation_stage);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001988 union {
Przemek Stekiel51eac532022-12-07 11:04:51 +01001989 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
Przemek Stekielac067d72023-01-26 16:31:03 +01001990 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001991 } MBEDTLS_PRIVATE(data);
Antonio de Angelis4380a332024-02-02 14:21:24 +00001992#endif
Janos Follath702cf092021-05-26 12:58:23 +01001993};
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001996{
1997 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001999}
2000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01002002{
2003 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 return v;
Janos Follath702cf092021-05-26 12:58:23 +01002005}
2006
Gilles Peskinee59236f2018-01-27 23:32:46 +01002007#ifdef __cplusplus
2008}
2009#endif
2010
2011#endif /* PSA_CRYPTO_EXTRA_H */