blob: 18dccae0a03c9938eda59fa95a3fcd3b1060a2c4 [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.
Ryan Everett16abd592024-01-24 17:37:46 +0000201 * This function is not thread safe, it wipes every key slot regardless of
202 * state and reader count. It should only be called when no slot is in use.
Gilles Peskinee59236f2018-01-27 23:32:46 +0100203 *
204 * This is an Mbed TLS extension.
205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100207
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200208/** \brief Statistics about
209 * resource consumption related to the PSA keystore.
210 *
211 * \note The content of this structure is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200212 * of Mbed TLS and may change arbitrarily from version to version.
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200215 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200216 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200217 /** Number of slots containing key material for a key which is in
218 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200219 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200220 /** Number of slots containing a reference to a key in a
221 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200222 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200223 /** Number of slots which are occupied, but do not contain
224 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200225 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200226 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200227 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200228 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200229 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100230 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200231 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200232 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200233 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200234 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200235 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200236} mbedtls_psa_stats_t;
237
238/** \brief Get statistics about
239 * resource consumption related to the PSA keystore.
240 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200241 * \note When Mbed TLS is built as part of a service, with isolation
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200242 * between the application and the keystore, the service may or
243 * may not expose this function.
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200246
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200247/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100248 * \brief Inject an initial entropy seed for the random generator into
249 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100250 *
251 * This function injects data to be used as a seed for the random generator
252 * used by the PSA Crypto implementation. On devices that lack a trusted
253 * entropy source (preferably a hardware random number generator),
254 * the Mbed PSA Crypto implementation uses this value to seed its
255 * random generator.
256 *
257 * On devices without a trusted entropy source, this function must be
258 * called exactly once in the lifetime of the device. On devices with
259 * a trusted entropy source, calling this function is optional.
260 * In all cases, this function may only be called before calling any
261 * other function in the PSA Crypto API, including psa_crypto_init().
262 *
263 * When this function returns successfully, it populates a file in
264 * persistent storage. Once the file has been created, this function
265 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100266 *
267 * If any error occurs, this function does not change the system state.
268 * You can call this function again after correcting the reason for the
269 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200270 *
271 * \warning This function **can** fail! Callers MUST check the return status.
272 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100273 * \warning If you use this function, you should use it as part of a
274 * factory provisioning process. The value of the injected seed
275 * is critical to the security of the device. It must be
276 * *secret*, *unpredictable* and (statistically) *unique per device*.
277 * You should be generate it randomly using a cryptographically
278 * secure random generator seeded from trusted entropy sources.
279 * You should transmit it securely to the device and ensure
280 * that its value is not leaked or stored anywhere beyond the
281 * needs of transmitting it from the point of generation to
282 * the call of this function, and erase all copies of the value
283 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200284 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100285 * This is an Mbed TLS extension.
286 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200287 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100288 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
289 * Note that you must provide compatible implementations of
290 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200291 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200292 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200293 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200294 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200295 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000296 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
297 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
298 * in `library/entropy_poll.h` in the Mbed TLS source
299 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200300 * It must be less or equal to
301 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200302 *
303 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100304 * The seed value was injected successfully. The random generator
305 * of the PSA Crypto implementation is now ready for use.
306 * You may now call psa_crypto_init() and use the PSA Crypto
307 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100309 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200310 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100311 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200312 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100313 * The library has already been initialized. It is no longer
314 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200315 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100316psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200317 size_t seed_size);
318
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200319/** \addtogroup crypto_types
320 * @{
321 */
322
Gilles Peskinea1302192019-05-16 13:58:24 +0200323/** DSA public key.
324 *
325 * The import and export format is the
326 * representation of the public key `y = g^x mod p` as a big-endian byte
327 * string. The length of the byte string is the length of the base prime `p`
328 * in bytes.
329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200331
332/** DSA key pair (private and public key).
333 *
334 * The import and export format is the
335 * representation of the private key `x` as a big-endian byte string. The
336 * length of the byte string is the private key size in bytes (leading zeroes
337 * are not stripped).
338 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800339 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200340 * FIPS 186-4 §B.1.2: interpret the byte string as integer
341 * in big-endian order. Discard it if it is not in the range
342 * [0, *N* - 2] where *N* is the boundary of the private key domain
343 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
344 * or the order of the curve's base point for ECC).
345 * Add 1 to the resulting integer and use this as the private key *x*.
346 *
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200349
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100350/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200351#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200352 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200355/** DSA signature with hashing.
356 *
357 * This is the signature scheme defined by FIPS 186-4,
358 * with a random per-message secret number (*k*).
359 *
360 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
361 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
362 * This includes #PSA_ALG_ANY_HASH
363 * when specifying the algorithm in a usage policy.
364 *
365 * \return The corresponding DSA signature algorithm.
366 * \return Unspecified if \p hash_alg is not a supported
367 * hash algorithm.
368 */
369#define PSA_ALG_DSA(hash_alg) \
370 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100371#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100372#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200373/** Deterministic DSA signature with hashing.
374 *
375 * This is the deterministic variant defined by RFC 6979 of
376 * the signature scheme defined by FIPS 186-4.
377 *
378 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
379 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
380 * This includes #PSA_ALG_ANY_HASH
381 * when specifying the algorithm in a usage policy.
382 *
383 * \return The corresponding DSA signature algorithm.
384 * \return Unspecified if \p hash_alg is not a supported
385 * hash algorithm.
386 */
387#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
388 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
389#define PSA_ALG_IS_DSA(alg) \
390 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
391 PSA_ALG_DSA_BASE)
392#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
393 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
394#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
395 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
396#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
397 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
398
399
400/* We need to expand the sample definition of this macro from
401 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100402#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
403#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
404 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200405
406/**@}*/
407
Gilles Peskine24f10f82019-05-16 12:18:32 +0200408/** \addtogroup attributes
409 * @{
410 */
411
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200412/** Custom Diffie-Hellman group.
413 *
Paul Elliott75e27032020-06-03 15:17:39 +0100414 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
415 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200416 * from domain parameters set by psa_set_key_domain_parameters().
417 */
Paul Elliott75e27032020-06-03 15:17:39 +0100418#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200419
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100420/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100421#define PSA_PAKE_OPERATION_STAGE_SETUP 0
422#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
423#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200424
Gilles Peskine24f10f82019-05-16 12:18:32 +0200425/**
426 * \brief Set domain parameters for a key.
427 *
428 * Some key types require additional domain parameters in addition to
429 * the key type identifier and the key size. Use this function instead
430 * of psa_set_key_type() when you need to specify domain parameters.
431 *
432 * The format for the required domain parameters varies based on the key type.
Gilles Peskine5ad95392023-12-19 12:22:46 +0100433 * Mbed TLS supports the following key type with domain parameters:
Gilles Peskine24f10f82019-05-16 12:18:32 +0200434 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200435 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200436 * the domain parameter data consists of the public exponent,
437 * represented as a big-endian integer with no leading zeros.
438 * This information is used when generating an RSA key pair.
439 * When importing a key, the public exponent is read from the imported
440 * key data and the exponent recorded in the attribute structure is ignored.
441 * As an exception, the public exponent 65537 is represented by an empty
442 * byte string.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200443 *
444 * \note This function may allocate memory or other resources.
445 * Once you have called this function on an attribute structure,
446 * you must call psa_reset_key_attributes() to free these resources.
447 *
448 * \note This is an experimental extension to the interface. It may change
449 * in future versions of the library.
450 *
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100451 * \note Due to an implementation limitation, domain parameters are ignored
452 * for keys that are managed by a driver.
453 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200454 * \param[in,out] attributes Attribute structure where the specified domain
455 * parameters will be stored.
456 * If this function fails, the content of
457 * \p attributes is not modified.
458 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
459 * \param[in] data Buffer containing the key domain parameters.
460 * The content of this buffer is interpreted
461 * according to \p type as described above.
462 * \param data_length Size of the \p data buffer in bytes.
463 *
Gilles Peskineed733552023-02-14 19:21:09 +0100464 * \retval #PSA_SUCCESS \emptydescription
465 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
466 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
467 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200468 */
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100469#if !defined(PSA_SET_KEY_DOMAIN_PARAMETERS)
470#define PSA_SET_KEY_DOMAIN_PARAMETERS
Gilles Peskine24f10f82019-05-16 12:18:32 +0200471psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
472 psa_key_type_t type,
473 const uint8_t *data,
474 size_t data_length);
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100475#endif /* PSA_SET_KEY_DOMAIN_PARAMETERS */
Gilles Peskine24f10f82019-05-16 12:18:32 +0200476
477/**
478 * \brief Get domain parameters for a key.
479 *
480 * Get the domain parameters for a key with this function, if any. The format
481 * of the domain parameters written to \p data is specified in the
482 * documentation for psa_set_key_domain_parameters().
483 *
484 * \note This is an experimental extension to the interface. It may change
485 * in future versions of the library.
486 *
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100487 * \note Due to an implementation limitation, domain parameters are not
488 * supported with keys that are managed by a driver.
489 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200490 * \param[in] attributes The key attribute structure to query.
491 * \param[out] data On success, the key domain parameters.
492 * \param data_size Size of the \p data buffer in bytes.
493 * The buffer is guaranteed to be large
494 * enough if its size in bytes is at least
495 * the value given by
496 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
497 * \param[out] data_length On success, the number of bytes
498 * that make up the key domain parameters data.
499 *
Gilles Peskineed733552023-02-14 19:21:09 +0100500 * \retval #PSA_SUCCESS \emptydescription
501 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100502 * \retval #PSA_ERROR_NOT_SUPPORTED
503 * The key is managed by a driver.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200504 */
505psa_status_t psa_get_key_domain_parameters(
506 const psa_key_attributes_t *attributes,
507 uint8_t *data,
508 size_t data_size,
509 size_t *data_length);
510
511/** Safe output buffer size for psa_get_key_domain_parameters().
512 *
513 * This macro returns a compile-time constant if its arguments are
514 * compile-time constants.
515 *
516 * \warning This function may call its arguments multiple times or
517 * zero times, so you should not pass arguments that contain
518 * side effects.
519 *
520 * \note This is an experimental extension to the interface. It may change
521 * in future versions of the library.
522 *
523 * \param key_type A supported key type.
524 * \param key_bits The size of the key in bits.
525 *
526 * \return If the parameters are valid and supported, return
527 * a buffer size in bytes that guarantees that
528 * psa_get_key_domain_parameters() will not fail with
529 * #PSA_ERROR_BUFFER_TOO_SMALL.
530 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200531 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200532 * sensible size or 0.
533 * If the parameters are not valid, the
534 * return value is unspecified.
535 */
536#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
537 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
538 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
539 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
540 0)
541#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
542 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
543#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
544 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
545
546/**@}*/
547
Gilles Peskine5055b232019-12-12 17:49:31 +0100548
Gilles Peskineb8af2282020-11-13 18:00:34 +0100549/** \defgroup psa_external_rng External random generator
550 * @{
551 */
552
553#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
554/** External random generator function, implemented by the platform.
555 *
556 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
557 * this function replaces Mbed TLS's entropy and DRBG modules for all
558 * random generation triggered via PSA crypto interfaces.
559 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100560 * \note This random generator must deliver random numbers with cryptographic
561 * quality and high performance. It must supply unpredictable numbers
562 * with a uniform distribution. The implementation of this function
563 * is responsible for ensuring that the random generator is seeded
564 * with sufficient entropy. If you have a hardware TRNG which is slow
565 * or delivers non-uniform output, declare it as an entropy source
566 * with mbedtls_entropy_add_source() instead of enabling this option.
567 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100568 * \param[in,out] context Pointer to the random generator context.
569 * This is all-bits-zero on the first call
570 * and preserved between successive calls.
571 * \param[out] output Output buffer. On success, this buffer
572 * contains random data with a uniform
573 * distribution.
574 * \param output_size The size of the \p output buffer in bytes.
575 * \param[out] output_length On success, set this value to \p output_size.
576 *
577 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100578 * Success. The output buffer contains \p output_size bytes of
579 * cryptographic-quality random data, and \c *output_length is
580 * set to \p output_size.
581 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
582 * The random generator requires extra entropy and there is no
583 * way to obtain entropy under current environment conditions.
584 * This error should not happen under normal circumstances since
585 * this function is responsible for obtaining as much entropy as
586 * it needs. However implementations of this function may return
587 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
588 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100589 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100590 * A failure of the random generator hardware that isn't covered
591 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100592 */
593psa_status_t mbedtls_psa_external_get_random(
594 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100596#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
597
598/**@}*/
599
Steven Cooreman6801f082021-02-19 17:21:22 +0100600/** \defgroup psa_builtin_keys Built-in keys
601 * @{
602 */
603
604/** The minimum value for a key identifier that is built into the
605 * implementation.
606 *
607 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
608 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
609 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
610 * with any other set of implementation-chosen key identifiers.
611 *
612 * This value is part of the library's ABI since changing it would invalidate
613 * the values of built-in key identifiers in applications.
614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100616
617/** The maximum value for a key identifier that is built into the
618 * implementation.
619 *
620 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
621 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100623
624/** A slot number identifying a key in a driver.
625 *
626 * Values of this type are used to identify built-in keys.
627 */
628typedef uint64_t psa_drv_slot_number_t;
629
630#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
631/** Test whether a key identifier belongs to the builtin key range.
632 *
633 * \param key_id Key identifier to test.
634 *
635 * \retval 1
636 * The key identifier is a builtin key identifier.
637 * \retval 0
638 * The key identifier is not a builtin key identifier.
639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
643 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100644}
645
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200646/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100647 *
648 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100649 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100650 * as part of a platform's system image.
651 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100652 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100653 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
654 *
655 * In a multi-application configuration
656 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
657 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
658 * is allowed to use the given key.
659 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100660 * \param key_id The key ID for which to retrieve the
661 * location and slot attributes.
662 * \param[out] lifetime On success, the lifetime associated with the key
663 * corresponding to \p key_id. Lifetime is a
664 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200665 * and with what persistence level the key is
666 * intended to be used. If the platform
667 * implementation does not contain specific
668 * information about the intended key persistence
669 * level, the persistence level may be reported as
670 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100671 * \param[out] slot_number On success, the slot number known to the driver
672 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200673 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100674 * requested built-in key.
675 *
676 * \retval #PSA_SUCCESS
677 * The requested key identifier designates a built-in key.
678 * In a multi-application configuration, the requested owner
679 * is allowed to access it.
680 * \retval #PSA_ERROR_DOES_NOT_EXIST
681 * The requested key identifier is not a built-in key which is known
682 * to this function. If a key exists in the key storage with this
683 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100684 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100685 * Any other error is propagated to the function that requested the key.
686 * Common errors include:
687 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
688 * is not allowed to access it.
689 */
690psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100691 mbedtls_svc_key_id_t key_id,
692 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100694#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
695
696/** @} */
697
Janos Follath702cf092021-05-26 12:58:23 +0100698/** \addtogroup crypto_types
699 * @{
700 */
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100703
704/** Whether the specified algorithm is a password-authenticated key exchange.
705 *
706 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
707 *
708 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
709 * algorithm, 0 otherwise.
710 * This macro may return either 0 or 1 if \p alg is not a supported
711 * algorithm identifier.
712 */
713#define PSA_ALG_IS_PAKE(alg) \
714 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
715
716/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
717 *
718 * This is J-PAKE as defined by RFC 8236, instantiated with the following
719 * parameters:
720 *
721 * - The group can be either an elliptic curve or defined over a finite field.
722 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
723 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100724 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100725 *
Janos Follath46c02372021-06-08 15:22:51 +0100726 * To select these parameters and set up the cipher suite, call these functions
727 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100728 *
729 * \code
730 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
731 * psa_pake_cs_set_primitive(cipher_suite,
732 * PSA_PAKE_PRIMITIVE(type, family, bits));
733 * psa_pake_cs_set_hash(cipher_suite, hash);
734 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100735 *
736 * For more information on how to set a specific curve or field, refer to the
737 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
738 *
739 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100740 *
741 * \code
742 * psa_pake_setup(operation, cipher_suite);
743 * psa_pake_set_user(operation, ...);
744 * psa_pake_set_peer(operation, ...);
745 * psa_pake_set_password_key(operation, ...);
746 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100747 *
Neil Armstrong16145372022-05-20 10:42:36 +0200748 * The password is provided as a key. This can be the password text itself,
749 * in an agreed character encoding, or some value derived from the password
750 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100751 *
Neil Armstrong16145372022-05-20 10:42:36 +0200752 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100753 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
754 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
755 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200756 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100757 * of the reduction is 0.)
758 *
759 * The key exchange flow for J-PAKE is as follows:
760 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100761 * \code
762 * // Get g1
763 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
764 * // Get the ZKP public key for x1
765 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
766 * // Get the ZKP proof for x1
767 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
768 * // Get g2
769 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
770 * // Get the ZKP public key for x2
771 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
772 * // Get the ZKP proof for x2
773 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
774 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100775 * -# To provide the first round data received from the peer to the operation,
776 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100777 * \code
778 * // Set g3
779 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
780 * // Set the ZKP public key for x3
781 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
782 * // Set the ZKP proof for x3
783 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
784 * // Set g4
785 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
786 * // Set the ZKP public key for x4
787 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
788 * // Set the ZKP proof for x4
789 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
790 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100791 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100792 * \code
793 * // Get A
794 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
795 * // Get ZKP public key for x2*s
796 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
797 * // Get ZKP proof for x2*s
798 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
799 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100800 * -# To provide the second round data received from the peer to the operation,
801 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100802 * \code
803 * // Set B
804 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
805 * // Set ZKP public key for x4*s
806 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
807 * // Set ZKP proof for x4*s
808 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
809 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100810 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100811 * \code
812 * // Get Ka=Kb=K
813 * psa_pake_get_implicit_key()
814 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100815 *
816 * For more information consult the documentation of the individual
817 * \c PSA_PAKE_STEP_XXX constants.
818 *
819 * At this point there is a cryptographic guarantee that only the authenticated
820 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100821 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100822 *
823 * That is, the authentication is only implicit (the peer is not authenticated
824 * at this point, and no action should be taken that assume that they are - like
825 * for example accessing restricted files).
826 *
827 * To make the authentication explicit there are various methods, see Section 5
828 * of RFC 8236 for two examples.
829 *
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100832
833/** @} */
834
835/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100836 *
837 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
838 * the official PSA Crypto API yet.
839 *
840 * \note The content of this section is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200841 * of Mbed TLS and may change arbitrarily from version to version.
Janos Follath7d69b3a2021-05-26 13:10:56 +0100842 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
843 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100844 * @{
845 */
846
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200847/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100848 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200849 * Encodes the application's role in the algorithm is being executed. For more
850 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
851 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100852 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200853typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100854
855/** Encoding of input and output indicators for PAKE.
856 *
857 * Some PAKE algorithms need to exchange more data than just a single key share.
858 * This type is for encoding additional input and output data for such
859 * algorithms.
860 */
861typedef uint8_t psa_pake_step_t;
862
863/** Encoding of the type of the PAKE's primitive.
864 *
865 * Values defined by this standard will never be in the range 0x80-0xff.
866 * Vendors who define additional types must use an encoding in this range.
867 *
868 * For more information see the documentation of individual
869 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
870 */
871typedef uint8_t psa_pake_primitive_type_t;
872
873/** \brief Encoding of the family of the primitive associated with the PAKE.
874 *
875 * For more information see the documentation of individual
876 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
877 */
878typedef uint8_t psa_pake_family_t;
879
880/** \brief Encoding of the primitive associated with the PAKE.
881 *
882 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
883 */
884typedef uint32_t psa_pake_primitive_t;
885
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200886/** A value to indicate no role in a PAKE algorithm.
887 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
888 * algorithms which do not assign roles.
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200891
Janos Follath702cf092021-05-26 12:58:23 +0100892/** The first peer in a balanced PAKE.
893 *
894 * Although balanced PAKE algorithms are symmetric, some of them needs an
895 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200896 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100897 * accepted.
898 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100900
901/** The second peer in a balanced PAKE.
902 *
903 * Although balanced PAKE algorithms are symmetric, some of them needs an
904 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200905 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100906 * accepted.
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100909
910/** The client in an augmented PAKE.
911 *
912 * Augmented PAKE algorithms need to differentiate between client and server.
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +0100915
916/** The server in an augmented PAKE.
917 *
918 * Augmented PAKE algorithms need to differentiate between client and server.
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +0100921
922/** The PAKE primitive type indicating the use of elliptic curves.
923 *
924 * The values of the \c family and \c bits fields of the cipher suite identify a
925 * specific elliptic curve, using the same mapping that is used for ECC
926 * (::psa_ecc_family_t) keys.
927 *
928 * (Here \c family means the value returned by psa_pake_cs_get_family() and
929 * \c bits means the value returned by psa_pake_cs_get_bits().)
930 *
931 * Input and output during the operation can involve group elements and scalar
932 * values:
933 * -# The format for group elements is the same as for public keys on the
934 * specific curve would be. For more information, consult the documentation of
935 * psa_export_public_key().
936 * -# The format for scalars is the same as for private keys on the specific
937 * curve would be. For more information, consult the documentation of
938 * psa_export_key().
939 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100941
942/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
943 *
944 * The values of the \c family and \c bits fields of the cipher suite identify
945 * a specific Diffie-Hellman group, using the same mapping that is used for
946 * Diffie-Hellman (::psa_dh_family_t) keys.
947 *
948 * (Here \c family means the value returned by psa_pake_cs_get_family() and
949 * \c bits means the value returned by psa_pake_cs_get_bits().)
950 *
951 * Input and output during the operation can involve group elements and scalar
952 * values:
953 * -# The format for group elements is the same as for public keys on the
954 * specific group would be. For more information, consult the documentation of
955 * psa_export_public_key().
956 * -# The format for scalars is the same as for private keys on the specific
957 * group would be. For more information, consult the documentation of
958 * psa_export_key().
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100961
962/** Construct a PAKE primitive from type, family and bit-size.
963 *
964 * \param pake_type The type of the primitive
965 * (value of type ::psa_pake_primitive_type_t).
966 * \param pake_family The family of the primitive
967 * (the type and interpretation of this parameter depends
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500968 * on \p pake_type, for more information consult the
Janos Follath702cf092021-05-26 12:58:23 +0100969 * documentation of individual ::psa_pake_primitive_type_t
970 * constants).
971 * \param pake_bits The bit-size of the primitive
972 * (Value of type \c size_t. The interpretation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500973 * of this parameter depends on \p pake_family, for more
Janos Follath702cf092021-05-26 12:58:23 +0100974 * information consult the documentation of individual
975 * ::psa_pake_primitive_type_t constants).
976 *
977 * \return The constructed primitive value of type ::psa_pake_primitive_t.
978 * Return 0 if the requested primitive can't be encoded as
979 * ::psa_pake_primitive_t.
980 */
981#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
982 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
983 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +0100985
986/** The key share being sent to or received from the peer.
987 *
988 * The format for both input and output at this step is the same as for public
989 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
990 * be.
991 *
992 * For more information on the format, consult the documentation of
993 * psa_export_public_key().
994 *
995 * For information regarding how the group is determined, consult the
996 * documentation #PSA_PAKE_PRIMITIVE.
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100999
1000/** A Schnorr NIZKP public key.
1001 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001002 * This is the ephemeral public key in the Schnorr Non-Interactive
1003 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1004 *
Janos Follath702cf092021-05-26 12:58:23 +01001005 * The format for both input and output at this step is the same as for public
1006 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1007 * be.
1008 *
1009 * For more information on the format, consult the documentation of
1010 * psa_export_public_key().
1011 *
1012 * For information regarding how the group is determined, consult the
1013 * documentation #PSA_PAKE_PRIMITIVE.
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001016
1017/** A Schnorr NIZKP proof.
1018 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001019 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1020 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +01001021 *
Janos Follath1f013182021-06-08 15:30:48 +01001022 * Both for input and output, the value at this step is an integer less than
1023 * the order of the group selected in the cipher suite. The format depends on
1024 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +01001025 *
Janos Follath1f013182021-06-08 15:30:48 +01001026 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +01001027 * - For everything else the encoding is big endian (see Section 2.3.8 of
1028 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +01001029 *
Janos Follath1f013182021-06-08 15:30:48 +01001030 * In both cases leading zeroes are allowed as long as the length in bytes does
1031 * not exceed the byte length of the group order.
1032 *
Janos Follath702cf092021-05-26 12:58:23 +01001033 * For information regarding how the group is determined, consult the
1034 * documentation #PSA_PAKE_PRIMITIVE.
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +01001037
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001038/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +01001039 *
1040 * This is an implementation-defined \c struct. Applications should not
1041 * make any assumptions about the content of this structure.
1042 * Implementation details can change in future versions without notice.
1043 */
1044typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1045
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001046/** Return an initial value for a PAKE cipher suite object.
1047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001049
Janos Follath702cf092021-05-26 12:58:23 +01001050/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1051 *
Janos Follath702cf092021-05-26 12:58:23 +01001052 * \param[in] cipher_suite The cipher suite structure to query.
1053 *
1054 * \return The PAKE algorithm stored in the cipher suite structure.
1055 */
1056static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001058
1059/** Declare the PAKE algorithm for the cipher suite.
1060 *
1061 * This function overwrites any PAKE algorithm
1062 * previously set in \p cipher_suite.
1063 *
Janos Follath702cf092021-05-26 12:58:23 +01001064 * \param[out] cipher_suite The cipher suite structure to write to.
1065 * \param algorithm The PAKE algorithm to write.
1066 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1067 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1068 * If this is 0, the PAKE algorithm in
1069 * \p cipher_suite becomes unspecified.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1072 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +01001073
1074/** Retrieve the primitive from a PAKE cipher suite.
1075 *
Janos Follath702cf092021-05-26 12:58:23 +01001076 * \param[in] cipher_suite The cipher suite structure to query.
1077 *
1078 * \return The primitive stored in the cipher suite structure.
1079 */
1080static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001082
1083/** Declare the primitive for a PAKE cipher suite.
1084 *
1085 * This function overwrites any primitive previously set in \p cipher_suite.
1086 *
Janos Follath702cf092021-05-26 12:58:23 +01001087 * \param[out] cipher_suite The cipher suite structure to write to.
1088 * \param primitive The primitive to write. If this is 0, the
1089 * primitive type in \p cipher_suite becomes
1090 * unspecified.
1091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1093 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +01001094
Neil Armstrongff9cac72022-05-20 10:25:15 +02001095/** Retrieve the PAKE family from a PAKE cipher suite.
1096 *
Neil Armstrongff9cac72022-05-20 10:25:15 +02001097 * \param[in] cipher_suite The cipher suite structure to query.
1098 *
1099 * \return The PAKE family stored in the cipher suite structure.
1100 */
1101static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +02001103
Neil Armstrongd5a48252022-05-20 10:26:36 +02001104/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1105 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001106 * \param[in] cipher_suite The cipher suite structure to query.
1107 *
1108 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1109 */
1110static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001112
Janos Follath702cf092021-05-26 12:58:23 +01001113/** Retrieve the hash algorithm from a PAKE cipher suite.
1114 *
Janos Follath702cf092021-05-26 12:58:23 +01001115 * \param[in] cipher_suite The cipher suite structure to query.
1116 *
1117 * \return The hash algorithm stored in the cipher suite structure. The return
1118 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1119 * the hash algorithm is not set.
1120 */
1121static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001123
1124/** Declare the hash algorithm for a PAKE cipher suite.
1125 *
1126 * This function overwrites any hash algorithm
1127 * previously set in \p cipher_suite.
1128 *
Janos Follath702cf092021-05-26 12:58:23 +01001129 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1130 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1131 * for more information.
1132 *
1133 * \param[out] cipher_suite The cipher suite structure to write to.
1134 * \param hash The hash involved in the cipher suite.
1135 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1136 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1137 * If this is 0, the hash algorithm in
1138 * \p cipher_suite becomes unspecified.
1139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1141 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001142
1143/** The type of the state data structure for PAKE operations.
1144 *
1145 * Before calling any function on a PAKE operation object, the application
1146 * must initialize it by any of the following means:
1147 * - Set the structure to all-bits-zero, for example:
1148 * \code
1149 * psa_pake_operation_t operation;
1150 * memset(&operation, 0, sizeof(operation));
1151 * \endcode
1152 * - Initialize the structure to logical zero values, for example:
1153 * \code
1154 * psa_pake_operation_t operation = {0};
1155 * \endcode
1156 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1157 * for example:
1158 * \code
1159 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1160 * \endcode
1161 * - Assign the result of the function psa_pake_operation_init()
1162 * to the structure, for example:
1163 * \code
1164 * psa_pake_operation_t operation;
1165 * operation = psa_pake_operation_init();
1166 * \endcode
1167 *
1168 * This is an implementation-defined \c struct. Applications should not
1169 * make any assumptions about the content of this structure.
1170 * Implementation details can change in future versions without notice. */
1171typedef struct psa_pake_operation_s psa_pake_operation_t;
1172
Przemek Stekiel51eac532022-12-07 11:04:51 +01001173/** The type of input values for PAKE operations. */
1174typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1175
Przemek Stekielb09c4872023-01-17 12:05:38 +01001176/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001177typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1178
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001179/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001182
Przemek Stekielc0e62502023-03-14 11:49:36 +01001183/** Get the length of the password in bytes from given inputs.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001184 *
1185 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001186 * \param[out] password_len Password length.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001187 *
1188 * \retval #PSA_SUCCESS
1189 * Success.
1190 * \retval #PSA_ERROR_BAD_STATE
1191 * Password hasn't been set yet.
1192 */
1193psa_status_t psa_crypto_driver_pake_get_password_len(
1194 const psa_crypto_driver_pake_inputs_t *inputs,
1195 size_t *password_len);
1196
1197/** Get the password from given inputs.
1198 *
1199 * \param[in] inputs Operation inputs.
1200 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001201 * \param buffer_size Size of the return buffer in bytes.
1202 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001203 *
1204 * \retval #PSA_SUCCESS
1205 * Success.
1206 * \retval #PSA_ERROR_BAD_STATE
1207 * Password hasn't been set yet.
1208 */
1209psa_status_t psa_crypto_driver_pake_get_password(
1210 const psa_crypto_driver_pake_inputs_t *inputs,
1211 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1212
Przemek Stekielc0e62502023-03-14 11:49:36 +01001213/** Get the length of the user id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001214 *
1215 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001216 * \param[out] user_len User id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001217 *
1218 * \retval #PSA_SUCCESS
1219 * Success.
1220 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001221 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001222 */
1223psa_status_t psa_crypto_driver_pake_get_user_len(
1224 const psa_crypto_driver_pake_inputs_t *inputs,
1225 size_t *user_len);
1226
Przemek Stekielc0e62502023-03-14 11:49:36 +01001227/** Get the length of the peer id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001228 *
1229 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001230 * \param[out] peer_len Peer id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001231 *
1232 * \retval #PSA_SUCCESS
1233 * Success.
1234 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001235 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001236 */
1237psa_status_t psa_crypto_driver_pake_get_peer_len(
1238 const psa_crypto_driver_pake_inputs_t *inputs,
1239 size_t *peer_len);
1240
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001241/** Get the user id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001242 *
1243 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001244 * \param[out] user_id User id.
1245 * \param user_id_size Size of \p user_id in bytes.
1246 * \param[out] user_id_len Size of the user id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001247 *
1248 * \retval #PSA_SUCCESS
1249 * Success.
1250 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001251 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001252 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001253 * The size of the \p user_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001254 */
1255psa_status_t psa_crypto_driver_pake_get_user(
1256 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001257 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001258
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001259/** Get the peer id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001260 *
1261 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001262 * \param[out] peer_id Peer id.
1263 * \param peer_id_size Size of \p peer_id in bytes.
1264 * \param[out] peer_id_length Size of the peer id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001265 *
1266 * \retval #PSA_SUCCESS
1267 * Success.
1268 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001269 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001270 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001271 * The size of the \p peer_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001272 */
1273psa_status_t psa_crypto_driver_pake_get_peer(
1274 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001275 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001276
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001277/** Get the cipher suite from given inputs.
1278 *
1279 * \param[in] inputs Operation inputs.
1280 * \param[out] cipher_suite Return buffer for role.
1281 *
1282 * \retval #PSA_SUCCESS
1283 * Success.
1284 * \retval #PSA_ERROR_BAD_STATE
1285 * Cipher_suite hasn't been set yet.
1286 */
1287psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1288 const psa_crypto_driver_pake_inputs_t *inputs,
1289 psa_pake_cipher_suite_t *cipher_suite);
1290
Janos Follath702cf092021-05-26 12:58:23 +01001291/** Set the session information for a password-authenticated key exchange.
1292 *
1293 * The sequence of operations to set up a password-authenticated key exchange
1294 * is as follows:
1295 * -# Allocate an operation object which will be passed to all the functions
1296 * listed here.
1297 * -# Initialize the operation object with one of the methods described in the
1298 * documentation for #psa_pake_operation_t, e.g.
1299 * #PSA_PAKE_OPERATION_INIT.
1300 * -# Call psa_pake_setup() to specify the cipher suite.
1301 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1302 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1303 * to be called depends on the algorithm in use.
1304 *
1305 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1306 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1307 * for more information.
1308 *
1309 * A typical sequence of calls to perform a password-authenticated key
1310 * exchange:
1311 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1312 * key share that needs to be sent to the peer.
1313 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1314 * the key share that was received from the peer.
1315 * -# Depending on the algorithm additional calls to psa_pake_output() and
1316 * psa_pake_input() might be necessary.
1317 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1318 *
1319 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1320 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1321 * for more information.
1322 *
1323 * If an error occurs at any step after a call to psa_pake_setup(),
1324 * the operation will need to be reset by a call to psa_pake_abort(). The
1325 * application may call psa_pake_abort() at any time after the operation
1326 * has been initialized.
1327 *
1328 * After a successful call to psa_pake_setup(), the application must
1329 * eventually terminate the operation. The following events terminate an
1330 * operation:
1331 * - A call to psa_pake_abort().
1332 * - A successful call to psa_pake_get_implicit_key().
1333 *
1334 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001335 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001336 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001337 * characterizes a PAKE algorithm and determines
1338 * the algorithm as well.)
1339 *
1340 * \retval #PSA_SUCCESS
1341 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001342 * \retval #PSA_ERROR_INVALID_ARGUMENT
1343 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1344 * PAKE primitive in \p cipher_suite is not compatible with the
1345 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1346 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001347 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001348 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1349 * or the PAKE primitive in \p cipher_suite is not supported or not
1350 * compatible with the PAKE algorithm, or the hash algorithm in
1351 * \p cipher_suite is not supported or not compatible with the PAKE
1352 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001353 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1354 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001355 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001356 * The operation state is not valid, or
1357 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001358 * It is implementation-dependent whether a failure to initialize
1359 * results in this error code.
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1362 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001363
1364/** Set the password for a password-authenticated key exchange from key ID.
1365 *
1366 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001367 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001368 *
1369 * \param[in,out] operation The operation object to set the password for. It
1370 * must have been set up by psa_pake_setup() and
1371 * not yet in use (neither psa_pake_output() nor
1372 * psa_pake_input() has been called yet). It must
1373 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001374 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001375 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001376 * \param password Identifier of the key holding the password or a
1377 * value derived from the password (eg. by a
1378 * memory-hard function). It must remain valid
1379 * until the operation terminates. It must be of
1380 * type #PSA_KEY_TYPE_PASSWORD or
1381 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1382 * the usage #PSA_KEY_USAGE_DERIVE.
1383 *
1384 * \retval #PSA_SUCCESS
1385 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001386 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001387 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001388 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001389 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1390 * permit the \p operation's algorithm.
1391 * \retval #PSA_ERROR_INVALID_ARGUMENT
1392 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1393 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1394 * the \p operation's cipher suite.
1395 * \retval #PSA_ERROR_NOT_SUPPORTED
1396 * The key type or key size of \p password is not supported with the
1397 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1399 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1400 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1401 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1402 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001403 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001404 * The operation state is not valid (it must have been set up.), or
1405 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001406 * It is implementation-dependent whether a failure to initialize
1407 * results in this error code.
1408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1410 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001411
Janos Follath702cf092021-05-26 12:58:23 +01001412/** Set the user ID for a password-authenticated key exchange.
1413 *
1414 * Call this function to set the user ID. For PAKE algorithms that associate a
1415 * user identifier with each side of the session you need to call
1416 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1417 * user identifier with the session, call psa_pake_set_user() only.
1418 *
1419 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1420 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1421 * for more information.
1422 *
1423 * \param[in,out] operation The operation object to set the user ID for. It
1424 * must have been set up by psa_pake_setup() and
1425 * not yet in use (neither psa_pake_output() nor
1426 * psa_pake_input() has been called yet). It must
1427 * be on operation for which the user ID hasn't
1428 * been set (psa_pake_set_user() hasn't been
1429 * called yet).
1430 * \param[in] user_id The user ID to authenticate with.
1431 * \param user_id_len Size of the \p user_id buffer in bytes.
1432 *
1433 * \retval #PSA_SUCCESS
1434 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001435 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001436 * \p user_id is not valid for the \p operation's algorithm and cipher
1437 * suite.
1438 * \retval #PSA_ERROR_NOT_SUPPORTED
1439 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1442 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001443 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001444 * The operation state is not valid, or
1445 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001446 * It is implementation-dependent whether a failure to initialize
1447 * results in this error code.
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1450 const uint8_t *user_id,
1451 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001452
1453/** Set the peer ID for a password-authenticated key exchange.
1454 *
1455 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1456 * that associate a user identifier with each side of the session. For PAKE
1457 * algorithms that associate a single user identifier with the session, call
1458 * psa_pake_set_user() only.
1459 *
1460 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1461 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1462 * for more information.
1463 *
1464 * \param[in,out] operation The operation object to set the peer ID for. It
1465 * must have been set up by psa_pake_setup() and
1466 * not yet in use (neither psa_pake_output() nor
1467 * psa_pake_input() has been called yet). It must
1468 * be on operation for which the peer ID hasn't
1469 * been set (psa_pake_set_peer() hasn't been
1470 * called yet).
1471 * \param[in] peer_id The peer's ID to authenticate.
1472 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001476 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001477 * \p peer_id is not valid for the \p operation's algorithm and cipher
Neil Armstrong16ff7882022-05-20 11:04:20 +02001478 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001479 * \retval #PSA_ERROR_NOT_SUPPORTED
1480 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1483 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001484 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001485 * Calling psa_pake_set_peer() is invalid with the \p operation's
1486 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001487 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001488 * It is implementation-dependent whether a failure to initialize
1489 * results in this error code.
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1492 const uint8_t *peer_id,
1493 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001494
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001495/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001496 *
1497 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001498 * It is optional to call this function for PAKEs that don't require a role
1499 * to be specified. For such PAKEs the application role parameter is ignored,
1500 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001501 *
1502 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1503 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1504 * for more information.
1505 *
Neil Armstrongef157512022-05-25 11:49:45 +02001506 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001507 * application's role for. It must have been set up
1508 * by psa_pake_setup() and not yet in use (neither
1509 * psa_pake_output() nor psa_pake_input() has been
1510 * called yet). It must be on operation for which
1511 * the application's role hasn't been specified
1512 * (psa_pake_set_role() hasn't been called yet).
1513 * \param role A value of type ::psa_pake_role_t indicating the
1514 * application's role in the PAKE the algorithm
1515 * that is being set up. For more information see
1516 * the documentation of \c PSA_PAKE_ROLE_XXX
1517 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001518 *
1519 * \retval #PSA_SUCCESS
1520 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001521 * \retval #PSA_ERROR_INVALID_ARGUMENT
1522 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001523 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001524 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1526 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001527 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001528 * The operation state is not valid, or
1529 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001530 * It is implementation-dependent whether a failure to initialize
1531 * results in this error code.
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1534 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001535
1536/** Get output for a step of a password-authenticated key exchange.
1537 *
1538 * Depending on the algorithm being executed, you might need to call this
1539 * function several times or you might not need to call this at all.
1540 *
1541 * The exact sequence of calls to perform a password-authenticated key
1542 * exchange depends on the algorithm in use. Refer to the documentation of
1543 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1544 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1545 * information.
1546 *
1547 * If this function returns an error status, the operation enters an error
1548 * state and must be aborted by calling psa_pake_abort().
1549 *
1550 * \param[in,out] operation Active PAKE operation.
1551 * \param step The step of the algorithm for which the output is
1552 * requested.
1553 * \param[out] output Buffer where the output is to be written in the
1554 * format appropriate for this \p step. Refer to
1555 * the documentation of the individual
1556 * \c PSA_PAKE_STEP_XXX constants for more
1557 * information.
1558 * \param output_size Size of the \p output buffer in bytes. This must
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001559 * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1560 * primitive, \p output_step) where \c alg and
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001561 * \p primitive are the PAKE algorithm and primitive
1562 * in the operation's cipher suite, and \p step is
1563 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001564 *
1565 * \param[out] output_length On success, the number of bytes of the returned
1566 * output.
1567 *
1568 * \retval #PSA_SUCCESS
1569 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001570 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1571 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001572 * \retval #PSA_ERROR_INVALID_ARGUMENT
1573 * \p step is not compatible with the operation's algorithm.
1574 * \retval #PSA_ERROR_NOT_SUPPORTED
1575 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001576 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1577 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1579 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1580 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1581 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1582 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001583 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001584 * The operation state is not valid (it must be active, and fully set
1585 * up, and this call must conform to the algorithm's requirements
1586 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001587 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001588 * It is implementation-dependent whether a failure to initialize
1589 * results in this error code.
1590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1592 psa_pake_step_t step,
1593 uint8_t *output,
1594 size_t output_size,
1595 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001596
1597/** Provide input for a step of a password-authenticated key exchange.
1598 *
1599 * Depending on the algorithm being executed, you might need to call this
1600 * function several times or you might not need to call this at all.
1601 *
1602 * The exact sequence of calls to perform a password-authenticated key
1603 * exchange depends on the algorithm in use. Refer to the documentation of
1604 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1605 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1606 * information.
1607 *
1608 * If this function returns an error status, the operation enters an error
1609 * state and must be aborted by calling psa_pake_abort().
1610 *
1611 * \param[in,out] operation Active PAKE operation.
1612 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001613 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001614 * appropriate for this \p step. Refer to the
1615 * documentation of the individual
1616 * \c PSA_PAKE_STEP_XXX constants for more
1617 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001618 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001619 *
1620 * \retval #PSA_SUCCESS
1621 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001622 * \retval #PSA_ERROR_INVALID_SIGNATURE
1623 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001624 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001625 * \p input_length is not compatible with the \p operation’s algorithm,
1626 * or the \p input is not valid for the \p operation's algorithm,
1627 * cipher suite or \p step.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001628 * \retval #PSA_ERROR_NOT_SUPPORTED
1629 * \p step p is not supported with the \p operation's algorithm, or the
1630 * \p input is not supported for the \p operation's algorithm, cipher
1631 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1634 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1635 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1636 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1637 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001638 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001639 * The operation state is not valid (it must be active, and fully set
1640 * up, and this call must conform to the algorithm's requirements
1641 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001642 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001643 * It is implementation-dependent whether a failure to initialize
1644 * results in this error code.
1645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1647 psa_pake_step_t step,
1648 const uint8_t *input,
1649 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001650
1651/** Get implicitly confirmed shared secret from a PAKE.
1652 *
1653 * At this point there is a cryptographic guarantee that only the authenticated
1654 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001655 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001656 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001657 * That is, the authentication is only implicit. Since the peer is not
1658 * authenticated yet, no action should be taken yet that assumes that the peer
1659 * is who it claims to be. For example, do not access restricted files on the
1660 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001661 *
1662 * This function can be called after the key exchange phase of the operation
1663 * has completed. It imports the shared secret output of the PAKE into the
1664 * provided derivation operation. The input step
1665 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1666 * material in the key derivation operation.
1667 *
1668 * The exact sequence of calls to perform a password-authenticated key
1669 * exchange depends on the algorithm in use. Refer to the documentation of
1670 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1671 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1672 * information.
1673 *
1674 * When this function returns successfully, \p operation becomes inactive.
1675 * If this function returns an error status, both \p operation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001676 * and \c key_derivation operations enter an error state and must be aborted by
Janos Follath702cf092021-05-26 12:58:23 +01001677 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1678 *
1679 * \param[in,out] operation Active PAKE operation.
1680 * \param[out] output A key derivation operation that is ready
1681 * for an input step of type
1682 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1683 *
1684 * \retval #PSA_SUCCESS
1685 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001686 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001687 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1688 * algorithm in the \p output key derivation operation.
1689 * \retval #PSA_ERROR_NOT_SUPPORTED
1690 * Input from a PAKE is not supported by the algorithm in the \p output
1691 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001692 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1693 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1694 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1695 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1696 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1697 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001698 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001699 * The PAKE operation state is not valid (it must be active, but beyond
1700 * that validity is specific to the algorithm), or
1701 * the library has not been previously initialized by psa_crypto_init(),
1702 * or the state of \p output is not valid for
1703 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1704 * step is out of order or the application has done this step already
1705 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001706 * It is implementation-dependent whether a failure to initialize
1707 * results in this error code.
1708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1710 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001711
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001712/** Abort a PAKE operation.
1713 *
1714 * Aborting an operation frees all associated resources except for the \c
1715 * operation structure itself. Once aborted, the operation object can be reused
1716 * for another operation by calling psa_pake_setup() again.
1717 *
1718 * This function may be called at any time after the operation
1719 * object has been initialized as described in #psa_pake_operation_t.
1720 *
1721 * In particular, calling psa_pake_abort() after the operation has been
1722 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1723 * is safe and has no effect.
1724 *
1725 * \param[in,out] operation The operation to abort.
1726 *
1727 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001728 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1730 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001731 * \retval #PSA_ERROR_BAD_STATE
1732 * The library has not been previously initialized by psa_crypto_init().
1733 * It is implementation-dependent whether a failure to initialize
1734 * results in this error code.
1735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001737
Janos Follath702cf092021-05-26 12:58:23 +01001738/**@}*/
1739
1740/** A sufficient output buffer size for psa_pake_output().
1741 *
1742 * If the size of the output buffer is at least this large, it is guaranteed
1743 * that psa_pake_output() will not fail due to an insufficient output buffer
1744 * size. The actual size of the output might be smaller in any given call.
1745 *
1746 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1747 *
Janos Follath46c02372021-06-08 15:22:51 +01001748 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001749 * #PSA_ALG_IS_PAKE(\p alg) is true).
1750 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1751 * compatible with algorithm \p alg.
1752 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1753 * algorithm \p alg.
1754 * \return A sufficient output buffer size for the specified
Neil Armstrongcd974d52022-05-20 10:30:12 +02001755 * PAKE algorithm, primitive, and output step. If the
1756 * PAKE algorithm, primitive, or output step is not
1757 * recognized, or the parameters are incompatible,
1758 * return 0.
Janos Follath702cf092021-05-26 12:58:23 +01001759 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001760#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 (alg == PSA_ALG_JPAKE && \
1762 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1763 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1764 ( \
1765 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1766 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1767 32 \
1768 ) : \
1769 0)
Janos Follath702cf092021-05-26 12:58:23 +01001770
1771/** A sufficient input buffer size for psa_pake_input().
1772 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001773 * The value returned by this macro is guaranteed to be large enough for any
1774 * valid input to psa_pake_input() in an operation with the specified
1775 * parameters.
Janos Follath702cf092021-05-26 12:58:23 +01001776 *
1777 * See also #PSA_PAKE_INPUT_MAX_SIZE
1778 *
Janos Follath46c02372021-06-08 15:22:51 +01001779 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001780 * #PSA_ALG_IS_PAKE(\p alg) is true).
1781 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1782 * compatible with algorithm \p alg.
Janos Follathec83eb62021-05-27 08:41:59 +01001783 * \param input_step A value of type ::psa_pake_step_t that is valid for the
Janos Follath702cf092021-05-26 12:58:23 +01001784 * algorithm \p alg.
Janos Follath38d29db2021-06-03 13:14:42 +01001785 * \return A sufficient input buffer size for the specified
1786 * input, cipher suite and algorithm. If the cipher suite,
1787 * the input type or PAKE algorithm is not recognized, or
Janos Follath702cf092021-05-26 12:58:23 +01001788 * the parameters are incompatible, return 0.
1789 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001790#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 (alg == PSA_ALG_JPAKE && \
1792 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1793 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1794 ( \
1795 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1796 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1797 32 \
1798 ) : \
1799 0)
Janos Follath702cf092021-05-26 12:58:23 +01001800
Neil Armstrong2056ce52022-05-25 11:38:15 +02001801/** Output buffer size for psa_pake_output() for any of the supported PAKE
1802 * algorithm and primitive suites and output step.
Janos Follath702cf092021-05-26 12:58:23 +01001803 *
1804 * This macro must expand to a compile-time constant integer.
1805 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001806 * The value of this macro must be at least as large as the largest value
1807 * returned by PSA_PAKE_OUTPUT_SIZE()
1808 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001809 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001810 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001811#define PSA_PAKE_OUTPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001812
Neil Armstrong2056ce52022-05-25 11:38:15 +02001813/** Input buffer size for psa_pake_input() for any of the supported PAKE
1814 * algorithm and primitive suites and input step.
Janos Follath702cf092021-05-26 12:58:23 +01001815 *
1816 * This macro must expand to a compile-time constant integer.
1817 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001818 * The value of this macro must be at least as large as the largest value
1819 * returned by PSA_PAKE_INPUT_SIZE()
1820 *
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001821 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
Janos Follath702cf092021-05-26 12:58:23 +01001822 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001823#define PSA_PAKE_INPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001824
Neil Armstrongfb993022022-05-20 10:08:58 +02001825/** Returns a suitable initializer for a PAKE cipher suite object of type
1826 * psa_pake_cipher_suite_t.
1827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
Neil Armstrongfb993022022-05-20 10:08:58 +02001829
Neil Armstrong0151c552022-05-20 10:13:53 +02001830/** Returns a suitable initializer for a PAKE operation object of type
1831 * psa_pake_operation_t.
1832 */
Przemek Stekiel656b2592023-03-22 13:15:33 +01001833#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001834 { 0 }, { { 0 } } }
Neil Armstrong0151c552022-05-20 10:13:53 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836struct psa_pake_cipher_suite_s {
Janos Follath702cf092021-05-26 12:58:23 +01001837 psa_algorithm_t algorithm;
1838 psa_pake_primitive_type_t type;
1839 psa_pake_family_t family;
1840 uint16_t bits;
1841 psa_algorithm_t hash;
1842};
1843
1844static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001846{
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001848}
1849
1850static inline void psa_pake_cs_set_algorithm(
1851 psa_pake_cipher_suite_t *cipher_suite,
1852 psa_algorithm_t algorithm)
1853{
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001855 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001857 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 }
Janos Follath702cf092021-05-26 12:58:23 +01001859}
1860
1861static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001863{
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1865 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001866}
1867
1868static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 psa_pake_cipher_suite_t *cipher_suite,
1870 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01001871{
1872 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1873 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1874 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1875}
1876
Neil Armstrongff9cac72022-05-20 10:25:15 +02001877static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02001879{
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02001881}
1882
Neil Armstrongd5a48252022-05-20 10:26:36 +02001883static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02001885{
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02001887}
1888
Janos Follath702cf092021-05-26 12:58:23 +01001889static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001891{
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01001893}
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1896 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01001897{
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01001899 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001901 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 }
Janos Follath702cf092021-05-26 12:58:23 +01001903}
1904
Przemek Stekiel51eac532022-12-07 11:04:51 +01001905struct psa_crypto_driver_pake_inputs_s {
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 uint8_t *MBEDTLS_PRIVATE(password);
Przemek Stekiel152ae072022-11-17 13:24:36 +01001907 size_t MBEDTLS_PRIVATE(password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01001908 uint8_t *MBEDTLS_PRIVATE(user);
1909 size_t MBEDTLS_PRIVATE(user_len);
1910 uint8_t *MBEDTLS_PRIVATE(peer);
1911 size_t MBEDTLS_PRIVATE(peer_len);
Przemek Stekiel9dd24402023-01-26 15:06:09 +01001912 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001913 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
1914};
1915
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001916typedef enum psa_crypto_driver_pake_step {
Przemek Stekielb09c4872023-01-17 12:05:38 +01001917 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1918 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1919 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1920 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1921 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1922 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1923 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1924 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1925 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1926 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1927 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1928 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1929 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 +01001930} psa_crypto_driver_pake_step_t;
Przemek Stekiel57980032023-01-09 15:07:26 +01001931
David Horstmanne7f21e62023-05-12 18:17:21 +01001932typedef enum psa_jpake_round {
David Horstmann5da95602023-06-08 15:37:12 +01001933 PSA_JPAKE_FIRST = 0,
1934 PSA_JPAKE_SECOND = 1,
1935 PSA_JPAKE_FINISHED = 2
David Horstmanne7f21e62023-05-12 18:17:21 +01001936} psa_jpake_round_t;
1937
1938typedef enum psa_jpake_io_mode {
David Horstmann5da95602023-06-08 15:37:12 +01001939 PSA_JPAKE_INPUT = 0,
1940 PSA_JPAKE_OUTPUT = 1
David Horstmanne7f21e62023-05-12 18:17:21 +01001941} psa_jpake_io_mode_t;
Przemek Stekielb09c4872023-01-17 12:05:38 +01001942
Przemek Stekiele12ed362022-12-21 12:54:46 +01001943struct psa_jpake_computation_stage_s {
David Horstmanne7f21e62023-05-12 18:17:21 +01001944 /* The J-PAKE round we are currently on */
1945 psa_jpake_round_t MBEDTLS_PRIVATE(round);
1946 /* The 'mode' we are currently in (inputting or outputting) */
David Horstmann024e5c52023-06-14 15:48:21 +01001947 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
David Horstmann279d2272023-06-14 17:13:56 +01001948 /* The number of completed inputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001949 uint8_t MBEDTLS_PRIVATE(inputs);
David Horstmann279d2272023-06-14 17:13:56 +01001950 /* The number of completed outputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01001951 uint8_t MBEDTLS_PRIVATE(outputs);
1952 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1953 psa_pake_step_t MBEDTLS_PRIVATE(step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001954};
1955
David Horstmann5dbe17d2023-06-27 10:30:28 +01001956#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1957 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1958#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1959 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
David Horstmanne7f21e62023-05-12 18:17:21 +01001960
Janos Follath702cf092021-05-26 12:58:23 +01001961struct psa_pake_operation_s {
Przemek Stekield91bcb72022-11-22 14:00:51 +01001962 /** Unique ID indicating which driver got assigned to do the
1963 * operation. Since driver contexts are driver-specific, swapping
1964 * drivers halfway through the operation is not supported.
1965 * ID values are auto-generated in psa_crypto_driver_wrappers.h
1966 * ID value zero means the context is not valid or not assigned to
1967 * any driver (i.e. none of the driver contexts are active). */
1968 unsigned int MBEDTLS_PRIVATE(id);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001969 /* Algorithm of the PAKE operation */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001970 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Przemek Stekiel656b2592023-03-22 13:15:33 +01001971 /* A primitive of type compatible with algorithm */
1972 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
Przemek Stekiel6b648622023-02-19 22:55:33 +01001973 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1974 * or computing. */
Przemek Stekiel51eac532022-12-07 11:04:51 +01001975 uint8_t MBEDTLS_PRIVATE(stage);
Przemek Stekiele12ed362022-12-21 12:54:46 +01001976 /* Holds computation stage of the PAKE algorithms. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 union {
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001978 uint8_t MBEDTLS_PRIVATE(dummy);
Przemek Stekiel4aa99402023-02-27 13:00:57 +01001979#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekieldde6a912023-01-26 08:46:37 +01001980 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
Neil Armstrong35269d92022-05-25 11:26:31 +02001981#endif
Przemek Stekieldde6a912023-01-26 08:46:37 +01001982 } MBEDTLS_PRIVATE(computation_stage);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001983 union {
Przemek Stekiel51eac532022-12-07 11:04:51 +01001984 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
Przemek Stekielac067d72023-01-26 16:31:03 +01001985 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001986 } MBEDTLS_PRIVATE(data);
Janos Follath702cf092021-05-26 12:58:23 +01001987};
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001990{
1991 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001993}
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01001996{
1997 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return v;
Janos Follath702cf092021-05-26 12:58:23 +01001999}
2000
Gilles Peskinee59236f2018-01-27 23:32:46 +01002001#ifdef __cplusplus
2002}
2003#endif
2004
2005#endif /* PSA_CRYPTO_EXTRA_H */