blob: 8484f17f90940e322472f4ff3c85d4453170eadb [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
Gilles Peskine09c02ee2021-11-25 20:30:47 +010032#include "crypto_types.h"
Gilles Peskine7a894f22019-11-26 16:06:46 +010033#include "crypto_compat.h"
34
Gilles Peskinee59236f2018-01-27 23:32:46 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
Netanel Gonen2bcd3122018-11-19 11:53:02 +020039/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020040#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020041
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020042/* See mbedtls_config.h for definition */
Steven Cooreman863470a2021-02-15 14:03:19 +010043#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
44#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
Steven Cooreman1f968fd2021-02-15 14:00:24 +010045#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000046
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020047/** \addtogroup attributes
48 * @{
49 */
50
51/** \brief Declare the enrollment algorithm for a key.
52 *
53 * An operation on a key may indifferently use the algorithm set with
54 * psa_set_key_algorithm() or with this function.
55 *
56 * \param[out] attributes The attribute structure to write to.
57 * \param alg2 A second algorithm that the key may be used
58 * for, in addition to the algorithm set with
59 * psa_set_key_algorithm().
60 *
61 * \warning Setting an enrollment algorithm is not recommended, because
62 * using the same key with different algorithms can allow some
63 * attacks based on arithmetic relations between different
64 * computations made with the same key, or can escalate harmless
65 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020066 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020067 * verified that the usage of the key with multiple algorithms
68 * is safe.
69 */
70static inline void psa_set_key_enrollment_algorithm(
71 psa_key_attributes_t *attributes,
72 psa_algorithm_t alg2)
73{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020074 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020075}
76
77/** Retrieve the enrollment algorithm policy from key attributes.
78 *
79 * \param[in] attributes The key attribute structure to query.
80 *
81 * \return The enrollment algorithm stored in the attribute structure.
82 */
83static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
84 const psa_key_attributes_t *attributes)
85{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020087}
88
Gilles Peskinec8000c02019-08-02 20:15:51 +020089#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
90
91/** Retrieve the slot number where a key is stored.
92 *
93 * A slot number is only defined for keys that are stored in a secure
94 * element.
95 *
96 * This information is only useful if the secure element is not entirely
97 * managed through the PSA Cryptography API. It is up to the secure
98 * element driver to decide how PSA slot numbers map to any other interface
99 * that the secure element may have.
100 *
101 * \param[in] attributes The key attribute structure to query.
102 * \param[out] slot_number On success, the slot number containing the key.
103 *
104 * \retval #PSA_SUCCESS
105 * The key is located in a secure element, and \p *slot_number
106 * indicates the slot number that contains it.
107 * \retval #PSA_ERROR_NOT_PERMITTED
108 * The caller is not permitted to query the slot number.
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200109 * Mbed TLS currently does not return this error.
Gilles Peskinec8000c02019-08-02 20:15:51 +0200110 * \retval #PSA_ERROR_INVALID_ARGUMENT
111 * The key is not located in a secure element.
112 */
113psa_status_t psa_get_key_slot_number(
114 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 psa_key_slot_number_t *slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200116
117/** Choose the slot number where a key is stored.
118 *
119 * This function declares a slot number in the specified attribute
120 * structure.
121 *
122 * A slot number is only meaningful for keys that are stored in a secure
123 * element. It is up to the secure element driver to decide how PSA slot
124 * numbers map to any other interface that the secure element may have.
125 *
126 * \note Setting a slot number in key attributes for a key creation can
127 * cause the following errors when creating the key:
128 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
129 * not support choosing a specific slot number.
130 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
131 * choose slot numbers in general or to choose this specific slot.
132 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
133 * valid in general or not valid for this specific key.
134 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
135 * selected slot.
136 *
137 * \param[out] attributes The attribute structure to write to.
138 * \param slot_number The slot number to set.
139 */
140static inline void psa_set_key_slot_number(
141 psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 psa_key_slot_number_t slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +0200143{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200144 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
145 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200146}
147
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200148/** Remove the slot number attribute from a key attribute structure.
149 *
150 * This function undoes the action of psa_set_key_slot_number().
151 *
152 * \param[out] attributes The attribute structure to write to.
153 */
154static inline void psa_clear_key_slot_number(
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 psa_key_attributes_t *attributes)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200156{
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &=
158 ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200159}
160
Gilles Peskined7729582019-08-05 15:55:54 +0200161/** Register a key that is already present in a secure element.
162 *
163 * The key must be located in a secure element designated by the
164 * lifetime field in \p attributes, in the slot set with
165 * psa_set_key_slot_number() in the attribute structure.
166 * This function makes the key available through the key identifier
167 * specified in \p attributes.
168 *
169 * \param[in] attributes The attributes of the existing key.
170 *
171 * \retval #PSA_SUCCESS
172 * The key was successfully registered.
173 * Note that depending on the design of the driver, this may or may
174 * not guarantee that a key actually exists in the designated slot
175 * and is compatible with the specified attributes.
176 * \retval #PSA_ERROR_ALREADY_EXISTS
177 * There is already a key with the identifier specified in
178 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200179 * \retval #PSA_ERROR_NOT_SUPPORTED
180 * The secure element driver for the specified lifetime does not
181 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200182 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200183 * The identifier in \p attributes is invalid, namely the identifier is
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500184 * not in the user range, or
Gilles Peskined7729582019-08-05 15:55:54 +0200185 * \p attributes specifies a lifetime which is not located
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500186 * in a secure element, or no slot number is specified in \p attributes,
Gilles Peskined7729582019-08-05 15:55:54 +0200187 * or the specified slot number is not valid.
188 * \retval #PSA_ERROR_NOT_PERMITTED
189 * The caller is not authorized to register the specified key slot.
Gilles Peskineed733552023-02-14 19:21:09 +0100190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
191 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
193 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
194 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
195 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Gilles Peskined7729582019-08-05 15:55:54 +0200196 * \retval #PSA_ERROR_BAD_STATE
197 * The library has not been previously initialized by psa_crypto_init().
198 * It is implementation-dependent whether a failure to initialize
199 * results in this error code.
200 */
201psa_status_t mbedtls_psa_register_se_key(
202 const psa_key_attributes_t *attributes);
203
Gilles Peskinec8000c02019-08-02 20:15:51 +0200204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
205
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200206/**@}*/
207
Gilles Peskinee59236f2018-01-27 23:32:46 +0100208/**
209 * \brief Library deinitialization.
210 *
211 * This function clears all data associated with the PSA layer,
212 * including the whole key store.
213 *
214 * This is an Mbed TLS extension.
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216void mbedtls_psa_crypto_free(void);
Gilles Peskinee59236f2018-01-27 23:32:46 +0100217
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200218/** \brief Statistics about
219 * resource consumption related to the PSA keystore.
220 *
221 * \note The content of this structure is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200222 * of Mbed TLS and may change arbitrarily from version to version.
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224typedef struct mbedtls_psa_stats_s {
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200225 /** Number of slots containing key material for a volatile key. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200226 size_t MBEDTLS_PRIVATE(volatile_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200227 /** Number of slots containing key material for a key which is in
228 * internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200229 size_t MBEDTLS_PRIVATE(persistent_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200230 /** Number of slots containing a reference to a key in a
231 * secure element. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200232 size_t MBEDTLS_PRIVATE(external_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200233 /** Number of slots which are occupied, but do not contain
234 * key material yet. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200235 size_t MBEDTLS_PRIVATE(half_filled_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200236 /** Number of slots that contain cache data. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200237 size_t MBEDTLS_PRIVATE(cache_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200238 /** Number of slots that are not used for anything. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200239 size_t MBEDTLS_PRIVATE(empty_slots);
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100240 /** Number of slots that are locked. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200241 size_t MBEDTLS_PRIVATE(locked_slots);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200242 /** Largest key id value among open keys in internal persistent storage. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200243 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200244 /** Largest key id value among open keys in secure elements. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200245 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200246} mbedtls_psa_stats_t;
247
248/** \brief Get statistics about
249 * resource consumption related to the PSA keystore.
250 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200251 * \note When Mbed TLS is built as part of a service, with isolation
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200252 * between the application and the keystore, the service may or
253 * may not expose this function.
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200256
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200257/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100258 * \brief Inject an initial entropy seed for the random generator into
259 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100260 *
261 * This function injects data to be used as a seed for the random generator
262 * used by the PSA Crypto implementation. On devices that lack a trusted
263 * entropy source (preferably a hardware random number generator),
264 * the Mbed PSA Crypto implementation uses this value to seed its
265 * random generator.
266 *
267 * On devices without a trusted entropy source, this function must be
268 * called exactly once in the lifetime of the device. On devices with
269 * a trusted entropy source, calling this function is optional.
270 * In all cases, this function may only be called before calling any
271 * other function in the PSA Crypto API, including psa_crypto_init().
272 *
273 * When this function returns successfully, it populates a file in
274 * persistent storage. Once the file has been created, this function
275 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100276 *
277 * If any error occurs, this function does not change the system state.
278 * You can call this function again after correcting the reason for the
279 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200280 *
281 * \warning This function **can** fail! Callers MUST check the return status.
282 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100283 * \warning If you use this function, you should use it as part of a
284 * factory provisioning process. The value of the injected seed
285 * is critical to the security of the device. It must be
286 * *secret*, *unpredictable* and (statistically) *unique per device*.
287 * You should be generate it randomly using a cryptographically
288 * secure random generator seeded from trusted entropy sources.
289 * You should transmit it securely to the device and ensure
290 * that its value is not leaked or stored anywhere beyond the
291 * needs of transmitting it from the point of generation to
292 * the call of this function, and erase all copies of the value
293 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200294 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100295 * This is an Mbed TLS extension.
296 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200297 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100298 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
299 * Note that you must provide compatible implementations of
300 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200301 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200302 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200303 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200304 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200305 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000306 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
307 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
308 * in `library/entropy_poll.h` in the Mbed TLS source
309 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200310 * It must be less or equal to
311 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200312 *
313 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100314 * The seed value was injected successfully. The random generator
315 * of the PSA Crypto implementation is now ready for use.
316 * You may now call psa_crypto_init() and use the PSA Crypto
317 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200318 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100319 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200320 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100321 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200322 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100323 * The library has already been initialized. It is no longer
324 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200325 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100326psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200327 size_t seed_size);
328
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200329/** \addtogroup crypto_types
330 * @{
331 */
332
Gilles Peskinea1302192019-05-16 13:58:24 +0200333/** DSA public key.
334 *
335 * The import and export format is the
336 * representation of the public key `y = g^x mod p` as a big-endian byte
337 * string. The length of the byte string is the length of the base prime `p`
338 * in bytes.
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200341
342/** DSA key pair (private and public key).
343 *
344 * The import and export format is the
345 * representation of the private key `x` as a big-endian byte string. The
346 * length of the byte string is the private key size in bytes (leading zeroes
347 * are not stripped).
348 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800349 * Deterministic DSA key derivation with psa_generate_derived_key follows
Gilles Peskinea1302192019-05-16 13:58:24 +0200350 * FIPS 186-4 §B.1.2: interpret the byte string as integer
351 * in big-endian order. Discard it if it is not in the range
352 * [0, *N* - 2] where *N* is the boundary of the private key domain
353 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
354 * or the order of the curve's base point for ECC).
355 * Add 1 to the resulting integer and use this as the private key *x*.
356 *
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200359
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100360/** Whether a key type is a DSA key (pair or public-only). */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200361#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200362 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200365/** DSA signature with hashing.
366 *
367 * This is the signature scheme defined by FIPS 186-4,
368 * with a random per-message secret number (*k*).
369 *
370 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
371 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
372 * This includes #PSA_ALG_ANY_HASH
373 * when specifying the algorithm in a usage policy.
374 *
375 * \return The corresponding DSA signature algorithm.
376 * \return Unspecified if \p hash_alg is not a supported
377 * hash algorithm.
378 */
379#define PSA_ALG_DSA(hash_alg) \
380 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskine449bd832023-01-11 14:50:10 +0100381#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100382#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200383/** Deterministic DSA signature with hashing.
384 *
385 * This is the deterministic variant defined by RFC 6979 of
386 * the signature scheme defined by FIPS 186-4.
387 *
388 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
389 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
390 * This includes #PSA_ALG_ANY_HASH
391 * when specifying the algorithm in a usage policy.
392 *
393 * \return The corresponding DSA signature algorithm.
394 * \return Unspecified if \p hash_alg is not a supported
395 * hash algorithm.
396 */
397#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
398 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
399#define PSA_ALG_IS_DSA(alg) \
400 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
401 PSA_ALG_DSA_BASE)
402#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
403 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
404#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
405 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
406#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
407 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
408
409
410/* We need to expand the sample definition of this macro from
411 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100412#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
413#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
414 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200415
416/**@}*/
417
Gilles Peskine24f10f82019-05-16 12:18:32 +0200418/** \addtogroup attributes
419 * @{
420 */
421
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200422/** Custom Diffie-Hellman group.
423 *
Paul Elliott75e27032020-06-03 15:17:39 +0100424 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
425 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200426 * from domain parameters set by psa_set_key_domain_parameters().
427 */
Paul Elliott75e27032020-06-03 15:17:39 +0100428#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200429
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100430/** PAKE operation stages. */
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +0100431#define PSA_PAKE_OPERATION_STAGE_SETUP 0
432#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
433#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200434
Gilles Peskine24f10f82019-05-16 12:18:32 +0200435/**
436 * \brief Set domain parameters for a key.
437 *
438 * Some key types require additional domain parameters in addition to
439 * the key type identifier and the key size. Use this function instead
440 * of psa_set_key_type() when you need to specify domain parameters.
441 *
442 * The format for the required domain parameters varies based on the key type.
443 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200444 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200445 * the domain parameter data consists of the public exponent,
446 * represented as a big-endian integer with no leading zeros.
447 * This information is used when generating an RSA key pair.
448 * When importing a key, the public exponent is read from the imported
449 * key data and the exponent recorded in the attribute structure is ignored.
450 * As an exception, the public exponent 65537 is represented by an empty
451 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200452 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400453 * the `Dss-Params` format as defined by RFC 3279 §2.3.2.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200454 * ```
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400455 * Dss-Params ::= SEQUENCE {
Gilles Peskine24f10f82019-05-16 12:18:32 +0200456 * p INTEGER,
457 * q INTEGER,
458 * g INTEGER
459 * }
460 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200461 * - For Diffie-Hellman key exchange keys
Paul Elliott75e27032020-06-03 15:17:39 +0100462 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
463 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200464 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
465 * ```
466 * DomainParameters ::= SEQUENCE {
467 * p INTEGER, -- odd prime, p=jq +1
468 * g INTEGER, -- generator, g
469 * q INTEGER, -- factor of p-1
470 * j INTEGER OPTIONAL, -- subgroup factor
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400471 * validationParams ValidationParams OPTIONAL
Gilles Peskine24f10f82019-05-16 12:18:32 +0200472 * }
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400473 * ValidationParams ::= SEQUENCE {
Gilles Peskine24f10f82019-05-16 12:18:32 +0200474 * seed BIT STRING,
475 * pgenCounter INTEGER
476 * }
477 * ```
478 *
479 * \note This function may allocate memory or other resources.
480 * Once you have called this function on an attribute structure,
481 * you must call psa_reset_key_attributes() to free these resources.
482 *
483 * \note This is an experimental extension to the interface. It may change
484 * in future versions of the library.
485 *
486 * \param[in,out] attributes Attribute structure where the specified domain
487 * parameters will be stored.
488 * If this function fails, the content of
489 * \p attributes is not modified.
490 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
491 * \param[in] data Buffer containing the key domain parameters.
492 * The content of this buffer is interpreted
493 * according to \p type as described above.
494 * \param data_length Size of the \p data buffer in bytes.
495 *
Gilles Peskineed733552023-02-14 19:21:09 +0100496 * \retval #PSA_SUCCESS \emptydescription
497 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
498 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200500 */
501psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
502 psa_key_type_t type,
503 const uint8_t *data,
504 size_t data_length);
505
506/**
507 * \brief Get domain parameters for a key.
508 *
509 * Get the domain parameters for a key with this function, if any. The format
510 * of the domain parameters written to \p data is specified in the
511 * documentation for psa_set_key_domain_parameters().
512 *
513 * \note This is an experimental extension to the interface. It may change
514 * in future versions of the library.
515 *
516 * \param[in] attributes The key attribute structure to query.
517 * \param[out] data On success, the key domain parameters.
518 * \param data_size Size of the \p data buffer in bytes.
519 * The buffer is guaranteed to be large
520 * enough if its size in bytes is at least
521 * the value given by
522 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
523 * \param[out] data_length On success, the number of bytes
524 * that make up the key domain parameters data.
525 *
Gilles Peskineed733552023-02-14 19:21:09 +0100526 * \retval #PSA_SUCCESS \emptydescription
527 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
Gilles Peskine24f10f82019-05-16 12:18:32 +0200528 */
529psa_status_t psa_get_key_domain_parameters(
530 const psa_key_attributes_t *attributes,
531 uint8_t *data,
532 size_t data_size,
533 size_t *data_length);
534
535/** Safe output buffer size for psa_get_key_domain_parameters().
536 *
537 * This macro returns a compile-time constant if its arguments are
538 * compile-time constants.
539 *
540 * \warning This function may call its arguments multiple times or
541 * zero times, so you should not pass arguments that contain
542 * side effects.
543 *
544 * \note This is an experimental extension to the interface. It may change
545 * in future versions of the library.
546 *
547 * \param key_type A supported key type.
548 * \param key_bits The size of the key in bits.
549 *
550 * \return If the parameters are valid and supported, return
551 * a buffer size in bytes that guarantees that
552 * psa_get_key_domain_parameters() will not fail with
553 * #PSA_ERROR_BUFFER_TOO_SMALL.
554 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200555 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200556 * sensible size or 0.
557 * If the parameters are not valid, the
558 * return value is unspecified.
559 */
560#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
561 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
562 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
563 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
564 0)
565#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
566 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
567#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
568 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
569
570/**@}*/
571
Gilles Peskine5055b232019-12-12 17:49:31 +0100572/** \defgroup psa_tls_helpers TLS helper functions
573 * @{
574 */
Valerio Settia9aab1a2023-06-19 13:39:54 +0200575#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine5055b232019-12-12 17:49:31 +0100576#include <mbedtls/ecp.h>
577
578/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
579 *
580 * \note This function is provided solely for the convenience of
581 * Mbed TLS and may be removed at any time without notice.
582 *
583 * \param grpid An Mbed TLS elliptic curve identifier
584 * (`MBEDTLS_ECP_DP_xxx`).
585 * \param[out] bits On success, the bit size of the curve.
586 *
587 * \return The corresponding PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100588 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100589 * \return \c 0 on failure (\p grpid is not recognized).
590 */
Valerio Settibc2b1d32023-06-19 12:15:13 +0200591psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
592 size_t *bits);
Gilles Peskine5055b232019-12-12 17:49:31 +0100593
594/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
595 *
596 * \note This function is provided solely for the convenience of
597 * Mbed TLS and may be removed at any time without notice.
598 *
599 * \param curve A PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100600 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100601 * \param bits The bit-length of a private key on \p curve.
602 * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
603 * to the nearest multiple of 8. This allows the caller
604 * to infer the exact curve from the length of a key
605 * which is supplied as a byte string.
Gilles Peskine5055b232019-12-12 17:49:31 +0100606 *
607 * \return The corresponding Mbed TLS elliptic curve identifier
608 * (`MBEDTLS_ECP_DP_xxx`).
609 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100610 * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
Gilles Peskine5055b232019-12-12 17:49:31 +0100611 * correct for \p curve.
612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
614 size_t bits,
615 int bits_is_sloppy);
Valerio Settia9aab1a2023-06-19 13:39:54 +0200616#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine5055b232019-12-12 17:49:31 +0100617
618/**@}*/
619
Gilles Peskineb8af2282020-11-13 18:00:34 +0100620/** \defgroup psa_external_rng External random generator
621 * @{
622 */
623
624#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
625/** External random generator function, implemented by the platform.
626 *
627 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
628 * this function replaces Mbed TLS's entropy and DRBG modules for all
629 * random generation triggered via PSA crypto interfaces.
630 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100631 * \note This random generator must deliver random numbers with cryptographic
632 * quality and high performance. It must supply unpredictable numbers
633 * with a uniform distribution. The implementation of this function
634 * is responsible for ensuring that the random generator is seeded
635 * with sufficient entropy. If you have a hardware TRNG which is slow
636 * or delivers non-uniform output, declare it as an entropy source
637 * with mbedtls_entropy_add_source() instead of enabling this option.
638 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100639 * \param[in,out] context Pointer to the random generator context.
640 * This is all-bits-zero on the first call
641 * and preserved between successive calls.
642 * \param[out] output Output buffer. On success, this buffer
643 * contains random data with a uniform
644 * distribution.
645 * \param output_size The size of the \p output buffer in bytes.
646 * \param[out] output_length On success, set this value to \p output_size.
647 *
648 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100649 * Success. The output buffer contains \p output_size bytes of
650 * cryptographic-quality random data, and \c *output_length is
651 * set to \p output_size.
652 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
653 * The random generator requires extra entropy and there is no
654 * way to obtain entropy under current environment conditions.
655 * This error should not happen under normal circumstances since
656 * this function is responsible for obtaining as much entropy as
657 * it needs. However implementations of this function may return
658 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
659 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100660 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100661 * A failure of the random generator hardware that isn't covered
662 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100663 */
664psa_status_t mbedtls_psa_external_get_random(
665 mbedtls_psa_external_random_context_t *context,
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 uint8_t *output, size_t output_size, size_t *output_length);
Gilles Peskineb8af2282020-11-13 18:00:34 +0100667#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
668
669/**@}*/
670
Steven Cooreman6801f082021-02-19 17:21:22 +0100671/** \defgroup psa_builtin_keys Built-in keys
672 * @{
673 */
674
675/** The minimum value for a key identifier that is built into the
676 * implementation.
677 *
678 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
679 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
680 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
681 * with any other set of implementation-chosen key identifiers.
682 *
683 * This value is part of the library's ABI since changing it would invalidate
684 * the values of built-in key identifiers in applications.
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
Steven Cooreman6801f082021-02-19 17:21:22 +0100687
688/** The maximum value for a key identifier that is built into the
689 * implementation.
690 *
691 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
692 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
Steven Cooreman6801f082021-02-19 17:21:22 +0100694
695/** A slot number identifying a key in a driver.
696 *
697 * Values of this type are used to identify built-in keys.
698 */
699typedef uint64_t psa_drv_slot_number_t;
700
701#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
702/** Test whether a key identifier belongs to the builtin key range.
703 *
704 * \param key_id Key identifier to test.
705 *
706 * \retval 1
707 * The key identifier is a builtin key identifier.
708 * \retval 0
709 * The key identifier is not a builtin key identifier.
710 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
Steven Cooreman6801f082021-02-19 17:21:22 +0100712{
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
714 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
Steven Cooreman6801f082021-02-19 17:21:22 +0100715}
716
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200717/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100718 *
719 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100720 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100721 * as part of a platform's system image.
722 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100723 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100724 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
725 *
726 * In a multi-application configuration
727 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
728 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
729 * is allowed to use the given key.
730 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100731 * \param key_id The key ID for which to retrieve the
732 * location and slot attributes.
733 * \param[out] lifetime On success, the lifetime associated with the key
734 * corresponding to \p key_id. Lifetime is a
735 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200736 * and with what persistence level the key is
737 * intended to be used. If the platform
738 * implementation does not contain specific
739 * information about the intended key persistence
740 * level, the persistence level may be reported as
741 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100742 * \param[out] slot_number On success, the slot number known to the driver
743 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200744 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100745 * requested built-in key.
746 *
747 * \retval #PSA_SUCCESS
748 * The requested key identifier designates a built-in key.
749 * In a multi-application configuration, the requested owner
750 * is allowed to access it.
751 * \retval #PSA_ERROR_DOES_NOT_EXIST
752 * The requested key identifier is not a built-in key which is known
753 * to this function. If a key exists in the key storage with this
754 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100755 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100756 * Any other error is propagated to the function that requested the key.
757 * Common errors include:
758 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
759 * is not allowed to access it.
760 */
761psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100762 mbedtls_svc_key_id_t key_id,
763 psa_key_lifetime_t *lifetime,
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 psa_drv_slot_number_t *slot_number);
Steven Cooreman6801f082021-02-19 17:21:22 +0100765#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
766
767/** @} */
768
Janos Follath702cf092021-05-26 12:58:23 +0100769/** \addtogroup crypto_types
770 * @{
771 */
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
Janos Follath702cf092021-05-26 12:58:23 +0100774
775/** Whether the specified algorithm is a password-authenticated key exchange.
776 *
777 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
778 *
779 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
780 * algorithm, 0 otherwise.
781 * This macro may return either 0 or 1 if \p alg is not a supported
782 * algorithm identifier.
783 */
784#define PSA_ALG_IS_PAKE(alg) \
785 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
786
787/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
788 *
789 * This is J-PAKE as defined by RFC 8236, instantiated with the following
790 * parameters:
791 *
792 * - The group can be either an elliptic curve or defined over a finite field.
793 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
794 * J-PAKE algorithm.
Janos Follath46c02372021-06-08 15:22:51 +0100795 * - A cryptographic hash function.
Janos Follath702cf092021-05-26 12:58:23 +0100796 *
Janos Follath46c02372021-06-08 15:22:51 +0100797 * To select these parameters and set up the cipher suite, call these functions
798 * in any order:
Janos Follathb384ec12021-06-03 14:48:51 +0100799 *
800 * \code
801 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
802 * psa_pake_cs_set_primitive(cipher_suite,
803 * PSA_PAKE_PRIMITIVE(type, family, bits));
804 * psa_pake_cs_set_hash(cipher_suite, hash);
805 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100806 *
807 * For more information on how to set a specific curve or field, refer to the
808 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
809 *
810 * After initializing a J-PAKE operation, call
Janos Follathb384ec12021-06-03 14:48:51 +0100811 *
812 * \code
813 * psa_pake_setup(operation, cipher_suite);
814 * psa_pake_set_user(operation, ...);
815 * psa_pake_set_peer(operation, ...);
816 * psa_pake_set_password_key(operation, ...);
817 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100818 *
Neil Armstrong16145372022-05-20 10:42:36 +0200819 * The password is provided as a key. This can be the password text itself,
820 * in an agreed character encoding, or some value derived from the password
821 * as required by a higher level protocol.
Janos Follath702cf092021-05-26 12:58:23 +0100822 *
Neil Armstrong16145372022-05-20 10:42:36 +0200823 * (The implementation converts the key material to a number as described in
Janos Follath702cf092021-05-26 12:58:23 +0100824 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
825 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
826 * \c q is order of the group defined by the primitive set in the cipher suite.
Neil Armstrong5892aa62022-05-27 09:44:47 +0200827 * The \c psa_pake_set_password_key() function returns an error if the result
Janos Follath702cf092021-05-26 12:58:23 +0100828 * of the reduction is 0.)
829 *
830 * The key exchange flow for J-PAKE is as follows:
831 * -# To get the first round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100832 * \code
833 * // Get g1
834 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
835 * // Get the ZKP public key for x1
836 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
837 * // Get the ZKP proof for x1
838 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
839 * // Get g2
840 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
841 * // Get the ZKP public key for x2
842 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
843 * // Get the ZKP proof for x2
844 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
845 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100846 * -# To provide the first round data received from the peer to the operation,
847 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100848 * \code
849 * // Set g3
850 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
851 * // Set the ZKP public key for x3
852 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
853 * // Set the ZKP proof for x3
854 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
855 * // Set g4
856 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
857 * // Set the ZKP public key for x4
858 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
859 * // Set the ZKP proof for x4
860 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
861 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100862 * -# To get the second round data that needs to be sent to the peer, call
Janos Follathb384ec12021-06-03 14:48:51 +0100863 * \code
864 * // Get A
865 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
866 * // Get ZKP public key for x2*s
867 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
868 * // Get ZKP proof for x2*s
869 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
870 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100871 * -# To provide the second round data received from the peer to the operation,
872 * call
Janos Follathb384ec12021-06-03 14:48:51 +0100873 * \code
874 * // Set B
875 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
876 * // Set ZKP public key for x4*s
877 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
878 * // Set ZKP proof for x4*s
879 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
880 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100881 * -# To access the shared secret call
Janos Follathb384ec12021-06-03 14:48:51 +0100882 * \code
883 * // Get Ka=Kb=K
884 * psa_pake_get_implicit_key()
885 * \endcode
Janos Follath702cf092021-05-26 12:58:23 +0100886 *
887 * For more information consult the documentation of the individual
888 * \c PSA_PAKE_STEP_XXX constants.
889 *
890 * At this point there is a cryptographic guarantee that only the authenticated
891 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +0100892 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +0100893 *
894 * That is, the authentication is only implicit (the peer is not authenticated
895 * at this point, and no action should be taken that assume that they are - like
896 * for example accessing restricted files).
897 *
898 * To make the authentication explicit there are various methods, see Section 5
899 * of RFC 8236 for two examples.
900 *
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
Janos Follath702cf092021-05-26 12:58:23 +0100903
904/** @} */
905
906/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100907 *
908 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
909 * the official PSA Crypto API yet.
910 *
911 * \note The content of this section is not part of the stable API and ABI
Fredrik Hessecc207bc2021-09-28 21:06:08 +0200912 * of Mbed TLS and may change arbitrarily from version to version.
Janos Follath7d69b3a2021-05-26 13:10:56 +0100913 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
914 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100915 * @{
916 */
917
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200918/** \brief Encoding of the application role of PAKE
Janos Follath702cf092021-05-26 12:58:23 +0100919 *
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200920 * Encodes the application's role in the algorithm is being executed. For more
921 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
922 * constants.
Janos Follath702cf092021-05-26 12:58:23 +0100923 */
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200924typedef uint8_t psa_pake_role_t;
Janos Follath702cf092021-05-26 12:58:23 +0100925
926/** Encoding of input and output indicators for PAKE.
927 *
928 * Some PAKE algorithms need to exchange more data than just a single key share.
929 * This type is for encoding additional input and output data for such
930 * algorithms.
931 */
932typedef uint8_t psa_pake_step_t;
933
934/** Encoding of the type of the PAKE's primitive.
935 *
936 * Values defined by this standard will never be in the range 0x80-0xff.
937 * Vendors who define additional types must use an encoding in this range.
938 *
939 * For more information see the documentation of individual
940 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
941 */
942typedef uint8_t psa_pake_primitive_type_t;
943
944/** \brief Encoding of the family of the primitive associated with the PAKE.
945 *
946 * For more information see the documentation of individual
947 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
948 */
949typedef uint8_t psa_pake_family_t;
950
951/** \brief Encoding of the primitive associated with the PAKE.
952 *
953 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
954 */
955typedef uint32_t psa_pake_primitive_t;
956
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200957/** A value to indicate no role in a PAKE algorithm.
958 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
959 * algorithms which do not assign roles.
960 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200962
Janos Follath702cf092021-05-26 12:58:23 +0100963/** The first peer in a balanced PAKE.
964 *
965 * Although balanced PAKE algorithms are symmetric, some of them needs an
966 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200967 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100968 * accepted.
969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +0100971
972/** The second peer in a balanced PAKE.
973 *
974 * Although balanced PAKE algorithms are symmetric, some of them needs an
975 * ordering of peers for the transcript calculations. If the algorithm does not
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +0200976 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
Janos Follath702cf092021-05-26 12:58:23 +0100977 * accepted.
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +0100980
981/** The client in an augmented PAKE.
982 *
983 * Augmented PAKE algorithms need to differentiate between client and server.
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
Janos Follath702cf092021-05-26 12:58:23 +0100986
987/** The server in an augmented PAKE.
988 *
989 * Augmented PAKE algorithms need to differentiate between client and server.
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
Janos Follath702cf092021-05-26 12:58:23 +0100992
993/** The PAKE primitive type indicating the use of elliptic curves.
994 *
995 * The values of the \c family and \c bits fields of the cipher suite identify a
996 * specific elliptic curve, using the same mapping that is used for ECC
997 * (::psa_ecc_family_t) keys.
998 *
999 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1000 * \c bits means the value returned by psa_pake_cs_get_bits().)
1001 *
1002 * Input and output during the operation can involve group elements and scalar
1003 * values:
1004 * -# The format for group elements is the same as for public keys on the
1005 * specific curve would be. For more information, consult the documentation of
1006 * psa_export_public_key().
1007 * -# The format for scalars is the same as for private keys on the specific
1008 * curve would be. For more information, consult the documentation of
1009 * psa_export_key().
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +01001012
1013/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
1014 *
1015 * The values of the \c family and \c bits fields of the cipher suite identify
1016 * a specific Diffie-Hellman group, using the same mapping that is used for
1017 * Diffie-Hellman (::psa_dh_family_t) keys.
1018 *
1019 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1020 * \c bits means the value returned by psa_pake_cs_get_bits().)
1021 *
1022 * Input and output during the operation can involve group elements and scalar
1023 * values:
1024 * -# The format for group elements is the same as for public keys on the
1025 * specific group would be. For more information, consult the documentation of
1026 * psa_export_public_key().
1027 * -# The format for scalars is the same as for private keys on the specific
1028 * group would be. For more information, consult the documentation of
1029 * psa_export_key().
1030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001032
1033/** Construct a PAKE primitive from type, family and bit-size.
1034 *
1035 * \param pake_type The type of the primitive
1036 * (value of type ::psa_pake_primitive_type_t).
1037 * \param pake_family The family of the primitive
1038 * (the type and interpretation of this parameter depends
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001039 * on \p pake_type, for more information consult the
Janos Follath702cf092021-05-26 12:58:23 +01001040 * documentation of individual ::psa_pake_primitive_type_t
1041 * constants).
1042 * \param pake_bits The bit-size of the primitive
1043 * (Value of type \c size_t. The interpretation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001044 * of this parameter depends on \p pake_family, for more
Janos Follath702cf092021-05-26 12:58:23 +01001045 * information consult the documentation of individual
1046 * ::psa_pake_primitive_type_t constants).
1047 *
1048 * \return The constructed primitive value of type ::psa_pake_primitive_t.
1049 * Return 0 if the requested primitive can't be encoded as
1050 * ::psa_pake_primitive_t.
1051 */
1052#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
1053 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
1054 ((psa_pake_primitive_t) (((pake_type) << 24 | \
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 (pake_family) << 16) | (pake_bits)))
Janos Follath702cf092021-05-26 12:58:23 +01001056
1057/** The key share being sent to or received from the peer.
1058 *
1059 * The format for both input and output at this step is the same as for public
1060 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1061 * be.
1062 *
1063 * For more information on the format, consult the documentation of
1064 * psa_export_public_key().
1065 *
1066 * For information regarding how the group is determined, consult the
1067 * documentation #PSA_PAKE_PRIMITIVE.
1068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
Janos Follath702cf092021-05-26 12:58:23 +01001070
1071/** A Schnorr NIZKP public key.
1072 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001073 * This is the ephemeral public key in the Schnorr Non-Interactive
1074 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1075 *
Janos Follath702cf092021-05-26 12:58:23 +01001076 * The format for both input and output at this step is the same as for public
1077 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1078 * be.
1079 *
1080 * For more information on the format, consult the documentation of
1081 * psa_export_public_key().
1082 *
1083 * For information regarding how the group is determined, consult the
1084 * documentation #PSA_PAKE_PRIMITIVE.
1085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
Janos Follath702cf092021-05-26 12:58:23 +01001087
1088/** A Schnorr NIZKP proof.
1089 *
Janos Follath55dd5dc2021-06-03 15:51:09 +01001090 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1091 * value denoted by the letter 'r' in RFC 8235).
Janos Follath702cf092021-05-26 12:58:23 +01001092 *
Janos Follath1f013182021-06-08 15:30:48 +01001093 * Both for input and output, the value at this step is an integer less than
1094 * the order of the group selected in the cipher suite. The format depends on
1095 * the group as well:
Janos Follath702cf092021-05-26 12:58:23 +01001096 *
Janos Follath1f013182021-06-08 15:30:48 +01001097 * - For Montgomery curves, the encoding is little endian.
Janos Follath55dd5dc2021-06-03 15:51:09 +01001098 * - For everything else the encoding is big endian (see Section 2.3.8 of
1099 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
Janos Follath702cf092021-05-26 12:58:23 +01001100 *
Janos Follath1f013182021-06-08 15:30:48 +01001101 * In both cases leading zeroes are allowed as long as the length in bytes does
1102 * not exceed the byte length of the group order.
1103 *
Janos Follath702cf092021-05-26 12:58:23 +01001104 * For information regarding how the group is determined, consult the
1105 * documentation #PSA_PAKE_PRIMITIVE.
1106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
Janos Follath702cf092021-05-26 12:58:23 +01001108
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001109/** The type of the data structure for PAKE cipher suites.
Janos Follath702cf092021-05-26 12:58:23 +01001110 *
1111 * This is an implementation-defined \c struct. Applications should not
1112 * make any assumptions about the content of this structure.
1113 * Implementation details can change in future versions without notice.
1114 */
1115typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1116
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001117/** Return an initial value for a PAKE cipher suite object.
1118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02001120
Janos Follath702cf092021-05-26 12:58:23 +01001121/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1122 *
Janos Follath702cf092021-05-26 12:58:23 +01001123 * \param[in] cipher_suite The cipher suite structure to query.
1124 *
1125 * \return The PAKE algorithm stored in the cipher suite structure.
1126 */
1127static psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001129
1130/** Declare the PAKE algorithm for the cipher suite.
1131 *
1132 * This function overwrites any PAKE algorithm
1133 * previously set in \p cipher_suite.
1134 *
Janos Follath702cf092021-05-26 12:58:23 +01001135 * \param[out] cipher_suite The cipher suite structure to write to.
1136 * \param algorithm The PAKE algorithm to write.
1137 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1138 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1139 * If this is 0, the PAKE algorithm in
1140 * \p cipher_suite becomes unspecified.
1141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1143 psa_algorithm_t algorithm);
Janos Follath702cf092021-05-26 12:58:23 +01001144
1145/** Retrieve the primitive from a PAKE cipher suite.
1146 *
Janos Follath702cf092021-05-26 12:58:23 +01001147 * \param[in] cipher_suite The cipher suite structure to query.
1148 *
1149 * \return The primitive stored in the cipher suite structure.
1150 */
1151static psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001153
1154/** Declare the primitive for a PAKE cipher suite.
1155 *
1156 * This function overwrites any primitive previously set in \p cipher_suite.
1157 *
Janos Follath702cf092021-05-26 12:58:23 +01001158 * \param[out] cipher_suite The cipher suite structure to write to.
1159 * \param primitive The primitive to write. If this is 0, the
1160 * primitive type in \p cipher_suite becomes
1161 * unspecified.
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1164 psa_pake_primitive_t primitive);
Janos Follath702cf092021-05-26 12:58:23 +01001165
Neil Armstrongff9cac72022-05-20 10:25:15 +02001166/** Retrieve the PAKE family from a PAKE cipher suite.
1167 *
Neil Armstrongff9cac72022-05-20 10:25:15 +02001168 * \param[in] cipher_suite The cipher suite structure to query.
1169 *
1170 * \return The PAKE family stored in the cipher suite structure.
1171 */
1172static psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongff9cac72022-05-20 10:25:15 +02001174
Neil Armstrongd5a48252022-05-20 10:26:36 +02001175/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1176 *
Neil Armstrongd5a48252022-05-20 10:26:36 +02001177 * \param[in] cipher_suite The cipher suite structure to query.
1178 *
1179 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1180 */
1181static uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 const psa_pake_cipher_suite_t *cipher_suite);
Neil Armstrongd5a48252022-05-20 10:26:36 +02001183
Janos Follath702cf092021-05-26 12:58:23 +01001184/** Retrieve the hash algorithm from a PAKE cipher suite.
1185 *
Janos Follath702cf092021-05-26 12:58:23 +01001186 * \param[in] cipher_suite The cipher suite structure to query.
1187 *
1188 * \return The hash algorithm stored in the cipher suite structure. The return
1189 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1190 * the hash algorithm is not set.
1191 */
1192static psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001194
1195/** Declare the hash algorithm for a PAKE cipher suite.
1196 *
1197 * This function overwrites any hash algorithm
1198 * previously set in \p cipher_suite.
1199 *
Janos Follath702cf092021-05-26 12:58:23 +01001200 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1201 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1202 * for more information.
1203 *
1204 * \param[out] cipher_suite The cipher suite structure to write to.
1205 * \param hash The hash involved in the cipher suite.
1206 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1207 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1208 * If this is 0, the hash algorithm in
1209 * \p cipher_suite becomes unspecified.
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1212 psa_algorithm_t hash);
Janos Follath702cf092021-05-26 12:58:23 +01001213
1214/** The type of the state data structure for PAKE operations.
1215 *
1216 * Before calling any function on a PAKE operation object, the application
1217 * must initialize it by any of the following means:
1218 * - Set the structure to all-bits-zero, for example:
1219 * \code
1220 * psa_pake_operation_t operation;
1221 * memset(&operation, 0, sizeof(operation));
1222 * \endcode
1223 * - Initialize the structure to logical zero values, for example:
1224 * \code
1225 * psa_pake_operation_t operation = {0};
1226 * \endcode
1227 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1228 * for example:
1229 * \code
1230 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1231 * \endcode
1232 * - Assign the result of the function psa_pake_operation_init()
1233 * to the structure, for example:
1234 * \code
1235 * psa_pake_operation_t operation;
1236 * operation = psa_pake_operation_init();
1237 * \endcode
1238 *
1239 * This is an implementation-defined \c struct. Applications should not
1240 * make any assumptions about the content of this structure.
1241 * Implementation details can change in future versions without notice. */
1242typedef struct psa_pake_operation_s psa_pake_operation_t;
1243
Przemek Stekiel51eac532022-12-07 11:04:51 +01001244/** The type of input values for PAKE operations. */
1245typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1246
Przemek Stekielb09c4872023-01-17 12:05:38 +01001247/** The type of computation stage for J-PAKE operations. */
Przemek Stekiele12ed362022-12-21 12:54:46 +01001248typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1249
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001250/** Return an initial value for a PAKE operation object.
Janos Follath702cf092021-05-26 12:58:23 +01001251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252static psa_pake_operation_t psa_pake_operation_init(void);
Janos Follath702cf092021-05-26 12:58:23 +01001253
Przemek Stekielc0e62502023-03-14 11:49:36 +01001254/** Get the length of the password in bytes from given inputs.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001255 *
1256 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001257 * \param[out] password_len Password length.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001258 *
1259 * \retval #PSA_SUCCESS
1260 * Success.
1261 * \retval #PSA_ERROR_BAD_STATE
1262 * Password hasn't been set yet.
1263 */
1264psa_status_t psa_crypto_driver_pake_get_password_len(
1265 const psa_crypto_driver_pake_inputs_t *inputs,
1266 size_t *password_len);
1267
1268/** Get the password from given inputs.
1269 *
1270 * \param[in] inputs Operation inputs.
1271 * \param[out] buffer Return buffer for password.
Przemek Stekiel6b648622023-02-19 22:55:33 +01001272 * \param buffer_size Size of the return buffer in bytes.
1273 * \param[out] buffer_length Actual size of the password in bytes.
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001274 *
1275 * \retval #PSA_SUCCESS
1276 * Success.
1277 * \retval #PSA_ERROR_BAD_STATE
1278 * Password hasn't been set yet.
1279 */
1280psa_status_t psa_crypto_driver_pake_get_password(
1281 const psa_crypto_driver_pake_inputs_t *inputs,
1282 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1283
Przemek Stekielc0e62502023-03-14 11:49:36 +01001284/** Get the length of the user id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001285 *
1286 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001287 * \param[out] user_len User id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001288 *
1289 * \retval #PSA_SUCCESS
1290 * Success.
1291 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001292 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001293 */
1294psa_status_t psa_crypto_driver_pake_get_user_len(
1295 const psa_crypto_driver_pake_inputs_t *inputs,
1296 size_t *user_len);
1297
Przemek Stekielc0e62502023-03-14 11:49:36 +01001298/** Get the length of the peer id in bytes from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001299 *
1300 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001301 * \param[out] peer_len Peer id length.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001302 *
1303 * \retval #PSA_SUCCESS
1304 * Success.
1305 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001306 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001307 */
1308psa_status_t psa_crypto_driver_pake_get_peer_len(
1309 const psa_crypto_driver_pake_inputs_t *inputs,
1310 size_t *peer_len);
1311
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001312/** Get the user id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001313 *
1314 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001315 * \param[out] user_id User id.
1316 * \param user_id_size Size of \p user_id in bytes.
1317 * \param[out] user_id_len Size of the user id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001318 *
1319 * \retval #PSA_SUCCESS
1320 * Success.
1321 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001322 * User id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001323 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001324 * The size of the \p user_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001325 */
1326psa_status_t psa_crypto_driver_pake_get_user(
1327 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001328 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001329
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001330/** Get the peer id from given inputs.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001331 *
1332 * \param[in] inputs Operation inputs.
Przemek Stekielc0e62502023-03-14 11:49:36 +01001333 * \param[out] peer_id Peer id.
1334 * \param peer_id_size Size of \p peer_id in bytes.
1335 * \param[out] peer_id_length Size of the peer id in bytes.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001336 *
1337 * \retval #PSA_SUCCESS
1338 * Success.
1339 * \retval #PSA_ERROR_BAD_STATE
Przemek Stekield7f6ad72023-03-06 13:39:52 +01001340 * Peer id hasn't been set yet.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001341 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Przemek Stekielc0e62502023-03-14 11:49:36 +01001342 * The size of the \p peer_id is too small.
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001343 */
1344psa_status_t psa_crypto_driver_pake_get_peer(
1345 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01001346 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01001347
Przemek Stekielca8d2b22023-01-17 16:21:33 +01001348/** Get the cipher suite from given inputs.
1349 *
1350 * \param[in] inputs Operation inputs.
1351 * \param[out] cipher_suite Return buffer for role.
1352 *
1353 * \retval #PSA_SUCCESS
1354 * Success.
1355 * \retval #PSA_ERROR_BAD_STATE
1356 * Cipher_suite hasn't been set yet.
1357 */
1358psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1359 const psa_crypto_driver_pake_inputs_t *inputs,
1360 psa_pake_cipher_suite_t *cipher_suite);
1361
Janos Follath702cf092021-05-26 12:58:23 +01001362/** Set the session information for a password-authenticated key exchange.
1363 *
1364 * The sequence of operations to set up a password-authenticated key exchange
1365 * is as follows:
1366 * -# Allocate an operation object which will be passed to all the functions
1367 * listed here.
1368 * -# Initialize the operation object with one of the methods described in the
1369 * documentation for #psa_pake_operation_t, e.g.
1370 * #PSA_PAKE_OPERATION_INIT.
1371 * -# Call psa_pake_setup() to specify the cipher suite.
1372 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1373 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1374 * to be called depends on the algorithm in use.
1375 *
1376 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1377 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1378 * for more information.
1379 *
1380 * A typical sequence of calls to perform a password-authenticated key
1381 * exchange:
1382 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1383 * key share that needs to be sent to the peer.
1384 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1385 * the key share that was received from the peer.
1386 * -# Depending on the algorithm additional calls to psa_pake_output() and
1387 * psa_pake_input() might be necessary.
1388 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1389 *
1390 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1391 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1392 * for more information.
1393 *
1394 * If an error occurs at any step after a call to psa_pake_setup(),
1395 * the operation will need to be reset by a call to psa_pake_abort(). The
1396 * application may call psa_pake_abort() at any time after the operation
1397 * has been initialized.
1398 *
1399 * After a successful call to psa_pake_setup(), the application must
1400 * eventually terminate the operation. The following events terminate an
1401 * operation:
1402 * - A call to psa_pake_abort().
1403 * - A successful call to psa_pake_get_implicit_key().
1404 *
1405 * \param[in,out] operation The operation object to set up. It must have
Janos Follath3293dae2021-06-03 13:21:33 +01001406 * been initialized but not set up yet.
Neil Armstrong47e700e2022-05-20 10:16:41 +02001407 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
Janos Follath702cf092021-05-26 12:58:23 +01001408 * characterizes a PAKE algorithm and determines
1409 * the algorithm as well.)
1410 *
1411 * \retval #PSA_SUCCESS
1412 * Success.
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001413 * \retval #PSA_ERROR_INVALID_ARGUMENT
1414 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1415 * PAKE primitive in \p cipher_suite is not compatible with the
1416 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1417 * or not compatible with the PAKE algorithm and primitive.
Janos Follath702cf092021-05-26 12:58:23 +01001418 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong4721a6f2022-05-20 10:53:00 +02001419 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1420 * or the PAKE primitive in \p cipher_suite is not supported or not
1421 * compatible with the PAKE algorithm, or the hash algorithm in
1422 * \p cipher_suite is not supported or not compatible with the PAKE
1423 * algorithm and primitive.
Gilles Peskineed733552023-02-14 19:21:09 +01001424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1425 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001426 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001427 * The operation state is not valid, or
1428 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001429 * It is implementation-dependent whether a failure to initialize
1430 * results in this error code.
1431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1433 const psa_pake_cipher_suite_t *cipher_suite);
Janos Follath702cf092021-05-26 12:58:23 +01001434
1435/** Set the password for a password-authenticated key exchange from key ID.
1436 *
1437 * Call this function when the password, or a value derived from the password,
Janos Follath52f9efa2021-05-27 08:40:16 +01001438 * is already present in the key store.
Janos Follath702cf092021-05-26 12:58:23 +01001439 *
1440 * \param[in,out] operation The operation object to set the password for. It
1441 * must have been set up by psa_pake_setup() and
1442 * not yet in use (neither psa_pake_output() nor
1443 * psa_pake_input() has been called yet). It must
1444 * be on operation for which the password hasn't
Janos Follath52f9efa2021-05-27 08:40:16 +01001445 * been set yet (psa_pake_set_password_key()
Janos Follath559f05e2021-05-26 15:44:30 +01001446 * hasn't been called yet).
Janos Follath702cf092021-05-26 12:58:23 +01001447 * \param password Identifier of the key holding the password or a
1448 * value derived from the password (eg. by a
1449 * memory-hard function). It must remain valid
1450 * until the operation terminates. It must be of
1451 * type #PSA_KEY_TYPE_PASSWORD or
1452 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1453 * the usage #PSA_KEY_USAGE_DERIVE.
1454 *
1455 * \retval #PSA_SUCCESS
1456 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001457 * \retval #PSA_ERROR_INVALID_HANDLE
Neil Armstrong71cae612022-05-20 11:00:49 +02001458 * \p password is not a valid key identifier.
Janos Follath702cf092021-05-26 12:58:23 +01001459 * \retval #PSA_ERROR_NOT_PERMITTED
Neil Armstrong71cae612022-05-20 11:00:49 +02001460 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1461 * permit the \p operation's algorithm.
1462 * \retval #PSA_ERROR_INVALID_ARGUMENT
1463 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1464 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1465 * the \p operation's cipher suite.
1466 * \retval #PSA_ERROR_NOT_SUPPORTED
1467 * The key type or key size of \p password is not supported with the
1468 * \p operation's cipher suite.
Gilles Peskineed733552023-02-14 19:21:09 +01001469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1470 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1471 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1472 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1473 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001474 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001475 * The operation state is not valid (it must have been set up.), or
1476 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001477 * It is implementation-dependent whether a failure to initialize
1478 * results in this error code.
1479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1481 mbedtls_svc_key_id_t password);
Janos Follath702cf092021-05-26 12:58:23 +01001482
Janos Follath702cf092021-05-26 12:58:23 +01001483/** Set the user ID for a password-authenticated key exchange.
1484 *
1485 * Call this function to set the user ID. For PAKE algorithms that associate a
1486 * user identifier with each side of the session you need to call
1487 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1488 * user identifier with the session, call psa_pake_set_user() only.
1489 *
1490 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1491 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1492 * for more information.
1493 *
1494 * \param[in,out] operation The operation object to set the user ID for. It
1495 * must have been set up by psa_pake_setup() and
1496 * not yet in use (neither psa_pake_output() nor
1497 * psa_pake_input() has been called yet). It must
1498 * be on operation for which the user ID hasn't
1499 * been set (psa_pake_set_user() hasn't been
1500 * called yet).
1501 * \param[in] user_id The user ID to authenticate with.
1502 * \param user_id_len Size of the \p user_id buffer in bytes.
1503 *
1504 * \retval #PSA_SUCCESS
1505 * Success.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001506 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong35851682022-05-20 11:02:37 +02001507 * \p user_id is not valid for the \p operation's algorithm and cipher
1508 * suite.
1509 * \retval #PSA_ERROR_NOT_SUPPORTED
1510 * The value of \p user_id is not supported by the implementation.
Gilles Peskineed733552023-02-14 19:21:09 +01001511 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1513 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001514 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001515 * The operation state is not valid, or
1516 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001517 * It is implementation-dependent whether a failure to initialize
1518 * results in this error code.
1519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1521 const uint8_t *user_id,
1522 size_t user_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001523
1524/** Set the peer ID for a password-authenticated key exchange.
1525 *
1526 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1527 * that associate a user identifier with each side of the session. For PAKE
1528 * algorithms that associate a single user identifier with the session, call
1529 * psa_pake_set_user() only.
1530 *
1531 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1532 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1533 * for more information.
1534 *
1535 * \param[in,out] operation The operation object to set the peer ID for. It
1536 * must have been set up by psa_pake_setup() and
1537 * not yet in use (neither psa_pake_output() nor
1538 * psa_pake_input() has been called yet). It must
1539 * be on operation for which the peer ID hasn't
1540 * been set (psa_pake_set_peer() hasn't been
1541 * called yet).
1542 * \param[in] peer_id The peer's ID to authenticate.
1543 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1544 *
1545 * \retval #PSA_SUCCESS
1546 * Success.
Neil Armstrong16ff7882022-05-20 11:04:20 +02001547 * \retval #PSA_ERROR_INVALID_ARGUMENT
1548 * \p user_id is not valid for the \p operation's algorithm and cipher
1549 * suite.
Janos Follath702cf092021-05-26 12:58:23 +01001550 * \retval #PSA_ERROR_NOT_SUPPORTED
1551 * The algorithm doesn't associate a second identity with the session.
Gilles Peskineed733552023-02-14 19:21:09 +01001552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1554 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001555 * \retval #PSA_ERROR_BAD_STATE
Neil Armstrong0d245752022-05-20 11:35:40 +02001556 * Calling psa_pake_set_peer() is invalid with the \p operation's
1557 * algorithm, the operation state is not valid, or the library has not
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001558 * been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001559 * It is implementation-dependent whether a failure to initialize
1560 * results in this error code.
1561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1563 const uint8_t *peer_id,
1564 size_t peer_id_len);
Janos Follath702cf092021-05-26 12:58:23 +01001565
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001566/** Set the application role for a password-authenticated key exchange.
Janos Follath702cf092021-05-26 12:58:23 +01001567 *
1568 * Not all PAKE algorithms need to differentiate the communicating entities.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001569 * It is optional to call this function for PAKEs that don't require a role
1570 * to be specified. For such PAKEs the application role parameter is ignored,
1571 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
Janos Follath702cf092021-05-26 12:58:23 +01001572 *
1573 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1574 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1575 * for more information.
1576 *
Neil Armstrongef157512022-05-25 11:49:45 +02001577 * \param[in,out] operation The operation object to specify the
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001578 * application's role for. It must have been set up
1579 * by psa_pake_setup() and not yet in use (neither
1580 * psa_pake_output() nor psa_pake_input() has been
1581 * called yet). It must be on operation for which
1582 * the application's role hasn't been specified
1583 * (psa_pake_set_role() hasn't been called yet).
1584 * \param role A value of type ::psa_pake_role_t indicating the
1585 * application's role in the PAKE the algorithm
1586 * that is being set up. For more information see
1587 * the documentation of \c PSA_PAKE_ROLE_XXX
1588 * constants.
Janos Follath702cf092021-05-26 12:58:23 +01001589 *
1590 * \retval #PSA_SUCCESS
1591 * Success.
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001592 * \retval #PSA_ERROR_INVALID_ARGUMENT
1593 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
Janos Follath702cf092021-05-26 12:58:23 +01001594 * \retval #PSA_ERROR_NOT_SUPPORTED
Neil Armstrong2a6dd9c2022-05-20 11:17:10 +02001595 * The \p role for this algorithm is not supported or is not valid.
Gilles Peskineed733552023-02-14 19:21:09 +01001596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1597 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001598 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001599 * The operation state is not valid, or
1600 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001601 * It is implementation-dependent whether a failure to initialize
1602 * results in this error code.
1603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1605 psa_pake_role_t role);
Janos Follath702cf092021-05-26 12:58:23 +01001606
1607/** Get output for a step of a password-authenticated key exchange.
1608 *
1609 * Depending on the algorithm being executed, you might need to call this
1610 * function several times or you might not need to call this at all.
1611 *
1612 * The exact sequence of calls to perform a password-authenticated key
1613 * exchange depends on the algorithm in use. Refer to the documentation of
1614 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1615 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1616 * information.
1617 *
1618 * If this function returns an error status, the operation enters an error
1619 * state and must be aborted by calling psa_pake_abort().
1620 *
1621 * \param[in,out] operation Active PAKE operation.
1622 * \param step The step of the algorithm for which the output is
1623 * requested.
1624 * \param[out] output Buffer where the output is to be written in the
1625 * format appropriate for this \p step. Refer to
1626 * the documentation of the individual
1627 * \c PSA_PAKE_STEP_XXX constants for more
1628 * information.
1629 * \param output_size Size of the \p output buffer in bytes. This must
Neil Armstrong7bc71e92022-05-20 10:36:14 +02001630 * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
1631 * primitive, \p step) where \p alg and
1632 * \p primitive are the PAKE algorithm and primitive
1633 * in the operation's cipher suite, and \p step is
1634 * the output step.
Janos Follath702cf092021-05-26 12:58:23 +01001635 *
1636 * \param[out] output_length On success, the number of bytes of the returned
1637 * output.
1638 *
1639 * \retval #PSA_SUCCESS
1640 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001641 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1642 * The size of the \p output buffer is too small.
Neil Armstrong664077e2022-05-20 11:24:41 +02001643 * \retval #PSA_ERROR_INVALID_ARGUMENT
1644 * \p step is not compatible with the operation's algorithm.
1645 * \retval #PSA_ERROR_NOT_SUPPORTED
1646 * \p step is not supported with the operation's algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001647 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1650 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1651 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1652 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1653 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001654 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001655 * The operation state is not valid (it must be active, and fully set
1656 * up, and this call must conform to the algorithm's requirements
1657 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001658 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001659 * It is implementation-dependent whether a failure to initialize
1660 * results in this error code.
1661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1663 psa_pake_step_t step,
1664 uint8_t *output,
1665 size_t output_size,
1666 size_t *output_length);
Janos Follath702cf092021-05-26 12:58:23 +01001667
1668/** Provide input for a step of a password-authenticated key exchange.
1669 *
1670 * Depending on the algorithm being executed, you might need to call this
1671 * function several times or you might not need to call this at all.
1672 *
1673 * The exact sequence of calls to perform a password-authenticated key
1674 * exchange depends on the algorithm in use. Refer to the documentation of
1675 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1676 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1677 * information.
1678 *
1679 * If this function returns an error status, the operation enters an error
1680 * state and must be aborted by calling psa_pake_abort().
1681 *
1682 * \param[in,out] operation Active PAKE operation.
1683 * \param step The step for which the input is provided.
Neil Armstrong799106b2022-05-20 10:18:53 +02001684 * \param[in] input Buffer containing the input in the format
Janos Follath702cf092021-05-26 12:58:23 +01001685 * appropriate for this \p step. Refer to the
1686 * documentation of the individual
1687 * \c PSA_PAKE_STEP_XXX constants for more
1688 * information.
Neil Armstrong799106b2022-05-20 10:18:53 +02001689 * \param input_length Size of the \p input buffer in bytes.
Janos Follath702cf092021-05-26 12:58:23 +01001690 *
1691 * \retval #PSA_SUCCESS
1692 * Success.
Neil Armstrong407b27b2022-05-20 11:28:23 +02001693 * \retval #PSA_ERROR_INVALID_SIGNATURE
1694 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001695 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong407b27b2022-05-20 11:28:23 +02001696 * \p is not compatible with the \p operation’s algorithm, or the
1697 * \p input is not valid for the \p operation's algorithm, cipher suite
1698 * or \p step.
1699 * \retval #PSA_ERROR_NOT_SUPPORTED
1700 * \p step p is not supported with the \p operation's algorithm, or the
1701 * \p input is not supported for the \p operation's algorithm, cipher
1702 * suite or \p step.
Gilles Peskineed733552023-02-14 19:21:09 +01001703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1704 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1705 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1706 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1707 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1708 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001709 * \retval #PSA_ERROR_BAD_STATE
Neil Armstronge9b45812022-05-20 11:39:09 +02001710 * The operation state is not valid (it must be active, and fully set
1711 * up, and this call must conform to the algorithm's requirements
1712 * for ordering of input and output steps), or
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001713 * the library has not been previously initialized by psa_crypto_init().
Janos Follath702cf092021-05-26 12:58:23 +01001714 * It is implementation-dependent whether a failure to initialize
1715 * results in this error code.
1716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1718 psa_pake_step_t step,
1719 const uint8_t *input,
1720 size_t input_length);
Janos Follath702cf092021-05-26 12:58:23 +01001721
1722/** Get implicitly confirmed shared secret from a PAKE.
1723 *
1724 * At this point there is a cryptographic guarantee that only the authenticated
1725 * party who used the same password is able to compute the key. But there is no
Janos Follatha46e28f2021-06-03 13:07:03 +01001726 * guarantee that the peer is the party it claims to be and was able to do so.
Janos Follath702cf092021-05-26 12:58:23 +01001727 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001728 * That is, the authentication is only implicit. Since the peer is not
1729 * authenticated yet, no action should be taken yet that assumes that the peer
1730 * is who it claims to be. For example, do not access restricted files on the
1731 * peer's behalf until an explicit authentication has succeeded.
Janos Follath702cf092021-05-26 12:58:23 +01001732 *
1733 * This function can be called after the key exchange phase of the operation
1734 * has completed. It imports the shared secret output of the PAKE into the
1735 * provided derivation operation. The input step
1736 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1737 * material in the key derivation operation.
1738 *
1739 * The exact sequence of calls to perform a password-authenticated key
1740 * exchange depends on the algorithm in use. Refer to the documentation of
1741 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1742 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1743 * information.
1744 *
1745 * When this function returns successfully, \p operation becomes inactive.
1746 * If this function returns an error status, both \p operation
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -05001747 * and \c key_derivation operations enter an error state and must be aborted by
Janos Follath702cf092021-05-26 12:58:23 +01001748 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1749 *
1750 * \param[in,out] operation Active PAKE operation.
1751 * \param[out] output A key derivation operation that is ready
1752 * for an input step of type
1753 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1754 *
1755 * \retval #PSA_SUCCESS
1756 * Success.
Janos Follath702cf092021-05-26 12:58:23 +01001757 * \retval #PSA_ERROR_INVALID_ARGUMENT
Neil Armstrong97d74b82022-05-20 11:30:31 +02001758 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1759 * algorithm in the \p output key derivation operation.
1760 * \retval #PSA_ERROR_NOT_SUPPORTED
1761 * Input from a PAKE is not supported by the algorithm in the \p output
1762 * key derivation operation.
Gilles Peskineed733552023-02-14 19:21:09 +01001763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1764 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1765 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1766 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1767 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1768 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Janos Follath702cf092021-05-26 12:58:23 +01001769 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001770 * The PAKE operation state is not valid (it must be active, but beyond
1771 * that validity is specific to the algorithm), or
1772 * the library has not been previously initialized by psa_crypto_init(),
1773 * or the state of \p output is not valid for
1774 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1775 * step is out of order or the application has done this step already
1776 * and it may not be repeated.
Janos Follath702cf092021-05-26 12:58:23 +01001777 * It is implementation-dependent whether a failure to initialize
1778 * results in this error code.
1779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1781 psa_key_derivation_operation_t *output);
Janos Follath702cf092021-05-26 12:58:23 +01001782
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001783/** Abort a PAKE operation.
1784 *
1785 * Aborting an operation frees all associated resources except for the \c
1786 * operation structure itself. Once aborted, the operation object can be reused
1787 * for another operation by calling psa_pake_setup() again.
1788 *
1789 * This function may be called at any time after the operation
1790 * object has been initialized as described in #psa_pake_operation_t.
1791 *
1792 * In particular, calling psa_pake_abort() after the operation has been
1793 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1794 * is safe and has no effect.
1795 *
1796 * \param[in,out] operation The operation to abort.
1797 *
1798 * \retval #PSA_SUCCESS
Neil Armstrong59fa8ee2022-05-20 11:31:04 +02001799 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001800 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1801 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001802 * \retval #PSA_ERROR_BAD_STATE
1803 * The library has not been previously initialized by psa_crypto_init().
1804 * It is implementation-dependent whether a failure to initialize
1805 * results in this error code.
1806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001807psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
Neil Armstrong0c8ef932022-05-20 10:23:51 +02001808
Janos Follath702cf092021-05-26 12:58:23 +01001809/**@}*/
1810
1811/** A sufficient output buffer size for psa_pake_output().
1812 *
1813 * If the size of the output buffer is at least this large, it is guaranteed
1814 * that psa_pake_output() will not fail due to an insufficient output buffer
1815 * size. The actual size of the output might be smaller in any given call.
1816 *
1817 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1818 *
Janos Follath46c02372021-06-08 15:22:51 +01001819 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001820 * #PSA_ALG_IS_PAKE(\p alg) is true).
1821 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1822 * compatible with algorithm \p alg.
1823 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1824 * algorithm \p alg.
1825 * \return A sufficient output buffer size for the specified
Neil Armstrongcd974d52022-05-20 10:30:12 +02001826 * PAKE algorithm, primitive, and output step. If the
1827 * PAKE algorithm, primitive, or output step is not
1828 * recognized, or the parameters are incompatible,
1829 * return 0.
Janos Follath702cf092021-05-26 12:58:23 +01001830 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001831#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 (alg == PSA_ALG_JPAKE && \
1833 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1834 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1835 ( \
1836 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1837 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1838 32 \
1839 ) : \
1840 0)
Janos Follath702cf092021-05-26 12:58:23 +01001841
1842/** A sufficient input buffer size for psa_pake_input().
1843 *
Janos Follathb4db90f2021-06-03 13:17:09 +01001844 * The value returned by this macro is guaranteed to be large enough for any
1845 * valid input to psa_pake_input() in an operation with the specified
1846 * parameters.
Janos Follath702cf092021-05-26 12:58:23 +01001847 *
1848 * See also #PSA_PAKE_INPUT_MAX_SIZE
1849 *
Janos Follath46c02372021-06-08 15:22:51 +01001850 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
Janos Follath702cf092021-05-26 12:58:23 +01001851 * #PSA_ALG_IS_PAKE(\p alg) is true).
1852 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1853 * compatible with algorithm \p alg.
Janos Follathec83eb62021-05-27 08:41:59 +01001854 * \param input_step A value of type ::psa_pake_step_t that is valid for the
Janos Follath702cf092021-05-26 12:58:23 +01001855 * algorithm \p alg.
Janos Follath38d29db2021-06-03 13:14:42 +01001856 * \return A sufficient input buffer size for the specified
1857 * input, cipher suite and algorithm. If the cipher suite,
1858 * the input type or PAKE algorithm is not recognized, or
Janos Follath702cf092021-05-26 12:58:23 +01001859 * the parameters are incompatible, return 0.
1860 */
Neil Armstrong7aaa34a2022-06-08 14:05:02 +02001861#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 (alg == PSA_ALG_JPAKE && \
1863 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1864 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1865 ( \
1866 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1867 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1868 32 \
1869 ) : \
1870 0)
Janos Follath702cf092021-05-26 12:58:23 +01001871
Neil Armstrong2056ce52022-05-25 11:38:15 +02001872/** Output buffer size for psa_pake_output() for any of the supported PAKE
1873 * algorithm and primitive suites and output step.
Janos Follath702cf092021-05-26 12:58:23 +01001874 *
1875 * This macro must expand to a compile-time constant integer.
1876 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001877 * The value of this macro must be at least as large as the largest value
1878 * returned by PSA_PAKE_OUTPUT_SIZE()
1879 *
Neil Armstrong2056ce52022-05-25 11:38:15 +02001880 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p step).
Janos Follath702cf092021-05-26 12:58:23 +01001881 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001882#define PSA_PAKE_OUTPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001883
Neil Armstrong2056ce52022-05-25 11:38:15 +02001884/** Input buffer size for psa_pake_input() for any of the supported PAKE
1885 * algorithm and primitive suites and input step.
Janos Follath702cf092021-05-26 12:58:23 +01001886 *
1887 * This macro must expand to a compile-time constant integer.
1888 *
Przemek Stekiel7921a032023-04-14 14:29:57 +02001889 * The value of this macro must be at least as large as the largest value
1890 * returned by PSA_PAKE_INPUT_SIZE()
1891 *
Neil Armstrong2056ce52022-05-25 11:38:15 +02001892 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p step).
Janos Follath702cf092021-05-26 12:58:23 +01001893 */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001894#define PSA_PAKE_INPUT_MAX_SIZE 65
Janos Follath702cf092021-05-26 12:58:23 +01001895
Neil Armstrongfb993022022-05-20 10:08:58 +02001896/** Returns a suitable initializer for a PAKE cipher suite object of type
1897 * psa_pake_cipher_suite_t.
1898 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001899#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
Neil Armstrongfb993022022-05-20 10:08:58 +02001900
Neil Armstrong0151c552022-05-20 10:13:53 +02001901/** Returns a suitable initializer for a PAKE operation object of type
1902 * psa_pake_operation_t.
1903 */
Przemek Stekiel656b2592023-03-22 13:15:33 +01001904#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001905 { 0 }, { { 0 } } }
Neil Armstrong0151c552022-05-20 10:13:53 +02001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907struct psa_pake_cipher_suite_s {
Janos Follath702cf092021-05-26 12:58:23 +01001908 psa_algorithm_t algorithm;
1909 psa_pake_primitive_type_t type;
1910 psa_pake_family_t family;
1911 uint16_t bits;
1912 psa_algorithm_t hash;
1913};
1914
1915static inline psa_algorithm_t psa_pake_cs_get_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001917{
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return cipher_suite->algorithm;
Janos Follath702cf092021-05-26 12:58:23 +01001919}
1920
1921static inline void psa_pake_cs_set_algorithm(
1922 psa_pake_cipher_suite_t *cipher_suite,
1923 psa_algorithm_t algorithm)
1924{
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (!PSA_ALG_IS_PAKE(algorithm)) {
Janos Follath702cf092021-05-26 12:58:23 +01001926 cipher_suite->algorithm = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001928 cipher_suite->algorithm = algorithm;
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 }
Janos Follath702cf092021-05-26 12:58:23 +01001930}
1931
1932static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001934{
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1936 cipher_suite->bits);
Janos Follath702cf092021-05-26 12:58:23 +01001937}
1938
1939static inline void psa_pake_cs_set_primitive(
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 psa_pake_cipher_suite_t *cipher_suite,
1941 psa_pake_primitive_t primitive)
Janos Follath702cf092021-05-26 12:58:23 +01001942{
1943 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1944 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1945 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1946}
1947
Neil Armstrongff9cac72022-05-20 10:25:15 +02001948static inline psa_pake_family_t psa_pake_cs_get_family(
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongff9cac72022-05-20 10:25:15 +02001950{
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 return cipher_suite->family;
Neil Armstrongff9cac72022-05-20 10:25:15 +02001952}
1953
Neil Armstrongd5a48252022-05-20 10:26:36 +02001954static inline uint16_t psa_pake_cs_get_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstrongd5a48252022-05-20 10:26:36 +02001956{
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 return cipher_suite->bits;
Neil Armstrongd5a48252022-05-20 10:26:36 +02001958}
1959
Janos Follath702cf092021-05-26 12:58:23 +01001960static inline psa_algorithm_t psa_pake_cs_get_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 const psa_pake_cipher_suite_t *cipher_suite)
Janos Follath702cf092021-05-26 12:58:23 +01001962{
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 return cipher_suite->hash;
Janos Follath702cf092021-05-26 12:58:23 +01001964}
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1967 psa_algorithm_t hash)
Janos Follath702cf092021-05-26 12:58:23 +01001968{
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (!PSA_ALG_IS_HASH(hash)) {
Janos Follath702cf092021-05-26 12:58:23 +01001970 cipher_suite->hash = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 } else {
Janos Follath702cf092021-05-26 12:58:23 +01001972 cipher_suite->hash = hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 }
Janos Follath702cf092021-05-26 12:58:23 +01001974}
1975
Przemek Stekiel51eac532022-12-07 11:04:51 +01001976struct psa_crypto_driver_pake_inputs_s {
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 uint8_t *MBEDTLS_PRIVATE(password);
Przemek Stekiel152ae072022-11-17 13:24:36 +01001978 size_t MBEDTLS_PRIVATE(password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01001979 uint8_t *MBEDTLS_PRIVATE(user);
1980 size_t MBEDTLS_PRIVATE(user_len);
1981 uint8_t *MBEDTLS_PRIVATE(peer);
1982 size_t MBEDTLS_PRIVATE(peer_len);
Przemek Stekiel9dd24402023-01-26 15:06:09 +01001983 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Przemek Stekiel51eac532022-12-07 11:04:51 +01001984 psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
1985};
1986
Przemek Stekiel251e86a2023-02-17 14:30:50 +01001987typedef enum psa_crypto_driver_pake_step {
Przemek Stekielb09c4872023-01-17 12:05:38 +01001988 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1989 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1990 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1991 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1992 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1993 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1994 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1995 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1996 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1997 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1998 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1999 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
2000 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 +01002001} psa_crypto_driver_pake_step_t;
Przemek Stekiel57980032023-01-09 15:07:26 +01002002
David Horstmanne7f21e62023-05-12 18:17:21 +01002003typedef enum psa_jpake_round {
David Horstmann5da95602023-06-08 15:37:12 +01002004 PSA_JPAKE_FIRST = 0,
2005 PSA_JPAKE_SECOND = 1,
2006 PSA_JPAKE_FINISHED = 2
David Horstmanne7f21e62023-05-12 18:17:21 +01002007} psa_jpake_round_t;
2008
2009typedef enum psa_jpake_io_mode {
David Horstmann5da95602023-06-08 15:37:12 +01002010 PSA_JPAKE_INPUT = 0,
2011 PSA_JPAKE_OUTPUT = 1
David Horstmanne7f21e62023-05-12 18:17:21 +01002012} psa_jpake_io_mode_t;
Przemek Stekielb09c4872023-01-17 12:05:38 +01002013
Przemek Stekiele12ed362022-12-21 12:54:46 +01002014struct psa_jpake_computation_stage_s {
David Horstmanne7f21e62023-05-12 18:17:21 +01002015 /* The J-PAKE round we are currently on */
2016 psa_jpake_round_t MBEDTLS_PRIVATE(round);
2017 /* The 'mode' we are currently in (inputting or outputting) */
David Horstmann024e5c52023-06-14 15:48:21 +01002018 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
David Horstmann279d2272023-06-14 17:13:56 +01002019 /* The number of completed inputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01002020 uint8_t MBEDTLS_PRIVATE(inputs);
David Horstmann279d2272023-06-14 17:13:56 +01002021 /* The number of completed outputs so far this round */
David Horstmanne7f21e62023-05-12 18:17:21 +01002022 uint8_t MBEDTLS_PRIVATE(outputs);
2023 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
2024 psa_pake_step_t MBEDTLS_PRIVATE(step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01002025};
2026
David Horstmann5dbe17d2023-06-27 10:30:28 +01002027#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
2028 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
2029#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
2030 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
David Horstmanne7f21e62023-05-12 18:17:21 +01002031
Janos Follath702cf092021-05-26 12:58:23 +01002032struct psa_pake_operation_s {
Przemek Stekield91bcb72022-11-22 14:00:51 +01002033 /** Unique ID indicating which driver got assigned to do the
2034 * operation. Since driver contexts are driver-specific, swapping
2035 * drivers halfway through the operation is not supported.
2036 * ID values are auto-generated in psa_crypto_driver_wrappers.h
2037 * ID value zero means the context is not valid or not assigned to
2038 * any driver (i.e. none of the driver contexts are active). */
2039 unsigned int MBEDTLS_PRIVATE(id);
Przemek Stekiel6b648622023-02-19 22:55:33 +01002040 /* Algorithm of the PAKE operation */
Przemek Stekiele12ed362022-12-21 12:54:46 +01002041 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Przemek Stekiel656b2592023-03-22 13:15:33 +01002042 /* A primitive of type compatible with algorithm */
2043 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
Przemek Stekiel6b648622023-02-19 22:55:33 +01002044 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
2045 * or computing. */
Przemek Stekiel51eac532022-12-07 11:04:51 +01002046 uint8_t MBEDTLS_PRIVATE(stage);
Przemek Stekiele12ed362022-12-21 12:54:46 +01002047 /* Holds computation stage of the PAKE algorithms. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 union {
Przemek Stekiel251e86a2023-02-17 14:30:50 +01002049 uint8_t MBEDTLS_PRIVATE(dummy);
Przemek Stekiel4aa99402023-02-27 13:00:57 +01002050#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekieldde6a912023-01-26 08:46:37 +01002051 psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
Neil Armstrong35269d92022-05-25 11:26:31 +02002052#endif
Przemek Stekieldde6a912023-01-26 08:46:37 +01002053 } MBEDTLS_PRIVATE(computation_stage);
Przemek Stekiel51eac532022-12-07 11:04:51 +01002054 union {
Przemek Stekiel51eac532022-12-07 11:04:51 +01002055 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
Przemek Stekielac067d72023-01-26 16:31:03 +01002056 psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
Przemek Stekiel51eac532022-12-07 11:04:51 +01002057 } MBEDTLS_PRIVATE(data);
Janos Follath702cf092021-05-26 12:58:23 +01002058};
2059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02002061{
2062 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 return v;
Neil Armstrong5ff6a7f2022-05-20 10:12:01 +02002064}
2065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066static inline struct psa_pake_operation_s psa_pake_operation_init(void)
Janos Follath702cf092021-05-26 12:58:23 +01002067{
2068 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return v;
Janos Follath702cf092021-05-26 12:58:23 +01002070}
2071
Gilles Peskinee59236f2018-01-27 23:32:46 +01002072#ifdef __cplusplus
2073}
2074#endif
2075
2076#endif /* PSA_CRYPTO_EXTRA_H */