v2 / thirdparty / mbedtls / include / psa / crypto_extra.h
2145 lines · 2010 sloc · 91.22 KB · 3d9911f887ecec942f9ae2a5be02d064f233b729
Raw
1/**
2 * \file psa/crypto_extra.h
3 *
4 * \brief PSA cryptography module: Mbed TLS vendor extensions
5 *
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.
10 */
11/*
12 * Copyright The Mbed TLS Contributors
13 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
14 */
15
16#ifndef PSA_CRYPTO_EXTRA_H
17#define PSA_CRYPTO_EXTRA_H
18#include "mbedtls/private_access.h"
19
20#include "crypto_types.h"
21#include "crypto_compat.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* UID for secure storage seed */
28#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
29
30/* See mbedtls_config.h for definition */
31#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
32#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
33#endif
34
35/* If the size of static key slots is not explicitly defined by the user, then
36 * try to guess it based on some of the most common the key types enabled in the build.
37 * See mbedtls_config.h for the definition of MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE. */
38#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)
39
40#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE 1
41
42#if PSA_EXPORT_ASYMMETRIC_KEY_MAX_SIZE > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
43#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
44#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_EXPORT_ASYMMETRIC_KEY_MAX_SIZE
45#endif
46
47/* This covers ciphers, AEADs and CMAC. */
48#if PSA_CIPHER_MAX_KEY_LENGTH > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
49#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
50#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_CIPHER_MAX_KEY_LENGTH
51#endif
52
53/* For HMAC, it's typical but not mandatory to use a key size that is equal to
54 * the hash size. */
55#if defined(PSA_WANT_ALG_HMAC)
56#if PSA_HASH_MAX_SIZE > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
57#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
58#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_HASH_MAX_SIZE
59#endif
60#endif /* PSA_WANT_ALG_HMAC */
61
62#endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/
63
64/** \addtogroup attributes
65 * @{
66 */
67
68/** \brief Declare the enrollment algorithm for a key.
69 *
70 * An operation on a key may indifferently use the algorithm set with
71 * psa_set_key_algorithm() or with this function.
72 *
73 * \param[out] attributes The attribute structure to write to.
74 * \param alg2 A second algorithm that the key may be used
75 * for, in addition to the algorithm set with
76 * psa_set_key_algorithm().
77 *
78 * \warning Setting an enrollment algorithm is not recommended, because
79 * using the same key with different algorithms can allow some
80 * attacks based on arithmetic relations between different
81 * computations made with the same key, or can escalate harmless
82 * side channels into exploitable ones. Use this function only
83 * if it is necessary to support a protocol for which it has been
84 * verified that the usage of the key with multiple algorithms
85 * is safe.
86 */
87static inline void psa_set_key_enrollment_algorithm(
88 psa_key_attributes_t *attributes,
89 psa_algorithm_t alg2)
90{
91 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
92}
93
94/** Retrieve the enrollment algorithm policy from key attributes.
95 *
96 * \param[in] attributes The key attribute structure to query.
97 *
98 * \return The enrollment algorithm stored in the attribute structure.
99 */
100static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
101 const psa_key_attributes_t *attributes)
102{
103 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
104}
105
106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
107
108/** Retrieve the slot number where a key is stored.
109 *
110 * A slot number is only defined for keys that are stored in a secure
111 * element.
112 *
113 * This information is only useful if the secure element is not entirely
114 * managed through the PSA Cryptography API. It is up to the secure
115 * element driver to decide how PSA slot numbers map to any other interface
116 * that the secure element may have.
117 *
118 * \param[in] attributes The key attribute structure to query.
119 * \param[out] slot_number On success, the slot number containing the key.
120 *
121 * \retval #PSA_SUCCESS
122 * The key is located in a secure element, and \p *slot_number
123 * indicates the slot number that contains it.
124 * \retval #PSA_ERROR_NOT_PERMITTED
125 * The caller is not permitted to query the slot number.
126 * Mbed TLS currently does not return this error.
127 * \retval #PSA_ERROR_INVALID_ARGUMENT
128 * The key is not located in a secure element.
129 */
130psa_status_t psa_get_key_slot_number(
131 const psa_key_attributes_t *attributes,
132 psa_key_slot_number_t *slot_number);
133
134/** Choose the slot number where a key is stored.
135 *
136 * This function declares a slot number in the specified attribute
137 * structure.
138 *
139 * A slot number is only meaningful for keys that are stored in a secure
140 * element. It is up to the secure element driver to decide how PSA slot
141 * numbers map to any other interface that the secure element may have.
142 *
143 * \note Setting a slot number in key attributes for a key creation can
144 * cause the following errors when creating the key:
145 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
146 * not support choosing a specific slot number.
147 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
148 * choose slot numbers in general or to choose this specific slot.
149 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
150 * valid in general or not valid for this specific key.
151 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
152 * selected slot.
153 *
154 * \param[out] attributes The attribute structure to write to.
155 * \param slot_number The slot number to set.
156 */
157static inline void psa_set_key_slot_number(
158 psa_key_attributes_t *attributes,
159 psa_key_slot_number_t slot_number)
160{
161 attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;
162 attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
163}
164
165/** Remove the slot number attribute from a key attribute structure.
166 *
167 * This function undoes the action of psa_set_key_slot_number().
168 *
169 * \param[out] attributes The attribute structure to write to.
170 */
171static inline void psa_clear_key_slot_number(
172 psa_key_attributes_t *attributes)
173{
174 attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;
175}
176
177/** Register a key that is already present in a secure element.
178 *
179 * The key must be located in a secure element designated by the
180 * lifetime field in \p attributes, in the slot set with
181 * psa_set_key_slot_number() in the attribute structure.
182 * This function makes the key available through the key identifier
183 * specified in \p attributes.
184 *
185 * \param[in] attributes The attributes of the existing key.
186 * - The lifetime must be a persistent lifetime
187 * in a secure element. Volatile lifetimes are
188 * not currently supported.
189 * - The key identifier must be in the valid
190 * range for persistent keys.
191 * - The key type and size must be specified and
192 * must be consistent with the key material
193 * in the secure element.
194 *
195 * \retval #PSA_SUCCESS
196 * The key was successfully registered.
197 * Note that depending on the design of the driver, this may or may
198 * not guarantee that a key actually exists in the designated slot
199 * and is compatible with the specified attributes.
200 * \retval #PSA_ERROR_ALREADY_EXISTS
201 * There is already a key with the identifier specified in
202 * \p attributes.
203 * \retval #PSA_ERROR_NOT_SUPPORTED
204 * The secure element driver for the specified lifetime does not
205 * support registering a key.
206 * \retval #PSA_ERROR_INVALID_ARGUMENT
207 * The identifier in \p attributes is invalid, namely the identifier is
208 * not in the user range, or
209 * \p attributes specifies a lifetime which is not located
210 * in a secure element, or no slot number is specified in \p attributes,
211 * or the specified slot number is not valid.
212 * \retval #PSA_ERROR_NOT_PERMITTED
213 * The caller is not authorized to register the specified key slot.
214 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
215 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
217 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
218 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
219 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
220 * \retval #PSA_ERROR_BAD_STATE
221 * The library has not been previously initialized by psa_crypto_init().
222 * It is implementation-dependent whether a failure to initialize
223 * results in this error code.
224 */
225psa_status_t mbedtls_psa_register_se_key(
226 const psa_key_attributes_t *attributes);
227
228#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
229
230/**@}*/
231
232/**
233 * \brief Library deinitialization.
234 *
235 * This function clears all data associated with the PSA layer,
236 * including the whole key store.
237 * This function is not thread safe, it wipes every key slot regardless of
238 * state and reader count. It should only be called when no slot is in use.
239 *
240 * This is an Mbed TLS extension.
241 */
242void mbedtls_psa_crypto_free(void);
243
244/** \brief Statistics about
245 * resource consumption related to the PSA keystore.
246 *
247 * \note The content of this structure is not part of the stable API and ABI
248 * of Mbed TLS and may change arbitrarily from version to version.
249 */
250typedef struct mbedtls_psa_stats_s {
251 /** Number of slots containing key material for a volatile key. */
252 size_t MBEDTLS_PRIVATE(volatile_slots);
253 /** Number of slots containing key material for a key which is in
254 * internal persistent storage. */
255 size_t MBEDTLS_PRIVATE(persistent_slots);
256 /** Number of slots containing a reference to a key in a
257 * secure element. */
258 size_t MBEDTLS_PRIVATE(external_slots);
259 /** Number of slots which are occupied, but do not contain
260 * key material yet. */
261 size_t MBEDTLS_PRIVATE(half_filled_slots);
262 /** Number of slots that contain cache data. */
263 size_t MBEDTLS_PRIVATE(cache_slots);
264 /** Number of slots that are not used for anything. */
265 size_t MBEDTLS_PRIVATE(empty_slots);
266 /** Number of slots that are locked. */
267 size_t MBEDTLS_PRIVATE(locked_slots);
268 /** Largest key id value among open keys in internal persistent storage. */
269 psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
270 /** Largest key id value among open keys in secure elements. */
271 psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
272} mbedtls_psa_stats_t;
273
274/** \brief Get statistics about
275 * resource consumption related to the PSA keystore.
276 *
277 * \note When Mbed TLS is built as part of a service, with isolation
278 * between the application and the keystore, the service may or
279 * may not expose this function.
280 */
281void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
282
283/**
284 * \brief Inject an initial entropy seed for the random generator into
285 * secure storage.
286 *
287 * This function injects data to be used as a seed for the random generator
288 * used by the PSA Crypto implementation. On devices that lack a trusted
289 * entropy source (preferably a hardware random number generator),
290 * the Mbed PSA Crypto implementation uses this value to seed its
291 * random generator.
292 *
293 * On devices without a trusted entropy source, this function must be
294 * called exactly once in the lifetime of the device. On devices with
295 * a trusted entropy source, calling this function is optional.
296 * In all cases, this function may only be called before calling any
297 * other function in the PSA Crypto API, including psa_crypto_init().
298 *
299 * When this function returns successfully, it populates a file in
300 * persistent storage. Once the file has been created, this function
301 * can no longer succeed.
302 *
303 * If any error occurs, this function does not change the system state.
304 * You can call this function again after correcting the reason for the
305 * error if possible.
306 *
307 * \warning This function **can** fail! Callers MUST check the return status.
308 *
309 * \warning If you use this function, you should use it as part of a
310 * factory provisioning process. The value of the injected seed
311 * is critical to the security of the device. It must be
312 * *secret*, *unpredictable* and (statistically) *unique per device*.
313 * You should be generate it randomly using a cryptographically
314 * secure random generator seeded from trusted entropy sources.
315 * You should transmit it securely to the device and ensure
316 * that its value is not leaked or stored anywhere beyond the
317 * needs of transmitting it from the point of generation to
318 * the call of this function, and erase all copies of the value
319 * once this function returns.
320 *
321 * This is an Mbed TLS extension.
322 *
323 * \note This function is only available on the following platforms:
324 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
325 * Note that you must provide compatible implementations of
326 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
327 * * In a client-server integration of PSA Cryptography, on the client side,
328 * if the server supports this feature.
329 * \param[in] seed Buffer containing the seed value to inject.
330 * \param[in] seed_size Size of the \p seed buffer.
331 * The size of the seed in bytes must be greater
332 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
333 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
334 * in `library/entropy_poll.h` in the Mbed TLS source
335 * code.
336 * It must be less or equal to
337 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
338 *
339 * \retval #PSA_SUCCESS
340 * The seed value was injected successfully. The random generator
341 * of the PSA Crypto implementation is now ready for use.
342 * You may now call psa_crypto_init() and use the PSA Crypto
343 * implementation.
344 * \retval #PSA_ERROR_INVALID_ARGUMENT
345 * \p seed_size is out of range.
346 * \retval #PSA_ERROR_STORAGE_FAILURE
347 * There was a failure reading or writing from storage.
348 * \retval #PSA_ERROR_NOT_PERMITTED
349 * The library has already been initialized. It is no longer
350 * possible to call this function.
351 */
352psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
353 size_t seed_size);
354
355/** \addtogroup crypto_types
356 * @{
357 */
358
359/** DSA public key.
360 *
361 * The import and export format is the
362 * representation of the public key `y = g^x mod p` as a big-endian byte
363 * string. The length of the byte string is the length of the base prime `p`
364 * in bytes.
365 */
366#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
367
368/** DSA key pair (private and public key).
369 *
370 * The import and export format is the
371 * representation of the private key `x` as a big-endian byte string. The
372 * length of the byte string is the private key size in bytes (leading zeroes
373 * are not stripped).
374 *
375 * Deterministic DSA key derivation with psa_generate_derived_key follows
376 * FIPS 186-4 §B.1.2: interpret the byte string as integer
377 * in big-endian order. Discard it if it is not in the range
378 * [0, *N* - 2] where *N* is the boundary of the private key domain
379 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
380 * or the order of the curve's base point for ECC).
381 * Add 1 to the resulting integer and use this as the private key *x*.
382 *
383 */
384#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
385
386/** Whether a key type is a DSA key (pair or public-only). */
387#define PSA_KEY_TYPE_IS_DSA(type) \
388 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
389
390#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
391/** DSA signature with hashing.
392 *
393 * This is the signature scheme defined by FIPS 186-4,
394 * with a random per-message secret number (*k*).
395 *
396 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
397 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
398 * This includes #PSA_ALG_ANY_HASH
399 * when specifying the algorithm in a usage policy.
400 *
401 * \return The corresponding DSA signature algorithm.
402 * \return Unspecified if \p hash_alg is not a supported
403 * hash algorithm.
404 */
405#define PSA_ALG_DSA(hash_alg) \
406 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
407#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
408#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
409/** Deterministic DSA signature with hashing.
410 *
411 * This is the deterministic variant defined by RFC 6979 of
412 * the signature scheme defined by FIPS 186-4.
413 *
414 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
415 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
416 * This includes #PSA_ALG_ANY_HASH
417 * when specifying the algorithm in a usage policy.
418 *
419 * \return The corresponding DSA signature algorithm.
420 * \return Unspecified if \p hash_alg is not a supported
421 * hash algorithm.
422 */
423#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
424 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
425#define PSA_ALG_IS_DSA(alg) \
426 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
427 PSA_ALG_DSA_BASE)
428#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
429 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
430#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
431 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
432#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
433 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
434
435
436/* We need to expand the sample definition of this macro from
437 * the API definition. */
438#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
439#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
440 PSA_ALG_IS_DSA(alg)
441
442/**@}*/
443
444/** \addtogroup attributes
445 * @{
446 */
447
448/** PAKE operation stages. */
449#define PSA_PAKE_OPERATION_STAGE_SETUP 0
450#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
451#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
452
453/**@}*/
454
455
456/** \defgroup psa_rng Random generator
457 * @{
458 */
459
460#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
461/** External random generator function, implemented by the platform.
462 *
463 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
464 * this function replaces Mbed TLS's entropy and DRBG modules for all
465 * random generation triggered via PSA crypto interfaces.
466 *
467 * \note This random generator must deliver random numbers with cryptographic
468 * quality and high performance. It must supply unpredictable numbers
469 * with a uniform distribution. The implementation of this function
470 * is responsible for ensuring that the random generator is seeded
471 * with sufficient entropy. If you have a hardware TRNG which is slow
472 * or delivers non-uniform output, declare it as an entropy source
473 * with mbedtls_entropy_add_source() instead of enabling this option.
474 *
475 * \param[in,out] context Pointer to the random generator context.
476 * This is all-bits-zero on the first call
477 * and preserved between successive calls.
478 * \param[out] output Output buffer. On success, this buffer
479 * contains random data with a uniform
480 * distribution.
481 * \param output_size The size of the \p output buffer in bytes.
482 * \param[out] output_length On success, set this value to \p output_size.
483 *
484 * \retval #PSA_SUCCESS
485 * Success. The output buffer contains \p output_size bytes of
486 * cryptographic-quality random data, and \c *output_length is
487 * set to \p output_size.
488 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
489 * The random generator requires extra entropy and there is no
490 * way to obtain entropy under current environment conditions.
491 * This error should not happen under normal circumstances since
492 * this function is responsible for obtaining as much entropy as
493 * it needs. However implementations of this function may return
494 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
495 * entropy without blocking indefinitely.
496 * \retval #PSA_ERROR_HARDWARE_FAILURE
497 * A failure of the random generator hardware that isn't covered
498 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
499 */
500psa_status_t mbedtls_psa_external_get_random(
501 mbedtls_psa_external_random_context_t *context,
502 uint8_t *output, size_t output_size, size_t *output_length);
503#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
504
505/** Force an immediate reseed of the PSA random generator.
506 *
507 * The entropy source(s) are the ones configured at compile time.
508 *
509 * The random generator is always seeded automatically before use, and
510 * it is reseeded as needed based on the configured policy, so most
511 * applications do not need to call this function.
512 *
513 * The main reason to call this function is in scenarios where the process
514 * state is cloned (i.e. duplicated) while the random generator is active.
515 * In such scenarios, you must call this function in every clone of
516 * the original process before performing any cryptographic operation
517 * that uses randomness. (Note that any operation that uses a private or
518 * secret key may use randomness internally even if the result is not
519 * randomized, but hashing and signature verification are ok.) For example:
520 *
521 * - If the process is part of a live virtual machine that is cloned,
522 * call this function after cloning so that the new instance has a
523 * distinct random generator state.
524 * - If the process is part of a hibernated image that may be resumed
525 * multiple times, call this function after resuming so that each
526 * resumed instance has a distinct random generator state.
527 * - If the process is cloned through the fork() system call, the
528 * child process should call this function before using the random
529 * generator.
530 *
531 * An additional consideration applies in configurations where there is no
532 * actual entropy source, only a nonvolatile seed (i.e.
533 * #MBEDTLS_ENTROPY_NV_SEED is enabled, #MBEDTLS_NO_PLATFORM_ENTROPY is
534 * enabled and #MBEDTLS_ENTROPY_HARDWARE_ALT is disabled).
535 * In such configurations, simply calling psa_random_reseed() in multiple
536 * cloned processes would result in the same random generator state in
537 * all the clones. To avoid this, in such configurations, you must pass
538 * a unique \p perso string in every clone.
539 *
540 * \note This function has no effect when the compilation option
541 * #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled.
542 *
543 * \note In client-server builds, this function may not be available
544 * from clients, since the decision to reseed is generally based
545 * on the server state.
546 *
547 * \note If the entropy source fails, the random generator remains usable:
548 * subsequent calls to generate random data will succeed until
549 * the random generator itself decides to reseed. If you want to
550 * force a reseed, either treat the failure as a fatal error,
551 * or call psa_random_deplete() instead of this function (or in
552 * addition).
553 *
554 * \param[in] perso A personalization string, i.e. a byte string to
555 * inject into the random generator state in addition
556 * to entropy obtained from the normal source(s).
557 * In most cases, it is fine for \c perso to be
558 * empty. The main use case for a personalization
559 * string is when the random generator state is cloned,
560 * as described above, and there is no actual entropy
561 * source.
562 * \param perso_size Length of \c perso in bytes.
563 *
564 * \retval #PSA_SUCCESS
565 * The reseed succeeded.
566 * \retval #PSA_ERROR_BAD_STATE
567 * The PSA random generator is not active.
568 * \retval #PSA_ERROR_NOT_SUPPORTED
569 * PSA uses an external random generator because the compilation
570 * option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled. This
571 * configuration does not support explicit reseeding.
572 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
573 * The entropy source failed.
574 */
575psa_status_t psa_random_reseed(const uint8_t *perso, size_t perso_size);
576
577/** Force a reseed of the PSA random generator the next time it is used.
578 *
579 * The entropy source(s) are the ones configured at compile time.
580 *
581 * The random generator is always seeded automatically before use, and
582 * it is reseeded as needed based on the configured policy, so most
583 * applications do not need to call this function.
584 *
585 * This function has a similar purpose as psa_random_reseed(),
586 * but the reseed will happen the next time the random generator is used.
587 * The advantage of this function is that it does not fail unless the
588 * system is in an unintended state, so it can be used in contexts where
589 * propagating errors is difficult.
590 *
591 * \note This function has no effect when #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
592 * is enabled.
593 *
594 * \note If prediction resistance is enabled (either explicitly, or because
595 * the reseed interval is set to 1), calling this function is
596 * unnecessary since the random generator will always reseed anyway.
597 *
598 * \retval #PSA_SUCCESS
599 * The reseed succeeded.
600 * \retval #PSA_ERROR_BAD_STATE
601 * The PSA random generator is not active.
602 * \retval #PSA_ERROR_NOT_SUPPORTED
603 * PSA uses an external random generator because the compilation
604 * option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled. This
605 * configuration does not support explicit reseeding.
606 */
607psa_status_t psa_random_deplete(void);
608
609/** Enable or disable prediction resistance in the PSA random generator.
610 *
611 * When prediction resistance is enabled, the random generator
612 * injects extra entropy before each request regardless of its size.
613 * As a consequence, a temporary compromise of the random generator
614 * state does not, by itself, compromise future steps.
615 * Furthermore, duplicating the random generator state (because the
616 * running application instance is cloned) is safe since it will
617 * not lead to identical random generator outputs in the clones.
618 *
619 * When prediction resistance is disabled, the random generator injects
620 * extra entropy periodically only as determined by
621 * #MBEDTLS_CTR_DRBG_RESEED_INTERVAL if #MBEDTLS_CTR_DRBG_C
622 * is enabled, or #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL otherwise.
623 *
624 * Prediction resistance is disabled by default, although setting
625 * #MBEDTLS_CTR_DRBG_RESEED_INTERVAL or #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL
626 * to \c 1 satisfies the prediction resistance property even when the
627 * option is disabled.
628 *
629 * \note This function has no effect when #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
630 * is enabled.
631 *
632 * \note Prediction resistance cannot be enabled when the only entropy source
633 * is a nonvolatile seed, since prediction resistance is effectively
634 * impossible to achieve without actual entropy.
635 *
636 * \param enabled \c 1 to enable prediction resistance.
637 * \c 0 to disable prediction resistance.
638 *
639 * \retval #PSA_SUCCESS
640 * The PSA random generator is active, and prediction resistance
641 * has been changed to the desired option.
642 * \retval #PSA_ERROR_BAD_STATE
643 * The PSA random generator is not active.
644 * \retval #PSA_ERROR_INVALID_ARGUMENT
645 * \p enabled is not valid.
646 * \retval #PSA_ERROR_NOT_SUPPORTED
647 * PSA uses an external random generator because the compilation
648 * option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled.
649 * Or, the random generator only has a nonvolatile seed but no entropy
650 * source, and prediction resistance has been requested.
651 */
652psa_status_t psa_random_set_prediction_resistance(unsigned enabled);
653
654/**@}*/
655
656/** \defgroup psa_builtin_keys Built-in keys
657 * @{
658 */
659
660/** The minimum value for a key identifier that is built into the
661 * implementation.
662 *
663 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
664 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
665 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
666 * with any other set of implementation-chosen key identifiers.
667 *
668 * This value is part of the library's API since changing it would invalidate
669 * the values of built-in key identifiers in applications.
670 */
671#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
672
673/** The maximum value for a key identifier that is built into the
674 * implementation.
675 *
676 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
677 */
678#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
679
680/** A slot number identifying a key in a driver.
681 *
682 * Values of this type are used to identify built-in keys.
683 */
684typedef uint64_t psa_drv_slot_number_t;
685
686#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
687/** Test whether a key identifier belongs to the builtin key range.
688 *
689 * \param key_id Key identifier to test.
690 *
691 * \retval 1
692 * The key identifier is a builtin key identifier.
693 * \retval 0
694 * The key identifier is not a builtin key identifier.
695 */
696static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
697{
698 return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
699 (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
700}
701
702/** Platform function to obtain the location and slot number of a built-in key.
703 *
704 * An application-specific implementation of this function must be provided if
705 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
706 * as part of a platform's system image.
707 *
708 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
709 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
710 *
711 * In a multi-application configuration
712 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
713 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
714 * is allowed to use the given key.
715 *
716 * \param key_id The key ID for which to retrieve the
717 * location and slot attributes.
718 * \param[out] lifetime On success, the lifetime associated with the key
719 * corresponding to \p key_id. Lifetime is a
720 * combination of which driver contains the key,
721 * and with what persistence level the key is
722 * intended to be used. If the platform
723 * implementation does not contain specific
724 * information about the intended key persistence
725 * level, the persistence level may be reported as
726 * #PSA_KEY_PERSISTENCE_DEFAULT.
727 * \param[out] slot_number On success, the slot number known to the driver
728 * registered at the lifetime location reported
729 * through \p lifetime which corresponds to the
730 * requested built-in key.
731 *
732 * \retval #PSA_SUCCESS
733 * The requested key identifier designates a built-in key.
734 * In a multi-application configuration, the requested owner
735 * is allowed to access it.
736 * \retval #PSA_ERROR_DOES_NOT_EXIST
737 * The requested key identifier is not a built-in key which is known
738 * to this function. If a key exists in the key storage with this
739 * identifier, the data from the storage will be used.
740 * \return (any other error)
741 * Any other error is propagated to the function that requested the key.
742 * Common errors include:
743 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
744 * is not allowed to access it.
745 */
746psa_status_t mbedtls_psa_platform_get_builtin_key(
747 mbedtls_svc_key_id_t key_id,
748 psa_key_lifetime_t *lifetime,
749 psa_drv_slot_number_t *slot_number);
750#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
751
752/** @} */
753
754/** \defgroup psa_crypto_client Functions defined by a client provider
755 *
756 * The functions in this group are meant to be implemented by providers of
757 * the PSA Crypto client interface. They are provided by the library when
758 * #MBEDTLS_PSA_CRYPTO_C is enabled.
759 *
760 * \note All functions in this group are experimental, as using
761 * alternative client interface providers is experimental.
762 *
763 * @{
764 */
765
766/** Check if PSA is capable of handling the specified hash algorithm.
767 *
768 * This means that PSA core was built with the corresponding PSA_WANT_ALG_xxx
769 * set and that psa_crypto_init has already been called.
770 *
771 * \note When using the built-in version of the PSA core (i.e.
772 * #MBEDTLS_PSA_CRYPTO_C is set), for now, this function only checks
773 * the state of the driver subsystem, not the algorithm.
774 * This might be improved in the future.
775 *
776 * \param hash_alg The hash algorithm.
777 *
778 * \return 1 if the PSA can handle \p hash_alg, 0 otherwise.
779 */
780int psa_can_do_hash(psa_algorithm_t hash_alg);
781
782/**
783 * Tell if PSA is ready for this cipher.
784 *
785 * \note When using the built-in version of the PSA core (i.e.
786 * #MBEDTLS_PSA_CRYPTO_C is set), for now, this function only checks
787 * the state of the driver subsystem, not the key type and algorithm.
788 * This might be improved in the future.
789 *
790 * \param key_type The key type.
791 * \param cipher_alg The cipher algorithm.
792 *
793 * \return 1 if the PSA can handle \p cipher_alg, 0 otherwise.
794 */
795int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg);
796
797/**@}*/
798
799/** \addtogroup crypto_types
800 * @{
801 */
802
803#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
804
805/** Whether the specified algorithm is a password-authenticated key exchange.
806 *
807 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
808 *
809 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
810 * algorithm, 0 otherwise.
811 * This macro may return either 0 or 1 if \p alg is not a supported
812 * algorithm identifier.
813 */
814#define PSA_ALG_IS_PAKE(alg) \
815 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
816
817/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
818 *
819 * This is J-PAKE as defined by RFC 8236, instantiated with the following
820 * parameters:
821 *
822 * - The group can be either an elliptic curve or defined over a finite field.
823 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
824 * J-PAKE algorithm.
825 * - A cryptographic hash function.
826 *
827 * To select these parameters and set up the cipher suite, call these functions
828 * in any order:
829 *
830 * \code
831 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
832 * psa_pake_cs_set_primitive(cipher_suite,
833 * PSA_PAKE_PRIMITIVE(type, family, bits));
834 * psa_pake_cs_set_hash(cipher_suite, hash);
835 * \endcode
836 *
837 * For more information on how to set a specific curve or field, refer to the
838 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
839 *
840 * After initializing a J-PAKE operation, call
841 *
842 * \code
843 * psa_pake_setup(operation, cipher_suite);
844 * psa_pake_set_user(operation, ...);
845 * psa_pake_set_peer(operation, ...);
846 * psa_pake_set_password_key(operation, ...);
847 * \endcode
848 *
849 * The password is provided as a key. This can be the password text itself,
850 * in an agreed character encoding, or some value derived from the password
851 * as required by a higher level protocol.
852 *
853 * (The implementation converts the key material to a number as described in
854 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
855 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
856 * \c q is order of the group defined by the primitive set in the cipher suite.
857 * The \c psa_pake_set_password_key() function returns an error if the result
858 * of the reduction is 0.)
859 *
860 * The key exchange flow for J-PAKE is as follows:
861 * -# To get the first round data that needs to be sent to the peer, call
862 * \code
863 * // Get g1
864 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
865 * // Get the ZKP public key for x1
866 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
867 * // Get the ZKP proof for x1
868 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
869 * // Get g2
870 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
871 * // Get the ZKP public key for x2
872 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
873 * // Get the ZKP proof for x2
874 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
875 * \endcode
876 * -# To provide the first round data received from the peer to the operation,
877 * call
878 * \code
879 * // Set g3
880 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
881 * // Set the ZKP public key for x3
882 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
883 * // Set the ZKP proof for x3
884 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
885 * // Set g4
886 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
887 * // Set the ZKP public key for x4
888 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
889 * // Set the ZKP proof for x4
890 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
891 * \endcode
892 * -# To get the second round data that needs to be sent to the peer, call
893 * \code
894 * // Get A
895 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
896 * // Get ZKP public key for x2*s
897 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
898 * // Get ZKP proof for x2*s
899 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
900 * \endcode
901 * -# To provide the second round data received from the peer to the operation,
902 * call
903 * \code
904 * // Set B
905 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
906 * // Set ZKP public key for x4*s
907 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
908 * // Set ZKP proof for x4*s
909 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
910 * \endcode
911 * -# To access the shared secret call
912 * \code
913 * // Get Ka=Kb=K
914 * psa_pake_get_implicit_key()
915 * \endcode
916 *
917 * For more information consult the documentation of the individual
918 * \c PSA_PAKE_STEP_XXX constants.
919 *
920 * At this point there is a cryptographic guarantee that only the authenticated
921 * party who used the same password is able to compute the key. But there is no
922 * guarantee that the peer is the party it claims to be and was able to do so.
923 *
924 * That is, the authentication is only implicit (the peer is not authenticated
925 * at this point, and no action should be taken that assume that they are - like
926 * for example accessing restricted files).
927 *
928 * To make the authentication explicit there are various methods, see Section 5
929 * of RFC 8236 for two examples.
930 *
931 * \note The JPAKE implementation has the following limitations:
932 * - The only supported primitive is ECC on the curve secp256r1, i.e.
933 * `PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
934 * PSA_ECC_FAMILY_SECP_R1, 256)`.
935 * - The only supported hash algorithm is SHA-256, i.e.
936 * `PSA_ALG_SHA_256`.
937 * - When using the built-in implementation, the user ID and the peer ID
938 * must be `"client"` (6-byte string) and `"server"` (6-byte string),
939 * or the other way round.
940 * Third-party drivers may or may not have this limitation.
941 *
942 */
943#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
944
945/** @} */
946
947/** \defgroup pake Password-authenticated key exchange (PAKE)
948 *
949 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
950 * the official PSA Crypto API yet.
951 *
952 * \note The content of this section is not part of the stable API and ABI
953 * of Mbed TLS and may change arbitrarily from version to version.
954 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
955 * #PSA_ALG_JPAKE.
956 * @{
957 */
958
959/** \brief Encoding of the application role of PAKE
960 *
961 * Encodes the application's role in the algorithm is being executed. For more
962 * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
963 * constants.
964 */
965typedef uint8_t psa_pake_role_t;
966
967/** Encoding of input and output indicators for PAKE.
968 *
969 * Some PAKE algorithms need to exchange more data than just a single key share.
970 * This type is for encoding additional input and output data for such
971 * algorithms.
972 */
973typedef uint8_t psa_pake_step_t;
974
975/** Encoding of the type of the PAKE's primitive.
976 *
977 * Values defined by this standard will never be in the range 0x80-0xff.
978 * Vendors who define additional types must use an encoding in this range.
979 *
980 * For more information see the documentation of individual
981 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
982 */
983typedef uint8_t psa_pake_primitive_type_t;
984
985/** \brief Encoding of the family of the primitive associated with the PAKE.
986 *
987 * For more information see the documentation of individual
988 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
989 */
990typedef uint8_t psa_pake_family_t;
991
992/** \brief Encoding of the primitive associated with the PAKE.
993 *
994 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
995 */
996typedef uint32_t psa_pake_primitive_t;
997
998/** A value to indicate no role in a PAKE algorithm.
999 * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
1000 * algorithms which do not assign roles.
1001 */
1002#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
1003
1004/** The first peer in a balanced PAKE.
1005 *
1006 * Although balanced PAKE algorithms are symmetric, some of them needs an
1007 * ordering of peers for the transcript calculations. If the algorithm does not
1008 * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
1009 * accepted.
1010 */
1011#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
1012
1013/** The second peer in a balanced PAKE.
1014 *
1015 * Although balanced PAKE algorithms are symmetric, some of them needs an
1016 * ordering of peers for the transcript calculations. If the algorithm does not
1017 * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
1018 * accepted.
1019 */
1020#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
1021
1022/** The client in an augmented PAKE.
1023 *
1024 * Augmented PAKE algorithms need to differentiate between client and server.
1025 */
1026#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
1027
1028/** The server in an augmented PAKE.
1029 *
1030 * Augmented PAKE algorithms need to differentiate between client and server.
1031 */
1032#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
1033
1034/** The PAKE primitive type indicating the use of elliptic curves.
1035 *
1036 * The values of the \c family and \c bits fields of the cipher suite identify a
1037 * specific elliptic curve, using the same mapping that is used for ECC
1038 * (::psa_ecc_family_t) keys.
1039 *
1040 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1041 * \c bits means the value returned by psa_pake_cs_get_bits().)
1042 *
1043 * Input and output during the operation can involve group elements and scalar
1044 * values:
1045 * -# The format for group elements is the same as for public keys on the
1046 * specific curve would be. For more information, consult the documentation of
1047 * psa_export_public_key().
1048 * -# The format for scalars is the same as for private keys on the specific
1049 * curve would be. For more information, consult the documentation of
1050 * psa_export_key().
1051 */
1052#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
1053
1054/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
1055 *
1056 * The values of the \c family and \c bits fields of the cipher suite identify
1057 * a specific Diffie-Hellman group, using the same mapping that is used for
1058 * Diffie-Hellman (::psa_dh_family_t) keys.
1059 *
1060 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1061 * \c bits means the value returned by psa_pake_cs_get_bits().)
1062 *
1063 * Input and output during the operation can involve group elements and scalar
1064 * values:
1065 * -# The format for group elements is the same as for public keys on the
1066 * specific group would be. For more information, consult the documentation of
1067 * psa_export_public_key().
1068 * -# The format for scalars is the same as for private keys on the specific
1069 * group would be. For more information, consult the documentation of
1070 * psa_export_key().
1071 */
1072#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
1073
1074/** Construct a PAKE primitive from type, family and bit-size.
1075 *
1076 * \param pake_type The type of the primitive
1077 * (value of type ::psa_pake_primitive_type_t).
1078 * \param pake_family The family of the primitive
1079 * (the type and interpretation of this parameter depends
1080 * on \p pake_type, for more information consult the
1081 * documentation of individual ::psa_pake_primitive_type_t
1082 * constants).
1083 * \param pake_bits The bit-size of the primitive
1084 * (Value of type \c size_t. The interpretation
1085 * of this parameter depends on \p pake_family, for more
1086 * information consult the documentation of individual
1087 * ::psa_pake_primitive_type_t constants).
1088 *
1089 * \return The constructed primitive value of type ::psa_pake_primitive_t.
1090 * Return 0 if the requested primitive can't be encoded as
1091 * ::psa_pake_primitive_t.
1092 */
1093#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
1094 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
1095 ((psa_pake_primitive_t) (((pake_type) << 24 | \
1096 (pake_family) << 16) | (pake_bits)))
1097
1098/** The key share being sent to or received from the peer.
1099 *
1100 * The format for both input and output at this step is the same as for public
1101 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1102 * be.
1103 *
1104 * For more information on the format, consult the documentation of
1105 * psa_export_public_key().
1106 *
1107 * For information regarding how the group is determined, consult the
1108 * documentation #PSA_PAKE_PRIMITIVE.
1109 */
1110#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
1111
1112/** A Schnorr NIZKP public key.
1113 *
1114 * This is the ephemeral public key in the Schnorr Non-Interactive
1115 * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1116 *
1117 * The format for both input and output at this step is the same as for public
1118 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1119 * be.
1120 *
1121 * For more information on the format, consult the documentation of
1122 * psa_export_public_key().
1123 *
1124 * For information regarding how the group is determined, consult the
1125 * documentation #PSA_PAKE_PRIMITIVE.
1126 */
1127#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
1128
1129/** A Schnorr NIZKP proof.
1130 *
1131 * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1132 * value denoted by the letter 'r' in RFC 8235).
1133 *
1134 * Both for input and output, the value at this step is an integer less than
1135 * the order of the group selected in the cipher suite. The format depends on
1136 * the group as well:
1137 *
1138 * - For Montgomery curves, the encoding is little endian.
1139 * - For everything else the encoding is big endian (see Section 2.3.8 of
1140 * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
1141 *
1142 * In both cases leading zeroes are allowed as long as the length in bytes does
1143 * not exceed the byte length of the group order.
1144 *
1145 * For information regarding how the group is determined, consult the
1146 * documentation #PSA_PAKE_PRIMITIVE.
1147 */
1148#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
1149
1150/**@}*/
1151
1152/** A sufficient output buffer size for psa_pake_output().
1153 *
1154 * If the size of the output buffer is at least this large, it is guaranteed
1155 * that psa_pake_output() will not fail due to an insufficient output buffer
1156 * size. The actual size of the output might be smaller in any given call.
1157 *
1158 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1159 *
1160 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
1161 * #PSA_ALG_IS_PAKE(\p alg) is true).
1162 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1163 * compatible with algorithm \p alg.
1164 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1165 * algorithm \p alg.
1166 * \return A sufficient output buffer size for the specified
1167 * PAKE algorithm, primitive, and output step. If the
1168 * PAKE algorithm, primitive, or output step is not
1169 * recognized, or the parameters are incompatible,
1170 * return 0.
1171 */
1172#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
1173 (alg == PSA_ALG_JPAKE && \
1174 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1175 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1176 ( \
1177 output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1178 output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1179 32 \
1180 ) : \
1181 0)
1182
1183/** A sufficient input buffer size for psa_pake_input().
1184 *
1185 * The value returned by this macro is guaranteed to be large enough for any
1186 * valid input to psa_pake_input() in an operation with the specified
1187 * parameters.
1188 *
1189 * See also #PSA_PAKE_INPUT_MAX_SIZE
1190 *
1191 * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
1192 * #PSA_ALG_IS_PAKE(\p alg) is true).
1193 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1194 * compatible with algorithm \p alg.
1195 * \param input_step A value of type ::psa_pake_step_t that is valid for the
1196 * algorithm \p alg.
1197 * \return A sufficient input buffer size for the specified
1198 * input, cipher suite and algorithm. If the cipher suite,
1199 * the input type or PAKE algorithm is not recognized, or
1200 * the parameters are incompatible, return 0.
1201 */
1202#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
1203 (alg == PSA_ALG_JPAKE && \
1204 primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1205 PSA_ECC_FAMILY_SECP_R1, 256) ? \
1206 ( \
1207 input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1208 input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1209 32 \
1210 ) : \
1211 0)
1212
1213/** Output buffer size for psa_pake_output() for any of the supported PAKE
1214 * algorithm and primitive suites and output step.
1215 *
1216 * This macro must expand to a compile-time constant integer.
1217 *
1218 * The value of this macro must be at least as large as the largest value
1219 * returned by PSA_PAKE_OUTPUT_SIZE()
1220 *
1221 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
1222 */
1223#define PSA_PAKE_OUTPUT_MAX_SIZE 65
1224
1225/** Input buffer size for psa_pake_input() for any of the supported PAKE
1226 * algorithm and primitive suites and input step.
1227 *
1228 * This macro must expand to a compile-time constant integer.
1229 *
1230 * The value of this macro must be at least as large as the largest value
1231 * returned by PSA_PAKE_INPUT_SIZE()
1232 *
1233 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
1234 */
1235#define PSA_PAKE_INPUT_MAX_SIZE 65
1236
1237/** Returns a suitable initializer for a PAKE cipher suite object of type
1238 * psa_pake_cipher_suite_t.
1239 */
1240#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
1241
1242/** Returns a suitable initializer for a PAKE operation object of type
1243 * psa_pake_operation_t.
1244 */
1245#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1246#define PSA_PAKE_OPERATION_INIT { 0 }
1247#else
1248#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
1249 { 0 }, { { 0 } } }
1250#endif
1251
1252struct psa_pake_cipher_suite_s {
1253 psa_algorithm_t algorithm;
1254 psa_pake_primitive_type_t type;
1255 psa_pake_family_t family;
1256 uint16_t bits;
1257 psa_algorithm_t hash;
1258};
1259
1260struct psa_crypto_driver_pake_inputs_s {
1261 uint8_t *MBEDTLS_PRIVATE(password);
1262 size_t MBEDTLS_PRIVATE(password_len);
1263 uint8_t *MBEDTLS_PRIVATE(user);
1264 size_t MBEDTLS_PRIVATE(user_len);
1265 uint8_t *MBEDTLS_PRIVATE(peer);
1266 size_t MBEDTLS_PRIVATE(peer_len);
1267 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
1268 struct psa_pake_cipher_suite_s MBEDTLS_PRIVATE(cipher_suite);
1269};
1270
1271typedef enum psa_crypto_driver_pake_step {
1272 PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1273 PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1274 PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1275 PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1276 PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1277 PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1278 PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1279 PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1280 PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1281 PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1282 PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1283 PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1284 PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
1285} psa_crypto_driver_pake_step_t;
1286
1287typedef enum psa_jpake_round {
1288 PSA_JPAKE_FIRST = 0,
1289 PSA_JPAKE_SECOND = 1,
1290 PSA_JPAKE_FINISHED = 2
1291} psa_jpake_round_t;
1292
1293typedef enum psa_jpake_io_mode {
1294 PSA_JPAKE_INPUT = 0,
1295 PSA_JPAKE_OUTPUT = 1
1296} psa_jpake_io_mode_t;
1297
1298struct psa_jpake_computation_stage_s {
1299 /* The J-PAKE round we are currently on */
1300 psa_jpake_round_t MBEDTLS_PRIVATE(round);
1301 /* The 'mode' we are currently in (inputting or outputting) */
1302 psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
1303 /* The number of completed inputs so far this round */
1304 uint8_t MBEDTLS_PRIVATE(inputs);
1305 /* The number of completed outputs so far this round */
1306 uint8_t MBEDTLS_PRIVATE(outputs);
1307 /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1308 psa_pake_step_t MBEDTLS_PRIVATE(step);
1309};
1310
1311#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1312 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1313#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1314 ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1315
1316struct psa_pake_operation_s {
1317#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1318 mbedtls_psa_client_handle_t handle;
1319#else
1320 /** Unique ID indicating which driver got assigned to do the
1321 * operation. Since driver contexts are driver-specific, swapping
1322 * drivers halfway through the operation is not supported.
1323 * ID values are auto-generated in psa_crypto_driver_wrappers.h
1324 * ID value zero means the context is not valid or not assigned to
1325 * any driver (i.e. none of the driver contexts are active). */
1326 unsigned int MBEDTLS_PRIVATE(id);
1327 /* Algorithm of the PAKE operation */
1328 psa_algorithm_t MBEDTLS_PRIVATE(alg);
1329 /* A primitive of type compatible with algorithm */
1330 psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
1331 /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1332 * or computing. */
1333 uint8_t MBEDTLS_PRIVATE(stage);
1334 /* Holds computation stage of the PAKE algorithms. */
1335 union {
1336 uint8_t MBEDTLS_PRIVATE(dummy);
1337#if defined(PSA_WANT_ALG_JPAKE)
1338 struct psa_jpake_computation_stage_s MBEDTLS_PRIVATE(jpake);
1339#endif
1340 } MBEDTLS_PRIVATE(computation_stage);
1341 union {
1342 psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
1343 struct psa_crypto_driver_pake_inputs_s MBEDTLS_PRIVATE(inputs);
1344 } MBEDTLS_PRIVATE(data);
1345#endif
1346};
1347
1348/** \addtogroup pake
1349 * @{
1350 */
1351
1352/** The type of the data structure for PAKE cipher suites.
1353 *
1354 * This is an implementation-defined \c struct. Applications should not
1355 * make any assumptions about the content of this structure.
1356 * Implementation details can change in future versions without notice.
1357 */
1358typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1359
1360/** Return an initial value for a PAKE cipher suite object.
1361 */
1362static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
1363
1364/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1365 *
1366 * \param[in] cipher_suite The cipher suite structure to query.
1367 *
1368 * \return The PAKE algorithm stored in the cipher suite structure.
1369 */
1370static psa_algorithm_t psa_pake_cs_get_algorithm(
1371 const psa_pake_cipher_suite_t *cipher_suite);
1372
1373/** Declare the PAKE algorithm for the cipher suite.
1374 *
1375 * This function overwrites any PAKE algorithm
1376 * previously set in \p cipher_suite.
1377 *
1378 * \note For #PSA_ALG_JPAKE, the only supported hash algorithm is SHA-256.
1379 *
1380 * \param[out] cipher_suite The cipher suite structure to write to.
1381 * \param algorithm The PAKE algorithm to write.
1382 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1383 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1384 * If this is 0, the PAKE algorithm in
1385 * \p cipher_suite becomes unspecified.
1386 */
1387static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1388 psa_algorithm_t algorithm);
1389
1390/** Retrieve the primitive from a PAKE cipher suite.
1391 *
1392 * \param[in] cipher_suite The cipher suite structure to query.
1393 *
1394 * \return The primitive stored in the cipher suite structure.
1395 */
1396static psa_pake_primitive_t psa_pake_cs_get_primitive(
1397 const psa_pake_cipher_suite_t *cipher_suite);
1398
1399/** Declare the primitive for a PAKE cipher suite.
1400 *
1401 * This function overwrites any primitive previously set in \p cipher_suite.
1402 *
1403 * \note For #PSA_ALG_JPAKE, the only supported primitive is ECC on the curve
1404 * secp256r1, i.e. `PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1405 * PSA_ECC_FAMILY_SECP_R1, 256)`.
1406 *
1407 * \param[out] cipher_suite The cipher suite structure to write to.
1408 * \param primitive The primitive to write. If this is 0, the
1409 * primitive type in \p cipher_suite becomes
1410 * unspecified.
1411 */
1412static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1413 psa_pake_primitive_t primitive);
1414
1415/** Retrieve the PAKE family from a PAKE cipher suite.
1416 *
1417 * \param[in] cipher_suite The cipher suite structure to query.
1418 *
1419 * \return The PAKE family stored in the cipher suite structure.
1420 */
1421static psa_pake_family_t psa_pake_cs_get_family(
1422 const psa_pake_cipher_suite_t *cipher_suite);
1423
1424/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1425 *
1426 * \param[in] cipher_suite The cipher suite structure to query.
1427 *
1428 * \return The PAKE primitive bit-size stored in the cipher suite structure.
1429 */
1430static uint16_t psa_pake_cs_get_bits(
1431 const psa_pake_cipher_suite_t *cipher_suite);
1432
1433/** Retrieve the hash algorithm from a PAKE cipher suite.
1434 *
1435 * \param[in] cipher_suite The cipher suite structure to query.
1436 *
1437 * \return The hash algorithm stored in the cipher suite structure. The return
1438 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1439 * the hash algorithm is not set.
1440 */
1441static psa_algorithm_t psa_pake_cs_get_hash(
1442 const psa_pake_cipher_suite_t *cipher_suite);
1443
1444/** Declare the hash algorithm for a PAKE cipher suite.
1445 *
1446 * This function overwrites any hash algorithm
1447 * previously set in \p cipher_suite.
1448 *
1449 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1450 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1451 * for more information.
1452 *
1453 * \param[out] cipher_suite The cipher suite structure to write to.
1454 * \param hash The hash involved in the cipher suite.
1455 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1456 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1457 * If this is 0, the hash algorithm in
1458 * \p cipher_suite becomes unspecified.
1459 */
1460static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1461 psa_algorithm_t hash);
1462
1463/** The type of the state data structure for PAKE operations.
1464 *
1465 * Before calling any function on a PAKE operation object, the application
1466 * must initialize it by any of the following means:
1467 * - Set the structure to all-bits-zero, for example:
1468 * \code
1469 * psa_pake_operation_t operation;
1470 * memset(&operation, 0, sizeof(operation));
1471 * \endcode
1472 * - Initialize the structure to logical zero values, for example:
1473 * \code
1474 * psa_pake_operation_t operation = {0};
1475 * \endcode
1476 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1477 * for example:
1478 * \code
1479 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1480 * \endcode
1481 * - Assign the result of the function psa_pake_operation_init()
1482 * to the structure, for example:
1483 * \code
1484 * psa_pake_operation_t operation;
1485 * operation = psa_pake_operation_init();
1486 * \endcode
1487 *
1488 * This is an implementation-defined \c struct. Applications should not
1489 * make any assumptions about the content of this structure.
1490 * Implementation details can change in future versions without notice. */
1491typedef struct psa_pake_operation_s psa_pake_operation_t;
1492
1493/** The type of input values for PAKE operations. */
1494typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1495
1496/** The type of computation stage for J-PAKE operations. */
1497typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1498
1499/** Return an initial value for a PAKE operation object.
1500 */
1501static psa_pake_operation_t psa_pake_operation_init(void);
1502
1503/** Get the length of the password in bytes from given inputs.
1504 *
1505 * \param[in] inputs Operation inputs.
1506 * \param[out] password_len Password length.
1507 *
1508 * \retval #PSA_SUCCESS
1509 * Success.
1510 * \retval #PSA_ERROR_BAD_STATE
1511 * Password hasn't been set yet.
1512 */
1513psa_status_t psa_crypto_driver_pake_get_password_len(
1514 const psa_crypto_driver_pake_inputs_t *inputs,
1515 size_t *password_len);
1516
1517/** Get the password from given inputs.
1518 *
1519 * \param[in] inputs Operation inputs.
1520 * \param[out] buffer Return buffer for password.
1521 * \param buffer_size Size of the return buffer in bytes.
1522 * \param[out] buffer_length Actual size of the password in bytes.
1523 *
1524 * \retval #PSA_SUCCESS
1525 * Success.
1526 * \retval #PSA_ERROR_BAD_STATE
1527 * Password hasn't been set yet.
1528 */
1529psa_status_t psa_crypto_driver_pake_get_password(
1530 const psa_crypto_driver_pake_inputs_t *inputs,
1531 uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1532
1533/** Get the length of the user id in bytes from given inputs.
1534 *
1535 * \param[in] inputs Operation inputs.
1536 * \param[out] user_len User id length.
1537 *
1538 * \retval #PSA_SUCCESS
1539 * Success.
1540 * \retval #PSA_ERROR_BAD_STATE
1541 * User id hasn't been set yet.
1542 */
1543psa_status_t psa_crypto_driver_pake_get_user_len(
1544 const psa_crypto_driver_pake_inputs_t *inputs,
1545 size_t *user_len);
1546
1547/** Get the length of the peer id in bytes from given inputs.
1548 *
1549 * \param[in] inputs Operation inputs.
1550 * \param[out] peer_len Peer id length.
1551 *
1552 * \retval #PSA_SUCCESS
1553 * Success.
1554 * \retval #PSA_ERROR_BAD_STATE
1555 * Peer id hasn't been set yet.
1556 */
1557psa_status_t psa_crypto_driver_pake_get_peer_len(
1558 const psa_crypto_driver_pake_inputs_t *inputs,
1559 size_t *peer_len);
1560
1561/** Get the user id from given inputs.
1562 *
1563 * \param[in] inputs Operation inputs.
1564 * \param[out] user_id User id.
1565 * \param user_id_size Size of \p user_id in bytes.
1566 * \param[out] user_id_len Size of the user id in bytes.
1567 *
1568 * \retval #PSA_SUCCESS
1569 * Success.
1570 * \retval #PSA_ERROR_BAD_STATE
1571 * User id hasn't been set yet.
1572 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1573 * The size of the \p user_id is too small.
1574 */
1575psa_status_t psa_crypto_driver_pake_get_user(
1576 const psa_crypto_driver_pake_inputs_t *inputs,
1577 uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
1578
1579/** Get the peer id from given inputs.
1580 *
1581 * \param[in] inputs Operation inputs.
1582 * \param[out] peer_id Peer id.
1583 * \param peer_id_size Size of \p peer_id in bytes.
1584 * \param[out] peer_id_length Size of the peer id in bytes.
1585 *
1586 * \retval #PSA_SUCCESS
1587 * Success.
1588 * \retval #PSA_ERROR_BAD_STATE
1589 * Peer id hasn't been set yet.
1590 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1591 * The size of the \p peer_id is too small.
1592 */
1593psa_status_t psa_crypto_driver_pake_get_peer(
1594 const psa_crypto_driver_pake_inputs_t *inputs,
1595 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
1596
1597/** Get the cipher suite from given inputs.
1598 *
1599 * \param[in] inputs Operation inputs.
1600 * \param[out] cipher_suite Return buffer for role.
1601 *
1602 * \retval #PSA_SUCCESS
1603 * Success.
1604 * \retval #PSA_ERROR_BAD_STATE
1605 * Cipher_suite hasn't been set yet.
1606 */
1607psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1608 const psa_crypto_driver_pake_inputs_t *inputs,
1609 psa_pake_cipher_suite_t *cipher_suite);
1610
1611/** Set the session information for a password-authenticated key exchange.
1612 *
1613 * The sequence of operations to set up a password-authenticated key exchange
1614 * is as follows:
1615 * -# Allocate an operation object which will be passed to all the functions
1616 * listed here.
1617 * -# Initialize the operation object with one of the methods described in the
1618 * documentation for #psa_pake_operation_t, e.g.
1619 * #PSA_PAKE_OPERATION_INIT.
1620 * -# Call psa_pake_setup() to specify the cipher suite.
1621 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1622 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1623 * to be called depends on the algorithm in use.
1624 *
1625 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1626 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1627 * for more information.
1628 *
1629 * A typical sequence of calls to perform a password-authenticated key
1630 * exchange:
1631 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1632 * key share that needs to be sent to the peer.
1633 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1634 * the key share that was received from the peer.
1635 * -# Depending on the algorithm additional calls to psa_pake_output() and
1636 * psa_pake_input() might be necessary.
1637 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1638 *
1639 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1640 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1641 * for more information.
1642 *
1643 * If an error occurs at any step after a call to psa_pake_setup(),
1644 * the operation will need to be reset by a call to psa_pake_abort(). The
1645 * application may call psa_pake_abort() at any time after the operation
1646 * has been initialized.
1647 *
1648 * After a successful call to psa_pake_setup(), the application must
1649 * eventually terminate the operation. The following events terminate an
1650 * operation:
1651 * - A call to psa_pake_abort().
1652 * - A successful call to psa_pake_get_implicit_key().
1653 *
1654 * \param[in,out] operation The operation object to set up. It must have
1655 * been initialized but not set up yet.
1656 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
1657 * characterizes a PAKE algorithm and determines
1658 * the algorithm as well.)
1659 *
1660 * \retval #PSA_SUCCESS
1661 * Success.
1662 * \retval #PSA_ERROR_INVALID_ARGUMENT
1663 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1664 * PAKE primitive in \p cipher_suite is not compatible with the
1665 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1666 * or not compatible with the PAKE algorithm and primitive.
1667 * \retval #PSA_ERROR_NOT_SUPPORTED
1668 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1669 * or the PAKE primitive in \p cipher_suite is not supported or not
1670 * compatible with the PAKE algorithm, or the hash algorithm in
1671 * \p cipher_suite is not supported or not compatible with the PAKE
1672 * algorithm and primitive.
1673 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1674 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1675 * \retval #PSA_ERROR_BAD_STATE
1676 * The operation state is not valid, or
1677 * the library has not been previously initialized by psa_crypto_init().
1678 * It is implementation-dependent whether a failure to initialize
1679 * results in this error code.
1680 */
1681psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1682 const psa_pake_cipher_suite_t *cipher_suite);
1683
1684/** Set the password for a password-authenticated key exchange from key ID.
1685 *
1686 * Call this function when the password, or a value derived from the password,
1687 * is already present in the key store.
1688 *
1689 * \param[in,out] operation The operation object to set the password for. It
1690 * must have been set up by psa_pake_setup() and
1691 * not yet in use (neither psa_pake_output() nor
1692 * psa_pake_input() has been called yet). It must
1693 * be on operation for which the password hasn't
1694 * been set yet (psa_pake_set_password_key()
1695 * hasn't been called yet).
1696 * \param password Identifier of the key holding the password or a
1697 * value derived from the password (eg. by a
1698 * memory-hard function). It must remain valid
1699 * until the operation terminates. It must be of
1700 * type #PSA_KEY_TYPE_PASSWORD or
1701 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1702 * the usage #PSA_KEY_USAGE_DERIVE.
1703 *
1704 * \retval #PSA_SUCCESS
1705 * Success.
1706 * \retval #PSA_ERROR_INVALID_HANDLE
1707 * \p password is not a valid key identifier.
1708 * \retval #PSA_ERROR_NOT_PERMITTED
1709 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1710 * permit the \p operation's algorithm.
1711 * \retval #PSA_ERROR_INVALID_ARGUMENT
1712 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1713 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1714 * the \p operation's cipher suite.
1715 * \retval #PSA_ERROR_NOT_SUPPORTED
1716 * The key type or key size of \p password is not supported with the
1717 * \p operation's cipher suite.
1718 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1719 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1720 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1721 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1722 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1723 * \retval #PSA_ERROR_BAD_STATE
1724 * The operation state is not valid (it must have been set up.), or
1725 * the library has not been previously initialized by psa_crypto_init().
1726 * It is implementation-dependent whether a failure to initialize
1727 * results in this error code.
1728 */
1729psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1730 mbedtls_svc_key_id_t password);
1731
1732/** Set the user ID for a password-authenticated key exchange.
1733 *
1734 * Call this function to set the user ID. For PAKE algorithms that associate a
1735 * user identifier with each side of the session you need to call
1736 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1737 * user identifier with the session, call psa_pake_set_user() only.
1738 *
1739 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1740 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1741 * for more information.
1742 *
1743 * \note When using the built-in implementation of #PSA_ALG_JPAKE, the user ID
1744 * must be `"client"` (6-byte string) or `"server"` (6-byte string).
1745 * Third-party drivers may or may not have this limitation.
1746 *
1747 * \param[in,out] operation The operation object to set the user ID for. It
1748 * must have been set up by psa_pake_setup() and
1749 * not yet in use (neither psa_pake_output() nor
1750 * psa_pake_input() has been called yet). It must
1751 * be on operation for which the user ID hasn't
1752 * been set (psa_pake_set_user() hasn't been
1753 * called yet).
1754 * \param[in] user_id The user ID to authenticate with.
1755 * \param user_id_len Size of the \p user_id buffer in bytes.
1756 *
1757 * \retval #PSA_SUCCESS
1758 * Success.
1759 * \retval #PSA_ERROR_INVALID_ARGUMENT
1760 * \p user_id is not valid for the \p operation's algorithm and cipher
1761 * suite.
1762 * \retval #PSA_ERROR_NOT_SUPPORTED
1763 * The value of \p user_id is not supported by the implementation.
1764 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1766 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1767 * \retval #PSA_ERROR_BAD_STATE
1768 * The operation state is not valid, or
1769 * the library has not been previously initialized by psa_crypto_init().
1770 * It is implementation-dependent whether a failure to initialize
1771 * results in this error code.
1772 */
1773psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1774 const uint8_t *user_id,
1775 size_t user_id_len);
1776
1777/** Set the peer ID for a password-authenticated key exchange.
1778 *
1779 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1780 * that associate a user identifier with each side of the session. For PAKE
1781 * algorithms that associate a single user identifier with the session, call
1782 * psa_pake_set_user() only.
1783 *
1784 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1785 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1786 * for more information.
1787 *
1788 * \note When using the built-in implementation of #PSA_ALG_JPAKE, the peer ID
1789 * must be `"client"` (6-byte string) or `"server"` (6-byte string).
1790 * Third-party drivers may or may not have this limitation.
1791 *
1792 * \param[in,out] operation The operation object to set the peer ID for. It
1793 * must have been set up by psa_pake_setup() and
1794 * not yet in use (neither psa_pake_output() nor
1795 * psa_pake_input() has been called yet). It must
1796 * be on operation for which the peer ID hasn't
1797 * been set (psa_pake_set_peer() hasn't been
1798 * called yet).
1799 * \param[in] peer_id The peer's ID to authenticate.
1800 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1801 *
1802 * \retval #PSA_SUCCESS
1803 * Success.
1804 * \retval #PSA_ERROR_INVALID_ARGUMENT
1805 * \p peer_id is not valid for the \p operation's algorithm and cipher
1806 * suite.
1807 * \retval #PSA_ERROR_NOT_SUPPORTED
1808 * The algorithm doesn't associate a second identity with the session.
1809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1811 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1812 * \retval #PSA_ERROR_BAD_STATE
1813 * Calling psa_pake_set_peer() is invalid with the \p operation's
1814 * algorithm, the operation state is not valid, or the library has not
1815 * been previously initialized by psa_crypto_init().
1816 * It is implementation-dependent whether a failure to initialize
1817 * results in this error code.
1818 */
1819psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1820 const uint8_t *peer_id,
1821 size_t peer_id_len);
1822
1823/** Set the application role for a password-authenticated key exchange.
1824 *
1825 * Not all PAKE algorithms need to differentiate the communicating entities.
1826 * It is optional to call this function for PAKEs that don't require a role
1827 * to be specified. For such PAKEs the application role parameter is ignored,
1828 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
1829 *
1830 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1831 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1832 * for more information.
1833 *
1834 * \param[in,out] operation The operation object to specify the
1835 * application's role for. It must have been set up
1836 * by psa_pake_setup() and not yet in use (neither
1837 * psa_pake_output() nor psa_pake_input() has been
1838 * called yet). It must be on operation for which
1839 * the application's role hasn't been specified
1840 * (psa_pake_set_role() hasn't been called yet).
1841 * \param role A value of type ::psa_pake_role_t indicating the
1842 * application's role in the PAKE the algorithm
1843 * that is being set up. For more information see
1844 * the documentation of \c PSA_PAKE_ROLE_XXX
1845 * constants.
1846 *
1847 * \retval #PSA_SUCCESS
1848 * Success.
1849 * \retval #PSA_ERROR_INVALID_ARGUMENT
1850 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
1851 * \retval #PSA_ERROR_NOT_SUPPORTED
1852 * The \p role for this algorithm is not supported or is not valid.
1853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1854 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1855 * \retval #PSA_ERROR_BAD_STATE
1856 * The operation state is not valid, or
1857 * the library has not been previously initialized by psa_crypto_init().
1858 * It is implementation-dependent whether a failure to initialize
1859 * results in this error code.
1860 */
1861psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1862 psa_pake_role_t role);
1863
1864/** Get output for a step of a password-authenticated key exchange.
1865 *
1866 * Depending on the algorithm being executed, you might need to call this
1867 * function several times or you might not need to call this at all.
1868 *
1869 * The exact sequence of calls to perform a password-authenticated key
1870 * exchange depends on the algorithm in use. Refer to the documentation of
1871 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1872 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1873 * information.
1874 *
1875 * If this function returns an error status, the operation enters an error
1876 * state and must be aborted by calling psa_pake_abort().
1877 *
1878 * \param[in,out] operation Active PAKE operation.
1879 * \param step The step of the algorithm for which the output is
1880 * requested.
1881 * \param[out] output Buffer where the output is to be written in the
1882 * format appropriate for this \p step. Refer to
1883 * the documentation of the individual
1884 * \c PSA_PAKE_STEP_XXX constants for more
1885 * information.
1886 * \param output_size Size of the \p output buffer in bytes. This must
1887 * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1888 * primitive, \p output_step) where \c alg and
1889 * \p primitive are the PAKE algorithm and primitive
1890 * in the operation's cipher suite, and \p step is
1891 * the output step.
1892 *
1893 * \param[out] output_length On success, the number of bytes of the returned
1894 * output.
1895 *
1896 * \retval #PSA_SUCCESS
1897 * Success.
1898 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1899 * The size of the \p output buffer is too small.
1900 * \retval #PSA_ERROR_INVALID_ARGUMENT
1901 * \p step is not compatible with the operation's algorithm.
1902 * \retval #PSA_ERROR_NOT_SUPPORTED
1903 * \p step is not supported with the operation's algorithm.
1904 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1905 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1907 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1908 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1909 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1910 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1911 * \retval #PSA_ERROR_BAD_STATE
1912 * The operation state is not valid (it must be active, and fully set
1913 * up, and this call must conform to the algorithm's requirements
1914 * for ordering of input and output steps), or
1915 * the library has not been previously initialized by psa_crypto_init().
1916 * It is implementation-dependent whether a failure to initialize
1917 * results in this error code.
1918 */
1919psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1920 psa_pake_step_t step,
1921 uint8_t *output,
1922 size_t output_size,
1923 size_t *output_length);
1924
1925/** Provide input for a step of a password-authenticated key exchange.
1926 *
1927 * Depending on the algorithm being executed, you might need to call this
1928 * function several times or you might not need to call this at all.
1929 *
1930 * The exact sequence of calls to perform a password-authenticated key
1931 * exchange depends on the algorithm in use. Refer to the documentation of
1932 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1933 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1934 * information.
1935 *
1936 * If this function returns an error status, the operation enters an error
1937 * state and must be aborted by calling psa_pake_abort().
1938 *
1939 * \param[in,out] operation Active PAKE operation.
1940 * \param step The step for which the input is provided.
1941 * \param[in] input Buffer containing the input in the format
1942 * appropriate for this \p step. Refer to the
1943 * documentation of the individual
1944 * \c PSA_PAKE_STEP_XXX constants for more
1945 * information.
1946 * \param input_length Size of the \p input buffer in bytes.
1947 *
1948 * \retval #PSA_SUCCESS
1949 * Success.
1950 * \retval #PSA_ERROR_INVALID_SIGNATURE
1951 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
1952 * \retval #PSA_ERROR_INVALID_ARGUMENT
1953 * \p input_length is not compatible with the \p operation’s algorithm,
1954 * or the \p input is not valid for the \p operation's algorithm,
1955 * cipher suite or \p step.
1956 * \retval #PSA_ERROR_NOT_SUPPORTED
1957 * \p step p is not supported with the \p operation's algorithm, or the
1958 * \p input is not supported for the \p operation's algorithm, cipher
1959 * suite or \p step.
1960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1962 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1963 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1964 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1965 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1966 * \retval #PSA_ERROR_BAD_STATE
1967 * The operation state is not valid (it must be active, and fully set
1968 * up, and this call must conform to the algorithm's requirements
1969 * for ordering of input and output steps), or
1970 * the library has not been previously initialized by psa_crypto_init().
1971 * It is implementation-dependent whether a failure to initialize
1972 * results in this error code.
1973 */
1974psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1975 psa_pake_step_t step,
1976 const uint8_t *input,
1977 size_t input_length);
1978
1979/** Get implicitly confirmed shared secret from a PAKE.
1980 *
1981 * At this point there is a cryptographic guarantee that only the authenticated
1982 * party who used the same password is able to compute the key. But there is no
1983 * guarantee that the peer is the party it claims to be and was able to do so.
1984 *
1985 * That is, the authentication is only implicit. Since the peer is not
1986 * authenticated yet, no action should be taken yet that assumes that the peer
1987 * is who it claims to be. For example, do not access restricted files on the
1988 * peer's behalf until an explicit authentication has succeeded.
1989 *
1990 * This function can be called after the key exchange phase of the operation
1991 * has completed. It imports the shared secret output of the PAKE into the
1992 * provided derivation operation. The input step
1993 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1994 * material in the key derivation operation.
1995 *
1996 * The exact sequence of calls to perform a password-authenticated key
1997 * exchange depends on the algorithm in use. Refer to the documentation of
1998 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1999 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
2000 * information.
2001 *
2002 * When this function returns successfully, \p operation becomes inactive.
2003 * If this function returns an error status, both \p operation
2004 * and \c key_derivation operations enter an error state and must be aborted by
2005 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
2006 *
2007 * \param[in,out] operation Active PAKE operation.
2008 * \param[out] output A key derivation operation that is ready
2009 * for an input step of type
2010 * #PSA_KEY_DERIVATION_INPUT_SECRET.
2011 *
2012 * \retval #PSA_SUCCESS
2013 * Success.
2014 * \retval #PSA_ERROR_INVALID_ARGUMENT
2015 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
2016 * algorithm in the \p output key derivation operation.
2017 * \retval #PSA_ERROR_NOT_SUPPORTED
2018 * Input from a PAKE is not supported by the algorithm in the \p output
2019 * key derivation operation.
2020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2022 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2023 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2024 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2025 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2026 * \retval #PSA_ERROR_BAD_STATE
2027 * The PAKE operation state is not valid (it must be active, but beyond
2028 * that validity is specific to the algorithm), or
2029 * the library has not been previously initialized by psa_crypto_init(),
2030 * or the state of \p output is not valid for
2031 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
2032 * step is out of order or the application has done this step already
2033 * and it may not be repeated.
2034 * It is implementation-dependent whether a failure to initialize
2035 * results in this error code.
2036 */
2037psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
2038 psa_key_derivation_operation_t *output);
2039
2040/** Abort a PAKE operation.
2041 *
2042 * Aborting an operation frees all associated resources except for the \c
2043 * operation structure itself. Once aborted, the operation object can be reused
2044 * for another operation by calling psa_pake_setup() again.
2045 *
2046 * This function may be called at any time after the operation
2047 * object has been initialized as described in #psa_pake_operation_t.
2048 *
2049 * In particular, calling psa_pake_abort() after the operation has been
2050 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
2051 * is safe and has no effect.
2052 *
2053 * \param[in,out] operation The operation to abort.
2054 *
2055 * \retval #PSA_SUCCESS
2056 * Success.
2057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2058 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2059 * \retval #PSA_ERROR_BAD_STATE
2060 * The library has not been previously initialized by psa_crypto_init().
2061 * It is implementation-dependent whether a failure to initialize
2062 * results in this error code.
2063 */
2064psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
2065
2066/**@}*/
2067
2068static inline psa_algorithm_t psa_pake_cs_get_algorithm(
2069 const psa_pake_cipher_suite_t *cipher_suite)
2070{
2071 return cipher_suite->algorithm;
2072}
2073
2074static inline void psa_pake_cs_set_algorithm(
2075 psa_pake_cipher_suite_t *cipher_suite,
2076 psa_algorithm_t algorithm)
2077{
2078 if (!PSA_ALG_IS_PAKE(algorithm)) {
2079 cipher_suite->algorithm = 0;
2080 } else {
2081 cipher_suite->algorithm = algorithm;
2082 }
2083}
2084
2085static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
2086 const psa_pake_cipher_suite_t *cipher_suite)
2087{
2088 return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
2089 cipher_suite->bits);
2090}
2091
2092static inline void psa_pake_cs_set_primitive(
2093 psa_pake_cipher_suite_t *cipher_suite,
2094 psa_pake_primitive_t primitive)
2095{
2096 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
2097 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
2098 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
2099}
2100
2101static inline psa_pake_family_t psa_pake_cs_get_family(
2102 const psa_pake_cipher_suite_t *cipher_suite)
2103{
2104 return cipher_suite->family;
2105}
2106
2107static inline uint16_t psa_pake_cs_get_bits(
2108 const psa_pake_cipher_suite_t *cipher_suite)
2109{
2110 return cipher_suite->bits;
2111}
2112
2113static inline psa_algorithm_t psa_pake_cs_get_hash(
2114 const psa_pake_cipher_suite_t *cipher_suite)
2115{
2116 return cipher_suite->hash;
2117}
2118
2119static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
2120 psa_algorithm_t hash)
2121{
2122 if (!PSA_ALG_IS_HASH(hash)) {
2123 cipher_suite->hash = 0;
2124 } else {
2125 cipher_suite->hash = hash;
2126 }
2127}
2128
2129static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
2130{
2131 const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
2132 return v;
2133}
2134
2135static inline struct psa_pake_operation_s psa_pake_operation_init(void)
2136{
2137 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
2138 return v;
2139}
2140
2141#ifdef __cplusplus
2142}
2143#endif
2144
2145#endif /* PSA_CRYPTO_EXTRA_H */
2146