blob: a1792fe5624f80d96ef1f1a2e7ba898a2efdae8f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
Ronald Crond3324fd2025-06-02 11:14:57 +02002
Gilles Peskinee59236f2018-01-27 23:32:46 +01003 * \file psa/crypto_extra.h
4 *
5 * \brief PSA cryptography module: Mbed TLS vendor extensions
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 *
7 * \note This file may not be included directly. Applications must
8 * include psa/crypto.h.
9 *
10 * This file is reserved for vendor-specific definitions.
Gilles Peskinee59236f2018-01-27 23:32:46 +010011 */
12/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020013 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +010015 */
16
17#ifndef PSA_CRYPTO_EXTRA_H
18#define PSA_CRYPTO_EXTRA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020019#include "mbedtls/private_access.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Gilles Peskine09c02ee2021-11-25 20:30:47 +010021#include "crypto_types.h"
Gilles Peskine7a894f22019-11-26 16:06:46 +010022#include "crypto_compat.h"
23
Gilles Peskinee59236f2018-01-27 23:32:46 +010024#ifdef __cplusplus
25extern "C" {
26#endif
27
Netanel Gonen2bcd3122018-11-19 11:53:02 +020028/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020029#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020030
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020031/* See mbedtls_config.h for definition */
Steven Cooreman863470a2021-02-15 14:03:19 +010032#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
33#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
Steven Cooreman1f968fd2021-02-15 14:00:24 +010034#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000035
Valerio Settice849212024-08-29 15:02:47 +020036/* If the size of static key slots is not explicitly defined by the user, then
37 * set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE and
38 * PSA_CIPHER_MAX_KEY_LENGTH.
39 * See mbedtls_config.h for the definition. */
40#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)
41#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE \
42 ((PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > PSA_CIPHER_MAX_KEY_LENGTH) ? \
43 PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE : PSA_CIPHER_MAX_KEY_LENGTH)
44#endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/
45
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020046/** \addtogroup attributes
47 * @{
48 */
49
50/** \brief Declare the enrollment algorithm for a key.
51 *
52 * An operation on a key may indifferently use the algorithm set with
53 * psa_set_key_algorithm() or with this function.
54 *
55 * \param[out] attributes The attribute structure to write to.
56 * \param alg2 A second algorithm that the key may be used
57 * for, in addition to the algorithm set with
58 * psa_set_key_algorithm().
59 *
60 * \warning Setting an enrollment algorithm is not recommended, because
61 * using the same key with different algorithms can allow some
62 * attacks based on arithmetic relations between different
63 * computations made with the same key, or can escalate harmless
64 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020065 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020066 * verified that the usage of the key with multiple algorithms
67 * is safe.
68 */
69static inline void psa_set_key_enrollment_algorithm(
70 psa_key_attributes_t *attributes,
71 psa_algorithm_t alg2)
72{
Gilles Peskine2f107ae2024-02-28 01:26:46 +010073 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020074}
75
76/** Retrieve the enrollment algorithm policy from key attributes.
77 *
78 * \param[in] attributes The key attribute structure to query.
79 *
80 * \return The enrollment algorithm stored in the attribute structure.
81 */
82static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
83 const psa_key_attributes_t *attributes)
84{
Gilles Peskine2f107ae2024-02-28 01:26:46 +010085 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020086}
87
Gilles Peskinec8000c02019-08-02 20:15:51 +020088#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
89
90/** Retrieve the slot number where a key is stored.
91 *
92 * A slot number is only defined for keys that are stored in a secure
93 * element.
94 *
95 * This information is only useful if the secure element is not entirely
96 * managed through the PSA Cryptography API. It is up to the secure
97 * element driver to decide how PSA slot numbers map to any other interface
98 * that the secure element may have.
99 *
100 * \param[in] attributes The key attribute structure to query.
101 * \param[out] slot_number On success, the slot number containing the key.
102 *
103 * \retval #PSA_SUCCESS
104 * The key is located in a secure element, and \p *slot_number
105 * indicates the slot number that contains it.
106 * \retval #PSA_ERROR_NOT_PERMITTED
107 * The caller is not permitted to query the slot number.
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200108 * Mbed TLS currently does not return this error.
Gilles Peskinec8000c02019-08-02 20:15:51 +0200109 * \retval #PSA_ERROR_INVALID_ARGUMENT
110 * The key is not located in a secure element.
111 */
112psa_status_t psa_get_key_slot_number(
113 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 psa_key_slot_number_t *slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200115
116/** Choose the slot number where a key is stored.
117 *
118 * This function declares a slot number in the specified attribute
119 * structure.
120 *
121 * A slot number is only meaningful for keys that are stored in a secure
122 * element. It is up to the secure element driver to decide how PSA slot
123 * numbers map to any other interface that the secure element may have.
124 *
125 * \note Setting a slot number in key attributes for a key creation can
126 * cause the following errors when creating the key:
127 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
128 * not support choosing a specific slot number.
129 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
130 * choose slot numbers in general or to choose this specific slot.
131 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
132 * valid in general or not valid for this specific key.
133 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
134 * selected slot.
135 *
136 * \param[out] attributes The attribute structure to write to.
137 * \param slot_number The slot number to set.
138 */
139static inline void psa_set_key_slot_number(
140 psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 psa_key_slot_number_t slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +0200142{
Gilles Peskine972539c2024-02-28 01:49:45 +0100143 attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100144 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200145}
146
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200147/** Remove the slot number attribute from a key attribute structure.
148 *
149 * This function undoes the action of psa_set_key_slot_number().
150 *
151 * \param[out] attributes The attribute structure to write to.
152 */
153static inline void psa_clear_key_slot_number(
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 psa_key_attributes_t *attributes)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200155{
Gilles Peskine972539c2024-02-28 01:49:45 +0100156 attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200157}
158
Gilles Peskined7729582019-08-05 15:55:54 +0200159/** Register a key that is already present in a secure element.
160 *
161 * The key must be located in a secure element designated by the
162 * lifetime field in \p attributes, in the slot set with
163 * psa_set_key_slot_number() in the attribute structure.
164 * This function makes the key available through the key identifier
165 * specified in \p attributes.
166 *
167 * \param[in] attributes The attributes of the existing key.
Gilles Peskined72ad732024-06-13 16:06:45 +0200168 * - The lifetime must be a persistent lifetime
169 * in a secure element. Volatile lifetimes are
170 * not currently supported.
171 * - The key identifier must be in the valid
172 * range for persistent keys.
173 * - The key type and size must be specified and
174 * must be consistent with the key material
175 * in the secure element.
Gilles Peskined7729582019-08-05 15:55:54 +0200176 *
177 * \retval #PSA_SUCCESS
178 * The key was successfully registered.
179 * Note that depending on the design of the driver, this may or may
180 * not guarantee that a key actually exists in the designated slot
181 * and is compatible with the specified attributes.
182 * \retval #PSA_ERROR_ALREADY_EXISTS
183 * There is already a key with the identifier specified in
184 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200185 * \retval #PSA_ERROR_NOT_SUPPORTED
186 * The secure element driver for the specified lifetime does not
187 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200188 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200189 * The identifier in \p attributes is invalid, namely the identifier is
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500190 * not in the user range, or
Gilles Peskined7729582019-08-05 15:55:54 +0200191 * \p attributes specifies a lifetime which is not located
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500192 * in a secure element, or no slot number is specified in \p attributes,
Gilles Peskined7729582019-08-05 15:55:54 +0200193 * or the specified slot number is not valid.
194 * \retval #PSA_ERROR_NOT_PERMITTED
195 * The caller is not authorized to register the specified key slot.
Gilles Peskineed733552023-02-14 19:21:09 +0100196 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
197 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
199 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
200 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
201 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskined7729582019-08-05 15:55:54 +0200202 * \retval #PSA_ERROR_BAD_STATE
203 * The library has not been previously initialized by psa_crypto_init().
204 * It is implementation-dependent whether a failure to initialize
205 * results in this error code.
206 */
207psa_status_t mbedtls_psa_register_se_key(
208 const psa_key_attributes_t *attributes);
209
Gilles Peskinec8000c02019-08-02 20:15:51 +0200210#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
211
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200212/**@}*/
213
Gilles Peskinee59236f2018-01-27 23:32:46 +0100214/**
215 * \brief Library deinitialization.
216 *
217 * This function clears all data associated with the PSA layer,
218 * including the whole key store.
Ryan Everett16abd592024-01-24 17:37:46 +0000219 * This function is not thread safe, it wipes every key slot regardless of
220 * state and reader count. It should only be called when no slot is in use.
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221 *
222 * This is an Mbed TLS extension.
223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200226/** \brief Statistics about
227 * resource consumption related to the PSA keystore.
228 *
229 * \note The content of this structure is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200230 * of Mbed TLS and may change arbitrarily from version to version.
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200233 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200234 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200235 /** Number of slots containing key material for a key which is in
236 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200237 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200238 /** Number of slots containing a reference to a key in a
239 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200240 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200241 /** Number of slots which are occupied, but do not contain
242 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200243 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200244 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200245 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200246 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200247 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100248 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200249 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200250 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200251 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200252 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200253 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200254} mbedtls_psa_stats_t;
255
256/** \brief Get statistics about
257 * resource consumption related to the PSA keystore.
258 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200259 * \note When Mbed TLS is built as part of a service, with isolation
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200260 * between the application and the keystore, the service may or
261 * may not expose this function.
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200264
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200265/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100266 * \brief Inject an initial entropy seed for the random generator into
267 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100268 *
269 * This function injects data to be used as a seed for the random generator
270 * used by the PSA Crypto implementation. On devices that lack a trusted
271 * entropy source (preferably a hardware random number generator),
272 * the Mbed PSA Crypto implementation uses this value to seed its
273 * random generator.
274 *
275 * On devices without a trusted entropy source, this function must be
276 * called exactly once in the lifetime of the device. On devices with
277 * a trusted entropy source, calling this function is optional.
278 * In all cases, this function may only be called before calling any
279 * other function in the PSA Crypto API, including psa_crypto_init().
280 *
281 * When this function returns successfully, it populates a file in
282 * persistent storage. Once the file has been created, this function
283 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100284 *
285 * If any error occurs, this function does not change the system state.
286 * You can call this function again after correcting the reason for the
287 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200288 *
289 * \warning This function **can** fail! Callers MUST check the return status.
290 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100291 * \warning If you use this function, you should use it as part of a
292 * factory provisioning process. The value of the injected seed
293 * is critical to the security of the device. It must be
294 * *secret*, *unpredictable* and (statistically) *unique per device*.
295 * You should be generate it randomly using a cryptographically
296 * secure random generator seeded from trusted entropy sources.
297 * You should transmit it securely to the device and ensure
298 * that its value is not leaked or stored anywhere beyond the
299 * needs of transmitting it from the point of generation to
300 * the call of this function, and erase all copies of the value
301 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200302 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100303 * This is an Mbed TLS extension.
304 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200305 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100306 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
307 * Note that you must provide compatible implementations of
308 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200309 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200310 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200311 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200312 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200313 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000314 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
315 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
316 * in `library/entropy_poll.h` in the Mbed TLS source
317 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200318 * It must be less or equal to
319 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200320 *
321 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100322 * The seed value was injected successfully. The random generator
323 * of the PSA Crypto implementation is now ready for use.
324 * You may now call psa_crypto_init() and use the PSA Crypto
325 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200326 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100327 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200328 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100329 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200330 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100331 * The library has already been initialized. It is no longer
332 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200333 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100334psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200335 size_t seed_size);
336
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200337/** \addtogroup crypto_types
338 * @{
339 */
340
Gilles Peskinea1302192019-05-16 13:58:24 +0200341/** DSA public key.
342 *
343 * The import and export format is the
344 * representation of the public key `y = g^x mod p` as a big-endian byte
345 * string. The length of the byte string is the length of the base prime `p`
346 * in bytes.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200349
350/** DSA key pair (private and public key).
351 *
352 * The import and export format is the
353 * representation of the private key `x` as a big-endian byte string. The
354 * length of the byte string is the private key size in bytes (leading zeroes
355 * are not stripped).
356 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800357 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200358 * FIPS 186-4 §B.1.2: interpret the byte string as integer
359 * in big-endian order. Discard it if it is not in the range
360 * [0, *N* - 2] where *N* is the boundary of the private key domain
361 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
362 * or the order of the curve's base point for ECC).
363 * Add 1 to the resulting integer and use this as the private key *x*.
364 *
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200367
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100368/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200369#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200370 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200373/** DSA signature with hashing.
374 *
375 * This is the signature scheme defined by FIPS 186-4,
376 * with a random per-message secret number (*k*).
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_DSA(hash_alg) \
388 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100389#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100390#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200391/** Deterministic DSA signature with hashing.
392 *
393 * This is the deterministic variant defined by RFC 6979 of
394 * the signature scheme defined by FIPS 186-4.
395 *
396 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
397 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
398 * This includes #PSA_ALG_ANY_HASH
399 * when specifying the algorithm in a usage policy.
400 *
401 * \return The corresponding DSA signature algorithm.
402 * \return Unspecified if \p hash_alg is not a supported
403 * hash algorithm.
404 */
405#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
406 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
407#define PSA_ALG_IS_DSA(alg) \
408 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
409 PSA_ALG_DSA_BASE)
410#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
411 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
412#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
413 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
414#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
415 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
416
417
418/* We need to expand the sample definition of this macro from
419 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100420#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
421#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
422 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200423
424/**@}*/
425
Gilles Peskine24f10f82019-05-16 12:18:32 +0200426/** \addtogroup attributes
427 * @{
428 */
429
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100430/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100431#define PSA_PAKE_OPERATION_STAGE_SETUP 0
432#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
433#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200434
Gilles Peskine24f10f82019-05-16 12:18:32 +0200435/**@}*/
436
Gilles Peskine5055b232019-12-12 17:49:31 +0100437
Gilles Peskineb8af2282020-11-13 18:00:34 +0100438/** \defgroup psa_external_rng External random generator
439 * @{
440 */
441
442#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
443/** External random generator function, implemented by the platform.
444 *
445 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
446 * this function replaces Mbed TLS's entropy and DRBG modules for all
447 * random generation triggered via PSA crypto interfaces.
448 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100449 * \note This random generator must deliver random numbers with cryptographic
450 * quality and high performance. It must supply unpredictable numbers
451 * with a uniform distribution. The implementation of this function
452 * is responsible for ensuring that the random generator is seeded
453 * with sufficient entropy. If you have a hardware TRNG which is slow
454 * or delivers non-uniform output, declare it as an entropy source
455 * with mbedtls_entropy_add_source() instead of enabling this option.
456 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100457 * \param[in,out] context Pointer to the random generator context.
458 * This is all-bits-zero on the first call
459 * and preserved between successive calls.
460 * \param[out] output Output buffer. On success, this buffer
461 * contains random data with a uniform
462 * distribution.
463 * \param output_size The size of the \p output buffer in bytes.
464 * \param[out] output_length On success, set this value to \p output_size.
465 *
466 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100467 * Success. The output buffer contains \p output_size bytes of
468 * cryptographic-quality random data, and \c *output_length is
469 * set to \p output_size.
470 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
471 * The random generator requires extra entropy and there is no
472 * way to obtain entropy under current environment conditions.
473 * This error should not happen under normal circumstances since
474 * this function is responsible for obtaining as much entropy as
475 * it needs. However implementations of this function may return
476 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
477 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100478 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100479 * A failure of the random generator hardware that isn't covered
480 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100481 */
482psa_status_t mbedtls_psa_external_get_random(
483 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100485#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
486
487/**@}*/
488
Steven Cooreman6801f082021-02-19 17:21:22 +0100489/** \defgroup psa_builtin_keys Built-in keys
490 * @{
491 */
492
493/** The minimum value for a key identifier that is built into the
494 * implementation.
495 *
496 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
497 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
498 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
499 * with any other set of implementation-chosen key identifiers.
500 *
Gilles Peskine543909d2024-06-20 22:10:08 +0200501 * This value is part of the library's API since changing it would invalidate
Steven Cooreman6801f082021-02-19 17:21:22 +0100502 * the values of built-in key identifiers in applications.
503 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100505
506/** The maximum value for a key identifier that is built into the
507 * implementation.
508 *
509 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100512
513/** A slot number identifying a key in a driver.
514 *
515 * Values of this type are used to identify built-in keys.
516 */
517typedef uint64_t psa_drv_slot_number_t;
518
519#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
520/** Test whether a key identifier belongs to the builtin key range.
521 *
522 * \param key_id Key identifier to test.
523 *
524 * \retval 1
525 * The key identifier is a builtin key identifier.
526 * \retval 0
527 * The key identifier is not a builtin key identifier.
528 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100530{
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
532 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100533}
534
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200535/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100536 *
537 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100538 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100539 * as part of a platform's system image.
540 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100541 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100542 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
543 *
544 * In a multi-application configuration
545 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
546 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
547 * is allowed to use the given key.
548 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100549 * \param key_id The key ID for which to retrieve the
550 * location and slot attributes.
551 * \param[out] lifetime On success, the lifetime associated with the key
552 * corresponding to \p key_id. Lifetime is a
553 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200554 * and with what persistence level the key is
555 * intended to be used. If the platform
556 * implementation does not contain specific
557 * information about the intended key persistence
558 * level, the persistence level may be reported as
559 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100560 * \param[out] slot_number On success, the slot number known to the driver
561 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200562 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100563 * requested built-in key.
564 *
565 * \retval #PSA_SUCCESS
566 * The requested key identifier designates a built-in key.
567 * In a multi-application configuration, the requested owner
568 * is allowed to access it.
569 * \retval #PSA_ERROR_DOES_NOT_EXIST
570 * The requested key identifier is not a built-in key which is known
571 * to this function. If a key exists in the key storage with this
572 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100573 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100574 * Any other error is propagated to the function that requested the key.
575 * Common errors include:
576 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
577 * is not allowed to access it.
578 */
579psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100580 mbedtls_svc_key_id_t key_id,
581 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100583#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
584
585/** @} */
586
Valerio Setti05b38352025-02-21 14:40:51 +0100587/** \defgroup psa_crypto_client Functions defined by a client provider
588 *
589 * The functions in this group are meant to be implemented by providers of
590 * the PSA Crypto client interface. They are provided by the library when
591 * #MBEDTLS_PSA_CRYPTO_C is enabled.
592 *
593 * \note All functions in this group are experimental, as using
594 * alternative client interface providers is experimental.
595 *
596 * @{
597 */
598
Valerio Setti79a98bd2025-02-21 15:00:11 +0100599/** Check if PSA is capable of handling the specified hash algorithm.
Valerio Setti05b38352025-02-21 14:40:51 +0100600 *
Valerio Setti79a98bd2025-02-21 15:00:11 +0100601 * This means that PSA core was built with the corresponding PSA_WANT_ALG_xxx
602 * set and that psa_crypto_init has already been called.
Valerio Setti05b38352025-02-21 14:40:51 +0100603 *
Valerio Setti79a98bd2025-02-21 15:00:11 +0100604 * \note When using Mbed TLS version of PSA core (i.e. MBEDTLS_PSA_CRYPTO_C is
605 * set) for now this function only checks the state of the driver
606 * subsystem, not the algorithm. This might be improved in the future.
Valerio Setti05b38352025-02-21 14:40:51 +0100607 *
Valerio Setti79a98bd2025-02-21 15:00:11 +0100608 * \param hash_alg The hash algorithm.
609 *
610 * \return 1 if the PSA can handle \p hash_alg, 0 otherwise.
Valerio Setti05b38352025-02-21 14:40:51 +0100611 */
612int psa_can_do_hash(psa_algorithm_t hash_alg);
613
614/**@}*/
615
Janos Follath702cf092021-05-26 12:58:23 +0100616/** \addtogroup crypto_types
617 * @{
618 */
619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100621
622/** Whether the specified algorithm is a password-authenticated key exchange.
623 *
624 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
625 *
626 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
627 * algorithm, 0 otherwise.
628 * This macro may return either 0 or 1 if \p alg is not a supported
629 * algorithm identifier.
630 */
631#define PSA_ALG_IS_PAKE(alg) \
632 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
633
634/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
635 *
636 * This is J-PAKE as defined by RFC 8236, instantiated with the following
637 * parameters:
638 *
639 * - The group can be either an elliptic curve or defined over a finite field.
640 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
641 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100642 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100643 *
Janos Follath46c02372021-06-08 15:22:51 +0100644 * To select these parameters and set up the cipher suite, call these functions
645 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100646 *
647 * \code
648 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
649 * psa_pake_cs_set_primitive(cipher_suite,
650 * PSA_PAKE_PRIMITIVE(type, family, bits));
651 * psa_pake_cs_set_hash(cipher_suite, hash);
652 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100653 *
654 * For more information on how to set a specific curve or field, refer to the
655 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
656 *
657 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100658 *
659 * \code
660 * psa_pake_setup(operation, cipher_suite);
661 * psa_pake_set_user(operation, ...);
662 * psa_pake_set_peer(operation, ...);
663 * psa_pake_set_password_key(operation, ...);
664 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100665 *
Neil Armstrong16145372022-05-20 10:42:36 +0200666 * The password is provided as a key. This can be the password text itself,
667 * in an agreed character encoding, or some value derived from the password
668 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100669 *
Neil Armstrong16145372022-05-20 10:42:36 +0200670 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100671 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
672 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
673 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200674 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100675 * of the reduction is 0.)
676 *
677 * The key exchange flow for J-PAKE is as follows:
678 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100679 * \code
680 * // Get g1
681 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
682 * // Get the ZKP public key for x1
683 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
684 * // Get the ZKP proof for x1
685 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
686 * // Get g2
687 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
688 * // Get the ZKP public key for x2
689 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
690 * // Get the ZKP proof for x2
691 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
692 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100693 * -# To provide the first round data received from the peer to the operation,
694 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100695 * \code
696 * // Set g3
697 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
698 * // Set the ZKP public key for x3
699 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
700 * // Set the ZKP proof for x3
701 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
702 * // Set g4
703 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
704 * // Set the ZKP public key for x4
705 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
706 * // Set the ZKP proof for x4
707 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
708 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100709 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100710 * \code
711 * // Get A
712 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
713 * // Get ZKP public key for x2*s
714 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
715 * // Get ZKP proof for x2*s
716 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
717 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100718 * -# To provide the second round data received from the peer to the operation,
719 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100720 * \code
721 * // Set B
722 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
723 * // Set ZKP public key for x4*s
724 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
725 * // Set ZKP proof for x4*s
726 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
727 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100728 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100729 * \code
730 * // Get Ka=Kb=K
731 * psa_pake_get_implicit_key()
732 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100733 *
734 * For more information consult the documentation of the individual
735 * \c PSA_PAKE_STEP_XXX constants.
736 *
737 * At this point there is a cryptographic guarantee that only the authenticated
738 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100739 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100740 *
741 * That is, the authentication is only implicit (the peer is not authenticated
742 * at this point, and no action should be taken that assume that they are - like
743 * for example accessing restricted files).
744 *
745 * To make the authentication explicit there are various methods, see Section 5
746 * of RFC 8236 for two examples.
747 *
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100750
751/** @} */
752
753/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100754 *
755 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
756 * the official PSA Crypto API yet.
757 *
758 * \note The content of this section is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200759 * of Mbed TLS and may change arbitrarily from version to version.
Janos Follath7d69b3a2021-05-26 13:10:56 +0100760 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
761 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100762 * @{
763 */
764
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200765/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100766 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200767 * Encodes the application's role in the algorithm is being executed. For more
768 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
769 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100770 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200771typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100772
773/** Encoding of input and output indicators for PAKE.
774 *
775 * Some PAKE algorithms need to exchange more data than just a single key share.
776 * This type is for encoding additional input and output data for such
777 * algorithms.
778 */
779typedef uint8_t psa_pake_step_t;
780
781/** Encoding of the type of the PAKE's primitive.
782 *
783 * Values defined by this standard will never be in the range 0x80-0xff.
784 * Vendors who define additional types must use an encoding in this range.
785 *
786 * For more information see the documentation of individual
787 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
788 */
789typedef uint8_t psa_pake_primitive_type_t;
790
791/** \brief Encoding of the family of the primitive associated with the PAKE.
792 *
793 * For more information see the documentation of individual
794 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
795 */
796typedef uint8_t psa_pake_family_t;
797
798/** \brief Encoding of the primitive associated with the PAKE.
799 *
800 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
801 */
802typedef uint32_t psa_pake_primitive_t;
803
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200804/** A value to indicate no role in a PAKE algorithm.
805 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
806 * algorithms which do not assign roles.
807 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200809
Janos Follath702cf092021-05-26 12:58:23 +0100810/** The first peer in a balanced PAKE.
811 *
812 * Although balanced PAKE algorithms are symmetric, some of them needs an
813 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200814 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100815 * accepted.
816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100818
819/** The second peer in a balanced PAKE.
820 *
821 * Although balanced PAKE algorithms are symmetric, some of them needs an
822 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200823 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100824 * accepted.
825 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100827
828/** The client in an augmented PAKE.
829 *
830 * Augmented PAKE algorithms need to differentiate between client and server.
831 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +0100833
834/** The server in an augmented PAKE.
835 *
836 * Augmented PAKE algorithms need to differentiate between client and server.
837 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +0100839
840/** The PAKE primitive type indicating the use of elliptic curves.
841 *
842 * The values of the \c family and \c bits fields of the cipher suite identify a
843 * specific elliptic curve, using the same mapping that is used for ECC
844 * (::psa_ecc_family_t) keys.
845 *
846 * (Here \c family means the value returned by psa_pake_cs_get_family() and
847 * \c bits means the value returned by psa_pake_cs_get_bits().)
848 *
849 * Input and output during the operation can involve group elements and scalar
850 * values:
851 * -# The format for group elements is the same as for public keys on the
852 * specific curve would be. For more information, consult the documentation of
853 * psa_export_public_key().
854 * -# The format for scalars is the same as for private keys on the specific
855 * curve would be. For more information, consult the documentation of
856 * psa_export_key().
857 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100859
860/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
861 *
862 * The values of the \c family and \c bits fields of the cipher suite identify
863 * a specific Diffie-Hellman group, using the same mapping that is used for
864 * Diffie-Hellman (::psa_dh_family_t) keys.
865 *
866 * (Here \c family means the value returned by psa_pake_cs_get_family() and
867 * \c bits means the value returned by psa_pake_cs_get_bits().)
868 *
869 * Input and output during the operation can involve group elements and scalar
870 * values:
871 * -# The format for group elements is the same as for public keys on the
872 * specific group would be. For more information, consult the documentation of
873 * psa_export_public_key().
874 * -# The format for scalars is the same as for private keys on the specific
875 * group would be. For more information, consult the documentation of
876 * psa_export_key().
877 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100879
880/** Construct a PAKE primitive from type, family and bit-size.
881 *
882 * \param pake_type The type of the primitive
883 * (value of type ::psa_pake_primitive_type_t).
884 * \param pake_family The family of the primitive
885 * (the type and interpretation of this parameter depends
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500886 * on \p pake_type, for more information consult the
Janos Follath702cf092021-05-26 12:58:23 +0100887 * documentation of individual ::psa_pake_primitive_type_t
888 * constants).
889 * \param pake_bits The bit-size of the primitive
890 * (Value of type \c size_t. The interpretation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500891 * of this parameter depends on \p pake_family, for more
Janos Follath702cf092021-05-26 12:58:23 +0100892 * information consult the documentation of individual
893 * ::psa_pake_primitive_type_t constants).
894 *
895 * \return The constructed primitive value of type ::psa_pake_primitive_t.
896 * Return 0 if the requested primitive can't be encoded as
897 * ::psa_pake_primitive_t.
898 */
899#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
900 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
901 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +0100903
904/** The key share being sent to or received from the peer.
905 *
906 * The format for both input and output at this step is the same as for public
907 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
908 * be.
909 *
910 * For more information on the format, consult the documentation of
911 * psa_export_public_key().
912 *
913 * For information regarding how the group is determined, consult the
914 * documentation #PSA_PAKE_PRIMITIVE.
915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100917
918/** A Schnorr NIZKP public key.
919 *
Janos Follath55dd5dc2021-06-03 15:51:09 +0100920 * This is the ephemeral public key in the Schnorr Non-Interactive
921 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
922 *
Janos Follath702cf092021-05-26 12:58:23 +0100923 * The format for both input and output at this step is the same as for public
924 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
925 * be.
926 *
927 * For more information on the format, consult the documentation of
928 * psa_export_public_key().
929 *
930 * For information regarding how the group is determined, consult the
931 * documentation #PSA_PAKE_PRIMITIVE.
932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100934
935/** A Schnorr NIZKP proof.
936 *
Janos Follath55dd5dc2021-06-03 15:51:09 +0100937 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
938 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +0100939 *
Janos Follath1f013182021-06-08 15:30:48 +0100940 * Both for input and output, the value at this step is an integer less than
941 * the order of the group selected in the cipher suite. The format depends on
942 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +0100943 *
Janos Follath1f013182021-06-08 15:30:48 +0100944 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +0100945 * - For everything else the encoding is big endian (see Section 2.3.8 of
946 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +0100947 *
Janos Follath1f013182021-06-08 15:30:48 +0100948 * In both cases leading zeroes are allowed as long as the length in bytes does
949 * not exceed the byte length of the group order.
950 *
Janos Follath702cf092021-05-26 12:58:23 +0100951 * For information regarding how the group is determined, consult the
952 * documentation #PSA_PAKE_PRIMITIVE.
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +0100955
Ronald Crond3324fd2025-06-02 11:14:57 +0200956/**@}*/
957
958/** A sufficient output buffer size for psa_pake_output().
959 *
960 * If the size of the output buffer is at least this large, it is guaranteed
961 * that psa_pake_output() will not fail due to an insufficient output buffer
962 * size. The actual size of the output might be smaller in any given call.
963 *
964 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
965 *
966 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
967 * #PSA_ALG_IS_PAKE(\p alg) is true).
968 * \param primitive A primitive of type ::psa_pake_primitive_t that is
969 * compatible with algorithm \p alg.
970 * \param output_step A value of type ::psa_pake_step_t that is valid for the
971 * algorithm \p alg.
972 * \return A sufficient output buffer size for the specified
973 * PAKE algorithm, primitive, and output step. If the
974 * PAKE algorithm, primitive, or output step is not
975 * recognized, or the parameters are incompatible,
976 * return 0.
977 */
978#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
979 (alg == PSA_ALG_JPAKE && \
980 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
981 PSA_ECC_FAMILY_SECP_R1, 256) ? \
982 ( \
983 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
984 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
985 32 \
986 ) : \
987 0)
988
989/** A sufficient input buffer size for psa_pake_input().
990 *
991 * The value returned by this macro is guaranteed to be large enough for any
992 * valid input to psa_pake_input() in an operation with the specified
993 * parameters.
994 *
995 * See also #PSA_PAKE_INPUT_MAX_SIZE
996 *
997 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
998 * #PSA_ALG_IS_PAKE(\p alg) is true).
999 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1000 * compatible with algorithm \p alg.
1001 * \param input_step A value of type ::psa_pake_step_t that is valid for the
1002 * algorithm \p alg.
1003 * \return A sufficient input buffer size for the specified
1004 * input, cipher suite and algorithm. If the cipher suite,
1005 * the input type or PAKE algorithm is not recognized, or
1006 * the parameters are incompatible, return 0.
1007 */
1008#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
1009 (alg == PSA_ALG_JPAKE && \
1010 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1011 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1012 ( \
1013 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1014 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1015 32 \
1016 ) : \
1017 0)
1018
1019/** Output buffer size for psa_pake_output() for any of the supported PAKE
1020 * algorithm and primitive suites and output step.
1021 *
1022 * This macro must expand to a compile-time constant integer.
1023 *
1024 * The value of this macro must be at least as large as the largest value
1025 * returned by PSA_PAKE_OUTPUT_SIZE()
1026 *
1027 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
1028 */
1029#define PSA_PAKE_OUTPUT_MAX_SIZE 65
1030
1031/** Input buffer size for psa_pake_input() for any of the supported PAKE
1032 * algorithm and primitive suites and input step.
1033 *
1034 * This macro must expand to a compile-time constant integer.
1035 *
1036 * The value of this macro must be at least as large as the largest value
1037 * returned by PSA_PAKE_INPUT_SIZE()
1038 *
1039 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
1040 */
1041#define PSA_PAKE_INPUT_MAX_SIZE 65
1042
1043/** Returns a suitable initializer for a PAKE cipher suite object of type
1044 * psa_pake_cipher_suite_t.
1045 */
1046#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
1047
1048/** Returns a suitable initializer for a PAKE operation object of type
1049 * psa_pake_operation_t.
1050 */
1051#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1052#define PSA_PAKE_OPERATION_INIT { 0 }
1053#else
1054#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
1055 { 0 }, { { 0 } } }
1056#endif
1057
1058struct psa_pake_cipher_suite_s {
1059 psa_algorithm_t algorithm;
1060 psa_pake_primitive_type_t type;
1061 psa_pake_family_t family;
1062 uint16_t bits;
1063 psa_algorithm_t hash;
1064};
1065
1066struct psa_crypto_driver_pake_inputs_s {
1067 uint8_t *MBEDTLS_PRIVATE(password);
1068 size_t MBEDTLS_PRIVATE(password_len);
1069 uint8_t *MBEDTLS_PRIVATE(user);
1070 size_t MBEDTLS_PRIVATE(user_len);
1071 uint8_t *MBEDTLS_PRIVATE(peer);
1072 size_t MBEDTLS_PRIVATE(peer_len);
1073 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
1074 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
1075};
1076
1077typedef enum psa_crypto_driver_pake_step {
1078 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1079 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1080 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1081 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1082 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1083 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1084 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1085 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1086 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1087 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1088 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1089 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1090 PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
1091} psa_crypto_driver_pake_step_t;
1092
1093typedef enum psa_jpake_round {
1094 PSA_JPAKE_FIRST = 0,
1095 PSA_JPAKE_SECOND = 1,
1096 PSA_JPAKE_FINISHED = 2
1097} psa_jpake_round_t;
1098
1099typedef enum psa_jpake_io_mode {
1100 PSA_JPAKE_INPUT = 0,
1101 PSA_JPAKE_OUTPUT = 1
1102} psa_jpake_io_mode_t;
1103
1104struct psa_jpake_computation_stage_s {
1105 /* The J-PAKE round we are currently on */
1106 psa_jpake_round_t MBEDTLS_PRIVATE(round);
1107 /* The 'mode' we are currently in (inputting or outputting) */
1108 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
1109 /* The number of completed inputs so far this round */
1110 uint8_t MBEDTLS_PRIVATE(inputs);
1111 /* The number of completed outputs so far this round */
1112 uint8_t MBEDTLS_PRIVATE(outputs);
1113 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1114 psa_pake_step_t MBEDTLS_PRIVATE(step);
1115};
1116
1117#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1118 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1119#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1120 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1121
1122struct psa_pake_operation_s {
1123#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1124 mbedtls_psa_client_handle_t handle;
1125#else
1126 /** Unique ID indicating which driver got assigned to do the
1127 * operation. Since driver contexts are driver-specific, swapping
1128 * drivers halfway through the operation is not supported.
1129 * ID values are auto-generated in psa_crypto_driver_wrappers.h
1130 * ID value zero means the context is not valid or not assigned to
1131 * any driver (i.e. none of the driver contexts are active). */
1132 unsigned int MBEDTLS_PRIVATE(id);
1133 /* Algorithm of the PAKE operation */
1134 psa_algorithm_t MBEDTLS_PRIVATE(alg);
1135 /* A primitive of type compatible with algorithm */
1136 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
1137 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1138 * or computing. */
1139 uint8_t MBEDTLS_PRIVATE(stage);
1140 /* Holds computation stage of the PAKE algorithms. */
1141 union {
1142 uint8_t MBEDTLS_PRIVATE(dummy);
1143#if defined(PSA_WANT_ALG_JPAKE)
1144 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
1145#endif
1146 } MBEDTLS_PRIVATE(computation_stage);
1147 union {
1148 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
1149 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
1150 } MBEDTLS_PRIVATE(data);
1151#endif
1152};
1153
1154/** \addtogroup pake
1155 * @{
1156 */
1157
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001158/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +01001159 *
1160 * This is an implementation-defined \c struct. Applications should not
1161 * make any assumptions about the content of this structure.
1162 * Implementation details can change in future versions without notice.
1163 */
1164typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1165
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001166/** Return an initial value for a PAKE cipher suite object.
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001169
Janos Follath702cf092021-05-26 12:58:23 +01001170/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1171 *
Janos Follath702cf092021-05-26 12:58:23 +01001172 * \param[in] cipher_suite The cipher suite structure to query.
1173 *
1174 * \return The PAKE algorithm stored in the cipher suite structure.
1175 */
1176static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001178
1179/** Declare the PAKE algorithm for the cipher suite.
1180 *
1181 * This function overwrites any PAKE algorithm
1182 * previously set in \p cipher_suite.
1183 *
Janos Follath702cf092021-05-26 12:58:23 +01001184 * \param[out] cipher_suite The cipher suite structure to write to.
1185 * \param algorithm The PAKE algorithm to write.
1186 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1187 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1188 * If this is 0, the PAKE algorithm in
1189 * \p cipher_suite becomes unspecified.
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1192 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +01001193
1194/** Retrieve the primitive from a PAKE cipher suite.
1195 *
Janos Follath702cf092021-05-26 12:58:23 +01001196 * \param[in] cipher_suite The cipher suite structure to query.
1197 *
1198 * \return The primitive stored in the cipher suite structure.
1199 */
1200static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001202
1203/** Declare the primitive for a PAKE cipher suite.
1204 *
1205 * This function overwrites any primitive previously set in \p cipher_suite.
1206 *
Janos Follath702cf092021-05-26 12:58:23 +01001207 * \param[out] cipher_suite The cipher suite structure to write to.
1208 * \param primitive The primitive to write. If this is 0, the
1209 * primitive type in \p cipher_suite becomes
1210 * unspecified.
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1213 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +01001214
Neil Armstrongff9cac72022-05-20 10:25:15 +02001215/** Retrieve the PAKE family from a PAKE cipher suite.
1216 *
Neil Armstrongff9cac72022-05-20 10:25:15 +02001217 * \param[in] cipher_suite The cipher suite structure to query.
1218 *
1219 * \return The PAKE family stored in the cipher suite structure.
1220 */
1221static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +02001223
Neil Armstrongd5a48252022-05-20 10:26:36 +02001224/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1225 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001226 * \param[in] cipher_suite The cipher suite structure to query.
1227 *
1228 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1229 */
1230static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001232
Janos Follath702cf092021-05-26 12:58:23 +01001233/** Retrieve the hash algorithm from a PAKE cipher suite.
1234 *
Janos Follath702cf092021-05-26 12:58:23 +01001235 * \param[in] cipher_suite The cipher suite structure to query.
1236 *
1237 * \return The hash algorithm stored in the cipher suite structure. The return
1238 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1239 * the hash algorithm is not set.
1240 */
1241static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001243
1244/** Declare the hash algorithm for a PAKE cipher suite.
1245 *
1246 * This function overwrites any hash algorithm
1247 * previously set in \p cipher_suite.
1248 *
Janos Follath702cf092021-05-26 12:58:23 +01001249 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1250 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1251 * for more information.
1252 *
1253 * \param[out] cipher_suite The cipher suite structure to write to.
1254 * \param hash The hash involved in the cipher suite.
1255 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1256 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1257 * If this is 0, the hash algorithm in
1258 * \p cipher_suite becomes unspecified.
1259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1261 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001262
1263/** The type of the state data structure for PAKE operations.
1264 *
1265 * Before calling any function on a PAKE operation object, the application
1266 * must initialize it by any of the following means:
1267 * - Set the structure to all-bits-zero, for example:
1268 * \code
1269 * psa_pake_operation_t operation;
1270 * memset(&operation, 0, sizeof(operation));
1271 * \endcode
1272 * - Initialize the structure to logical zero values, for example:
1273 * \code
1274 * psa_pake_operation_t operation = {0};
1275 * \endcode
1276 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1277 * for example:
1278 * \code
1279 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1280 * \endcode
1281 * - Assign the result of the function psa_pake_operation_init()
1282 * to the structure, for example:
1283 * \code
1284 * psa_pake_operation_t operation;
1285 * operation = psa_pake_operation_init();
1286 * \endcode
1287 *
1288 * This is an implementation-defined \c struct. Applications should not
1289 * make any assumptions about the content of this structure.
1290 * Implementation details can change in future versions without notice. */
1291typedef struct psa_pake_operation_s psa_pake_operation_t;
1292
Przemek Stekiel51eac532022-12-07 11:04:51 +01001293/** The type of input values for PAKE operations. */
1294typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1295
Przemek Stekielb09c4872023-01-17 12:05:38 +01001296/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001297typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1298
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001299/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001302
Przemek Stekielc0e62502023-03-14 11:49:36 +01001303/** Get the length of the password in bytes from given inputs.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001304 *
1305 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001306 * \param[out] password_len Password length.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001307 *
1308 * \retval #PSA_SUCCESS
1309 * Success.
1310 * \retval #PSA_ERROR_BAD_STATE
1311 * Password hasn't been set yet.
1312 */
1313psa_status_t psa_crypto_driver_pake_get_password_len(
1314 const psa_crypto_driver_pake_inputs_t *inputs,
1315 size_t *password_len);
1316
1317/** Get the password from given inputs.
1318 *
1319 * \param[in] inputs Operation inputs.
1320 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001321 * \param buffer_size Size of the return buffer in bytes.
1322 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001323 *
1324 * \retval #PSA_SUCCESS
1325 * Success.
1326 * \retval #PSA_ERROR_BAD_STATE
1327 * Password hasn't been set yet.
1328 */
1329psa_status_t psa_crypto_driver_pake_get_password(
1330 const psa_crypto_driver_pake_inputs_t *inputs,
1331 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1332
Przemek Stekielc0e62502023-03-14 11:49:36 +01001333/** Get the length of the user id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001334 *
1335 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001336 * \param[out] user_len User id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001337 *
1338 * \retval #PSA_SUCCESS
1339 * Success.
1340 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001341 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001342 */
1343psa_status_t psa_crypto_driver_pake_get_user_len(
1344 const psa_crypto_driver_pake_inputs_t *inputs,
1345 size_t *user_len);
1346
Przemek Stekielc0e62502023-03-14 11:49:36 +01001347/** Get the length of the peer id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001348 *
1349 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001350 * \param[out] peer_len Peer id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001351 *
1352 * \retval #PSA_SUCCESS
1353 * Success.
1354 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001355 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001356 */
1357psa_status_t psa_crypto_driver_pake_get_peer_len(
1358 const psa_crypto_driver_pake_inputs_t *inputs,
1359 size_t *peer_len);
1360
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001361/** Get the user id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001362 *
1363 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001364 * \param[out] user_id User id.
1365 * \param user_id_size Size of \p user_id in bytes.
1366 * \param[out] user_id_len Size of the user id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001367 *
1368 * \retval #PSA_SUCCESS
1369 * Success.
1370 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001371 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001372 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001373 * The size of the \p user_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001374 */
1375psa_status_t psa_crypto_driver_pake_get_user(
1376 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001377 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001378
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001379/** Get the peer id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001380 *
1381 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001382 * \param[out] peer_id Peer id.
1383 * \param peer_id_size Size of \p peer_id in bytes.
1384 * \param[out] peer_id_length Size of the peer id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001385 *
1386 * \retval #PSA_SUCCESS
1387 * Success.
1388 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001389 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001390 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001391 * The size of the \p peer_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001392 */
1393psa_status_t psa_crypto_driver_pake_get_peer(
1394 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001395 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001396
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001397/** Get the cipher suite from given inputs.
1398 *
1399 * \param[in] inputs Operation inputs.
1400 * \param[out] cipher_suite Return buffer for role.
1401 *
1402 * \retval #PSA_SUCCESS
1403 * Success.
1404 * \retval #PSA_ERROR_BAD_STATE
1405 * Cipher_suite hasn't been set yet.
1406 */
1407psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1408 const psa_crypto_driver_pake_inputs_t *inputs,
1409 psa_pake_cipher_suite_t *cipher_suite);
1410
Janos Follath702cf092021-05-26 12:58:23 +01001411/** Set the session information for a password-authenticated key exchange.
1412 *
1413 * The sequence of operations to set up a password-authenticated key exchange
1414 * is as follows:
1415 * -# Allocate an operation object which will be passed to all the functions
1416 * listed here.
1417 * -# Initialize the operation object with one of the methods described in the
1418 * documentation for #psa_pake_operation_t, e.g.
1419 * #PSA_PAKE_OPERATION_INIT.
1420 * -# Call psa_pake_setup() to specify the cipher suite.
1421 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1422 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1423 * to be called depends on the algorithm in use.
1424 *
1425 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1426 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1427 * for more information.
1428 *
1429 * A typical sequence of calls to perform a password-authenticated key
1430 * exchange:
1431 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1432 * key share that needs to be sent to the peer.
1433 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1434 * the key share that was received from the peer.
1435 * -# Depending on the algorithm additional calls to psa_pake_output() and
1436 * psa_pake_input() might be necessary.
1437 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1438 *
1439 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1440 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1441 * for more information.
1442 *
1443 * If an error occurs at any step after a call to psa_pake_setup(),
1444 * the operation will need to be reset by a call to psa_pake_abort(). The
1445 * application may call psa_pake_abort() at any time after the operation
1446 * has been initialized.
1447 *
1448 * After a successful call to psa_pake_setup(), the application must
1449 * eventually terminate the operation. The following events terminate an
1450 * operation:
1451 * - A call to psa_pake_abort().
1452 * - A successful call to psa_pake_get_implicit_key().
1453 *
1454 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001455 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001456 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001457 * characterizes a PAKE algorithm and determines
1458 * the algorithm as well.)
1459 *
1460 * \retval #PSA_SUCCESS
1461 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001462 * \retval #PSA_ERROR_INVALID_ARGUMENT
1463 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1464 * PAKE primitive in \p cipher_suite is not compatible with the
1465 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1466 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001467 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001468 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1469 * or the PAKE primitive in \p cipher_suite is not supported or not
1470 * compatible with the PAKE algorithm, or the hash algorithm in
1471 * \p cipher_suite is not supported or not compatible with the PAKE
1472 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1474 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001475 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001476 * The operation state is not valid, or
1477 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001478 * It is implementation-dependent whether a failure to initialize
1479 * results in this error code.
1480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1482 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001483
1484/** Set the password for a password-authenticated key exchange from key ID.
1485 *
1486 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001487 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001488 *
1489 * \param[in,out] operation The operation object to set the password for. It
1490 * must have been set up by psa_pake_setup() and
1491 * not yet in use (neither psa_pake_output() nor
1492 * psa_pake_input() has been called yet). It must
1493 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001494 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001495 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001496 * \param password Identifier of the key holding the password or a
1497 * value derived from the password (eg. by a
1498 * memory-hard function). It must remain valid
1499 * until the operation terminates. It must be of
1500 * type #PSA_KEY_TYPE_PASSWORD or
1501 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1502 * the usage #PSA_KEY_USAGE_DERIVE.
1503 *
1504 * \retval #PSA_SUCCESS
1505 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001506 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001507 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001508 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001509 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1510 * permit the \p operation's algorithm.
1511 * \retval #PSA_ERROR_INVALID_ARGUMENT
1512 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1513 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1514 * the \p operation's cipher suite.
1515 * \retval #PSA_ERROR_NOT_SUPPORTED
1516 * The key type or key size of \p password is not supported with the
1517 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1519 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1520 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1521 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1522 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001523 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001524 * The operation state is not valid (it must have been set up.), or
1525 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001526 * It is implementation-dependent whether a failure to initialize
1527 * results in this error code.
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1530 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001531
Janos Follath702cf092021-05-26 12:58:23 +01001532/** Set the user ID for a password-authenticated key exchange.
1533 *
1534 * Call this function to set the user ID. For PAKE algorithms that associate a
1535 * user identifier with each side of the session you need to call
1536 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1537 * user identifier with the session, call psa_pake_set_user() only.
1538 *
1539 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1540 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1541 * for more information.
1542 *
1543 * \param[in,out] operation The operation object to set the user ID for. It
1544 * must have been set up by psa_pake_setup() and
1545 * not yet in use (neither psa_pake_output() nor
1546 * psa_pake_input() has been called yet). It must
1547 * be on operation for which the user ID hasn't
1548 * been set (psa_pake_set_user() hasn't been
1549 * called yet).
1550 * \param[in] user_id The user ID to authenticate with.
1551 * \param user_id_len Size of the \p user_id buffer in bytes.
1552 *
1553 * \retval #PSA_SUCCESS
1554 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001555 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001556 * \p user_id is not valid for the \p operation's algorithm and cipher
1557 * suite.
1558 * \retval #PSA_ERROR_NOT_SUPPORTED
1559 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001560 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1562 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001563 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001564 * The operation state is not valid, or
1565 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001566 * It is implementation-dependent whether a failure to initialize
1567 * results in this error code.
1568 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1570 const uint8_t *user_id,
1571 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001572
1573/** Set the peer ID for a password-authenticated key exchange.
1574 *
1575 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1576 * that associate a user identifier with each side of the session. For PAKE
1577 * algorithms that associate a single user identifier with the session, call
1578 * psa_pake_set_user() only.
1579 *
1580 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1581 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1582 * for more information.
1583 *
1584 * \param[in,out] operation The operation object to set the peer ID for. It
1585 * must have been set up by psa_pake_setup() and
1586 * not yet in use (neither psa_pake_output() nor
1587 * psa_pake_input() has been called yet). It must
1588 * be on operation for which the peer ID hasn't
1589 * been set (psa_pake_set_peer() hasn't been
1590 * called yet).
1591 * \param[in] peer_id The peer's ID to authenticate.
1592 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1593 *
1594 * \retval #PSA_SUCCESS
1595 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001596 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001597 * \p peer_id is not valid for the \p operation's algorithm and cipher
Neil Armstrong16ff7882022-05-20 11:04:20 +02001598 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001599 * \retval #PSA_ERROR_NOT_SUPPORTED
1600 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001601 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1602 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1603 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001604 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001605 * Calling psa_pake_set_peer() is invalid with the \p operation's
1606 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001607 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001608 * It is implementation-dependent whether a failure to initialize
1609 * results in this error code.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1612 const uint8_t *peer_id,
1613 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001614
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001615/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001616 *
1617 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001618 * It is optional to call this function for PAKEs that don't require a role
1619 * to be specified. For such PAKEs the application role parameter is ignored,
1620 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001621 *
1622 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1623 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1624 * for more information.
1625 *
Neil Armstrongef157512022-05-25 11:49:45 +02001626 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001627 * application's role for. It must have been set up
1628 * by psa_pake_setup() and not yet in use (neither
1629 * psa_pake_output() nor psa_pake_input() has been
1630 * called yet). It must be on operation for which
1631 * the application's role hasn't been specified
1632 * (psa_pake_set_role() hasn't been called yet).
1633 * \param role A value of type ::psa_pake_role_t indicating the
1634 * application's role in the PAKE the algorithm
1635 * that is being set up. For more information see
1636 * the documentation of \c PSA_PAKE_ROLE_XXX
1637 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001638 *
1639 * \retval #PSA_SUCCESS
1640 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001641 * \retval #PSA_ERROR_INVALID_ARGUMENT
1642 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001643 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001644 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001645 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1646 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001647 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001648 * The operation state is not valid, or
1649 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001650 * It is implementation-dependent whether a failure to initialize
1651 * results in this error code.
1652 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1654 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001655
1656/** Get output for a step of a password-authenticated key exchange.
1657 *
1658 * Depending on the algorithm being executed, you might need to call this
1659 * function several times or you might not need to call this at all.
1660 *
1661 * The exact sequence of calls to perform a password-authenticated key
1662 * exchange depends on the algorithm in use. Refer to the documentation of
1663 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1664 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1665 * information.
1666 *
1667 * If this function returns an error status, the operation enters an error
1668 * state and must be aborted by calling psa_pake_abort().
1669 *
1670 * \param[in,out] operation Active PAKE operation.
1671 * \param step The step of the algorithm for which the output is
1672 * requested.
1673 * \param[out] output Buffer where the output is to be written in the
1674 * format appropriate for this \p step. Refer to
1675 * the documentation of the individual
1676 * \c PSA_PAKE_STEP_XXX constants for more
1677 * information.
1678 * \param output_size Size of the \p output buffer in bytes. This must
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001679 * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1680 * primitive, \p output_step) where \c alg and
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001681 * \p primitive are the PAKE algorithm and primitive
1682 * in the operation's cipher suite, and \p step is
1683 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001684 *
1685 * \param[out] output_length On success, the number of bytes of the returned
1686 * output.
1687 *
1688 * \retval #PSA_SUCCESS
1689 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001690 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1691 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001692 * \retval #PSA_ERROR_INVALID_ARGUMENT
1693 * \p step is not compatible with the operation's algorithm.
1694 * \retval #PSA_ERROR_NOT_SUPPORTED
1695 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001696 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1697 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1699 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1700 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1701 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1702 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001703 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001704 * The operation state is not valid (it must be active, and fully set
1705 * up, and this call must conform to the algorithm's requirements
1706 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001707 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001708 * It is implementation-dependent whether a failure to initialize
1709 * results in this error code.
1710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1712 psa_pake_step_t step,
1713 uint8_t *output,
1714 size_t output_size,
1715 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001716
1717/** Provide input for a step of a password-authenticated key exchange.
1718 *
1719 * Depending on the algorithm being executed, you might need to call this
1720 * function several times or you might not need to call this at all.
1721 *
1722 * The exact sequence of calls to perform a password-authenticated key
1723 * exchange depends on the algorithm in use. Refer to the documentation of
1724 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1725 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1726 * information.
1727 *
1728 * If this function returns an error status, the operation enters an error
1729 * state and must be aborted by calling psa_pake_abort().
1730 *
1731 * \param[in,out] operation Active PAKE operation.
1732 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001733 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001734 * appropriate for this \p step. Refer to the
1735 * documentation of the individual
1736 * \c PSA_PAKE_STEP_XXX constants for more
1737 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001738 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001739 *
1740 * \retval #PSA_SUCCESS
1741 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001742 * \retval #PSA_ERROR_INVALID_SIGNATURE
1743 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001744 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurek00b54e62023-05-06 09:38:57 -04001745 * \p input_length is not compatible with the \p operation’s algorithm,
1746 * or the \p input is not valid for the \p operation's algorithm,
1747 * cipher suite or \p step.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001748 * \retval #PSA_ERROR_NOT_SUPPORTED
1749 * \p step p is not supported with the \p operation's algorithm, or the
1750 * \p input is not supported for the \p operation's algorithm, cipher
1751 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1754 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1755 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1756 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1757 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001758 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001759 * The operation state is not valid (it must be active, and fully set
1760 * up, and this call must conform to the algorithm's requirements
1761 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001762 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001763 * It is implementation-dependent whether a failure to initialize
1764 * results in this error code.
1765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1767 psa_pake_step_t step,
1768 const uint8_t *input,
1769 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001770
1771/** Get implicitly confirmed shared secret from a PAKE.
1772 *
1773 * At this point there is a cryptographic guarantee that only the authenticated
1774 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001775 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001776 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001777 * That is, the authentication is only implicit. Since the peer is not
1778 * authenticated yet, no action should be taken yet that assumes that the peer
1779 * is who it claims to be. For example, do not access restricted files on the
1780 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001781 *
1782 * This function can be called after the key exchange phase of the operation
1783 * has completed. It imports the shared secret output of the PAKE into the
1784 * provided derivation operation. The input step
1785 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1786 * material in the key derivation operation.
1787 *
1788 * The exact sequence of calls to perform a password-authenticated key
1789 * exchange depends on the algorithm in use. Refer to the documentation of
1790 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1791 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1792 * information.
1793 *
1794 * When this function returns successfully, \p operation becomes inactive.
1795 * If this function returns an error status, both \p operation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001796 * and \c key_derivation operations enter an error state and must be aborted by
Janos Follath702cf092021-05-26 12:58:23 +01001797 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1798 *
1799 * \param[in,out] operation Active PAKE operation.
1800 * \param[out] output A key derivation operation that is ready
1801 * for an input step of type
1802 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1803 *
1804 * \retval #PSA_SUCCESS
1805 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001806 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001807 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1808 * algorithm in the \p output key derivation operation.
1809 * \retval #PSA_ERROR_NOT_SUPPORTED
1810 * Input from a PAKE is not supported by the algorithm in the \p output
1811 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001812 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1814 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1815 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1816 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1817 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001818 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001819 * The PAKE operation state is not valid (it must be active, but beyond
1820 * that validity is specific to the algorithm), or
1821 * the library has not been previously initialized by psa_crypto_init(),
1822 * or the state of \p output is not valid for
1823 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1824 * step is out of order or the application has done this step already
1825 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001826 * It is implementation-dependent whether a failure to initialize
1827 * results in this error code.
1828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001829psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1830 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001831
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001832/** Abort a PAKE operation.
1833 *
1834 * Aborting an operation frees all associated resources except for the \c
1835 * operation structure itself. Once aborted, the operation object can be reused
1836 * for another operation by calling psa_pake_setup() again.
1837 *
1838 * This function may be called at any time after the operation
1839 * object has been initialized as described in #psa_pake_operation_t.
1840 *
1841 * In particular, calling psa_pake_abort() after the operation has been
1842 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1843 * is safe and has no effect.
1844 *
1845 * \param[in,out] operation The operation to abort.
1846 *
1847 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001848 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1850 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001851 * \retval #PSA_ERROR_BAD_STATE
1852 * The library has not been previously initialized by psa_crypto_init().
1853 * It is implementation-dependent whether a failure to initialize
1854 * results in this error code.
1855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001856psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001857
Janos Follath702cf092021-05-26 12:58:23 +01001858/**@}*/
1859
Janos Follath702cf092021-05-26 12:58:23 +01001860static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001862{
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001864}
1865
1866static inline void psa_pake_cs_set_algorithm(
1867 psa_pake_cipher_suite_t *cipher_suite,
1868 psa_algorithm_t algorithm)
1869{
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001871 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001873 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 }
Janos Follath702cf092021-05-26 12:58:23 +01001875}
1876
1877static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001879{
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1881 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001882}
1883
1884static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 psa_pake_cipher_suite_t *cipher_suite,
1886 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01001887{
1888 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1889 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1890 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1891}
1892
Neil Armstrongff9cac72022-05-20 10:25:15 +02001893static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02001895{
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02001897}
1898
Neil Armstrongd5a48252022-05-20 10:26:36 +02001899static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02001901{
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02001903}
1904
Janos Follath702cf092021-05-26 12:58:23 +01001905static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001907{
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01001909}
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1912 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01001913{
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01001915 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001917 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 }
Janos Follath702cf092021-05-26 12:58:23 +01001919}
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001922{
1923 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001925}
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01001928{
1929 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 return v;
Janos Follath702cf092021-05-26 12:58:23 +01001931}
1932
Gilles Peskinee59236f2018-01-27 23:32:46 +01001933#ifdef __cplusplus
1934}
1935#endif
1936
1937#endif /* PSA_CRYPTO_EXTRA_H */