blob: cbf90ccee7b3368b1b8ffb8b73697efecdf552fb [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto_extra.h
3 *
4 * \brief PSA cryptography module: Mbed TLS vendor extensions
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
9 * This file is reserved for vendor-specific definitions.
Gilles Peskinee59236f2018-01-27 23:32:46 +010010 */
11/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020012 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +010013 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010026 */
27
28#ifndef PSA_CRYPTO_EXTRA_H
29#define PSA_CRYPTO_EXTRA_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020030#include "mbedtls/private_access.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Jaeden Amero81cefed2019-02-25 08:51:27 +000032#include "mbedtls/platform_util.h"
33
Gilles Peskine09c02ee2021-11-25 20:30:47 +010034#include "crypto_types.h"
Gilles Peskine7a894f22019-11-26 16:06:46 +010035#include "crypto_compat.h"
36
Gilles Peskinee59236f2018-01-27 23:32:46 +010037#ifdef __cplusplus
38extern "C" {
39#endif
40
Netanel Gonen2bcd3122018-11-19 11:53:02 +020041/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020042#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020043
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020044/* See mbedtls_config.h for definition */
Steven Cooreman863470a2021-02-15 14:03:19 +010045#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
46#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
Steven Cooreman1f968fd2021-02-15 14:00:24 +010047#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000048
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020049/** \addtogroup attributes
50 * @{
51 */
52
53/** \brief Declare the enrollment algorithm for a key.
54 *
55 * An operation on a key may indifferently use the algorithm set with
56 * psa_set_key_algorithm() or with this function.
57 *
58 * \param[out] attributes The attribute structure to write to.
59 * \param alg2 A second algorithm that the key may be used
60 * for, in addition to the algorithm set with
61 * psa_set_key_algorithm().
62 *
63 * \warning Setting an enrollment algorithm is not recommended, because
64 * using the same key with different algorithms can allow some
65 * attacks based on arithmetic relations between different
66 * computations made with the same key, or can escalate harmless
67 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020068 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020069 * verified that the usage of the key with multiple algorithms
70 * is safe.
71 */
72static inline void psa_set_key_enrollment_algorithm(
73 psa_key_attributes_t *attributes,
74 psa_algorithm_t alg2)
75{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020076 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020077}
78
79/** Retrieve the enrollment algorithm policy from key attributes.
80 *
81 * \param[in] attributes The key attribute structure to query.
82 *
83 * \return The enrollment algorithm stored in the attribute structure.
84 */
85static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
86 const psa_key_attributes_t *attributes)
87{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020089}
90
Gilles Peskinec8000c02019-08-02 20:15:51 +020091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
92
93/** Retrieve the slot number where a key is stored.
94 *
95 * A slot number is only defined for keys that are stored in a secure
96 * element.
97 *
98 * This information is only useful if the secure element is not entirely
99 * managed through the PSA Cryptography API. It is up to the secure
100 * element driver to decide how PSA slot numbers map to any other interface
101 * that the secure element may have.
102 *
103 * \param[in] attributes The key attribute structure to query.
104 * \param[out] slot_number On success, the slot number containing the key.
105 *
106 * \retval #PSA_SUCCESS
107 * The key is located in a secure element, and \p *slot_number
108 * indicates the slot number that contains it.
109 * \retval #PSA_ERROR_NOT_PERMITTED
110 * The caller is not permitted to query the slot number.
111 * Mbed Crypto currently does not return this error.
112 * \retval #PSA_ERROR_INVALID_ARGUMENT
113 * The key is not located in a secure element.
114 */
115psa_status_t psa_get_key_slot_number(
116 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 psa_key_slot_number_t *slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200118
119/** Choose the slot number where a key is stored.
120 *
121 * This function declares a slot number in the specified attribute
122 * structure.
123 *
124 * A slot number is only meaningful for keys that are stored in a secure
125 * element. It is up to the secure element driver to decide how PSA slot
126 * numbers map to any other interface that the secure element may have.
127 *
128 * \note Setting a slot number in key attributes for a key creation can
129 * cause the following errors when creating the key:
130 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
131 * not support choosing a specific slot number.
132 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
133 * choose slot numbers in general or to choose this specific slot.
134 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
135 * valid in general or not valid for this specific key.
136 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
137 * selected slot.
138 *
139 * \param[out] attributes The attribute structure to write to.
140 * \param slot_number The slot number to set.
141 */
142static inline void psa_set_key_slot_number(
143 psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 psa_key_slot_number_t slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +0200145{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200146 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
147 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200148}
149
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200150/** Remove the slot number attribute from a key attribute structure.
151 *
152 * This function undoes the action of psa_set_key_slot_number().
153 *
154 * \param[out] attributes The attribute structure to write to.
155 */
156static inline void psa_clear_key_slot_number(
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 psa_key_attributes_t *attributes)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200158{
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &=
160 ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200161}
162
Gilles Peskined7729582019-08-05 15:55:54 +0200163/** Register a key that is already present in a secure element.
164 *
165 * The key must be located in a secure element designated by the
166 * lifetime field in \p attributes, in the slot set with
167 * psa_set_key_slot_number() in the attribute structure.
168 * This function makes the key available through the key identifier
169 * specified in \p attributes.
170 *
171 * \param[in] attributes The attributes of the existing key.
172 *
173 * \retval #PSA_SUCCESS
174 * The key was successfully registered.
175 * Note that depending on the design of the driver, this may or may
176 * not guarantee that a key actually exists in the designated slot
177 * and is compatible with the specified attributes.
178 * \retval #PSA_ERROR_ALREADY_EXISTS
179 * There is already a key with the identifier specified in
180 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200181 * \retval #PSA_ERROR_NOT_SUPPORTED
182 * The secure element driver for the specified lifetime does not
183 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200184 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200185 * The identifier in \p attributes is invalid, namely the identifier is
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500186 * not in the user range, or
Gilles Peskined7729582019-08-05 15:55:54 +0200187 * \p attributes specifies a lifetime which is not located
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500188 * in a secure element, or no slot number is specified in \p attributes,
Gilles Peskined7729582019-08-05 15:55:54 +0200189 * or the specified slot number is not valid.
190 * \retval #PSA_ERROR_NOT_PERMITTED
191 * The caller is not authorized to register the specified key slot.
Gilles Peskineed733552023-02-14 19:21:09 +0100192 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
193 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
194 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
195 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
196 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
197 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskined7729582019-08-05 15:55:54 +0200198 * \retval #PSA_ERROR_BAD_STATE
199 * The library has not been previously initialized by psa_crypto_init().
200 * It is implementation-dependent whether a failure to initialize
201 * results in this error code.
202 */
203psa_status_t mbedtls_psa_register_se_key(
204 const psa_key_attributes_t *attributes);
205
Gilles Peskinec8000c02019-08-02 20:15:51 +0200206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
207
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200208/**@}*/
209
Gilles Peskinee59236f2018-01-27 23:32:46 +0100210/**
211 * \brief Library deinitialization.
212 *
213 * This function clears all data associated with the PSA layer,
214 * including the whole key store.
215 *
216 * This is an Mbed TLS extension.
217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100219
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200220/** \brief Statistics about
221 * resource consumption related to the PSA keystore.
222 *
223 * \note The content of this structure is not part of the stable API and ABI
224 * of Mbed Crypto and may change arbitrarily from version to version.
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200227 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200228 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200229 /** Number of slots containing key material for a key which is in
230 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200231 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200232 /** Number of slots containing a reference to a key in a
233 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200234 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200235 /** Number of slots which are occupied, but do not contain
236 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200237 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200238 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200239 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200240 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200241 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100242 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200243 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200244 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200245 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200246 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200247 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200248} mbedtls_psa_stats_t;
249
250/** \brief Get statistics about
251 * resource consumption related to the PSA keystore.
252 *
253 * \note When Mbed Crypto is built as part of a service, with isolation
254 * between the application and the keystore, the service may or
255 * may not expose this function.
256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200258
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200259/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100260 * \brief Inject an initial entropy seed for the random generator into
261 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100262 *
263 * This function injects data to be used as a seed for the random generator
264 * used by the PSA Crypto implementation. On devices that lack a trusted
265 * entropy source (preferably a hardware random number generator),
266 * the Mbed PSA Crypto implementation uses this value to seed its
267 * random generator.
268 *
269 * On devices without a trusted entropy source, this function must be
270 * called exactly once in the lifetime of the device. On devices with
271 * a trusted entropy source, calling this function is optional.
272 * In all cases, this function may only be called before calling any
273 * other function in the PSA Crypto API, including psa_crypto_init().
274 *
275 * When this function returns successfully, it populates a file in
276 * persistent storage. Once the file has been created, this function
277 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100278 *
279 * If any error occurs, this function does not change the system state.
280 * You can call this function again after correcting the reason for the
281 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200282 *
283 * \warning This function **can** fail! Callers MUST check the return status.
284 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100285 * \warning If you use this function, you should use it as part of a
286 * factory provisioning process. The value of the injected seed
287 * is critical to the security of the device. It must be
288 * *secret*, *unpredictable* and (statistically) *unique per device*.
289 * You should be generate it randomly using a cryptographically
290 * secure random generator seeded from trusted entropy sources.
291 * You should transmit it securely to the device and ensure
292 * that its value is not leaked or stored anywhere beyond the
293 * needs of transmitting it from the point of generation to
294 * the call of this function, and erase all copies of the value
295 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200296 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100297 * This is an Mbed TLS extension.
298 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200299 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100300 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
301 * Note that you must provide compatible implementations of
302 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200303 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200304 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200305 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200306 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200307 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000308 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
309 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
310 * in `library/entropy_poll.h` in the Mbed TLS source
311 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200312 * It must be less or equal to
313 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200314 *
315 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100316 * The seed value was injected successfully. The random generator
317 * of the PSA Crypto implementation is now ready for use.
318 * You may now call psa_crypto_init() and use the PSA Crypto
319 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200320 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100321 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200322 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100323 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200324 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100325 * The library has already been initialized. It is no longer
326 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200327 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100328psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200329 size_t seed_size);
330
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200331/** \addtogroup crypto_types
332 * @{
333 */
334
Gilles Peskinea1302192019-05-16 13:58:24 +0200335/** DSA public key.
336 *
337 * The import and export format is the
338 * representation of the public key `y = g^x mod p` as a big-endian byte
339 * string. The length of the byte string is the length of the base prime `p`
340 * in bytes.
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200343
344/** DSA key pair (private and public key).
345 *
346 * The import and export format is the
347 * representation of the private key `x` as a big-endian byte string. The
348 * length of the byte string is the private key size in bytes (leading zeroes
349 * are not stripped).
350 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800351 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200352 * FIPS 186-4 §B.1.2: interpret the byte string as integer
353 * in big-endian order. Discard it if it is not in the range
354 * [0, *N* - 2] where *N* is the boundary of the private key domain
355 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
356 * or the order of the curve's base point for ECC).
357 * Add 1 to the resulting integer and use this as the private key *x*.
358 *
359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200361
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100362/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200363#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200364 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200367/** DSA signature with hashing.
368 *
369 * This is the signature scheme defined by FIPS 186-4,
370 * with a random per-message secret number (*k*).
371 *
372 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
373 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
374 * This includes #PSA_ALG_ANY_HASH
375 * when specifying the algorithm in a usage policy.
376 *
377 * \return The corresponding DSA signature algorithm.
378 * \return Unspecified if \p hash_alg is not a supported
379 * hash algorithm.
380 */
381#define PSA_ALG_DSA(hash_alg) \
382 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100383#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100384#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200385/** Deterministic DSA signature with hashing.
386 *
387 * This is the deterministic variant defined by RFC 6979 of
388 * the signature scheme defined by FIPS 186-4.
389 *
390 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
391 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
392 * This includes #PSA_ALG_ANY_HASH
393 * when specifying the algorithm in a usage policy.
394 *
395 * \return The corresponding DSA signature algorithm.
396 * \return Unspecified if \p hash_alg is not a supported
397 * hash algorithm.
398 */
399#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
400 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
401#define PSA_ALG_IS_DSA(alg) \
402 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
403 PSA_ALG_DSA_BASE)
404#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
405 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
406#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
407 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
408#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
409 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
410
411
412/* We need to expand the sample definition of this macro from
413 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100414#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
415#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
416 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200417
418/**@}*/
419
Gilles Peskine24f10f82019-05-16 12:18:32 +0200420/** \addtogroup attributes
421 * @{
422 */
423
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200424/** Custom Diffie-Hellman group.
425 *
Paul Elliott75e27032020-06-03 15:17:39 +0100426 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
427 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200428 * from domain parameters set by psa_set_key_domain_parameters().
429 */
Paul Elliott75e27032020-06-03 15:17:39 +0100430#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200431
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100432/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100433#define PSA_PAKE_OPERATION_STAGE_SETUP 0
434#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
435#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200436
Przemek Stekiele9254a02023-03-01 11:18:09 +0100437/** JPAKE user/peer ids. */
Przemek Stekiel26c909d2023-02-28 12:34:03 +0100438#define PSA_JPAKE_SERVER_ID "server"
439#define PSA_JPAKE_CLIENT_ID "client"
Gilles Peskine24f10f82019-05-16 12:18:32 +0200440/**
441 * \brief Set domain parameters for a key.
442 *
443 * Some key types require additional domain parameters in addition to
444 * the key type identifier and the key size. Use this function instead
445 * of psa_set_key_type() when you need to specify domain parameters.
446 *
447 * The format for the required domain parameters varies based on the key type.
448 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200449 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200450 * the domain parameter data consists of the public exponent,
451 * represented as a big-endian integer with no leading zeros.
452 * This information is used when generating an RSA key pair.
453 * When importing a key, the public exponent is read from the imported
454 * key data and the exponent recorded in the attribute structure is ignored.
455 * As an exception, the public exponent 65537 is represented by an empty
456 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200457 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400458 * the `Dss-Params` format as defined by RFC 3279 §2.3.2.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200459 * ```
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400460 * Dss-Params ::= SEQUENCE {
Gilles Peskine24f10f82019-05-16 12:18:32 +0200461 * p INTEGER,
462 * q INTEGER,
463 * g INTEGER
464 * }
465 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200466 * - For Diffie-Hellman key exchange keys
Paul Elliott75e27032020-06-03 15:17:39 +0100467 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
468 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200469 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
470 * ```
471 * DomainParameters ::= SEQUENCE {
472 * p INTEGER, -- odd prime, p=jq +1
473 * g INTEGER, -- generator, g
474 * q INTEGER, -- factor of p-1
475 * j INTEGER OPTIONAL, -- subgroup factor
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400476 * validationParams ValidationParams OPTIONAL
Gilles Peskine24f10f82019-05-16 12:18:32 +0200477 * }
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400478 * ValidationParams ::= SEQUENCE {
Gilles Peskine24f10f82019-05-16 12:18:32 +0200479 * seed BIT STRING,
480 * pgenCounter INTEGER
481 * }
482 * ```
483 *
484 * \note This function may allocate memory or other resources.
485 * Once you have called this function on an attribute structure,
486 * you must call psa_reset_key_attributes() to free these resources.
487 *
488 * \note This is an experimental extension to the interface. It may change
489 * in future versions of the library.
490 *
491 * \param[in,out] attributes Attribute structure where the specified domain
492 * parameters will be stored.
493 * If this function fails, the content of
494 * \p attributes is not modified.
495 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
496 * \param[in] data Buffer containing the key domain parameters.
497 * The content of this buffer is interpreted
498 * according to \p type as described above.
499 * \param data_length Size of the \p data buffer in bytes.
500 *
Gilles Peskineed733552023-02-14 19:21:09 +0100501 * \retval #PSA_SUCCESS \emptydescription
502 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
503 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200505 */
506psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
507 psa_key_type_t type,
508 const uint8_t *data,
509 size_t data_length);
510
511/**
512 * \brief Get domain parameters for a key.
513 *
514 * Get the domain parameters for a key with this function, if any. The format
515 * of the domain parameters written to \p data is specified in the
516 * documentation for psa_set_key_domain_parameters().
517 *
518 * \note This is an experimental extension to the interface. It may change
519 * in future versions of the library.
520 *
521 * \param[in] attributes The key attribute structure to query.
522 * \param[out] data On success, the key domain parameters.
523 * \param data_size Size of the \p data buffer in bytes.
524 * The buffer is guaranteed to be large
525 * enough if its size in bytes is at least
526 * the value given by
527 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
528 * \param[out] data_length On success, the number of bytes
529 * that make up the key domain parameters data.
530 *
Gilles Peskineed733552023-02-14 19:21:09 +0100531 * \retval #PSA_SUCCESS \emptydescription
532 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200533 */
534psa_status_t psa_get_key_domain_parameters(
535 const psa_key_attributes_t *attributes,
536 uint8_t *data,
537 size_t data_size,
538 size_t *data_length);
539
540/** Safe output buffer size for psa_get_key_domain_parameters().
541 *
542 * This macro returns a compile-time constant if its arguments are
543 * compile-time constants.
544 *
545 * \warning This function may call its arguments multiple times or
546 * zero times, so you should not pass arguments that contain
547 * side effects.
548 *
549 * \note This is an experimental extension to the interface. It may change
550 * in future versions of the library.
551 *
552 * \param key_type A supported key type.
553 * \param key_bits The size of the key in bits.
554 *
555 * \return If the parameters are valid and supported, return
556 * a buffer size in bytes that guarantees that
557 * psa_get_key_domain_parameters() will not fail with
558 * #PSA_ERROR_BUFFER_TOO_SMALL.
559 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200560 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200561 * sensible size or 0.
562 * If the parameters are not valid, the
563 * return value is unspecified.
564 */
565#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
566 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
567 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
568 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
569 0)
570#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
571 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
572#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
573 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
574
575/**@}*/
576
Gilles Peskine5055b232019-12-12 17:49:31 +0100577/** \defgroup psa_tls_helpers TLS helper functions
578 * @{
579 */
580
581#if defined(MBEDTLS_ECP_C)
582#include <mbedtls/ecp.h>
583
584/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
585 *
586 * \note This function is provided solely for the convenience of
587 * Mbed TLS and may be removed at any time without notice.
588 *
589 * \param grpid An Mbed TLS elliptic curve identifier
590 * (`MBEDTLS_ECP_DP_xxx`).
591 * \param[out] bits On success, the bit size of the curve.
592 *
593 * \return The corresponding PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100594 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100595 * \return \c 0 on failure (\p grpid is not recognized).
596 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597static inline psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
598 size_t *bits)
Darryl Green2f0eb512020-04-24 15:21:14 +0100599{
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 switch (grpid) {
Darryl Green2f0eb512020-04-24 15:21:14 +0100601 case MBEDTLS_ECP_DP_SECP192R1:
602 *bits = 192;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return PSA_ECC_FAMILY_SECP_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100604 case MBEDTLS_ECP_DP_SECP224R1:
605 *bits = 224;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return PSA_ECC_FAMILY_SECP_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100607 case MBEDTLS_ECP_DP_SECP256R1:
608 *bits = 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return PSA_ECC_FAMILY_SECP_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100610 case MBEDTLS_ECP_DP_SECP384R1:
611 *bits = 384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return PSA_ECC_FAMILY_SECP_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100613 case MBEDTLS_ECP_DP_SECP521R1:
614 *bits = 521;
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return PSA_ECC_FAMILY_SECP_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100616 case MBEDTLS_ECP_DP_BP256R1:
617 *bits = 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100619 case MBEDTLS_ECP_DP_BP384R1:
620 *bits = 384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100622 case MBEDTLS_ECP_DP_BP512R1:
623 *bits = 512;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100625 case MBEDTLS_ECP_DP_CURVE25519:
626 *bits = 255;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return PSA_ECC_FAMILY_MONTGOMERY;
Darryl Green2f0eb512020-04-24 15:21:14 +0100628 case MBEDTLS_ECP_DP_SECP192K1:
629 *bits = 192;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return PSA_ECC_FAMILY_SECP_K1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100631 case MBEDTLS_ECP_DP_SECP224K1:
632 *bits = 224;
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return PSA_ECC_FAMILY_SECP_K1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100634 case MBEDTLS_ECP_DP_SECP256K1:
635 *bits = 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return PSA_ECC_FAMILY_SECP_K1;
Darryl Green2f0eb512020-04-24 15:21:14 +0100637 case MBEDTLS_ECP_DP_CURVE448:
638 *bits = 448;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_ECC_FAMILY_MONTGOMERY;
Darryl Green2f0eb512020-04-24 15:21:14 +0100640 default:
641 *bits = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return 0;
Darryl Green2f0eb512020-04-24 15:21:14 +0100643 }
644}
Gilles Peskine5055b232019-12-12 17:49:31 +0100645
646/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
647 *
648 * \note This function is provided solely for the convenience of
649 * Mbed TLS and may be removed at any time without notice.
650 *
651 * \param curve A PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100652 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100653 * \param bits The bit-length of a private key on \p curve.
654 * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
655 * to the nearest multiple of 8. This allows the caller
656 * to infer the exact curve from the length of a key
657 * which is supplied as a byte string.
Gilles Peskine5055b232019-12-12 17:49:31 +0100658 *
659 * \return The corresponding Mbed TLS elliptic curve identifier
660 * (`MBEDTLS_ECP_DP_xxx`).
661 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100662 * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
Gilles Peskine5055b232019-12-12 17:49:31 +0100663 * correct for \p curve.
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
666 size_t bits,
667 int bits_is_sloppy);
Gilles Peskine5055b232019-12-12 17:49:31 +0100668#endif /* MBEDTLS_ECP_C */
669
670/**@}*/
671
Gilles Peskineb8af2282020-11-13 18:00:34 +0100672/** \defgroup psa_external_rng External random generator
673 * @{
674 */
675
676#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
677/** External random generator function, implemented by the platform.
678 *
679 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
680 * this function replaces Mbed TLS's entropy and DRBG modules for all
681 * random generation triggered via PSA crypto interfaces.
682 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100683 * \note This random generator must deliver random numbers with cryptographic
684 * quality and high performance. It must supply unpredictable numbers
685 * with a uniform distribution. The implementation of this function
686 * is responsible for ensuring that the random generator is seeded
687 * with sufficient entropy. If you have a hardware TRNG which is slow
688 * or delivers non-uniform output, declare it as an entropy source
689 * with mbedtls_entropy_add_source() instead of enabling this option.
690 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100691 * \param[in,out] context Pointer to the random generator context.
692 * This is all-bits-zero on the first call
693 * and preserved between successive calls.
694 * \param[out] output Output buffer. On success, this buffer
695 * contains random data with a uniform
696 * distribution.
697 * \param output_size The size of the \p output buffer in bytes.
698 * \param[out] output_length On success, set this value to \p output_size.
699 *
700 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100701 * Success. The output buffer contains \p output_size bytes of
702 * cryptographic-quality random data, and \c *output_length is
703 * set to \p output_size.
704 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
705 * The random generator requires extra entropy and there is no
706 * way to obtain entropy under current environment conditions.
707 * This error should not happen under normal circumstances since
708 * this function is responsible for obtaining as much entropy as
709 * it needs. However implementations of this function may return
710 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
711 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100713 * A failure of the random generator hardware that isn't covered
714 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100715 */
716psa_status_t mbedtls_psa_external_get_random(
717 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100719#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
720
721/**@}*/
722
Steven Cooreman6801f082021-02-19 17:21:22 +0100723/** \defgroup psa_builtin_keys Built-in keys
724 * @{
725 */
726
727/** The minimum value for a key identifier that is built into the
728 * implementation.
729 *
730 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
731 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
732 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
733 * with any other set of implementation-chosen key identifiers.
734 *
735 * This value is part of the library's ABI since changing it would invalidate
736 * the values of built-in key identifiers in applications.
737 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100739
740/** The maximum value for a key identifier that is built into the
741 * implementation.
742 *
743 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
744 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100746
747/** A slot number identifying a key in a driver.
748 *
749 * Values of this type are used to identify built-in keys.
750 */
751typedef uint64_t psa_drv_slot_number_t;
752
753#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
754/** Test whether a key identifier belongs to the builtin key range.
755 *
756 * \param key_id Key identifier to test.
757 *
758 * \retval 1
759 * The key identifier is a builtin key identifier.
760 * \retval 0
761 * The key identifier is not a builtin key identifier.
762 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100764{
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
766 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100767}
768
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200769/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100770 *
771 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100772 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100773 * as part of a platform's system image.
774 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100775 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100776 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
777 *
778 * In a multi-application configuration
779 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
780 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
781 * is allowed to use the given key.
782 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100783 * \param key_id The key ID for which to retrieve the
784 * location and slot attributes.
785 * \param[out] lifetime On success, the lifetime associated with the key
786 * corresponding to \p key_id. Lifetime is a
787 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200788 * and with what persistence level the key is
789 * intended to be used. If the platform
790 * implementation does not contain specific
791 * information about the intended key persistence
792 * level, the persistence level may be reported as
793 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100794 * \param[out] slot_number On success, the slot number known to the driver
795 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200796 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100797 * requested built-in key.
798 *
799 * \retval #PSA_SUCCESS
800 * The requested key identifier designates a built-in key.
801 * In a multi-application configuration, the requested owner
802 * is allowed to access it.
803 * \retval #PSA_ERROR_DOES_NOT_EXIST
804 * The requested key identifier is not a built-in key which is known
805 * to this function. If a key exists in the key storage with this
806 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100807 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100808 * Any other error is propagated to the function that requested the key.
809 * Common errors include:
810 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
811 * is not allowed to access it.
812 */
813psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100814 mbedtls_svc_key_id_t key_id,
815 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100817#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
818
819/** @} */
820
Janos Follath702cf092021-05-26 12:58:23 +0100821/** \addtogroup crypto_types
822 * @{
823 */
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100826
827/** Whether the specified algorithm is a password-authenticated key exchange.
828 *
829 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
830 *
831 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
832 * algorithm, 0 otherwise.
833 * This macro may return either 0 or 1 if \p alg is not a supported
834 * algorithm identifier.
835 */
836#define PSA_ALG_IS_PAKE(alg) \
837 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
838
839/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
840 *
841 * This is J-PAKE as defined by RFC 8236, instantiated with the following
842 * parameters:
843 *
844 * - The group can be either an elliptic curve or defined over a finite field.
845 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
846 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100847 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100848 *
Janos Follath46c02372021-06-08 15:22:51 +0100849 * To select these parameters and set up the cipher suite, call these functions
850 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100851 *
852 * \code
853 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
854 * psa_pake_cs_set_primitive(cipher_suite,
855 * PSA_PAKE_PRIMITIVE(type, family, bits));
856 * psa_pake_cs_set_hash(cipher_suite, hash);
857 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100858 *
859 * For more information on how to set a specific curve or field, refer to the
860 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
861 *
862 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100863 *
864 * \code
865 * psa_pake_setup(operation, cipher_suite);
866 * psa_pake_set_user(operation, ...);
867 * psa_pake_set_peer(operation, ...);
868 * psa_pake_set_password_key(operation, ...);
869 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100870 *
Neil Armstrong16145372022-05-20 10:42:36 +0200871 * The password is provided as a key. This can be the password text itself,
872 * in an agreed character encoding, or some value derived from the password
873 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100874 *
Neil Armstrong16145372022-05-20 10:42:36 +0200875 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100876 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
877 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
878 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200879 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100880 * of the reduction is 0.)
881 *
882 * The key exchange flow for J-PAKE is as follows:
883 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100884 * \code
885 * // Get g1
886 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
887 * // Get the ZKP public key for x1
888 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
889 * // Get the ZKP proof for x1
890 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
891 * // Get g2
892 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
893 * // Get the ZKP public key for x2
894 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
895 * // Get the ZKP proof for x2
896 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
897 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100898 * -# To provide the first round data received from the peer to the operation,
899 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100900 * \code
901 * // Set g3
902 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
903 * // Set the ZKP public key for x3
904 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
905 * // Set the ZKP proof for x3
906 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
907 * // Set g4
908 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
909 * // Set the ZKP public key for x4
910 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
911 * // Set the ZKP proof for x4
912 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
913 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100914 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100915 * \code
916 * // Get A
917 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
918 * // Get ZKP public key for x2*s
919 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
920 * // Get ZKP proof for x2*s
921 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
922 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100923 * -# To provide the second round data received from the peer to the operation,
924 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100925 * \code
926 * // Set B
927 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
928 * // Set ZKP public key for x4*s
929 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
930 * // Set ZKP proof for x4*s
931 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
932 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100933 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100934 * \code
935 * // Get Ka=Kb=K
936 * psa_pake_get_implicit_key()
937 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100938 *
939 * For more information consult the documentation of the individual
940 * \c PSA_PAKE_STEP_XXX constants.
941 *
942 * At this point there is a cryptographic guarantee that only the authenticated
943 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100944 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100945 *
946 * That is, the authentication is only implicit (the peer is not authenticated
947 * at this point, and no action should be taken that assume that they are - like
948 * for example accessing restricted files).
949 *
950 * To make the authentication explicit there are various methods, see Section 5
951 * of RFC 8236 for two examples.
952 *
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100955
956/** @} */
957
958/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100959 *
960 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
961 * the official PSA Crypto API yet.
962 *
963 * \note The content of this section is not part of the stable API and ABI
964 * of Mbed Crypto and may change arbitrarily from version to version.
965 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
966 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100967 * @{
968 */
969
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200970/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100971 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200972 * Encodes the application's role in the algorithm is being executed. For more
973 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
974 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100975 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200976typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100977
978/** Encoding of input and output indicators for PAKE.
979 *
980 * Some PAKE algorithms need to exchange more data than just a single key share.
981 * This type is for encoding additional input and output data for such
982 * algorithms.
983 */
984typedef uint8_t psa_pake_step_t;
985
986/** Encoding of the type of the PAKE's primitive.
987 *
988 * Values defined by this standard will never be in the range 0x80-0xff.
989 * Vendors who define additional types must use an encoding in this range.
990 *
991 * For more information see the documentation of individual
992 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
993 */
994typedef uint8_t psa_pake_primitive_type_t;
995
996/** \brief Encoding of the family of the primitive associated with the PAKE.
997 *
998 * For more information see the documentation of individual
999 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
1000 */
1001typedef uint8_t psa_pake_family_t;
1002
1003/** \brief Encoding of the primitive associated with the PAKE.
1004 *
1005 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
1006 */
1007typedef uint32_t psa_pake_primitive_t;
1008
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001009/** A value to indicate no role in a PAKE algorithm.
1010 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
1011 * algorithms which do not assign roles.
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001014
Janos Follath702cf092021-05-26 12:58:23 +01001015/** The first peer in a balanced PAKE.
1016 *
1017 * Although balanced PAKE algorithms are symmetric, some of them needs an
1018 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001019 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +01001020 * accepted.
1021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +01001023
1024/** The second peer in a balanced PAKE.
1025 *
1026 * Although balanced PAKE algorithms are symmetric, some of them needs an
1027 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001028 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +01001029 * accepted.
1030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001032
1033/** The client in an augmented PAKE.
1034 *
1035 * Augmented PAKE algorithms need to differentiate between client and server.
1036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +01001038
1039/** The server in an augmented PAKE.
1040 *
1041 * Augmented PAKE algorithms need to differentiate between client and server.
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +01001044
1045/** The PAKE primitive type indicating the use of elliptic curves.
1046 *
1047 * The values of the \c family and \c bits fields of the cipher suite identify a
1048 * specific elliptic curve, using the same mapping that is used for ECC
1049 * (::psa_ecc_family_t) keys.
1050 *
1051 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1052 * \c bits means the value returned by psa_pake_cs_get_bits().)
1053 *
1054 * Input and output during the operation can involve group elements and scalar
1055 * values:
1056 * -# The format for group elements is the same as for public keys on the
1057 * specific curve would be. For more information, consult the documentation of
1058 * psa_export_public_key().
1059 * -# The format for scalars is the same as for private keys on the specific
1060 * curve would be. For more information, consult the documentation of
1061 * psa_export_key().
1062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +01001064
1065/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
1066 *
1067 * The values of the \c family and \c bits fields of the cipher suite identify
1068 * a specific Diffie-Hellman group, using the same mapping that is used for
1069 * Diffie-Hellman (::psa_dh_family_t) keys.
1070 *
1071 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1072 * \c bits means the value returned by psa_pake_cs_get_bits().)
1073 *
1074 * Input and output during the operation can involve group elements and scalar
1075 * values:
1076 * -# The format for group elements is the same as for public keys on the
1077 * specific group would be. For more information, consult the documentation of
1078 * psa_export_public_key().
1079 * -# The format for scalars is the same as for private keys on the specific
1080 * group would be. For more information, consult the documentation of
1081 * psa_export_key().
1082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001084
1085/** Construct a PAKE primitive from type, family and bit-size.
1086 *
1087 * \param pake_type The type of the primitive
1088 * (value of type ::psa_pake_primitive_type_t).
1089 * \param pake_family The family of the primitive
1090 * (the type and interpretation of this parameter depends
1091 * on \p type, for more information consult the
1092 * documentation of individual ::psa_pake_primitive_type_t
1093 * constants).
1094 * \param pake_bits The bit-size of the primitive
1095 * (Value of type \c size_t. The interpretation
1096 * of this parameter depends on \p family, for more
1097 * information consult the documentation of individual
1098 * ::psa_pake_primitive_type_t constants).
1099 *
1100 * \return The constructed primitive value of type ::psa_pake_primitive_t.
1101 * Return 0 if the requested primitive can't be encoded as
1102 * ::psa_pake_primitive_t.
1103 */
1104#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
1105 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
1106 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +01001108
1109/** The key share being sent to or received from the peer.
1110 *
1111 * The format for both input and output at this step is the same as for public
1112 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1113 * be.
1114 *
1115 * For more information on the format, consult the documentation of
1116 * psa_export_public_key().
1117 *
1118 * For information regarding how the group is determined, consult the
1119 * documentation #PSA_PAKE_PRIMITIVE.
1120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +01001122
1123/** A Schnorr NIZKP public key.
1124 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001125 * This is the ephemeral public key in the Schnorr Non-Interactive
1126 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1127 *
Janos Follath702cf092021-05-26 12:58:23 +01001128 * The format for both input and output at this step is the same as for public
1129 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1130 * be.
1131 *
1132 * For more information on the format, consult the documentation of
1133 * psa_export_public_key().
1134 *
1135 * For information regarding how the group is determined, consult the
1136 * documentation #PSA_PAKE_PRIMITIVE.
1137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001139
1140/** A Schnorr NIZKP proof.
1141 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001142 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1143 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +01001144 *
Janos Follath1f013182021-06-08 15:30:48 +01001145 * Both for input and output, the value at this step is an integer less than
1146 * the order of the group selected in the cipher suite. The format depends on
1147 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +01001148 *
Janos Follath1f013182021-06-08 15:30:48 +01001149 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +01001150 * - For everything else the encoding is big endian (see Section 2.3.8 of
1151 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +01001152 *
Janos Follath1f013182021-06-08 15:30:48 +01001153 * In both cases leading zeroes are allowed as long as the length in bytes does
1154 * not exceed the byte length of the group order.
1155 *
Janos Follath702cf092021-05-26 12:58:23 +01001156 * For information regarding how the group is determined, consult the
1157 * documentation #PSA_PAKE_PRIMITIVE.
1158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +01001160
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001161/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +01001162 *
1163 * This is an implementation-defined \c struct. Applications should not
1164 * make any assumptions about the content of this structure.
1165 * Implementation details can change in future versions without notice.
1166 */
1167typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1168
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001169/** Return an initial value for a PAKE cipher suite object.
1170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001172
Janos Follath702cf092021-05-26 12:58:23 +01001173/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1174 *
Janos Follath702cf092021-05-26 12:58:23 +01001175 * \param[in] cipher_suite The cipher suite structure to query.
1176 *
1177 * \return The PAKE algorithm stored in the cipher suite structure.
1178 */
1179static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001181
1182/** Declare the PAKE algorithm for the cipher suite.
1183 *
1184 * This function overwrites any PAKE algorithm
1185 * previously set in \p cipher_suite.
1186 *
Janos Follath702cf092021-05-26 12:58:23 +01001187 * \param[out] cipher_suite The cipher suite structure to write to.
1188 * \param algorithm The PAKE algorithm to write.
1189 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1190 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1191 * If this is 0, the PAKE algorithm in
1192 * \p cipher_suite becomes unspecified.
1193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1195 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +01001196
1197/** Retrieve the primitive from a PAKE cipher suite.
1198 *
Janos Follath702cf092021-05-26 12:58:23 +01001199 * \param[in] cipher_suite The cipher suite structure to query.
1200 *
1201 * \return The primitive stored in the cipher suite structure.
1202 */
1203static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001205
1206/** Declare the primitive for a PAKE cipher suite.
1207 *
1208 * This function overwrites any primitive previously set in \p cipher_suite.
1209 *
Janos Follath702cf092021-05-26 12:58:23 +01001210 * \param[out] cipher_suite The cipher suite structure to write to.
1211 * \param primitive The primitive to write. If this is 0, the
1212 * primitive type in \p cipher_suite becomes
1213 * unspecified.
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1216 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +01001217
Neil Armstrongff9cac72022-05-20 10:25:15 +02001218/** Retrieve the PAKE family from a PAKE cipher suite.
1219 *
Neil Armstrongff9cac72022-05-20 10:25:15 +02001220 * \param[in] cipher_suite The cipher suite structure to query.
1221 *
1222 * \return The PAKE family stored in the cipher suite structure.
1223 */
1224static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +02001226
Neil Armstrongd5a48252022-05-20 10:26:36 +02001227/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1228 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001229 * \param[in] cipher_suite The cipher suite structure to query.
1230 *
1231 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1232 */
1233static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001235
Janos Follath702cf092021-05-26 12:58:23 +01001236/** Retrieve the hash algorithm from a PAKE cipher suite.
1237 *
Janos Follath702cf092021-05-26 12:58:23 +01001238 * \param[in] cipher_suite The cipher suite structure to query.
1239 *
1240 * \return The hash algorithm stored in the cipher suite structure. The return
1241 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1242 * the hash algorithm is not set.
1243 */
1244static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001246
1247/** Declare the hash algorithm for a PAKE cipher suite.
1248 *
1249 * This function overwrites any hash algorithm
1250 * previously set in \p cipher_suite.
1251 *
Janos Follath702cf092021-05-26 12:58:23 +01001252 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1253 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1254 * for more information.
1255 *
1256 * \param[out] cipher_suite The cipher suite structure to write to.
1257 * \param hash The hash involved in the cipher suite.
1258 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1259 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1260 * If this is 0, the hash algorithm in
1261 * \p cipher_suite becomes unspecified.
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1264 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001265
1266/** The type of the state data structure for PAKE operations.
1267 *
1268 * Before calling any function on a PAKE operation object, the application
1269 * must initialize it by any of the following means:
1270 * - Set the structure to all-bits-zero, for example:
1271 * \code
1272 * psa_pake_operation_t operation;
1273 * memset(&operation, 0, sizeof(operation));
1274 * \endcode
1275 * - Initialize the structure to logical zero values, for example:
1276 * \code
1277 * psa_pake_operation_t operation = {0};
1278 * \endcode
1279 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1280 * for example:
1281 * \code
1282 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1283 * \endcode
1284 * - Assign the result of the function psa_pake_operation_init()
1285 * to the structure, for example:
1286 * \code
1287 * psa_pake_operation_t operation;
1288 * operation = psa_pake_operation_init();
1289 * \endcode
1290 *
1291 * This is an implementation-defined \c struct. Applications should not
1292 * make any assumptions about the content of this structure.
1293 * Implementation details can change in future versions without notice. */
1294typedef struct psa_pake_operation_s psa_pake_operation_t;
1295
Przemek Stekiel51eac532022-12-07 11:04:51 +01001296/** The type of input values for PAKE operations. */
1297typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1298
Przemek Stekielb09c4872023-01-17 12:05:38 +01001299/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001300typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1301
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001302/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001305
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001306/** Get the lengths of the password in bytes from given inputs.
1307 *
1308 * \param[in] inputs Operation inputs.
1309 * \param[out] password_len Return buffer for password length.
1310 *
1311 * \retval #PSA_SUCCESS
1312 * Success.
1313 * \retval #PSA_ERROR_BAD_STATE
1314 * Password hasn't been set yet.
1315 */
1316psa_status_t psa_crypto_driver_pake_get_password_len(
1317 const psa_crypto_driver_pake_inputs_t *inputs,
1318 size_t *password_len);
1319
1320/** Get the password from given inputs.
1321 *
1322 * \param[in] inputs Operation inputs.
1323 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001324 * \param buffer_size Size of the return buffer in bytes.
1325 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001326 *
1327 * \retval #PSA_SUCCESS
1328 * Success.
1329 * \retval #PSA_ERROR_BAD_STATE
1330 * Password hasn't been set yet.
1331 */
1332psa_status_t psa_crypto_driver_pake_get_password(
1333 const psa_crypto_driver_pake_inputs_t *inputs,
1334 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1335
1336/** Get the role from given inputs.
1337 *
1338 * \param[in] inputs Operation inputs.
1339 * \param[out] role Return buffer for role.
1340 *
1341 * \retval #PSA_SUCCESS
1342 * Success.
1343 * \retval #PSA_ERROR_BAD_STATE
1344 * Role hasn't been set yet.
1345 */
1346psa_status_t psa_crypto_driver_pake_get_role(
1347 const psa_crypto_driver_pake_inputs_t *inputs,
1348 psa_pake_role_t *role);
1349
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001350/** Get the lengths of the user in bytes from given inputs.
1351 *
1352 * \param[in] inputs Operation inputs.
1353 * \param[out] user_len Return buffer for user length.
1354 *
1355 * \retval #PSA_SUCCESS
1356 * Success.
1357 * \retval #PSA_ERROR_BAD_STATE
1358 * User hasn't been set yet.
1359 */
1360psa_status_t psa_crypto_driver_pake_get_user_len(
1361 const psa_crypto_driver_pake_inputs_t *inputs,
1362 size_t *user_len);
1363
1364/** Get the lengths of the peer in bytes from given inputs.
1365 *
1366 * \param[in] inputs Operation inputs.
1367 * \param[out] peer_len Return buffer for peer length.
1368 *
1369 * \retval #PSA_SUCCESS
1370 * Success.
1371 * \retval #PSA_ERROR_BAD_STATE
1372 * Peer hasn't been set yet.
1373 */
1374psa_status_t psa_crypto_driver_pake_get_peer_len(
1375 const psa_crypto_driver_pake_inputs_t *inputs,
1376 size_t *peer_len);
1377
1378/** Get the user from given inputs.
1379 *
1380 * \param[in] inputs Operation inputs.
1381 * \param[out] buffer Return buffer for user.
1382 * \param buffer_size Size of the return buffer in bytes.
1383 * \param[out] buffer_length Actual size of the password in bytes.
1384 *
1385 * \retval #PSA_SUCCESS
1386 * Success.
1387 * \retval #PSA_ERROR_BAD_STATE
1388 * User hasn't been set yet.
1389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1390 * The size of the \p buffer is too small.
1391 */
1392psa_status_t psa_crypto_driver_pake_get_user(
1393 const psa_crypto_driver_pake_inputs_t *inputs,
1394 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1395
1396/** Get the peer from given inputs.
1397 *
1398 * \param[in] inputs Operation inputs.
1399 * \param[out] buffer Return buffer for user.
1400 * \param buffer_size Size of the return buffer in bytes.
1401 * \param[out] buffer_length Actual size of the password in bytes.
1402 *
1403 * \retval #PSA_SUCCESS
1404 * Success.
1405 * \retval #PSA_ERROR_BAD_STATE
1406 * Peer hasn't been set yet.
1407 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1408 * The size of the \p buffer is too small.
1409 */
1410psa_status_t psa_crypto_driver_pake_get_peer(
1411 const psa_crypto_driver_pake_inputs_t *inputs,
1412 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1413
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001414/** Get the cipher suite from given inputs.
1415 *
1416 * \param[in] inputs Operation inputs.
1417 * \param[out] cipher_suite Return buffer for role.
1418 *
1419 * \retval #PSA_SUCCESS
1420 * Success.
1421 * \retval #PSA_ERROR_BAD_STATE
1422 * Cipher_suite hasn't been set yet.
1423 */
1424psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1425 const psa_crypto_driver_pake_inputs_t *inputs,
1426 psa_pake_cipher_suite_t *cipher_suite);
1427
Janos Follath702cf092021-05-26 12:58:23 +01001428/** Set the session information for a password-authenticated key exchange.
1429 *
1430 * The sequence of operations to set up a password-authenticated key exchange
1431 * is as follows:
1432 * -# Allocate an operation object which will be passed to all the functions
1433 * listed here.
1434 * -# Initialize the operation object with one of the methods described in the
1435 * documentation for #psa_pake_operation_t, e.g.
1436 * #PSA_PAKE_OPERATION_INIT.
1437 * -# Call psa_pake_setup() to specify the cipher suite.
1438 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1439 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1440 * to be called depends on the algorithm in use.
1441 *
1442 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1443 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1444 * for more information.
1445 *
1446 * A typical sequence of calls to perform a password-authenticated key
1447 * exchange:
1448 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1449 * key share that needs to be sent to the peer.
1450 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1451 * the key share that was received from the peer.
1452 * -# Depending on the algorithm additional calls to psa_pake_output() and
1453 * psa_pake_input() might be necessary.
1454 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1455 *
1456 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1457 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1458 * for more information.
1459 *
1460 * If an error occurs at any step after a call to psa_pake_setup(),
1461 * the operation will need to be reset by a call to psa_pake_abort(). The
1462 * application may call psa_pake_abort() at any time after the operation
1463 * has been initialized.
1464 *
1465 * After a successful call to psa_pake_setup(), the application must
1466 * eventually terminate the operation. The following events terminate an
1467 * operation:
1468 * - A call to psa_pake_abort().
1469 * - A successful call to psa_pake_get_implicit_key().
1470 *
1471 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001472 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001473 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001474 * characterizes a PAKE algorithm and determines
1475 * the algorithm as well.)
1476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001479 * \retval #PSA_ERROR_INVALID_ARGUMENT
1480 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1481 * PAKE primitive in \p cipher_suite is not compatible with the
1482 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1483 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001484 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001485 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1486 * or the PAKE primitive in \p cipher_suite is not supported or not
1487 * compatible with the PAKE algorithm, or the hash algorithm in
1488 * \p cipher_suite is not supported or not compatible with the PAKE
1489 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1491 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001492 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001493 * The operation state is not valid, or
1494 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001495 * It is implementation-dependent whether a failure to initialize
1496 * results in this error code.
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1499 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001500
1501/** Set the password for a password-authenticated key exchange from key ID.
1502 *
1503 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001504 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001505 *
1506 * \param[in,out] operation The operation object to set the password for. It
1507 * must have been set up by psa_pake_setup() and
1508 * not yet in use (neither psa_pake_output() nor
1509 * psa_pake_input() has been called yet). It must
1510 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001511 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001512 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001513 * \param password Identifier of the key holding the password or a
1514 * value derived from the password (eg. by a
1515 * memory-hard function). It must remain valid
1516 * until the operation terminates. It must be of
1517 * type #PSA_KEY_TYPE_PASSWORD or
1518 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1519 * the usage #PSA_KEY_USAGE_DERIVE.
1520 *
1521 * \retval #PSA_SUCCESS
1522 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001523 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001524 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001525 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001526 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1527 * permit the \p operation's algorithm.
1528 * \retval #PSA_ERROR_INVALID_ARGUMENT
1529 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1530 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1531 * the \p operation's cipher suite.
1532 * \retval #PSA_ERROR_NOT_SUPPORTED
1533 * The key type or key size of \p password is not supported with the
1534 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1536 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1537 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1538 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1539 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001540 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001541 * The operation state is not valid (it must have been set up.), or
1542 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001543 * It is implementation-dependent whether a failure to initialize
1544 * results in this error code.
1545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1547 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001548
Janos Follath702cf092021-05-26 12:58:23 +01001549/** Set the user ID for a password-authenticated key exchange.
1550 *
1551 * Call this function to set the user ID. For PAKE algorithms that associate a
1552 * user identifier with each side of the session you need to call
1553 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1554 * user identifier with the session, call psa_pake_set_user() only.
1555 *
1556 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1557 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1558 * for more information.
1559 *
1560 * \param[in,out] operation The operation object to set the user ID for. It
1561 * must have been set up by psa_pake_setup() and
1562 * not yet in use (neither psa_pake_output() nor
1563 * psa_pake_input() has been called yet). It must
1564 * be on operation for which the user ID hasn't
1565 * been set (psa_pake_set_user() hasn't been
1566 * called yet).
1567 * \param[in] user_id The user ID to authenticate with.
Przemek Stekiele9254a02023-03-01 11:18:09 +01001568 * ("client" or "server")
Janos Follath702cf092021-05-26 12:58:23 +01001569 * \param user_id_len Size of the \p user_id buffer in bytes.
1570 *
1571 * \retval #PSA_SUCCESS
1572 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001573 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001574 * \p user_id is not valid for the \p operation's algorithm and cipher
1575 * suite.
1576 * \retval #PSA_ERROR_NOT_SUPPORTED
1577 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1580 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001581 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001582 * The operation state is not valid, or
1583 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001584 * It is implementation-dependent whether a failure to initialize
1585 * results in this error code.
1586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1588 const uint8_t *user_id,
1589 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001590
1591/** Set the peer ID for a password-authenticated key exchange.
1592 *
1593 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1594 * that associate a user identifier with each side of the session. For PAKE
1595 * algorithms that associate a single user identifier with the session, call
1596 * psa_pake_set_user() only.
1597 *
1598 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1599 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1600 * for more information.
1601 *
1602 * \param[in,out] operation The operation object to set the peer ID for. It
1603 * must have been set up by psa_pake_setup() and
1604 * not yet in use (neither psa_pake_output() nor
1605 * psa_pake_input() has been called yet). It must
1606 * be on operation for which the peer ID hasn't
1607 * been set (psa_pake_set_peer() hasn't been
1608 * called yet).
1609 * \param[in] peer_id The peer's ID to authenticate.
Przemek Stekiele9254a02023-03-01 11:18:09 +01001610 * ("client" or "server")
Janos Follath702cf092021-05-26 12:58:23 +01001611 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1612 *
1613 * \retval #PSA_SUCCESS
1614 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001615 * \retval #PSA_ERROR_INVALID_ARGUMENT
1616 * \p user_id is not valid for the \p operation's algorithm and cipher
1617 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001618 * \retval #PSA_ERROR_NOT_SUPPORTED
1619 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1622 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001623 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001624 * Calling psa_pake_set_peer() is invalid with the \p operation's
1625 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001626 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001627 * It is implementation-dependent whether a failure to initialize
1628 * results in this error code.
1629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1631 const uint8_t *peer_id,
1632 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001633
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001634/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001635 *
1636 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001637 * It is optional to call this function for PAKEs that don't require a role
1638 * to be specified. For such PAKEs the application role parameter is ignored,
1639 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001640 *
1641 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1642 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1643 * for more information.
1644 *
Neil Armstrongef157512022-05-25 11:49:45 +02001645 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001646 * application's role for. It must have been set up
1647 * by psa_pake_setup() and not yet in use (neither
1648 * psa_pake_output() nor psa_pake_input() has been
1649 * called yet). It must be on operation for which
1650 * the application's role hasn't been specified
1651 * (psa_pake_set_role() hasn't been called yet).
1652 * \param role A value of type ::psa_pake_role_t indicating the
1653 * application's role in the PAKE the algorithm
1654 * that is being set up. For more information see
1655 * the documentation of \c PSA_PAKE_ROLE_XXX
1656 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001657 *
1658 * \retval #PSA_SUCCESS
1659 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001660 * \retval #PSA_ERROR_INVALID_ARGUMENT
1661 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001662 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001663 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1665 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001666 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001667 * The operation state is not valid, or
1668 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001669 * It is implementation-dependent whether a failure to initialize
1670 * results in this error code.
1671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001672psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1673 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001674
1675/** Get output for a step of a password-authenticated key exchange.
1676 *
1677 * Depending on the algorithm being executed, you might need to call this
1678 * function several times or you might not need to call this at all.
1679 *
1680 * The exact sequence of calls to perform a password-authenticated key
1681 * exchange depends on the algorithm in use. Refer to the documentation of
1682 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1683 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1684 * information.
1685 *
1686 * If this function returns an error status, the operation enters an error
1687 * state and must be aborted by calling psa_pake_abort().
1688 *
1689 * \param[in,out] operation Active PAKE operation.
1690 * \param step The step of the algorithm for which the output is
1691 * requested.
1692 * \param[out] output Buffer where the output is to be written in the
1693 * format appropriate for this \p step. Refer to
1694 * the documentation of the individual
1695 * \c PSA_PAKE_STEP_XXX constants for more
1696 * information.
1697 * \param output_size Size of the \p output buffer in bytes. This must
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001698 * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
1699 * primitive, \p step) where \p alg and
1700 * \p primitive are the PAKE algorithm and primitive
1701 * in the operation's cipher suite, and \p step is
1702 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001703 *
1704 * \param[out] output_length On success, the number of bytes of the returned
1705 * output.
1706 *
1707 * \retval #PSA_SUCCESS
1708 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001709 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1710 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001711 * \retval #PSA_ERROR_INVALID_ARGUMENT
1712 * \p step is not compatible with the operation's algorithm.
1713 * \retval #PSA_ERROR_NOT_SUPPORTED
1714 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001715 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1717 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1718 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1719 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1720 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1721 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001722 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001723 * The operation state is not valid (it must be active, and fully set
1724 * up, and this call must conform to the algorithm's requirements
1725 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001726 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001727 * It is implementation-dependent whether a failure to initialize
1728 * results in this error code.
1729 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1731 psa_pake_step_t step,
1732 uint8_t *output,
1733 size_t output_size,
1734 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001735
1736/** Provide input for a step of a password-authenticated key exchange.
1737 *
1738 * Depending on the algorithm being executed, you might need to call this
1739 * function several times or you might not need to call this at all.
1740 *
1741 * The exact sequence of calls to perform a password-authenticated key
1742 * exchange depends on the algorithm in use. Refer to the documentation of
1743 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1744 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1745 * information.
1746 *
1747 * If this function returns an error status, the operation enters an error
1748 * state and must be aborted by calling psa_pake_abort().
1749 *
1750 * \param[in,out] operation Active PAKE operation.
1751 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001752 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001753 * appropriate for this \p step. Refer to the
1754 * documentation of the individual
1755 * \c PSA_PAKE_STEP_XXX constants for more
1756 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001757 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001758 *
1759 * \retval #PSA_SUCCESS
1760 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001761 * \retval #PSA_ERROR_INVALID_SIGNATURE
1762 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001763 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong407b27b2022-05-20 11:28:23 +02001764 * \p is not compatible with the \p operation’s algorithm, or the
1765 * \p input is not valid for the \p operation's algorithm, cipher suite
1766 * or \p step.
1767 * \retval #PSA_ERROR_NOT_SUPPORTED
1768 * \p step p is not supported with the \p operation's algorithm, or the
1769 * \p input is not supported for the \p operation's algorithm, cipher
1770 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1773 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1774 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1775 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1776 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001777 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001778 * The operation state is not valid (it must be active, and fully set
1779 * up, and this call must conform to the algorithm's requirements
1780 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001781 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001782 * It is implementation-dependent whether a failure to initialize
1783 * results in this error code.
1784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1786 psa_pake_step_t step,
1787 const uint8_t *input,
1788 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001789
1790/** Get implicitly confirmed shared secret from a PAKE.
1791 *
1792 * At this point there is a cryptographic guarantee that only the authenticated
1793 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001794 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001795 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001796 * That is, the authentication is only implicit. Since the peer is not
1797 * authenticated yet, no action should be taken yet that assumes that the peer
1798 * is who it claims to be. For example, do not access restricted files on the
1799 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001800 *
1801 * This function can be called after the key exchange phase of the operation
1802 * has completed. It imports the shared secret output of the PAKE into the
1803 * provided derivation operation. The input step
1804 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1805 * material in the key derivation operation.
1806 *
1807 * The exact sequence of calls to perform a password-authenticated key
1808 * exchange depends on the algorithm in use. Refer to the documentation of
1809 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1810 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1811 * information.
1812 *
1813 * When this function returns successfully, \p operation becomes inactive.
1814 * If this function returns an error status, both \p operation
1815 * and \p key_derivation operations enter an error state and must be aborted by
1816 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1817 *
1818 * \param[in,out] operation Active PAKE operation.
1819 * \param[out] output A key derivation operation that is ready
1820 * for an input step of type
1821 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1822 *
1823 * \retval #PSA_SUCCESS
1824 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001825 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001826 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1827 * algorithm in the \p output key derivation operation.
1828 * \retval #PSA_ERROR_NOT_SUPPORTED
1829 * Input from a PAKE is not supported by the algorithm in the \p output
1830 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1833 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1834 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1835 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1836 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001837 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001838 * The PAKE operation state is not valid (it must be active, but beyond
1839 * that validity is specific to the algorithm), or
1840 * the library has not been previously initialized by psa_crypto_init(),
1841 * or the state of \p output is not valid for
1842 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1843 * step is out of order or the application has done this step already
1844 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001845 * It is implementation-dependent whether a failure to initialize
1846 * results in this error code.
1847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1849 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001850
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001851/** Abort a PAKE operation.
1852 *
1853 * Aborting an operation frees all associated resources except for the \c
1854 * operation structure itself. Once aborted, the operation object can be reused
1855 * for another operation by calling psa_pake_setup() again.
1856 *
1857 * This function may be called at any time after the operation
1858 * object has been initialized as described in #psa_pake_operation_t.
1859 *
1860 * In particular, calling psa_pake_abort() after the operation has been
1861 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1862 * is safe and has no effect.
1863 *
1864 * \param[in,out] operation The operation to abort.
1865 *
1866 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001867 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1869 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001870 * \retval #PSA_ERROR_BAD_STATE
1871 * The library has not been previously initialized by psa_crypto_init().
1872 * It is implementation-dependent whether a failure to initialize
1873 * results in this error code.
1874 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001876
Janos Follath702cf092021-05-26 12:58:23 +01001877/**@}*/
1878
1879/** A sufficient output buffer size for psa_pake_output().
1880 *
1881 * If the size of the output buffer is at least this large, it is guaranteed
1882 * that psa_pake_output() will not fail due to an insufficient output buffer
1883 * size. The actual size of the output might be smaller in any given call.
1884 *
1885 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1886 *
Janos Follath46c02372021-06-08 15:22:51 +01001887 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001888 * #PSA_ALG_IS_PAKE(\p alg) is true).
1889 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1890 * compatible with algorithm \p alg.
1891 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1892 * algorithm \p alg.
1893 * \return A sufficient output buffer size for the specified
Neil Armstrongcd974d52022-05-20 10:30:12 +02001894 * PAKE algorithm, primitive, and output step. If the
1895 * PAKE algorithm, primitive, or output step is not
1896 * recognized, or the parameters are incompatible,
1897 * return 0.
Janos Follath702cf092021-05-26 12:58:23 +01001898 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001899#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 (alg == PSA_ALG_JPAKE && \
1901 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1902 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1903 ( \
1904 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1905 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1906 32 \
1907 ) : \
1908 0)
Janos Follath702cf092021-05-26 12:58:23 +01001909
1910/** A sufficient input buffer size for psa_pake_input().
1911 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001912 * The value returned by this macro is guaranteed to be large enough for any
1913 * valid input to psa_pake_input() in an operation with the specified
1914 * parameters.
Janos Follath702cf092021-05-26 12:58:23 +01001915 *
1916 * See also #PSA_PAKE_INPUT_MAX_SIZE
1917 *
Janos Follath46c02372021-06-08 15:22:51 +01001918 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001919 * #PSA_ALG_IS_PAKE(\p alg) is true).
1920 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1921 * compatible with algorithm \p alg.
Janos Follathec83eb62021-05-27 08:41:59 +01001922 * \param input_step A value of type ::psa_pake_step_t that is valid for the
Janos Follath702cf092021-05-26 12:58:23 +01001923 * algorithm \p alg.
Janos Follath38d29db2021-06-03 13:14:42 +01001924 * \return A sufficient input buffer size for the specified
1925 * input, cipher suite and algorithm. If the cipher suite,
1926 * the input type or PAKE algorithm is not recognized, or
Janos Follath702cf092021-05-26 12:58:23 +01001927 * the parameters are incompatible, return 0.
1928 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001929#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 (alg == PSA_ALG_JPAKE && \
1931 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1932 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1933 ( \
1934 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1935 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1936 32 \
1937 ) : \
1938 0)
Janos Follath702cf092021-05-26 12:58:23 +01001939
Neil Armstrong2056ce52022-05-25 11:38:15 +02001940/** Output buffer size for psa_pake_output() for any of the supported PAKE
1941 * algorithm and primitive suites and output step.
Janos Follath702cf092021-05-26 12:58:23 +01001942 *
1943 * This macro must expand to a compile-time constant integer.
1944 *
Neil Armstrong2056ce52022-05-25 11:38:15 +02001945 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p step).
Janos Follath702cf092021-05-26 12:58:23 +01001946 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001947#define PSA_PAKE_OUTPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001948
Neil Armstrong2056ce52022-05-25 11:38:15 +02001949/** Input buffer size for psa_pake_input() for any of the supported PAKE
1950 * algorithm and primitive suites and input step.
Janos Follath702cf092021-05-26 12:58:23 +01001951 *
1952 * This macro must expand to a compile-time constant integer.
1953 *
Neil Armstrong2056ce52022-05-25 11:38:15 +02001954 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p step).
Janos Follath702cf092021-05-26 12:58:23 +01001955 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001956#define PSA_PAKE_INPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001957
Neil Armstrongfb993022022-05-20 10:08:58 +02001958/** Returns a suitable initializer for a PAKE cipher suite object of type
1959 * psa_pake_cipher_suite_t.
1960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
Neil Armstrongfb993022022-05-20 10:08:58 +02001962
Neil Armstrong0151c552022-05-20 10:13:53 +02001963/** Returns a suitable initializer for a PAKE operation object of type
1964 * psa_pake_operation_t.
1965 */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01001966#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, PSA_PAKE_OPERATION_STAGE_SETUP, \
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001967 { 0 }, { { 0 } } }
Neil Armstrong0151c552022-05-20 10:13:53 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969struct psa_pake_cipher_suite_s {
Janos Follath702cf092021-05-26 12:58:23 +01001970 psa_algorithm_t algorithm;
1971 psa_pake_primitive_type_t type;
1972 psa_pake_family_t family;
1973 uint16_t bits;
1974 psa_algorithm_t hash;
1975};
1976
1977static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001979{
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001981}
1982
1983static inline void psa_pake_cs_set_algorithm(
1984 psa_pake_cipher_suite_t *cipher_suite,
1985 psa_algorithm_t algorithm)
1986{
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001988 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001990 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 }
Janos Follath702cf092021-05-26 12:58:23 +01001992}
1993
1994static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001996{
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1998 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001999}
2000
2001static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 psa_pake_cipher_suite_t *cipher_suite,
2003 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01002004{
2005 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
2006 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
2007 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
2008}
2009
Neil Armstrongff9cac72022-05-20 10:25:15 +02002010static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02002012{
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02002014}
2015
Neil Armstrongd5a48252022-05-20 10:26:36 +02002016static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02002018{
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02002020}
2021
Janos Follath702cf092021-05-26 12:58:23 +01002022static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01002024{
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01002026}
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
2029 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01002030{
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01002032 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 } else {
Janos Follath702cf092021-05-26 12:58:23 +01002034 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Janos Follath702cf092021-05-26 12:58:23 +01002036}
2037
Przemek Stekiel51eac532022-12-07 11:04:51 +01002038struct psa_crypto_driver_pake_inputs_s {
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 uint8_t *MBEDTLS_PRIVATE(password);
Przemek Stekiel152ae072022-11-17 13:24:36 +01002040 size_t MBEDTLS_PRIVATE(password_len);
Neil Armstrong35269d92022-05-25 11:26:31 +02002041 psa_pake_role_t MBEDTLS_PRIVATE(role);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01002042 uint8_t *MBEDTLS_PRIVATE(user);
2043 size_t MBEDTLS_PRIVATE(user_len);
2044 uint8_t *MBEDTLS_PRIVATE(peer);
2045 size_t MBEDTLS_PRIVATE(peer_len);
Przemek Stekiel9dd24402023-01-26 15:06:09 +01002046 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Przemek Stekiel51eac532022-12-07 11:04:51 +01002047 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
2048};
2049
Przemek Stekiel57580f22023-03-01 12:21:26 +01002050typedef enum psa_jpake_step {
Przemek Stekiele12ed362022-12-21 12:54:46 +01002051 PSA_PAKE_STEP_INVALID = 0,
2052 PSA_PAKE_STEP_X1_X2 = 1,
2053 PSA_PAKE_STEP_X2S = 2,
2054 PSA_PAKE_STEP_DERIVE = 3,
Przemek Stekiel57580f22023-03-01 12:21:26 +01002055} psa_jpake_step_t;
Przemek Stekiele12ed362022-12-21 12:54:46 +01002056
Przemek Stekiel57580f22023-03-01 12:21:26 +01002057typedef enum psa_jpake_state {
Przemek Stekiele12ed362022-12-21 12:54:46 +01002058 PSA_PAKE_STATE_INVALID = 0,
2059 PSA_PAKE_STATE_SETUP = 1,
2060 PSA_PAKE_STATE_READY = 2,
2061 PSA_PAKE_OUTPUT_X1_X2 = 3,
2062 PSA_PAKE_OUTPUT_X2S = 4,
2063 PSA_PAKE_INPUT_X1_X2 = 5,
2064 PSA_PAKE_INPUT_X4S = 6,
Przemek Stekiel57580f22023-03-01 12:21:26 +01002065} psa_jpake_state_t;
Przemek Stekiele12ed362022-12-21 12:54:46 +01002066
Przemek Stekiel57580f22023-03-01 12:21:26 +01002067typedef enum psa_jpake_sequence {
Przemek Stekiele12ed362022-12-21 12:54:46 +01002068 PSA_PAKE_SEQ_INVALID = 0,
2069 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
2070 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
2071 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
2072 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
2073 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
2074 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
2075 PSA_PAKE_SEQ_END = 7,
Przemek Stekiel57580f22023-03-01 12:21:26 +01002076} psa_jpake_sequence_t;
Przemek Stekiele12ed362022-12-21 12:54:46 +01002077
Przemek Stekiel251e86a2023-02-17 14:30:50 +01002078typedef enum psa_crypto_driver_pake_step {
Przemek Stekielb09c4872023-01-17 12:05:38 +01002079 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
2080 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
2081 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
2082 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
2083 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
2084 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
2085 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
2086 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
2087 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
2088 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
2089 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
2090 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
2091 PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
Przemek Stekiel251e86a2023-02-17 14:30:50 +01002092} psa_crypto_driver_pake_step_t;
Przemek Stekiel57980032023-01-09 15:07:26 +01002093
Przemek Stekielb09c4872023-01-17 12:05:38 +01002094
Przemek Stekiele12ed362022-12-21 12:54:46 +01002095struct psa_jpake_computation_stage_s {
Przemek Stekiel57580f22023-03-01 12:21:26 +01002096 psa_jpake_state_t MBEDTLS_PRIVATE(state);
2097 psa_jpake_sequence_t MBEDTLS_PRIVATE(sequence);
2098 psa_jpake_step_t MBEDTLS_PRIVATE(input_step);
2099 psa_jpake_step_t MBEDTLS_PRIVATE(output_step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01002100};
2101
Janos Follath702cf092021-05-26 12:58:23 +01002102struct psa_pake_operation_s {
Przemek Stekield91bcb72022-11-22 14:00:51 +01002103 /** Unique ID indicating which driver got assigned to do the
2104 * operation. Since driver contexts are driver-specific, swapping
2105 * drivers halfway through the operation is not supported.
2106 * ID values are auto-generated in psa_crypto_driver_wrappers.h
2107 * ID value zero means the context is not valid or not assigned to
2108 * any driver (i.e. none of the driver contexts are active). */
2109 unsigned int MBEDTLS_PRIVATE(id);
Przemek Stekiel6b648622023-02-19 22:55:33 +01002110 /* Algorithm of the PAKE operation */
Przemek Stekiele12ed362022-12-21 12:54:46 +01002111 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Przemek Stekiel6b648622023-02-19 22:55:33 +01002112 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
2113 * or computing. */
Przemek Stekiel51eac532022-12-07 11:04:51 +01002114 uint8_t MBEDTLS_PRIVATE(stage);
Przemek Stekiele12ed362022-12-21 12:54:46 +01002115 /* Holds computation stage of the PAKE algorithms. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 union {
Przemek Stekiel251e86a2023-02-17 14:30:50 +01002117 uint8_t MBEDTLS_PRIVATE(dummy);
Przemek Stekiel4aa99402023-02-27 13:00:57 +01002118#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekieldde6a912023-01-26 08:46:37 +01002119 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
Neil Armstrong35269d92022-05-25 11:26:31 +02002120#endif
Przemek Stekieldde6a912023-01-26 08:46:37 +01002121 } MBEDTLS_PRIVATE(computation_stage);
Przemek Stekiel51eac532022-12-07 11:04:51 +01002122 union {
Przemek Stekiel51eac532022-12-07 11:04:51 +01002123 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
Przemek Stekielac067d72023-01-26 16:31:03 +01002124 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
Przemek Stekiel51eac532022-12-07 11:04:51 +01002125 } MBEDTLS_PRIVATE(data);
Janos Follath702cf092021-05-26 12:58:23 +01002126};
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02002129{
2130 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02002132}
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01002135{
2136 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 return v;
Janos Follath702cf092021-05-26 12:58:23 +01002138}
2139
Gilles Peskinee59236f2018-01-27 23:32:46 +01002140#ifdef __cplusplus
2141}
2142#endif
2143
2144#endif /* PSA_CRYPTO_EXTRA_H */