132b31808SJens Wiklander /**
232b31808SJens Wiklander * \file psa/crypto_extra.h
332b31808SJens Wiklander *
432b31808SJens Wiklander * \brief PSA cryptography module: Mbed TLS vendor extensions
532b31808SJens Wiklander *
632b31808SJens Wiklander * \note This file may not be included directly. Applications must
732b31808SJens Wiklander * include psa/crypto.h.
832b31808SJens Wiklander *
932b31808SJens Wiklander * This file is reserved for vendor-specific definitions.
1032b31808SJens Wiklander */
1132b31808SJens Wiklander /*
1232b31808SJens Wiklander * Copyright The Mbed TLS Contributors
13b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
1432b31808SJens Wiklander */
1532b31808SJens Wiklander
1632b31808SJens Wiklander #ifndef PSA_CRYPTO_EXTRA_H
1732b31808SJens Wiklander #define PSA_CRYPTO_EXTRA_H
1832b31808SJens Wiklander #include "mbedtls/private_access.h"
1932b31808SJens Wiklander
2032b31808SJens Wiklander #include "crypto_types.h"
2132b31808SJens Wiklander #include "crypto_compat.h"
2232b31808SJens Wiklander
2332b31808SJens Wiklander #ifdef __cplusplus
2432b31808SJens Wiklander extern "C" {
2532b31808SJens Wiklander #endif
2632b31808SJens Wiklander
2732b31808SJens Wiklander /* UID for secure storage seed */
2832b31808SJens Wiklander #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
2932b31808SJens Wiklander
3032b31808SJens Wiklander /* See mbedtls_config.h for definition */
3132b31808SJens Wiklander #if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
3232b31808SJens Wiklander #define MBEDTLS_PSA_KEY_SLOT_COUNT 32
3332b31808SJens Wiklander #endif
3432b31808SJens Wiklander
35c3deb3d6SEtienne Carriere /* If the size of static key slots is not explicitly defined by the user, then
36c3deb3d6SEtienne Carriere * set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE and
37c3deb3d6SEtienne Carriere * PSA_CIPHER_MAX_KEY_LENGTH.
38c3deb3d6SEtienne Carriere * See mbedtls_config.h for the definition. */
39c3deb3d6SEtienne Carriere #if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)
40c3deb3d6SEtienne Carriere #define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE \
41c3deb3d6SEtienne Carriere ((PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > PSA_CIPHER_MAX_KEY_LENGTH) ? \
42c3deb3d6SEtienne Carriere PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE : PSA_CIPHER_MAX_KEY_LENGTH)
43c3deb3d6SEtienne Carriere #endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/
44c3deb3d6SEtienne Carriere
4532b31808SJens Wiklander /** \addtogroup attributes
4632b31808SJens Wiklander * @{
4732b31808SJens Wiklander */
4832b31808SJens Wiklander
4932b31808SJens Wiklander /** \brief Declare the enrollment algorithm for a key.
5032b31808SJens Wiklander *
5132b31808SJens Wiklander * An operation on a key may indifferently use the algorithm set with
5232b31808SJens Wiklander * psa_set_key_algorithm() or with this function.
5332b31808SJens Wiklander *
5432b31808SJens Wiklander * \param[out] attributes The attribute structure to write to.
5532b31808SJens Wiklander * \param alg2 A second algorithm that the key may be used
5632b31808SJens Wiklander * for, in addition to the algorithm set with
5732b31808SJens Wiklander * psa_set_key_algorithm().
5832b31808SJens Wiklander *
5932b31808SJens Wiklander * \warning Setting an enrollment algorithm is not recommended, because
6032b31808SJens Wiklander * using the same key with different algorithms can allow some
6132b31808SJens Wiklander * attacks based on arithmetic relations between different
6232b31808SJens Wiklander * computations made with the same key, or can escalate harmless
6332b31808SJens Wiklander * side channels into exploitable ones. Use this function only
6432b31808SJens Wiklander * if it is necessary to support a protocol for which it has been
6532b31808SJens Wiklander * verified that the usage of the key with multiple algorithms
6632b31808SJens Wiklander * is safe.
6732b31808SJens Wiklander */
psa_set_key_enrollment_algorithm(psa_key_attributes_t * attributes,psa_algorithm_t alg2)6832b31808SJens Wiklander static inline void psa_set_key_enrollment_algorithm(
6932b31808SJens Wiklander psa_key_attributes_t *attributes,
7032b31808SJens Wiklander psa_algorithm_t alg2)
7132b31808SJens Wiklander {
72b0563631STom Van Eyck attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
7332b31808SJens Wiklander }
7432b31808SJens Wiklander
7532b31808SJens Wiklander /** Retrieve the enrollment algorithm policy from key attributes.
7632b31808SJens Wiklander *
7732b31808SJens Wiklander * \param[in] attributes The key attribute structure to query.
7832b31808SJens Wiklander *
7932b31808SJens Wiklander * \return The enrollment algorithm stored in the attribute structure.
8032b31808SJens Wiklander */
psa_get_key_enrollment_algorithm(const psa_key_attributes_t * attributes)8132b31808SJens Wiklander static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
8232b31808SJens Wiklander const psa_key_attributes_t *attributes)
8332b31808SJens Wiklander {
84b0563631STom Van Eyck return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
8532b31808SJens Wiklander }
8632b31808SJens Wiklander
8732b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
8832b31808SJens Wiklander
8932b31808SJens Wiklander /** Retrieve the slot number where a key is stored.
9032b31808SJens Wiklander *
9132b31808SJens Wiklander * A slot number is only defined for keys that are stored in a secure
9232b31808SJens Wiklander * element.
9332b31808SJens Wiklander *
9432b31808SJens Wiklander * This information is only useful if the secure element is not entirely
9532b31808SJens Wiklander * managed through the PSA Cryptography API. It is up to the secure
9632b31808SJens Wiklander * element driver to decide how PSA slot numbers map to any other interface
9732b31808SJens Wiklander * that the secure element may have.
9832b31808SJens Wiklander *
9932b31808SJens Wiklander * \param[in] attributes The key attribute structure to query.
10032b31808SJens Wiklander * \param[out] slot_number On success, the slot number containing the key.
10132b31808SJens Wiklander *
10232b31808SJens Wiklander * \retval #PSA_SUCCESS
10332b31808SJens Wiklander * The key is located in a secure element, and \p *slot_number
10432b31808SJens Wiklander * indicates the slot number that contains it.
10532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED
10632b31808SJens Wiklander * The caller is not permitted to query the slot number.
107b0563631STom Van Eyck * Mbed TLS currently does not return this error.
10832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
10932b31808SJens Wiklander * The key is not located in a secure element.
11032b31808SJens Wiklander */
11132b31808SJens Wiklander psa_status_t psa_get_key_slot_number(
11232b31808SJens Wiklander const psa_key_attributes_t *attributes,
11332b31808SJens Wiklander psa_key_slot_number_t *slot_number);
11432b31808SJens Wiklander
11532b31808SJens Wiklander /** Choose the slot number where a key is stored.
11632b31808SJens Wiklander *
11732b31808SJens Wiklander * This function declares a slot number in the specified attribute
11832b31808SJens Wiklander * structure.
11932b31808SJens Wiklander *
12032b31808SJens Wiklander * A slot number is only meaningful for keys that are stored in a secure
12132b31808SJens Wiklander * element. It is up to the secure element driver to decide how PSA slot
12232b31808SJens Wiklander * numbers map to any other interface that the secure element may have.
12332b31808SJens Wiklander *
12432b31808SJens Wiklander * \note Setting a slot number in key attributes for a key creation can
12532b31808SJens Wiklander * cause the following errors when creating the key:
12632b31808SJens Wiklander * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
12732b31808SJens Wiklander * not support choosing a specific slot number.
12832b31808SJens Wiklander * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
12932b31808SJens Wiklander * choose slot numbers in general or to choose this specific slot.
13032b31808SJens Wiklander * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
13132b31808SJens Wiklander * valid in general or not valid for this specific key.
13232b31808SJens Wiklander * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
13332b31808SJens Wiklander * selected slot.
13432b31808SJens Wiklander *
13532b31808SJens Wiklander * \param[out] attributes The attribute structure to write to.
13632b31808SJens Wiklander * \param slot_number The slot number to set.
13732b31808SJens Wiklander */
psa_set_key_slot_number(psa_key_attributes_t * attributes,psa_key_slot_number_t slot_number)13832b31808SJens Wiklander static inline void psa_set_key_slot_number(
13932b31808SJens Wiklander psa_key_attributes_t *attributes,
14032b31808SJens Wiklander psa_key_slot_number_t slot_number)
14132b31808SJens Wiklander {
142b0563631STom Van Eyck attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;
14332b31808SJens Wiklander attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
14432b31808SJens Wiklander }
14532b31808SJens Wiklander
14632b31808SJens Wiklander /** Remove the slot number attribute from a key attribute structure.
14732b31808SJens Wiklander *
14832b31808SJens Wiklander * This function undoes the action of psa_set_key_slot_number().
14932b31808SJens Wiklander *
15032b31808SJens Wiklander * \param[out] attributes The attribute structure to write to.
15132b31808SJens Wiklander */
psa_clear_key_slot_number(psa_key_attributes_t * attributes)15232b31808SJens Wiklander static inline void psa_clear_key_slot_number(
15332b31808SJens Wiklander psa_key_attributes_t *attributes)
15432b31808SJens Wiklander {
155b0563631STom Van Eyck attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;
15632b31808SJens Wiklander }
15732b31808SJens Wiklander
15832b31808SJens Wiklander /** Register a key that is already present in a secure element.
15932b31808SJens Wiklander *
16032b31808SJens Wiklander * The key must be located in a secure element designated by the
16132b31808SJens Wiklander * lifetime field in \p attributes, in the slot set with
16232b31808SJens Wiklander * psa_set_key_slot_number() in the attribute structure.
16332b31808SJens Wiklander * This function makes the key available through the key identifier
16432b31808SJens Wiklander * specified in \p attributes.
16532b31808SJens Wiklander *
16632b31808SJens Wiklander * \param[in] attributes The attributes of the existing key.
167cb034002SJerome Forissier * - The lifetime must be a persistent lifetime
168cb034002SJerome Forissier * in a secure element. Volatile lifetimes are
169cb034002SJerome Forissier * not currently supported.
170cb034002SJerome Forissier * - The key identifier must be in the valid
171cb034002SJerome Forissier * range for persistent keys.
172cb034002SJerome Forissier * - The key type and size must be specified and
173cb034002SJerome Forissier * must be consistent with the key material
174cb034002SJerome Forissier * in the secure element.
17532b31808SJens Wiklander *
17632b31808SJens Wiklander * \retval #PSA_SUCCESS
17732b31808SJens Wiklander * The key was successfully registered.
17832b31808SJens Wiklander * Note that depending on the design of the driver, this may or may
17932b31808SJens Wiklander * not guarantee that a key actually exists in the designated slot
18032b31808SJens Wiklander * and is compatible with the specified attributes.
18132b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS
18232b31808SJens Wiklander * There is already a key with the identifier specified in
18332b31808SJens Wiklander * \p attributes.
18432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
18532b31808SJens Wiklander * The secure element driver for the specified lifetime does not
18632b31808SJens Wiklander * support registering a key.
18732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
18832b31808SJens Wiklander * The identifier in \p attributes is invalid, namely the identifier is
18932b31808SJens Wiklander * not in the user range, or
19032b31808SJens Wiklander * \p attributes specifies a lifetime which is not located
19132b31808SJens Wiklander * in a secure element, or no slot number is specified in \p attributes,
19232b31808SJens Wiklander * or the specified slot number is not valid.
19332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED
19432b31808SJens Wiklander * The caller is not authorized to register the specified key slot.
19532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
19632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
19732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
19832b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription
19932b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
20032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
20132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
20232b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init().
20332b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
20432b31808SJens Wiklander * results in this error code.
20532b31808SJens Wiklander */
20632b31808SJens Wiklander psa_status_t mbedtls_psa_register_se_key(
20732b31808SJens Wiklander const psa_key_attributes_t *attributes);
20832b31808SJens Wiklander
20932b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
21032b31808SJens Wiklander
21132b31808SJens Wiklander /**@}*/
21232b31808SJens Wiklander
21332b31808SJens Wiklander /**
21432b31808SJens Wiklander * \brief Library deinitialization.
21532b31808SJens Wiklander *
21632b31808SJens Wiklander * This function clears all data associated with the PSA layer,
21732b31808SJens Wiklander * including the whole key store.
218b0563631STom Van Eyck * This function is not thread safe, it wipes every key slot regardless of
219b0563631STom Van Eyck * state and reader count. It should only be called when no slot is in use.
22032b31808SJens Wiklander *
22132b31808SJens Wiklander * This is an Mbed TLS extension.
22232b31808SJens Wiklander */
22332b31808SJens Wiklander void mbedtls_psa_crypto_free(void);
22432b31808SJens Wiklander
22532b31808SJens Wiklander /** \brief Statistics about
22632b31808SJens Wiklander * resource consumption related to the PSA keystore.
22732b31808SJens Wiklander *
22832b31808SJens Wiklander * \note The content of this structure is not part of the stable API and ABI
229b0563631STom Van Eyck * of Mbed TLS and may change arbitrarily from version to version.
23032b31808SJens Wiklander */
23132b31808SJens Wiklander typedef struct mbedtls_psa_stats_s {
23232b31808SJens Wiklander /** Number of slots containing key material for a volatile key. */
23332b31808SJens Wiklander size_t MBEDTLS_PRIVATE(volatile_slots);
23432b31808SJens Wiklander /** Number of slots containing key material for a key which is in
23532b31808SJens Wiklander * internal persistent storage. */
23632b31808SJens Wiklander size_t MBEDTLS_PRIVATE(persistent_slots);
23732b31808SJens Wiklander /** Number of slots containing a reference to a key in a
23832b31808SJens Wiklander * secure element. */
23932b31808SJens Wiklander size_t MBEDTLS_PRIVATE(external_slots);
24032b31808SJens Wiklander /** Number of slots which are occupied, but do not contain
24132b31808SJens Wiklander * key material yet. */
24232b31808SJens Wiklander size_t MBEDTLS_PRIVATE(half_filled_slots);
24332b31808SJens Wiklander /** Number of slots that contain cache data. */
24432b31808SJens Wiklander size_t MBEDTLS_PRIVATE(cache_slots);
24532b31808SJens Wiklander /** Number of slots that are not used for anything. */
24632b31808SJens Wiklander size_t MBEDTLS_PRIVATE(empty_slots);
24732b31808SJens Wiklander /** Number of slots that are locked. */
24832b31808SJens Wiklander size_t MBEDTLS_PRIVATE(locked_slots);
24932b31808SJens Wiklander /** Largest key id value among open keys in internal persistent storage. */
25032b31808SJens Wiklander psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
25132b31808SJens Wiklander /** Largest key id value among open keys in secure elements. */
25232b31808SJens Wiklander psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
25332b31808SJens Wiklander } mbedtls_psa_stats_t;
25432b31808SJens Wiklander
25532b31808SJens Wiklander /** \brief Get statistics about
25632b31808SJens Wiklander * resource consumption related to the PSA keystore.
25732b31808SJens Wiklander *
258b0563631STom Van Eyck * \note When Mbed TLS is built as part of a service, with isolation
25932b31808SJens Wiklander * between the application and the keystore, the service may or
26032b31808SJens Wiklander * may not expose this function.
26132b31808SJens Wiklander */
26232b31808SJens Wiklander void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
26332b31808SJens Wiklander
26432b31808SJens Wiklander /**
26532b31808SJens Wiklander * \brief Inject an initial entropy seed for the random generator into
26632b31808SJens Wiklander * secure storage.
26732b31808SJens Wiklander *
26832b31808SJens Wiklander * This function injects data to be used as a seed for the random generator
26932b31808SJens Wiklander * used by the PSA Crypto implementation. On devices that lack a trusted
27032b31808SJens Wiklander * entropy source (preferably a hardware random number generator),
27132b31808SJens Wiklander * the Mbed PSA Crypto implementation uses this value to seed its
27232b31808SJens Wiklander * random generator.
27332b31808SJens Wiklander *
27432b31808SJens Wiklander * On devices without a trusted entropy source, this function must be
27532b31808SJens Wiklander * called exactly once in the lifetime of the device. On devices with
27632b31808SJens Wiklander * a trusted entropy source, calling this function is optional.
27732b31808SJens Wiklander * In all cases, this function may only be called before calling any
27832b31808SJens Wiklander * other function in the PSA Crypto API, including psa_crypto_init().
27932b31808SJens Wiklander *
28032b31808SJens Wiklander * When this function returns successfully, it populates a file in
28132b31808SJens Wiklander * persistent storage. Once the file has been created, this function
28232b31808SJens Wiklander * can no longer succeed.
28332b31808SJens Wiklander *
28432b31808SJens Wiklander * If any error occurs, this function does not change the system state.
28532b31808SJens Wiklander * You can call this function again after correcting the reason for the
28632b31808SJens Wiklander * error if possible.
28732b31808SJens Wiklander *
28832b31808SJens Wiklander * \warning This function **can** fail! Callers MUST check the return status.
28932b31808SJens Wiklander *
29032b31808SJens Wiklander * \warning If you use this function, you should use it as part of a
29132b31808SJens Wiklander * factory provisioning process. The value of the injected seed
29232b31808SJens Wiklander * is critical to the security of the device. It must be
29332b31808SJens Wiklander * *secret*, *unpredictable* and (statistically) *unique per device*.
29432b31808SJens Wiklander * You should be generate it randomly using a cryptographically
29532b31808SJens Wiklander * secure random generator seeded from trusted entropy sources.
29632b31808SJens Wiklander * You should transmit it securely to the device and ensure
29732b31808SJens Wiklander * that its value is not leaked or stored anywhere beyond the
29832b31808SJens Wiklander * needs of transmitting it from the point of generation to
29932b31808SJens Wiklander * the call of this function, and erase all copies of the value
30032b31808SJens Wiklander * once this function returns.
30132b31808SJens Wiklander *
30232b31808SJens Wiklander * This is an Mbed TLS extension.
30332b31808SJens Wiklander *
30432b31808SJens Wiklander * \note This function is only available on the following platforms:
30532b31808SJens Wiklander * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
30632b31808SJens Wiklander * Note that you must provide compatible implementations of
30732b31808SJens Wiklander * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
30832b31808SJens Wiklander * * In a client-server integration of PSA Cryptography, on the client side,
30932b31808SJens Wiklander * if the server supports this feature.
31032b31808SJens Wiklander * \param[in] seed Buffer containing the seed value to inject.
31132b31808SJens Wiklander * \param[in] seed_size Size of the \p seed buffer.
31232b31808SJens Wiklander * The size of the seed in bytes must be greater
31332b31808SJens Wiklander * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
31432b31808SJens Wiklander * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
31532b31808SJens Wiklander * in `library/entropy_poll.h` in the Mbed TLS source
31632b31808SJens Wiklander * code.
31732b31808SJens Wiklander * It must be less or equal to
31832b31808SJens Wiklander * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
31932b31808SJens Wiklander *
32032b31808SJens Wiklander * \retval #PSA_SUCCESS
32132b31808SJens Wiklander * The seed value was injected successfully. The random generator
32232b31808SJens Wiklander * of the PSA Crypto implementation is now ready for use.
32332b31808SJens Wiklander * You may now call psa_crypto_init() and use the PSA Crypto
32432b31808SJens Wiklander * implementation.
32532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
32632b31808SJens Wiklander * \p seed_size is out of range.
32732b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE
32832b31808SJens Wiklander * There was a failure reading or writing from storage.
32932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED
33032b31808SJens Wiklander * The library has already been initialized. It is no longer
33132b31808SJens Wiklander * possible to call this function.
33232b31808SJens Wiklander */
33332b31808SJens Wiklander psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
33432b31808SJens Wiklander size_t seed_size);
33532b31808SJens Wiklander
33632b31808SJens Wiklander /** \addtogroup crypto_types
33732b31808SJens Wiklander * @{
33832b31808SJens Wiklander */
33932b31808SJens Wiklander
34032b31808SJens Wiklander /** DSA public key.
34132b31808SJens Wiklander *
34232b31808SJens Wiklander * The import and export format is the
34332b31808SJens Wiklander * representation of the public key `y = g^x mod p` as a big-endian byte
34432b31808SJens Wiklander * string. The length of the byte string is the length of the base prime `p`
34532b31808SJens Wiklander * in bytes.
34632b31808SJens Wiklander */
34732b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
34832b31808SJens Wiklander
34932b31808SJens Wiklander /** DSA key pair (private and public key).
35032b31808SJens Wiklander *
35132b31808SJens Wiklander * The import and export format is the
35232b31808SJens Wiklander * representation of the private key `x` as a big-endian byte string. The
35332b31808SJens Wiklander * length of the byte string is the private key size in bytes (leading zeroes
35432b31808SJens Wiklander * are not stripped).
35532b31808SJens Wiklander *
35632b31808SJens Wiklander * Deterministic DSA key derivation with psa_generate_derived_key follows
35732b31808SJens Wiklander * FIPS 186-4 §B.1.2: interpret the byte string as integer
35832b31808SJens Wiklander * in big-endian order. Discard it if it is not in the range
35932b31808SJens Wiklander * [0, *N* - 2] where *N* is the boundary of the private key domain
36032b31808SJens Wiklander * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
36132b31808SJens Wiklander * or the order of the curve's base point for ECC).
36232b31808SJens Wiklander * Add 1 to the resulting integer and use this as the private key *x*.
36332b31808SJens Wiklander *
36432b31808SJens Wiklander */
36532b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
36632b31808SJens Wiklander
36732b31808SJens Wiklander /** Whether a key type is a DSA key (pair or public-only). */
36832b31808SJens Wiklander #define PSA_KEY_TYPE_IS_DSA(type) \
36932b31808SJens Wiklander (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
37032b31808SJens Wiklander
37132b31808SJens Wiklander #define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
37232b31808SJens Wiklander /** DSA signature with hashing.
37332b31808SJens Wiklander *
37432b31808SJens Wiklander * This is the signature scheme defined by FIPS 186-4,
37532b31808SJens Wiklander * with a random per-message secret number (*k*).
37632b31808SJens Wiklander *
37732b31808SJens Wiklander * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
37832b31808SJens Wiklander * #PSA_ALG_IS_HASH(\p hash_alg) is true).
37932b31808SJens Wiklander * This includes #PSA_ALG_ANY_HASH
38032b31808SJens Wiklander * when specifying the algorithm in a usage policy.
38132b31808SJens Wiklander *
38232b31808SJens Wiklander * \return The corresponding DSA signature algorithm.
38332b31808SJens Wiklander * \return Unspecified if \p hash_alg is not a supported
38432b31808SJens Wiklander * hash algorithm.
38532b31808SJens Wiklander */
38632b31808SJens Wiklander #define PSA_ALG_DSA(hash_alg) \
38732b31808SJens Wiklander (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
38832b31808SJens Wiklander #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
38932b31808SJens Wiklander #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
39032b31808SJens Wiklander /** Deterministic DSA signature with hashing.
39132b31808SJens Wiklander *
39232b31808SJens Wiklander * This is the deterministic variant defined by RFC 6979 of
39332b31808SJens Wiklander * the signature scheme defined by FIPS 186-4.
39432b31808SJens Wiklander *
39532b31808SJens Wiklander * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
39632b31808SJens Wiklander * #PSA_ALG_IS_HASH(\p hash_alg) is true).
39732b31808SJens Wiklander * This includes #PSA_ALG_ANY_HASH
39832b31808SJens Wiklander * when specifying the algorithm in a usage policy.
39932b31808SJens Wiklander *
40032b31808SJens Wiklander * \return The corresponding DSA signature algorithm.
40132b31808SJens Wiklander * \return Unspecified if \p hash_alg is not a supported
40232b31808SJens Wiklander * hash algorithm.
40332b31808SJens Wiklander */
40432b31808SJens Wiklander #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
40532b31808SJens Wiklander (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
40632b31808SJens Wiklander #define PSA_ALG_IS_DSA(alg) \
40732b31808SJens Wiklander (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
40832b31808SJens Wiklander PSA_ALG_DSA_BASE)
40932b31808SJens Wiklander #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
41032b31808SJens Wiklander (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
41132b31808SJens Wiklander #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
41232b31808SJens Wiklander (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
41332b31808SJens Wiklander #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
41432b31808SJens Wiklander (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
41532b31808SJens Wiklander
41632b31808SJens Wiklander
41732b31808SJens Wiklander /* We need to expand the sample definition of this macro from
41832b31808SJens Wiklander * the API definition. */
41932b31808SJens Wiklander #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
42032b31808SJens Wiklander #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
42132b31808SJens Wiklander PSA_ALG_IS_DSA(alg)
42232b31808SJens Wiklander
42332b31808SJens Wiklander /**@}*/
42432b31808SJens Wiklander
42532b31808SJens Wiklander /** \addtogroup attributes
42632b31808SJens Wiklander * @{
42732b31808SJens Wiklander */
42832b31808SJens Wiklander
42932b31808SJens Wiklander /** PAKE operation stages. */
43032b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_SETUP 0
43132b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
43232b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
43332b31808SJens Wiklander
43432b31808SJens Wiklander /**@}*/
43532b31808SJens Wiklander
43632b31808SJens Wiklander
43732b31808SJens Wiklander /** \defgroup psa_external_rng External random generator
43832b31808SJens Wiklander * @{
43932b31808SJens Wiklander */
44032b31808SJens Wiklander
44132b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
44232b31808SJens Wiklander /** External random generator function, implemented by the platform.
44332b31808SJens Wiklander *
44432b31808SJens Wiklander * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
44532b31808SJens Wiklander * this function replaces Mbed TLS's entropy and DRBG modules for all
44632b31808SJens Wiklander * random generation triggered via PSA crypto interfaces.
44732b31808SJens Wiklander *
44832b31808SJens Wiklander * \note This random generator must deliver random numbers with cryptographic
44932b31808SJens Wiklander * quality and high performance. It must supply unpredictable numbers
45032b31808SJens Wiklander * with a uniform distribution. The implementation of this function
45132b31808SJens Wiklander * is responsible for ensuring that the random generator is seeded
45232b31808SJens Wiklander * with sufficient entropy. If you have a hardware TRNG which is slow
45332b31808SJens Wiklander * or delivers non-uniform output, declare it as an entropy source
45432b31808SJens Wiklander * with mbedtls_entropy_add_source() instead of enabling this option.
45532b31808SJens Wiklander *
45632b31808SJens Wiklander * \param[in,out] context Pointer to the random generator context.
45732b31808SJens Wiklander * This is all-bits-zero on the first call
45832b31808SJens Wiklander * and preserved between successive calls.
45932b31808SJens Wiklander * \param[out] output Output buffer. On success, this buffer
46032b31808SJens Wiklander * contains random data with a uniform
46132b31808SJens Wiklander * distribution.
46232b31808SJens Wiklander * \param output_size The size of the \p output buffer in bytes.
46332b31808SJens Wiklander * \param[out] output_length On success, set this value to \p output_size.
46432b31808SJens Wiklander *
46532b31808SJens Wiklander * \retval #PSA_SUCCESS
46632b31808SJens Wiklander * Success. The output buffer contains \p output_size bytes of
46732b31808SJens Wiklander * cryptographic-quality random data, and \c *output_length is
46832b31808SJens Wiklander * set to \p output_size.
46932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
47032b31808SJens Wiklander * The random generator requires extra entropy and there is no
47132b31808SJens Wiklander * way to obtain entropy under current environment conditions.
47232b31808SJens Wiklander * This error should not happen under normal circumstances since
47332b31808SJens Wiklander * this function is responsible for obtaining as much entropy as
47432b31808SJens Wiklander * it needs. However implementations of this function may return
47532b31808SJens Wiklander * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
47632b31808SJens Wiklander * entropy without blocking indefinitely.
47732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE
47832b31808SJens Wiklander * A failure of the random generator hardware that isn't covered
47932b31808SJens Wiklander * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
48032b31808SJens Wiklander */
48132b31808SJens Wiklander psa_status_t mbedtls_psa_external_get_random(
48232b31808SJens Wiklander mbedtls_psa_external_random_context_t *context,
48332b31808SJens Wiklander uint8_t *output, size_t output_size, size_t *output_length);
48432b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
48532b31808SJens Wiklander
48632b31808SJens Wiklander /**@}*/
48732b31808SJens Wiklander
48832b31808SJens Wiklander /** \defgroup psa_builtin_keys Built-in keys
48932b31808SJens Wiklander * @{
49032b31808SJens Wiklander */
49132b31808SJens Wiklander
49232b31808SJens Wiklander /** The minimum value for a key identifier that is built into the
49332b31808SJens Wiklander * implementation.
49432b31808SJens Wiklander *
49532b31808SJens Wiklander * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
49632b31808SJens Wiklander * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
49732b31808SJens Wiklander * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
49832b31808SJens Wiklander * with any other set of implementation-chosen key identifiers.
49932b31808SJens Wiklander *
500cb034002SJerome Forissier * This value is part of the library's API since changing it would invalidate
50132b31808SJens Wiklander * the values of built-in key identifiers in applications.
50232b31808SJens Wiklander */
50332b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
50432b31808SJens Wiklander
50532b31808SJens Wiklander /** The maximum value for a key identifier that is built into the
50632b31808SJens Wiklander * implementation.
50732b31808SJens Wiklander *
50832b31808SJens Wiklander * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
50932b31808SJens Wiklander */
51032b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
51132b31808SJens Wiklander
51232b31808SJens Wiklander /** A slot number identifying a key in a driver.
51332b31808SJens Wiklander *
51432b31808SJens Wiklander * Values of this type are used to identify built-in keys.
51532b31808SJens Wiklander */
51632b31808SJens Wiklander typedef uint64_t psa_drv_slot_number_t;
51732b31808SJens Wiklander
51832b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
51932b31808SJens Wiklander /** Test whether a key identifier belongs to the builtin key range.
52032b31808SJens Wiklander *
52132b31808SJens Wiklander * \param key_id Key identifier to test.
52232b31808SJens Wiklander *
52332b31808SJens Wiklander * \retval 1
52432b31808SJens Wiklander * The key identifier is a builtin key identifier.
52532b31808SJens Wiklander * \retval 0
52632b31808SJens Wiklander * The key identifier is not a builtin key identifier.
52732b31808SJens Wiklander */
psa_key_id_is_builtin(psa_key_id_t key_id)52832b31808SJens Wiklander static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
52932b31808SJens Wiklander {
53032b31808SJens Wiklander return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
53132b31808SJens Wiklander (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
53232b31808SJens Wiklander }
53332b31808SJens Wiklander
53432b31808SJens Wiklander /** Platform function to obtain the location and slot number of a built-in key.
53532b31808SJens Wiklander *
53632b31808SJens Wiklander * An application-specific implementation of this function must be provided if
53732b31808SJens Wiklander * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
53832b31808SJens Wiklander * as part of a platform's system image.
53932b31808SJens Wiklander *
54032b31808SJens Wiklander * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
54132b31808SJens Wiklander * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
54232b31808SJens Wiklander *
54332b31808SJens Wiklander * In a multi-application configuration
54432b31808SJens Wiklander * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
54532b31808SJens Wiklander * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
54632b31808SJens Wiklander * is allowed to use the given key.
54732b31808SJens Wiklander *
54832b31808SJens Wiklander * \param key_id The key ID for which to retrieve the
54932b31808SJens Wiklander * location and slot attributes.
55032b31808SJens Wiklander * \param[out] lifetime On success, the lifetime associated with the key
55132b31808SJens Wiklander * corresponding to \p key_id. Lifetime is a
55232b31808SJens Wiklander * combination of which driver contains the key,
55332b31808SJens Wiklander * and with what persistence level the key is
55432b31808SJens Wiklander * intended to be used. If the platform
55532b31808SJens Wiklander * implementation does not contain specific
55632b31808SJens Wiklander * information about the intended key persistence
55732b31808SJens Wiklander * level, the persistence level may be reported as
55832b31808SJens Wiklander * #PSA_KEY_PERSISTENCE_DEFAULT.
55932b31808SJens Wiklander * \param[out] slot_number On success, the slot number known to the driver
56032b31808SJens Wiklander * registered at the lifetime location reported
56132b31808SJens Wiklander * through \p lifetime which corresponds to the
56232b31808SJens Wiklander * requested built-in key.
56332b31808SJens Wiklander *
56432b31808SJens Wiklander * \retval #PSA_SUCCESS
56532b31808SJens Wiklander * The requested key identifier designates a built-in key.
56632b31808SJens Wiklander * In a multi-application configuration, the requested owner
56732b31808SJens Wiklander * is allowed to access it.
56832b31808SJens Wiklander * \retval #PSA_ERROR_DOES_NOT_EXIST
56932b31808SJens Wiklander * The requested key identifier is not a built-in key which is known
57032b31808SJens Wiklander * to this function. If a key exists in the key storage with this
57132b31808SJens Wiklander * identifier, the data from the storage will be used.
57232b31808SJens Wiklander * \return (any other error)
57332b31808SJens Wiklander * Any other error is propagated to the function that requested the key.
57432b31808SJens Wiklander * Common errors include:
57532b31808SJens Wiklander * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
57632b31808SJens Wiklander * is not allowed to access it.
57732b31808SJens Wiklander */
57832b31808SJens Wiklander psa_status_t mbedtls_psa_platform_get_builtin_key(
57932b31808SJens Wiklander mbedtls_svc_key_id_t key_id,
58032b31808SJens Wiklander psa_key_lifetime_t *lifetime,
58132b31808SJens Wiklander psa_drv_slot_number_t *slot_number);
58232b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
58332b31808SJens Wiklander
58432b31808SJens Wiklander /** @} */
58532b31808SJens Wiklander
586c3deb3d6SEtienne Carriere /** \defgroup psa_crypto_client Functions defined by a client provider
587c3deb3d6SEtienne Carriere *
588c3deb3d6SEtienne Carriere * The functions in this group are meant to be implemented by providers of
589c3deb3d6SEtienne Carriere * the PSA Crypto client interface. They are provided by the library when
590c3deb3d6SEtienne Carriere * #MBEDTLS_PSA_CRYPTO_C is enabled.
591c3deb3d6SEtienne Carriere *
592c3deb3d6SEtienne Carriere * \note All functions in this group are experimental, as using
593c3deb3d6SEtienne Carriere * alternative client interface providers is experimental.
594c3deb3d6SEtienne Carriere *
595c3deb3d6SEtienne Carriere * @{
596c3deb3d6SEtienne Carriere */
597c3deb3d6SEtienne Carriere
598c3deb3d6SEtienne Carriere /** Check if PSA is capable of handling the specified hash algorithm.
599c3deb3d6SEtienne Carriere *
600c3deb3d6SEtienne Carriere * This means that PSA core was built with the corresponding PSA_WANT_ALG_xxx
601c3deb3d6SEtienne Carriere * set and that psa_crypto_init has already been called.
602c3deb3d6SEtienne Carriere *
603c3deb3d6SEtienne Carriere * \note When using Mbed TLS version of PSA core (i.e. MBEDTLS_PSA_CRYPTO_C is
604c3deb3d6SEtienne Carriere * set) for now this function only checks the state of the driver
605c3deb3d6SEtienne Carriere * subsystem, not the algorithm. This might be improved in the future.
606c3deb3d6SEtienne Carriere *
607c3deb3d6SEtienne Carriere * \param hash_alg The hash algorithm.
608c3deb3d6SEtienne Carriere *
609c3deb3d6SEtienne Carriere * \return 1 if the PSA can handle \p hash_alg, 0 otherwise.
610c3deb3d6SEtienne Carriere */
611c3deb3d6SEtienne Carriere int psa_can_do_hash(psa_algorithm_t hash_alg);
612c3deb3d6SEtienne Carriere
613c3deb3d6SEtienne Carriere /**@}*/
614c3deb3d6SEtienne Carriere
61532b31808SJens Wiklander /** \addtogroup crypto_types
61632b31808SJens Wiklander * @{
61732b31808SJens Wiklander */
61832b31808SJens Wiklander
61932b31808SJens Wiklander #define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
62032b31808SJens Wiklander
62132b31808SJens Wiklander /** Whether the specified algorithm is a password-authenticated key exchange.
62232b31808SJens Wiklander *
62332b31808SJens Wiklander * \param alg An algorithm identifier (value of type #psa_algorithm_t).
62432b31808SJens Wiklander *
62532b31808SJens Wiklander * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
62632b31808SJens Wiklander * algorithm, 0 otherwise.
62732b31808SJens Wiklander * This macro may return either 0 or 1 if \p alg is not a supported
62832b31808SJens Wiklander * algorithm identifier.
62932b31808SJens Wiklander */
63032b31808SJens Wiklander #define PSA_ALG_IS_PAKE(alg) \
63132b31808SJens Wiklander (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
63232b31808SJens Wiklander
63332b31808SJens Wiklander /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
63432b31808SJens Wiklander *
63532b31808SJens Wiklander * This is J-PAKE as defined by RFC 8236, instantiated with the following
63632b31808SJens Wiklander * parameters:
63732b31808SJens Wiklander *
63832b31808SJens Wiklander * - The group can be either an elliptic curve or defined over a finite field.
63932b31808SJens Wiklander * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
64032b31808SJens Wiklander * J-PAKE algorithm.
64132b31808SJens Wiklander * - A cryptographic hash function.
64232b31808SJens Wiklander *
64332b31808SJens Wiklander * To select these parameters and set up the cipher suite, call these functions
64432b31808SJens Wiklander * in any order:
64532b31808SJens Wiklander *
64632b31808SJens Wiklander * \code
64732b31808SJens Wiklander * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
64832b31808SJens Wiklander * psa_pake_cs_set_primitive(cipher_suite,
64932b31808SJens Wiklander * PSA_PAKE_PRIMITIVE(type, family, bits));
65032b31808SJens Wiklander * psa_pake_cs_set_hash(cipher_suite, hash);
65132b31808SJens Wiklander * \endcode
65232b31808SJens Wiklander *
65332b31808SJens Wiklander * For more information on how to set a specific curve or field, refer to the
65432b31808SJens Wiklander * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
65532b31808SJens Wiklander *
65632b31808SJens Wiklander * After initializing a J-PAKE operation, call
65732b31808SJens Wiklander *
65832b31808SJens Wiklander * \code
65932b31808SJens Wiklander * psa_pake_setup(operation, cipher_suite);
66032b31808SJens Wiklander * psa_pake_set_user(operation, ...);
66132b31808SJens Wiklander * psa_pake_set_peer(operation, ...);
66232b31808SJens Wiklander * psa_pake_set_password_key(operation, ...);
66332b31808SJens Wiklander * \endcode
66432b31808SJens Wiklander *
66532b31808SJens Wiklander * The password is provided as a key. This can be the password text itself,
66632b31808SJens Wiklander * in an agreed character encoding, or some value derived from the password
66732b31808SJens Wiklander * as required by a higher level protocol.
66832b31808SJens Wiklander *
66932b31808SJens Wiklander * (The implementation converts the key material to a number as described in
67032b31808SJens Wiklander * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
67132b31808SJens Wiklander * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
67232b31808SJens Wiklander * \c q is order of the group defined by the primitive set in the cipher suite.
67332b31808SJens Wiklander * The \c psa_pake_set_password_key() function returns an error if the result
67432b31808SJens Wiklander * of the reduction is 0.)
67532b31808SJens Wiklander *
67632b31808SJens Wiklander * The key exchange flow for J-PAKE is as follows:
67732b31808SJens Wiklander * -# To get the first round data that needs to be sent to the peer, call
67832b31808SJens Wiklander * \code
67932b31808SJens Wiklander * // Get g1
68032b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
68132b31808SJens Wiklander * // Get the ZKP public key for x1
68232b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
68332b31808SJens Wiklander * // Get the ZKP proof for x1
68432b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
68532b31808SJens Wiklander * // Get g2
68632b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
68732b31808SJens Wiklander * // Get the ZKP public key for x2
68832b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
68932b31808SJens Wiklander * // Get the ZKP proof for x2
69032b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
69132b31808SJens Wiklander * \endcode
69232b31808SJens Wiklander * -# To provide the first round data received from the peer to the operation,
69332b31808SJens Wiklander * call
69432b31808SJens Wiklander * \code
69532b31808SJens Wiklander * // Set g3
69632b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
69732b31808SJens Wiklander * // Set the ZKP public key for x3
69832b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
69932b31808SJens Wiklander * // Set the ZKP proof for x3
70032b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
70132b31808SJens Wiklander * // Set g4
70232b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
70332b31808SJens Wiklander * // Set the ZKP public key for x4
70432b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
70532b31808SJens Wiklander * // Set the ZKP proof for x4
70632b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
70732b31808SJens Wiklander * \endcode
70832b31808SJens Wiklander * -# To get the second round data that needs to be sent to the peer, call
70932b31808SJens Wiklander * \code
71032b31808SJens Wiklander * // Get A
71132b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
71232b31808SJens Wiklander * // Get ZKP public key for x2*s
71332b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
71432b31808SJens Wiklander * // Get ZKP proof for x2*s
71532b31808SJens Wiklander * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
71632b31808SJens Wiklander * \endcode
71732b31808SJens Wiklander * -# To provide the second round data received from the peer to the operation,
71832b31808SJens Wiklander * call
71932b31808SJens Wiklander * \code
72032b31808SJens Wiklander * // Set B
72132b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
72232b31808SJens Wiklander * // Set ZKP public key for x4*s
72332b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
72432b31808SJens Wiklander * // Set ZKP proof for x4*s
72532b31808SJens Wiklander * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
72632b31808SJens Wiklander * \endcode
72732b31808SJens Wiklander * -# To access the shared secret call
72832b31808SJens Wiklander * \code
72932b31808SJens Wiklander * // Get Ka=Kb=K
73032b31808SJens Wiklander * psa_pake_get_implicit_key()
73132b31808SJens Wiklander * \endcode
73232b31808SJens Wiklander *
73332b31808SJens Wiklander * For more information consult the documentation of the individual
73432b31808SJens Wiklander * \c PSA_PAKE_STEP_XXX constants.
73532b31808SJens Wiklander *
73632b31808SJens Wiklander * At this point there is a cryptographic guarantee that only the authenticated
73732b31808SJens Wiklander * party who used the same password is able to compute the key. But there is no
73832b31808SJens Wiklander * guarantee that the peer is the party it claims to be and was able to do so.
73932b31808SJens Wiklander *
74032b31808SJens Wiklander * That is, the authentication is only implicit (the peer is not authenticated
74132b31808SJens Wiklander * at this point, and no action should be taken that assume that they are - like
74232b31808SJens Wiklander * for example accessing restricted files).
74332b31808SJens Wiklander *
74432b31808SJens Wiklander * To make the authentication explicit there are various methods, see Section 5
74532b31808SJens Wiklander * of RFC 8236 for two examples.
74632b31808SJens Wiklander *
74732b31808SJens Wiklander */
74832b31808SJens Wiklander #define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
74932b31808SJens Wiklander
75032b31808SJens Wiklander /** @} */
75132b31808SJens Wiklander
75232b31808SJens Wiklander /** \defgroup pake Password-authenticated key exchange (PAKE)
75332b31808SJens Wiklander *
75432b31808SJens Wiklander * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
75532b31808SJens Wiklander * the official PSA Crypto API yet.
75632b31808SJens Wiklander *
75732b31808SJens Wiklander * \note The content of this section is not part of the stable API and ABI
758b0563631STom Van Eyck * of Mbed TLS and may change arbitrarily from version to version.
75932b31808SJens Wiklander * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
76032b31808SJens Wiklander * #PSA_ALG_JPAKE.
76132b31808SJens Wiklander * @{
76232b31808SJens Wiklander */
76332b31808SJens Wiklander
76432b31808SJens Wiklander /** \brief Encoding of the application role of PAKE
76532b31808SJens Wiklander *
76632b31808SJens Wiklander * Encodes the application's role in the algorithm is being executed. For more
76732b31808SJens Wiklander * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
76832b31808SJens Wiklander * constants.
76932b31808SJens Wiklander */
77032b31808SJens Wiklander typedef uint8_t psa_pake_role_t;
77132b31808SJens Wiklander
77232b31808SJens Wiklander /** Encoding of input and output indicators for PAKE.
77332b31808SJens Wiklander *
77432b31808SJens Wiklander * Some PAKE algorithms need to exchange more data than just a single key share.
77532b31808SJens Wiklander * This type is for encoding additional input and output data for such
77632b31808SJens Wiklander * algorithms.
77732b31808SJens Wiklander */
77832b31808SJens Wiklander typedef uint8_t psa_pake_step_t;
77932b31808SJens Wiklander
78032b31808SJens Wiklander /** Encoding of the type of the PAKE's primitive.
78132b31808SJens Wiklander *
78232b31808SJens Wiklander * Values defined by this standard will never be in the range 0x80-0xff.
78332b31808SJens Wiklander * Vendors who define additional types must use an encoding in this range.
78432b31808SJens Wiklander *
78532b31808SJens Wiklander * For more information see the documentation of individual
78632b31808SJens Wiklander * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
78732b31808SJens Wiklander */
78832b31808SJens Wiklander typedef uint8_t psa_pake_primitive_type_t;
78932b31808SJens Wiklander
79032b31808SJens Wiklander /** \brief Encoding of the family of the primitive associated with the PAKE.
79132b31808SJens Wiklander *
79232b31808SJens Wiklander * For more information see the documentation of individual
79332b31808SJens Wiklander * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
79432b31808SJens Wiklander */
79532b31808SJens Wiklander typedef uint8_t psa_pake_family_t;
79632b31808SJens Wiklander
79732b31808SJens Wiklander /** \brief Encoding of the primitive associated with the PAKE.
79832b31808SJens Wiklander *
79932b31808SJens Wiklander * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
80032b31808SJens Wiklander */
80132b31808SJens Wiklander typedef uint32_t psa_pake_primitive_t;
80232b31808SJens Wiklander
80332b31808SJens Wiklander /** A value to indicate no role in a PAKE algorithm.
80432b31808SJens Wiklander * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
80532b31808SJens Wiklander * algorithms which do not assign roles.
80632b31808SJens Wiklander */
80732b31808SJens Wiklander #define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
80832b31808SJens Wiklander
80932b31808SJens Wiklander /** The first peer in a balanced PAKE.
81032b31808SJens Wiklander *
81132b31808SJens Wiklander * Although balanced PAKE algorithms are symmetric, some of them needs an
81232b31808SJens Wiklander * ordering of peers for the transcript calculations. If the algorithm does not
81332b31808SJens Wiklander * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
81432b31808SJens Wiklander * accepted.
81532b31808SJens Wiklander */
81632b31808SJens Wiklander #define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
81732b31808SJens Wiklander
81832b31808SJens Wiklander /** The second peer in a balanced PAKE.
81932b31808SJens Wiklander *
82032b31808SJens Wiklander * Although balanced PAKE algorithms are symmetric, some of them needs an
82132b31808SJens Wiklander * ordering of peers for the transcript calculations. If the algorithm does not
82232b31808SJens Wiklander * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
82332b31808SJens Wiklander * accepted.
82432b31808SJens Wiklander */
82532b31808SJens Wiklander #define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
82632b31808SJens Wiklander
82732b31808SJens Wiklander /** The client in an augmented PAKE.
82832b31808SJens Wiklander *
82932b31808SJens Wiklander * Augmented PAKE algorithms need to differentiate between client and server.
83032b31808SJens Wiklander */
83132b31808SJens Wiklander #define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
83232b31808SJens Wiklander
83332b31808SJens Wiklander /** The server in an augmented PAKE.
83432b31808SJens Wiklander *
83532b31808SJens Wiklander * Augmented PAKE algorithms need to differentiate between client and server.
83632b31808SJens Wiklander */
83732b31808SJens Wiklander #define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
83832b31808SJens Wiklander
83932b31808SJens Wiklander /** The PAKE primitive type indicating the use of elliptic curves.
84032b31808SJens Wiklander *
84132b31808SJens Wiklander * The values of the \c family and \c bits fields of the cipher suite identify a
84232b31808SJens Wiklander * specific elliptic curve, using the same mapping that is used for ECC
84332b31808SJens Wiklander * (::psa_ecc_family_t) keys.
84432b31808SJens Wiklander *
84532b31808SJens Wiklander * (Here \c family means the value returned by psa_pake_cs_get_family() and
84632b31808SJens Wiklander * \c bits means the value returned by psa_pake_cs_get_bits().)
84732b31808SJens Wiklander *
84832b31808SJens Wiklander * Input and output during the operation can involve group elements and scalar
84932b31808SJens Wiklander * values:
85032b31808SJens Wiklander * -# The format for group elements is the same as for public keys on the
85132b31808SJens Wiklander * specific curve would be. For more information, consult the documentation of
85232b31808SJens Wiklander * psa_export_public_key().
85332b31808SJens Wiklander * -# The format for scalars is the same as for private keys on the specific
85432b31808SJens Wiklander * curve would be. For more information, consult the documentation of
85532b31808SJens Wiklander * psa_export_key().
85632b31808SJens Wiklander */
85732b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
85832b31808SJens Wiklander
85932b31808SJens Wiklander /** The PAKE primitive type indicating the use of Diffie-Hellman groups.
86032b31808SJens Wiklander *
86132b31808SJens Wiklander * The values of the \c family and \c bits fields of the cipher suite identify
86232b31808SJens Wiklander * a specific Diffie-Hellman group, using the same mapping that is used for
86332b31808SJens Wiklander * Diffie-Hellman (::psa_dh_family_t) keys.
86432b31808SJens Wiklander *
86532b31808SJens Wiklander * (Here \c family means the value returned by psa_pake_cs_get_family() and
86632b31808SJens Wiklander * \c bits means the value returned by psa_pake_cs_get_bits().)
86732b31808SJens Wiklander *
86832b31808SJens Wiklander * Input and output during the operation can involve group elements and scalar
86932b31808SJens Wiklander * values:
87032b31808SJens Wiklander * -# The format for group elements is the same as for public keys on the
87132b31808SJens Wiklander * specific group would be. For more information, consult the documentation of
87232b31808SJens Wiklander * psa_export_public_key().
87332b31808SJens Wiklander * -# The format for scalars is the same as for private keys on the specific
87432b31808SJens Wiklander * group would be. For more information, consult the documentation of
87532b31808SJens Wiklander * psa_export_key().
87632b31808SJens Wiklander */
87732b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
87832b31808SJens Wiklander
87932b31808SJens Wiklander /** Construct a PAKE primitive from type, family and bit-size.
88032b31808SJens Wiklander *
88132b31808SJens Wiklander * \param pake_type The type of the primitive
88232b31808SJens Wiklander * (value of type ::psa_pake_primitive_type_t).
88332b31808SJens Wiklander * \param pake_family The family of the primitive
88432b31808SJens Wiklander * (the type and interpretation of this parameter depends
885b0563631STom Van Eyck * on \p pake_type, for more information consult the
88632b31808SJens Wiklander * documentation of individual ::psa_pake_primitive_type_t
88732b31808SJens Wiklander * constants).
88832b31808SJens Wiklander * \param pake_bits The bit-size of the primitive
88932b31808SJens Wiklander * (Value of type \c size_t. The interpretation
890b0563631STom Van Eyck * of this parameter depends on \p pake_family, for more
89132b31808SJens Wiklander * information consult the documentation of individual
89232b31808SJens Wiklander * ::psa_pake_primitive_type_t constants).
89332b31808SJens Wiklander *
89432b31808SJens Wiklander * \return The constructed primitive value of type ::psa_pake_primitive_t.
89532b31808SJens Wiklander * Return 0 if the requested primitive can't be encoded as
89632b31808SJens Wiklander * ::psa_pake_primitive_t.
89732b31808SJens Wiklander */
89832b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
89932b31808SJens Wiklander ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
90032b31808SJens Wiklander ((psa_pake_primitive_t) (((pake_type) << 24 | \
90132b31808SJens Wiklander (pake_family) << 16) | (pake_bits)))
90232b31808SJens Wiklander
90332b31808SJens Wiklander /** The key share being sent to or received from the peer.
90432b31808SJens Wiklander *
90532b31808SJens Wiklander * The format for both input and output at this step is the same as for public
90632b31808SJens Wiklander * keys on the group determined by the primitive (::psa_pake_primitive_t) would
90732b31808SJens Wiklander * be.
90832b31808SJens Wiklander *
90932b31808SJens Wiklander * For more information on the format, consult the documentation of
91032b31808SJens Wiklander * psa_export_public_key().
91132b31808SJens Wiklander *
91232b31808SJens Wiklander * For information regarding how the group is determined, consult the
91332b31808SJens Wiklander * documentation #PSA_PAKE_PRIMITIVE.
91432b31808SJens Wiklander */
91532b31808SJens Wiklander #define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
91632b31808SJens Wiklander
91732b31808SJens Wiklander /** A Schnorr NIZKP public key.
91832b31808SJens Wiklander *
91932b31808SJens Wiklander * This is the ephemeral public key in the Schnorr Non-Interactive
92032b31808SJens Wiklander * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
92132b31808SJens Wiklander *
92232b31808SJens Wiklander * The format for both input and output at this step is the same as for public
92332b31808SJens Wiklander * keys on the group determined by the primitive (::psa_pake_primitive_t) would
92432b31808SJens Wiklander * be.
92532b31808SJens Wiklander *
92632b31808SJens Wiklander * For more information on the format, consult the documentation of
92732b31808SJens Wiklander * psa_export_public_key().
92832b31808SJens Wiklander *
92932b31808SJens Wiklander * For information regarding how the group is determined, consult the
93032b31808SJens Wiklander * documentation #PSA_PAKE_PRIMITIVE.
93132b31808SJens Wiklander */
93232b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
93332b31808SJens Wiklander
93432b31808SJens Wiklander /** A Schnorr NIZKP proof.
93532b31808SJens Wiklander *
93632b31808SJens Wiklander * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
93732b31808SJens Wiklander * value denoted by the letter 'r' in RFC 8235).
93832b31808SJens Wiklander *
93932b31808SJens Wiklander * Both for input and output, the value at this step is an integer less than
94032b31808SJens Wiklander * the order of the group selected in the cipher suite. The format depends on
94132b31808SJens Wiklander * the group as well:
94232b31808SJens Wiklander *
94332b31808SJens Wiklander * - For Montgomery curves, the encoding is little endian.
94432b31808SJens Wiklander * - For everything else the encoding is big endian (see Section 2.3.8 of
94532b31808SJens Wiklander * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
94632b31808SJens Wiklander *
94732b31808SJens Wiklander * In both cases leading zeroes are allowed as long as the length in bytes does
94832b31808SJens Wiklander * not exceed the byte length of the group order.
94932b31808SJens Wiklander *
95032b31808SJens Wiklander * For information regarding how the group is determined, consult the
95132b31808SJens Wiklander * documentation #PSA_PAKE_PRIMITIVE.
95232b31808SJens Wiklander */
95332b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
95432b31808SJens Wiklander
955*273a583eSThomas Bourgoin /**@}*/
956*273a583eSThomas Bourgoin
957*273a583eSThomas Bourgoin /** A sufficient output buffer size for psa_pake_output().
958*273a583eSThomas Bourgoin *
959*273a583eSThomas Bourgoin * If the size of the output buffer is at least this large, it is guaranteed
960*273a583eSThomas Bourgoin * that psa_pake_output() will not fail due to an insufficient output buffer
961*273a583eSThomas Bourgoin * size. The actual size of the output might be smaller in any given call.
962*273a583eSThomas Bourgoin *
963*273a583eSThomas Bourgoin * See also #PSA_PAKE_OUTPUT_MAX_SIZE
964*273a583eSThomas Bourgoin *
965*273a583eSThomas Bourgoin * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
966*273a583eSThomas Bourgoin * #PSA_ALG_IS_PAKE(\p alg) is true).
967*273a583eSThomas Bourgoin * \param primitive A primitive of type ::psa_pake_primitive_t that is
968*273a583eSThomas Bourgoin * compatible with algorithm \p alg.
969*273a583eSThomas Bourgoin * \param output_step A value of type ::psa_pake_step_t that is valid for the
970*273a583eSThomas Bourgoin * algorithm \p alg.
971*273a583eSThomas Bourgoin * \return A sufficient output buffer size for the specified
972*273a583eSThomas Bourgoin * PAKE algorithm, primitive, and output step. If the
973*273a583eSThomas Bourgoin * PAKE algorithm, primitive, or output step is not
974*273a583eSThomas Bourgoin * recognized, or the parameters are incompatible,
975*273a583eSThomas Bourgoin * return 0.
976*273a583eSThomas Bourgoin */
977*273a583eSThomas Bourgoin #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
978*273a583eSThomas Bourgoin (alg == PSA_ALG_JPAKE && \
979*273a583eSThomas Bourgoin primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
980*273a583eSThomas Bourgoin PSA_ECC_FAMILY_SECP_R1, 256) ? \
981*273a583eSThomas Bourgoin ( \
982*273a583eSThomas Bourgoin output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
983*273a583eSThomas Bourgoin output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
984*273a583eSThomas Bourgoin 32 \
985*273a583eSThomas Bourgoin ) : \
986*273a583eSThomas Bourgoin 0)
987*273a583eSThomas Bourgoin
988*273a583eSThomas Bourgoin /** A sufficient input buffer size for psa_pake_input().
989*273a583eSThomas Bourgoin *
990*273a583eSThomas Bourgoin * The value returned by this macro is guaranteed to be large enough for any
991*273a583eSThomas Bourgoin * valid input to psa_pake_input() in an operation with the specified
992*273a583eSThomas Bourgoin * parameters.
993*273a583eSThomas Bourgoin *
994*273a583eSThomas Bourgoin * See also #PSA_PAKE_INPUT_MAX_SIZE
995*273a583eSThomas Bourgoin *
996*273a583eSThomas Bourgoin * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
997*273a583eSThomas Bourgoin * #PSA_ALG_IS_PAKE(\p alg) is true).
998*273a583eSThomas Bourgoin * \param primitive A primitive of type ::psa_pake_primitive_t that is
999*273a583eSThomas Bourgoin * compatible with algorithm \p alg.
1000*273a583eSThomas Bourgoin * \param input_step A value of type ::psa_pake_step_t that is valid for the
1001*273a583eSThomas Bourgoin * algorithm \p alg.
1002*273a583eSThomas Bourgoin * \return A sufficient input buffer size for the specified
1003*273a583eSThomas Bourgoin * input, cipher suite and algorithm. If the cipher suite,
1004*273a583eSThomas Bourgoin * the input type or PAKE algorithm is not recognized, or
1005*273a583eSThomas Bourgoin * the parameters are incompatible, return 0.
1006*273a583eSThomas Bourgoin */
1007*273a583eSThomas Bourgoin #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
1008*273a583eSThomas Bourgoin (alg == PSA_ALG_JPAKE && \
1009*273a583eSThomas Bourgoin primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
1010*273a583eSThomas Bourgoin PSA_ECC_FAMILY_SECP_R1, 256) ? \
1011*273a583eSThomas Bourgoin ( \
1012*273a583eSThomas Bourgoin input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
1013*273a583eSThomas Bourgoin input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
1014*273a583eSThomas Bourgoin 32 \
1015*273a583eSThomas Bourgoin ) : \
1016*273a583eSThomas Bourgoin 0)
1017*273a583eSThomas Bourgoin
1018*273a583eSThomas Bourgoin /** Output buffer size for psa_pake_output() for any of the supported PAKE
1019*273a583eSThomas Bourgoin * algorithm and primitive suites and output step.
1020*273a583eSThomas Bourgoin *
1021*273a583eSThomas Bourgoin * This macro must expand to a compile-time constant integer.
1022*273a583eSThomas Bourgoin *
1023*273a583eSThomas Bourgoin * The value of this macro must be at least as large as the largest value
1024*273a583eSThomas Bourgoin * returned by PSA_PAKE_OUTPUT_SIZE()
1025*273a583eSThomas Bourgoin *
1026*273a583eSThomas Bourgoin * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
1027*273a583eSThomas Bourgoin */
1028*273a583eSThomas Bourgoin #define PSA_PAKE_OUTPUT_MAX_SIZE 65
1029*273a583eSThomas Bourgoin
1030*273a583eSThomas Bourgoin /** Input buffer size for psa_pake_input() for any of the supported PAKE
1031*273a583eSThomas Bourgoin * algorithm and primitive suites and input step.
1032*273a583eSThomas Bourgoin *
1033*273a583eSThomas Bourgoin * This macro must expand to a compile-time constant integer.
1034*273a583eSThomas Bourgoin *
1035*273a583eSThomas Bourgoin * The value of this macro must be at least as large as the largest value
1036*273a583eSThomas Bourgoin * returned by PSA_PAKE_INPUT_SIZE()
1037*273a583eSThomas Bourgoin *
1038*273a583eSThomas Bourgoin * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
1039*273a583eSThomas Bourgoin */
1040*273a583eSThomas Bourgoin #define PSA_PAKE_INPUT_MAX_SIZE 65
1041*273a583eSThomas Bourgoin
1042*273a583eSThomas Bourgoin /** Returns a suitable initializer for a PAKE cipher suite object of type
1043*273a583eSThomas Bourgoin * psa_pake_cipher_suite_t.
1044*273a583eSThomas Bourgoin */
1045*273a583eSThomas Bourgoin #define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
1046*273a583eSThomas Bourgoin
1047*273a583eSThomas Bourgoin /** Returns a suitable initializer for a PAKE operation object of type
1048*273a583eSThomas Bourgoin * psa_pake_operation_t.
1049*273a583eSThomas Bourgoin */
1050*273a583eSThomas Bourgoin #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1051*273a583eSThomas Bourgoin #define PSA_PAKE_OPERATION_INIT { 0 }
1052*273a583eSThomas Bourgoin #else
1053*273a583eSThomas Bourgoin #define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
1054*273a583eSThomas Bourgoin { 0 }, { { 0 } } }
1055*273a583eSThomas Bourgoin #endif
1056*273a583eSThomas Bourgoin
1057*273a583eSThomas Bourgoin struct psa_pake_cipher_suite_s {
1058*273a583eSThomas Bourgoin psa_algorithm_t algorithm;
1059*273a583eSThomas Bourgoin psa_pake_primitive_type_t type;
1060*273a583eSThomas Bourgoin psa_pake_family_t family;
1061*273a583eSThomas Bourgoin uint16_t bits;
1062*273a583eSThomas Bourgoin psa_algorithm_t hash;
1063*273a583eSThomas Bourgoin };
1064*273a583eSThomas Bourgoin
1065*273a583eSThomas Bourgoin struct psa_crypto_driver_pake_inputs_s {
1066*273a583eSThomas Bourgoin uint8_t *MBEDTLS_PRIVATE(password);
1067*273a583eSThomas Bourgoin size_t MBEDTLS_PRIVATE(password_len);
1068*273a583eSThomas Bourgoin uint8_t *MBEDTLS_PRIVATE(user);
1069*273a583eSThomas Bourgoin size_t MBEDTLS_PRIVATE(user_len);
1070*273a583eSThomas Bourgoin uint8_t *MBEDTLS_PRIVATE(peer);
1071*273a583eSThomas Bourgoin size_t MBEDTLS_PRIVATE(peer_len);
1072*273a583eSThomas Bourgoin psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
1073*273a583eSThomas Bourgoin struct psa_pake_cipher_suite_s MBEDTLS_PRIVATE(cipher_suite);
1074*273a583eSThomas Bourgoin };
1075*273a583eSThomas Bourgoin
1076*273a583eSThomas Bourgoin typedef enum psa_crypto_driver_pake_step {
1077*273a583eSThomas Bourgoin PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
1078*273a583eSThomas Bourgoin PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
1079*273a583eSThomas Bourgoin PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
1080*273a583eSThomas Bourgoin PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
1081*273a583eSThomas Bourgoin PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
1082*273a583eSThomas Bourgoin PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
1083*273a583eSThomas Bourgoin PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
1084*273a583eSThomas Bourgoin PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
1085*273a583eSThomas Bourgoin PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
1086*273a583eSThomas Bourgoin PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
1087*273a583eSThomas Bourgoin PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
1088*273a583eSThomas Bourgoin PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
1089*273a583eSThomas Bourgoin PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
1090*273a583eSThomas Bourgoin } psa_crypto_driver_pake_step_t;
1091*273a583eSThomas Bourgoin
1092*273a583eSThomas Bourgoin typedef enum psa_jpake_round {
1093*273a583eSThomas Bourgoin PSA_JPAKE_FIRST = 0,
1094*273a583eSThomas Bourgoin PSA_JPAKE_SECOND = 1,
1095*273a583eSThomas Bourgoin PSA_JPAKE_FINISHED = 2
1096*273a583eSThomas Bourgoin } psa_jpake_round_t;
1097*273a583eSThomas Bourgoin
1098*273a583eSThomas Bourgoin typedef enum psa_jpake_io_mode {
1099*273a583eSThomas Bourgoin PSA_JPAKE_INPUT = 0,
1100*273a583eSThomas Bourgoin PSA_JPAKE_OUTPUT = 1
1101*273a583eSThomas Bourgoin } psa_jpake_io_mode_t;
1102*273a583eSThomas Bourgoin
1103*273a583eSThomas Bourgoin struct psa_jpake_computation_stage_s {
1104*273a583eSThomas Bourgoin /* The J-PAKE round we are currently on */
1105*273a583eSThomas Bourgoin psa_jpake_round_t MBEDTLS_PRIVATE(round);
1106*273a583eSThomas Bourgoin /* The 'mode' we are currently in (inputting or outputting) */
1107*273a583eSThomas Bourgoin psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
1108*273a583eSThomas Bourgoin /* The number of completed inputs so far this round */
1109*273a583eSThomas Bourgoin uint8_t MBEDTLS_PRIVATE(inputs);
1110*273a583eSThomas Bourgoin /* The number of completed outputs so far this round */
1111*273a583eSThomas Bourgoin uint8_t MBEDTLS_PRIVATE(outputs);
1112*273a583eSThomas Bourgoin /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1113*273a583eSThomas Bourgoin psa_pake_step_t MBEDTLS_PRIVATE(step);
1114*273a583eSThomas Bourgoin };
1115*273a583eSThomas Bourgoin
1116*273a583eSThomas Bourgoin #define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1117*273a583eSThomas Bourgoin ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1118*273a583eSThomas Bourgoin #define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1119*273a583eSThomas Bourgoin ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1120*273a583eSThomas Bourgoin
1121*273a583eSThomas Bourgoin struct psa_pake_operation_s {
1122*273a583eSThomas Bourgoin #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1123*273a583eSThomas Bourgoin mbedtls_psa_client_handle_t handle;
1124*273a583eSThomas Bourgoin #else
1125*273a583eSThomas Bourgoin /** Unique ID indicating which driver got assigned to do the
1126*273a583eSThomas Bourgoin * operation. Since driver contexts are driver-specific, swapping
1127*273a583eSThomas Bourgoin * drivers halfway through the operation is not supported.
1128*273a583eSThomas Bourgoin * ID values are auto-generated in psa_crypto_driver_wrappers.h
1129*273a583eSThomas Bourgoin * ID value zero means the context is not valid or not assigned to
1130*273a583eSThomas Bourgoin * any driver (i.e. none of the driver contexts are active). */
1131*273a583eSThomas Bourgoin unsigned int MBEDTLS_PRIVATE(id);
1132*273a583eSThomas Bourgoin /* Algorithm of the PAKE operation */
1133*273a583eSThomas Bourgoin psa_algorithm_t MBEDTLS_PRIVATE(alg);
1134*273a583eSThomas Bourgoin /* A primitive of type compatible with algorithm */
1135*273a583eSThomas Bourgoin psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
1136*273a583eSThomas Bourgoin /* Stage of the PAKE operation: waiting for the setup, collecting inputs
1137*273a583eSThomas Bourgoin * or computing. */
1138*273a583eSThomas Bourgoin uint8_t MBEDTLS_PRIVATE(stage);
1139*273a583eSThomas Bourgoin /* Holds computation stage of the PAKE algorithms. */
1140*273a583eSThomas Bourgoin union {
1141*273a583eSThomas Bourgoin uint8_t MBEDTLS_PRIVATE(dummy);
1142*273a583eSThomas Bourgoin #if defined(PSA_WANT_ALG_JPAKE)
1143*273a583eSThomas Bourgoin struct psa_jpake_computation_stage_s MBEDTLS_PRIVATE(jpake);
1144*273a583eSThomas Bourgoin #endif
1145*273a583eSThomas Bourgoin } MBEDTLS_PRIVATE(computation_stage);
1146*273a583eSThomas Bourgoin union {
1147*273a583eSThomas Bourgoin psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
1148*273a583eSThomas Bourgoin struct psa_crypto_driver_pake_inputs_s MBEDTLS_PRIVATE(inputs);
1149*273a583eSThomas Bourgoin } MBEDTLS_PRIVATE(data);
1150*273a583eSThomas Bourgoin #endif
1151*273a583eSThomas Bourgoin };
1152*273a583eSThomas Bourgoin
1153*273a583eSThomas Bourgoin /** \addtogroup pake
1154*273a583eSThomas Bourgoin * @{
1155*273a583eSThomas Bourgoin */
1156*273a583eSThomas Bourgoin
115732b31808SJens Wiklander /** The type of the data structure for PAKE cipher suites.
115832b31808SJens Wiklander *
115932b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not
116032b31808SJens Wiklander * make any assumptions about the content of this structure.
116132b31808SJens Wiklander * Implementation details can change in future versions without notice.
116232b31808SJens Wiklander */
116332b31808SJens Wiklander typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
116432b31808SJens Wiklander
116532b31808SJens Wiklander /** Return an initial value for a PAKE cipher suite object.
116632b31808SJens Wiklander */
116732b31808SJens Wiklander static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
116832b31808SJens Wiklander
116932b31808SJens Wiklander /** Retrieve the PAKE algorithm from a PAKE cipher suite.
117032b31808SJens Wiklander *
117132b31808SJens Wiklander * \param[in] cipher_suite The cipher suite structure to query.
117232b31808SJens Wiklander *
117332b31808SJens Wiklander * \return The PAKE algorithm stored in the cipher suite structure.
117432b31808SJens Wiklander */
117532b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_algorithm(
117632b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
117732b31808SJens Wiklander
117832b31808SJens Wiklander /** Declare the PAKE algorithm for the cipher suite.
117932b31808SJens Wiklander *
118032b31808SJens Wiklander * This function overwrites any PAKE algorithm
118132b31808SJens Wiklander * previously set in \p cipher_suite.
118232b31808SJens Wiklander *
118332b31808SJens Wiklander * \param[out] cipher_suite The cipher suite structure to write to.
118432b31808SJens Wiklander * \param algorithm The PAKE algorithm to write.
118532b31808SJens Wiklander * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
118632b31808SJens Wiklander * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
118732b31808SJens Wiklander * If this is 0, the PAKE algorithm in
118832b31808SJens Wiklander * \p cipher_suite becomes unspecified.
118932b31808SJens Wiklander */
119032b31808SJens Wiklander static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
119132b31808SJens Wiklander psa_algorithm_t algorithm);
119232b31808SJens Wiklander
119332b31808SJens Wiklander /** Retrieve the primitive from a PAKE cipher suite.
119432b31808SJens Wiklander *
119532b31808SJens Wiklander * \param[in] cipher_suite The cipher suite structure to query.
119632b31808SJens Wiklander *
119732b31808SJens Wiklander * \return The primitive stored in the cipher suite structure.
119832b31808SJens Wiklander */
119932b31808SJens Wiklander static psa_pake_primitive_t psa_pake_cs_get_primitive(
120032b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
120132b31808SJens Wiklander
120232b31808SJens Wiklander /** Declare the primitive for a PAKE cipher suite.
120332b31808SJens Wiklander *
120432b31808SJens Wiklander * This function overwrites any primitive previously set in \p cipher_suite.
120532b31808SJens Wiklander *
120632b31808SJens Wiklander * \param[out] cipher_suite The cipher suite structure to write to.
120732b31808SJens Wiklander * \param primitive The primitive to write. If this is 0, the
120832b31808SJens Wiklander * primitive type in \p cipher_suite becomes
120932b31808SJens Wiklander * unspecified.
121032b31808SJens Wiklander */
121132b31808SJens Wiklander static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
121232b31808SJens Wiklander psa_pake_primitive_t primitive);
121332b31808SJens Wiklander
121432b31808SJens Wiklander /** Retrieve the PAKE family from a PAKE cipher suite.
121532b31808SJens Wiklander *
121632b31808SJens Wiklander * \param[in] cipher_suite The cipher suite structure to query.
121732b31808SJens Wiklander *
121832b31808SJens Wiklander * \return The PAKE family stored in the cipher suite structure.
121932b31808SJens Wiklander */
122032b31808SJens Wiklander static psa_pake_family_t psa_pake_cs_get_family(
122132b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
122232b31808SJens Wiklander
122332b31808SJens Wiklander /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
122432b31808SJens Wiklander *
122532b31808SJens Wiklander * \param[in] cipher_suite The cipher suite structure to query.
122632b31808SJens Wiklander *
122732b31808SJens Wiklander * \return The PAKE primitive bit-size stored in the cipher suite structure.
122832b31808SJens Wiklander */
122932b31808SJens Wiklander static uint16_t psa_pake_cs_get_bits(
123032b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
123132b31808SJens Wiklander
123232b31808SJens Wiklander /** Retrieve the hash algorithm from a PAKE cipher suite.
123332b31808SJens Wiklander *
123432b31808SJens Wiklander * \param[in] cipher_suite The cipher suite structure to query.
123532b31808SJens Wiklander *
123632b31808SJens Wiklander * \return The hash algorithm stored in the cipher suite structure. The return
123732b31808SJens Wiklander * value is 0 if the PAKE is not parametrised by a hash algorithm or if
123832b31808SJens Wiklander * the hash algorithm is not set.
123932b31808SJens Wiklander */
124032b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_hash(
124132b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
124232b31808SJens Wiklander
124332b31808SJens Wiklander /** Declare the hash algorithm for a PAKE cipher suite.
124432b31808SJens Wiklander *
124532b31808SJens Wiklander * This function overwrites any hash algorithm
124632b31808SJens Wiklander * previously set in \p cipher_suite.
124732b31808SJens Wiklander *
124832b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
124932b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
125032b31808SJens Wiklander * for more information.
125132b31808SJens Wiklander *
125232b31808SJens Wiklander * \param[out] cipher_suite The cipher suite structure to write to.
125332b31808SJens Wiklander * \param hash The hash involved in the cipher suite.
125432b31808SJens Wiklander * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
125532b31808SJens Wiklander * such that #PSA_ALG_IS_HASH(\c alg) is true.)
125632b31808SJens Wiklander * If this is 0, the hash algorithm in
125732b31808SJens Wiklander * \p cipher_suite becomes unspecified.
125832b31808SJens Wiklander */
125932b31808SJens Wiklander static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
126032b31808SJens Wiklander psa_algorithm_t hash);
126132b31808SJens Wiklander
126232b31808SJens Wiklander /** The type of the state data structure for PAKE operations.
126332b31808SJens Wiklander *
126432b31808SJens Wiklander * Before calling any function on a PAKE operation object, the application
126532b31808SJens Wiklander * must initialize it by any of the following means:
126632b31808SJens Wiklander * - Set the structure to all-bits-zero, for example:
126732b31808SJens Wiklander * \code
126832b31808SJens Wiklander * psa_pake_operation_t operation;
126932b31808SJens Wiklander * memset(&operation, 0, sizeof(operation));
127032b31808SJens Wiklander * \endcode
127132b31808SJens Wiklander * - Initialize the structure to logical zero values, for example:
127232b31808SJens Wiklander * \code
127332b31808SJens Wiklander * psa_pake_operation_t operation = {0};
127432b31808SJens Wiklander * \endcode
127532b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
127632b31808SJens Wiklander * for example:
127732b31808SJens Wiklander * \code
127832b31808SJens Wiklander * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
127932b31808SJens Wiklander * \endcode
128032b31808SJens Wiklander * - Assign the result of the function psa_pake_operation_init()
128132b31808SJens Wiklander * to the structure, for example:
128232b31808SJens Wiklander * \code
128332b31808SJens Wiklander * psa_pake_operation_t operation;
128432b31808SJens Wiklander * operation = psa_pake_operation_init();
128532b31808SJens Wiklander * \endcode
128632b31808SJens Wiklander *
128732b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not
128832b31808SJens Wiklander * make any assumptions about the content of this structure.
128932b31808SJens Wiklander * Implementation details can change in future versions without notice. */
129032b31808SJens Wiklander typedef struct psa_pake_operation_s psa_pake_operation_t;
129132b31808SJens Wiklander
129232b31808SJens Wiklander /** The type of input values for PAKE operations. */
129332b31808SJens Wiklander typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
129432b31808SJens Wiklander
129532b31808SJens Wiklander /** The type of computation stage for J-PAKE operations. */
129632b31808SJens Wiklander typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
129732b31808SJens Wiklander
129832b31808SJens Wiklander /** Return an initial value for a PAKE operation object.
129932b31808SJens Wiklander */
130032b31808SJens Wiklander static psa_pake_operation_t psa_pake_operation_init(void);
130132b31808SJens Wiklander
130232b31808SJens Wiklander /** Get the length of the password in bytes from given inputs.
130332b31808SJens Wiklander *
130432b31808SJens Wiklander * \param[in] inputs Operation inputs.
130532b31808SJens Wiklander * \param[out] password_len Password length.
130632b31808SJens Wiklander *
130732b31808SJens Wiklander * \retval #PSA_SUCCESS
130832b31808SJens Wiklander * Success.
130932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
131032b31808SJens Wiklander * Password hasn't been set yet.
131132b31808SJens Wiklander */
131232b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password_len(
131332b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
131432b31808SJens Wiklander size_t *password_len);
131532b31808SJens Wiklander
131632b31808SJens Wiklander /** Get the password from given inputs.
131732b31808SJens Wiklander *
131832b31808SJens Wiklander * \param[in] inputs Operation inputs.
131932b31808SJens Wiklander * \param[out] buffer Return buffer for password.
132032b31808SJens Wiklander * \param buffer_size Size of the return buffer in bytes.
132132b31808SJens Wiklander * \param[out] buffer_length Actual size of the password in bytes.
132232b31808SJens Wiklander *
132332b31808SJens Wiklander * \retval #PSA_SUCCESS
132432b31808SJens Wiklander * Success.
132532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
132632b31808SJens Wiklander * Password hasn't been set yet.
132732b31808SJens Wiklander */
132832b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password(
132932b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
133032b31808SJens Wiklander uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
133132b31808SJens Wiklander
133232b31808SJens Wiklander /** Get the length of the user id in bytes from given inputs.
133332b31808SJens Wiklander *
133432b31808SJens Wiklander * \param[in] inputs Operation inputs.
133532b31808SJens Wiklander * \param[out] user_len User id length.
133632b31808SJens Wiklander *
133732b31808SJens Wiklander * \retval #PSA_SUCCESS
133832b31808SJens Wiklander * Success.
133932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
134032b31808SJens Wiklander * User id hasn't been set yet.
134132b31808SJens Wiklander */
134232b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user_len(
134332b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
134432b31808SJens Wiklander size_t *user_len);
134532b31808SJens Wiklander
134632b31808SJens Wiklander /** Get the length of the peer id in bytes from given inputs.
134732b31808SJens Wiklander *
134832b31808SJens Wiklander * \param[in] inputs Operation inputs.
134932b31808SJens Wiklander * \param[out] peer_len Peer id length.
135032b31808SJens Wiklander *
135132b31808SJens Wiklander * \retval #PSA_SUCCESS
135232b31808SJens Wiklander * Success.
135332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
135432b31808SJens Wiklander * Peer id hasn't been set yet.
135532b31808SJens Wiklander */
135632b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer_len(
135732b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
135832b31808SJens Wiklander size_t *peer_len);
135932b31808SJens Wiklander
136032b31808SJens Wiklander /** Get the user id from given inputs.
136132b31808SJens Wiklander *
136232b31808SJens Wiklander * \param[in] inputs Operation inputs.
136332b31808SJens Wiklander * \param[out] user_id User id.
136432b31808SJens Wiklander * \param user_id_size Size of \p user_id in bytes.
136532b31808SJens Wiklander * \param[out] user_id_len Size of the user id in bytes.
136632b31808SJens Wiklander *
136732b31808SJens Wiklander * \retval #PSA_SUCCESS
136832b31808SJens Wiklander * Success.
136932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
137032b31808SJens Wiklander * User id hasn't been set yet.
137132b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL
137232b31808SJens Wiklander * The size of the \p user_id is too small.
137332b31808SJens Wiklander */
137432b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user(
137532b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
137632b31808SJens Wiklander uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
137732b31808SJens Wiklander
137832b31808SJens Wiklander /** Get the peer id from given inputs.
137932b31808SJens Wiklander *
138032b31808SJens Wiklander * \param[in] inputs Operation inputs.
138132b31808SJens Wiklander * \param[out] peer_id Peer id.
138232b31808SJens Wiklander * \param peer_id_size Size of \p peer_id in bytes.
138332b31808SJens Wiklander * \param[out] peer_id_length Size of the peer id in bytes.
138432b31808SJens Wiklander *
138532b31808SJens Wiklander * \retval #PSA_SUCCESS
138632b31808SJens Wiklander * Success.
138732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
138832b31808SJens Wiklander * Peer id hasn't been set yet.
138932b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL
139032b31808SJens Wiklander * The size of the \p peer_id is too small.
139132b31808SJens Wiklander */
139232b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer(
139332b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
139432b31808SJens Wiklander uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
139532b31808SJens Wiklander
139632b31808SJens Wiklander /** Get the cipher suite from given inputs.
139732b31808SJens Wiklander *
139832b31808SJens Wiklander * \param[in] inputs Operation inputs.
139932b31808SJens Wiklander * \param[out] cipher_suite Return buffer for role.
140032b31808SJens Wiklander *
140132b31808SJens Wiklander * \retval #PSA_SUCCESS
140232b31808SJens Wiklander * Success.
140332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
140432b31808SJens Wiklander * Cipher_suite hasn't been set yet.
140532b31808SJens Wiklander */
140632b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_cipher_suite(
140732b31808SJens Wiklander const psa_crypto_driver_pake_inputs_t *inputs,
140832b31808SJens Wiklander psa_pake_cipher_suite_t *cipher_suite);
140932b31808SJens Wiklander
141032b31808SJens Wiklander /** Set the session information for a password-authenticated key exchange.
141132b31808SJens Wiklander *
141232b31808SJens Wiklander * The sequence of operations to set up a password-authenticated key exchange
141332b31808SJens Wiklander * is as follows:
141432b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions
141532b31808SJens Wiklander * listed here.
141632b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the
141732b31808SJens Wiklander * documentation for #psa_pake_operation_t, e.g.
141832b31808SJens Wiklander * #PSA_PAKE_OPERATION_INIT.
141932b31808SJens Wiklander * -# Call psa_pake_setup() to specify the cipher suite.
142032b31808SJens Wiklander * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
142132b31808SJens Wiklander * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
142232b31808SJens Wiklander * to be called depends on the algorithm in use.
142332b31808SJens Wiklander *
142432b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
142532b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
142632b31808SJens Wiklander * for more information.
142732b31808SJens Wiklander *
142832b31808SJens Wiklander * A typical sequence of calls to perform a password-authenticated key
142932b31808SJens Wiklander * exchange:
143032b31808SJens Wiklander * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
143132b31808SJens Wiklander * key share that needs to be sent to the peer.
143232b31808SJens Wiklander * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
143332b31808SJens Wiklander * the key share that was received from the peer.
143432b31808SJens Wiklander * -# Depending on the algorithm additional calls to psa_pake_output() and
143532b31808SJens Wiklander * psa_pake_input() might be necessary.
143632b31808SJens Wiklander * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
143732b31808SJens Wiklander *
143832b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
143932b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
144032b31808SJens Wiklander * for more information.
144132b31808SJens Wiklander *
144232b31808SJens Wiklander * If an error occurs at any step after a call to psa_pake_setup(),
144332b31808SJens Wiklander * the operation will need to be reset by a call to psa_pake_abort(). The
144432b31808SJens Wiklander * application may call psa_pake_abort() at any time after the operation
144532b31808SJens Wiklander * has been initialized.
144632b31808SJens Wiklander *
144732b31808SJens Wiklander * After a successful call to psa_pake_setup(), the application must
144832b31808SJens Wiklander * eventually terminate the operation. The following events terminate an
144932b31808SJens Wiklander * operation:
145032b31808SJens Wiklander * - A call to psa_pake_abort().
145132b31808SJens Wiklander * - A successful call to psa_pake_get_implicit_key().
145232b31808SJens Wiklander *
145332b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have
145432b31808SJens Wiklander * been initialized but not set up yet.
145532b31808SJens Wiklander * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
145632b31808SJens Wiklander * characterizes a PAKE algorithm and determines
145732b31808SJens Wiklander * the algorithm as well.)
145832b31808SJens Wiklander *
145932b31808SJens Wiklander * \retval #PSA_SUCCESS
146032b31808SJens Wiklander * Success.
146132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
146232b31808SJens Wiklander * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
146332b31808SJens Wiklander * PAKE primitive in \p cipher_suite is not compatible with the
146432b31808SJens Wiklander * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
146532b31808SJens Wiklander * or not compatible with the PAKE algorithm and primitive.
146632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
146732b31808SJens Wiklander * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
146832b31808SJens Wiklander * or the PAKE primitive in \p cipher_suite is not supported or not
146932b31808SJens Wiklander * compatible with the PAKE algorithm, or the hash algorithm in
147032b31808SJens Wiklander * \p cipher_suite is not supported or not compatible with the PAKE
147132b31808SJens Wiklander * algorithm and primitive.
147232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
147332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
147432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
147532b31808SJens Wiklander * The operation state is not valid, or
147632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
147732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
147832b31808SJens Wiklander * results in this error code.
147932b31808SJens Wiklander */
148032b31808SJens Wiklander psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
148132b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite);
148232b31808SJens Wiklander
148332b31808SJens Wiklander /** Set the password for a password-authenticated key exchange from key ID.
148432b31808SJens Wiklander *
148532b31808SJens Wiklander * Call this function when the password, or a value derived from the password,
148632b31808SJens Wiklander * is already present in the key store.
148732b31808SJens Wiklander *
148832b31808SJens Wiklander * \param[in,out] operation The operation object to set the password for. It
148932b31808SJens Wiklander * must have been set up by psa_pake_setup() and
149032b31808SJens Wiklander * not yet in use (neither psa_pake_output() nor
149132b31808SJens Wiklander * psa_pake_input() has been called yet). It must
149232b31808SJens Wiklander * be on operation for which the password hasn't
149332b31808SJens Wiklander * been set yet (psa_pake_set_password_key()
149432b31808SJens Wiklander * hasn't been called yet).
149532b31808SJens Wiklander * \param password Identifier of the key holding the password or a
149632b31808SJens Wiklander * value derived from the password (eg. by a
149732b31808SJens Wiklander * memory-hard function). It must remain valid
149832b31808SJens Wiklander * until the operation terminates. It must be of
149932b31808SJens Wiklander * type #PSA_KEY_TYPE_PASSWORD or
150032b31808SJens Wiklander * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
150132b31808SJens Wiklander * the usage #PSA_KEY_USAGE_DERIVE.
150232b31808SJens Wiklander *
150332b31808SJens Wiklander * \retval #PSA_SUCCESS
150432b31808SJens Wiklander * Success.
150532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE
150632b31808SJens Wiklander * \p password is not a valid key identifier.
150732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED
150832b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
150932b31808SJens Wiklander * permit the \p operation's algorithm.
151032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
151132b31808SJens Wiklander * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
151232b31808SJens Wiklander * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
151332b31808SJens Wiklander * the \p operation's cipher suite.
151432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
151532b31808SJens Wiklander * The key type or key size of \p password is not supported with the
151632b31808SJens Wiklander * \p operation's cipher suite.
151732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
151832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
151932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
152032b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
152132b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription
152232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
152332b31808SJens Wiklander * The operation state is not valid (it must have been set up.), or
152432b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
152532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
152632b31808SJens Wiklander * results in this error code.
152732b31808SJens Wiklander */
152832b31808SJens Wiklander psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
152932b31808SJens Wiklander mbedtls_svc_key_id_t password);
153032b31808SJens Wiklander
153132b31808SJens Wiklander /** Set the user ID for a password-authenticated key exchange.
153232b31808SJens Wiklander *
153332b31808SJens Wiklander * Call this function to set the user ID. For PAKE algorithms that associate a
153432b31808SJens Wiklander * user identifier with each side of the session you need to call
153532b31808SJens Wiklander * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
153632b31808SJens Wiklander * user identifier with the session, call psa_pake_set_user() only.
153732b31808SJens Wiklander *
153832b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
153932b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
154032b31808SJens Wiklander * for more information.
154132b31808SJens Wiklander *
154232b31808SJens Wiklander * \param[in,out] operation The operation object to set the user ID for. It
154332b31808SJens Wiklander * must have been set up by psa_pake_setup() and
154432b31808SJens Wiklander * not yet in use (neither psa_pake_output() nor
154532b31808SJens Wiklander * psa_pake_input() has been called yet). It must
154632b31808SJens Wiklander * be on operation for which the user ID hasn't
154732b31808SJens Wiklander * been set (psa_pake_set_user() hasn't been
154832b31808SJens Wiklander * called yet).
154932b31808SJens Wiklander * \param[in] user_id The user ID to authenticate with.
155032b31808SJens Wiklander * \param user_id_len Size of the \p user_id buffer in bytes.
155132b31808SJens Wiklander *
155232b31808SJens Wiklander * \retval #PSA_SUCCESS
155332b31808SJens Wiklander * Success.
155432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
155532b31808SJens Wiklander * \p user_id is not valid for the \p operation's algorithm and cipher
155632b31808SJens Wiklander * suite.
155732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
155832b31808SJens Wiklander * The value of \p user_id is not supported by the implementation.
155932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
156032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
156132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
156232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
156332b31808SJens Wiklander * The operation state is not valid, or
156432b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
156532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
156632b31808SJens Wiklander * results in this error code.
156732b31808SJens Wiklander */
156832b31808SJens Wiklander psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
156932b31808SJens Wiklander const uint8_t *user_id,
157032b31808SJens Wiklander size_t user_id_len);
157132b31808SJens Wiklander
157232b31808SJens Wiklander /** Set the peer ID for a password-authenticated key exchange.
157332b31808SJens Wiklander *
157432b31808SJens Wiklander * Call this function in addition to psa_pake_set_user() for PAKE algorithms
157532b31808SJens Wiklander * that associate a user identifier with each side of the session. For PAKE
157632b31808SJens Wiklander * algorithms that associate a single user identifier with the session, call
157732b31808SJens Wiklander * psa_pake_set_user() only.
157832b31808SJens Wiklander *
157932b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
158032b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
158132b31808SJens Wiklander * for more information.
158232b31808SJens Wiklander *
158332b31808SJens Wiklander * \param[in,out] operation The operation object to set the peer ID for. It
158432b31808SJens Wiklander * must have been set up by psa_pake_setup() and
158532b31808SJens Wiklander * not yet in use (neither psa_pake_output() nor
158632b31808SJens Wiklander * psa_pake_input() has been called yet). It must
158732b31808SJens Wiklander * be on operation for which the peer ID hasn't
158832b31808SJens Wiklander * been set (psa_pake_set_peer() hasn't been
158932b31808SJens Wiklander * called yet).
159032b31808SJens Wiklander * \param[in] peer_id The peer's ID to authenticate.
159132b31808SJens Wiklander * \param peer_id_len Size of the \p peer_id buffer in bytes.
159232b31808SJens Wiklander *
159332b31808SJens Wiklander * \retval #PSA_SUCCESS
159432b31808SJens Wiklander * Success.
159532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
1596b0563631STom Van Eyck * \p peer_id is not valid for the \p operation's algorithm and cipher
159732b31808SJens Wiklander * suite.
159832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
159932b31808SJens Wiklander * The algorithm doesn't associate a second identity with the session.
160032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
160132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
160232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
160332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
160432b31808SJens Wiklander * Calling psa_pake_set_peer() is invalid with the \p operation's
160532b31808SJens Wiklander * algorithm, the operation state is not valid, or the library has not
160632b31808SJens Wiklander * been previously initialized by psa_crypto_init().
160732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
160832b31808SJens Wiklander * results in this error code.
160932b31808SJens Wiklander */
161032b31808SJens Wiklander psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
161132b31808SJens Wiklander const uint8_t *peer_id,
161232b31808SJens Wiklander size_t peer_id_len);
161332b31808SJens Wiklander
161432b31808SJens Wiklander /** Set the application role for a password-authenticated key exchange.
161532b31808SJens Wiklander *
161632b31808SJens Wiklander * Not all PAKE algorithms need to differentiate the communicating entities.
161732b31808SJens Wiklander * It is optional to call this function for PAKEs that don't require a role
161832b31808SJens Wiklander * to be specified. For such PAKEs the application role parameter is ignored,
161932b31808SJens Wiklander * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
162032b31808SJens Wiklander *
162132b31808SJens Wiklander * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
162232b31808SJens Wiklander * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
162332b31808SJens Wiklander * for more information.
162432b31808SJens Wiklander *
162532b31808SJens Wiklander * \param[in,out] operation The operation object to specify the
162632b31808SJens Wiklander * application's role for. It must have been set up
162732b31808SJens Wiklander * by psa_pake_setup() and not yet in use (neither
162832b31808SJens Wiklander * psa_pake_output() nor psa_pake_input() has been
162932b31808SJens Wiklander * called yet). It must be on operation for which
163032b31808SJens Wiklander * the application's role hasn't been specified
163132b31808SJens Wiklander * (psa_pake_set_role() hasn't been called yet).
163232b31808SJens Wiklander * \param role A value of type ::psa_pake_role_t indicating the
163332b31808SJens Wiklander * application's role in the PAKE the algorithm
163432b31808SJens Wiklander * that is being set up. For more information see
163532b31808SJens Wiklander * the documentation of \c PSA_PAKE_ROLE_XXX
163632b31808SJens Wiklander * constants.
163732b31808SJens Wiklander *
163832b31808SJens Wiklander * \retval #PSA_SUCCESS
163932b31808SJens Wiklander * Success.
164032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
164132b31808SJens Wiklander * The \p role is not a valid PAKE role in the \p operation’s algorithm.
164232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
164332b31808SJens Wiklander * The \p role for this algorithm is not supported or is not valid.
164432b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
164532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
164632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
164732b31808SJens Wiklander * The operation state is not valid, or
164832b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
164932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
165032b31808SJens Wiklander * results in this error code.
165132b31808SJens Wiklander */
165232b31808SJens Wiklander psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
165332b31808SJens Wiklander psa_pake_role_t role);
165432b31808SJens Wiklander
165532b31808SJens Wiklander /** Get output for a step of a password-authenticated key exchange.
165632b31808SJens Wiklander *
165732b31808SJens Wiklander * Depending on the algorithm being executed, you might need to call this
165832b31808SJens Wiklander * function several times or you might not need to call this at all.
165932b31808SJens Wiklander *
166032b31808SJens Wiklander * The exact sequence of calls to perform a password-authenticated key
166132b31808SJens Wiklander * exchange depends on the algorithm in use. Refer to the documentation of
166232b31808SJens Wiklander * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
166332b31808SJens Wiklander * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
166432b31808SJens Wiklander * information.
166532b31808SJens Wiklander *
166632b31808SJens Wiklander * If this function returns an error status, the operation enters an error
166732b31808SJens Wiklander * state and must be aborted by calling psa_pake_abort().
166832b31808SJens Wiklander *
166932b31808SJens Wiklander * \param[in,out] operation Active PAKE operation.
167032b31808SJens Wiklander * \param step The step of the algorithm for which the output is
167132b31808SJens Wiklander * requested.
167232b31808SJens Wiklander * \param[out] output Buffer where the output is to be written in the
167332b31808SJens Wiklander * format appropriate for this \p step. Refer to
167432b31808SJens Wiklander * the documentation of the individual
167532b31808SJens Wiklander * \c PSA_PAKE_STEP_XXX constants for more
167632b31808SJens Wiklander * information.
167732b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. This must
1678b0563631STom Van Eyck * be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1679b0563631STom Van Eyck * primitive, \p output_step) where \c alg and
168032b31808SJens Wiklander * \p primitive are the PAKE algorithm and primitive
168132b31808SJens Wiklander * in the operation's cipher suite, and \p step is
168232b31808SJens Wiklander * the output step.
168332b31808SJens Wiklander *
168432b31808SJens Wiklander * \param[out] output_length On success, the number of bytes of the returned
168532b31808SJens Wiklander * output.
168632b31808SJens Wiklander *
168732b31808SJens Wiklander * \retval #PSA_SUCCESS
168832b31808SJens Wiklander * Success.
168932b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL
169032b31808SJens Wiklander * The size of the \p output buffer is too small.
169132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
169232b31808SJens Wiklander * \p step is not compatible with the operation's algorithm.
169332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
169432b31808SJens Wiklander * \p step is not supported with the operation's algorithm.
169532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
169632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
169732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
169832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
169932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
170032b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
170132b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription
170232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
170332b31808SJens Wiklander * The operation state is not valid (it must be active, and fully set
170432b31808SJens Wiklander * up, and this call must conform to the algorithm's requirements
170532b31808SJens Wiklander * for ordering of input and output steps), or
170632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
170732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
170832b31808SJens Wiklander * results in this error code.
170932b31808SJens Wiklander */
171032b31808SJens Wiklander psa_status_t psa_pake_output(psa_pake_operation_t *operation,
171132b31808SJens Wiklander psa_pake_step_t step,
171232b31808SJens Wiklander uint8_t *output,
171332b31808SJens Wiklander size_t output_size,
171432b31808SJens Wiklander size_t *output_length);
171532b31808SJens Wiklander
171632b31808SJens Wiklander /** Provide input for a step of a password-authenticated key exchange.
171732b31808SJens Wiklander *
171832b31808SJens Wiklander * Depending on the algorithm being executed, you might need to call this
171932b31808SJens Wiklander * function several times or you might not need to call this at all.
172032b31808SJens Wiklander *
172132b31808SJens Wiklander * The exact sequence of calls to perform a password-authenticated key
172232b31808SJens Wiklander * exchange depends on the algorithm in use. Refer to the documentation of
172332b31808SJens Wiklander * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
172432b31808SJens Wiklander * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
172532b31808SJens Wiklander * information.
172632b31808SJens Wiklander *
172732b31808SJens Wiklander * If this function returns an error status, the operation enters an error
172832b31808SJens Wiklander * state and must be aborted by calling psa_pake_abort().
172932b31808SJens Wiklander *
173032b31808SJens Wiklander * \param[in,out] operation Active PAKE operation.
173132b31808SJens Wiklander * \param step The step for which the input is provided.
173232b31808SJens Wiklander * \param[in] input Buffer containing the input in the format
173332b31808SJens Wiklander * appropriate for this \p step. Refer to the
173432b31808SJens Wiklander * documentation of the individual
173532b31808SJens Wiklander * \c PSA_PAKE_STEP_XXX constants for more
173632b31808SJens Wiklander * information.
173732b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes.
173832b31808SJens Wiklander *
173932b31808SJens Wiklander * \retval #PSA_SUCCESS
174032b31808SJens Wiklander * Success.
174132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE
174232b31808SJens Wiklander * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
174332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
1744b0563631STom Van Eyck * \p input_length is not compatible with the \p operation’s algorithm,
1745b0563631STom Van Eyck * or the \p input is not valid for the \p operation's algorithm,
1746b0563631STom Van Eyck * cipher suite or \p step.
174732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
174832b31808SJens Wiklander * \p step p is not supported with the \p operation's algorithm, or the
174932b31808SJens Wiklander * \p input is not supported for the \p operation's algorithm, cipher
175032b31808SJens Wiklander * suite or \p step.
175132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
175232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
175332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
175432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
175532b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
175632b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription
175732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
175832b31808SJens Wiklander * The operation state is not valid (it must be active, and fully set
175932b31808SJens Wiklander * up, and this call must conform to the algorithm's requirements
176032b31808SJens Wiklander * for ordering of input and output steps), or
176132b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init().
176232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
176332b31808SJens Wiklander * results in this error code.
176432b31808SJens Wiklander */
176532b31808SJens Wiklander psa_status_t psa_pake_input(psa_pake_operation_t *operation,
176632b31808SJens Wiklander psa_pake_step_t step,
176732b31808SJens Wiklander const uint8_t *input,
176832b31808SJens Wiklander size_t input_length);
176932b31808SJens Wiklander
177032b31808SJens Wiklander /** Get implicitly confirmed shared secret from a PAKE.
177132b31808SJens Wiklander *
177232b31808SJens Wiklander * At this point there is a cryptographic guarantee that only the authenticated
177332b31808SJens Wiklander * party who used the same password is able to compute the key. But there is no
177432b31808SJens Wiklander * guarantee that the peer is the party it claims to be and was able to do so.
177532b31808SJens Wiklander *
177632b31808SJens Wiklander * That is, the authentication is only implicit. Since the peer is not
177732b31808SJens Wiklander * authenticated yet, no action should be taken yet that assumes that the peer
177832b31808SJens Wiklander * is who it claims to be. For example, do not access restricted files on the
177932b31808SJens Wiklander * peer's behalf until an explicit authentication has succeeded.
178032b31808SJens Wiklander *
178132b31808SJens Wiklander * This function can be called after the key exchange phase of the operation
178232b31808SJens Wiklander * has completed. It imports the shared secret output of the PAKE into the
178332b31808SJens Wiklander * provided derivation operation. The input step
178432b31808SJens Wiklander * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
178532b31808SJens Wiklander * material in the key derivation operation.
178632b31808SJens Wiklander *
178732b31808SJens Wiklander * The exact sequence of calls to perform a password-authenticated key
178832b31808SJens Wiklander * exchange depends on the algorithm in use. Refer to the documentation of
178932b31808SJens Wiklander * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
179032b31808SJens Wiklander * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
179132b31808SJens Wiklander * information.
179232b31808SJens Wiklander *
179332b31808SJens Wiklander * When this function returns successfully, \p operation becomes inactive.
179432b31808SJens Wiklander * If this function returns an error status, both \p operation
1795b0563631STom Van Eyck * and \c key_derivation operations enter an error state and must be aborted by
179632b31808SJens Wiklander * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
179732b31808SJens Wiklander *
179832b31808SJens Wiklander * \param[in,out] operation Active PAKE operation.
179932b31808SJens Wiklander * \param[out] output A key derivation operation that is ready
180032b31808SJens Wiklander * for an input step of type
180132b31808SJens Wiklander * #PSA_KEY_DERIVATION_INPUT_SECRET.
180232b31808SJens Wiklander *
180332b31808SJens Wiklander * \retval #PSA_SUCCESS
180432b31808SJens Wiklander * Success.
180532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT
180632b31808SJens Wiklander * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
180732b31808SJens Wiklander * algorithm in the \p output key derivation operation.
180832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED
180932b31808SJens Wiklander * Input from a PAKE is not supported by the algorithm in the \p output
181032b31808SJens Wiklander * key derivation operation.
181132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
181232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
181332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
181432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
181532b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
181632b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription
181732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
181832b31808SJens Wiklander * The PAKE operation state is not valid (it must be active, but beyond
181932b31808SJens Wiklander * that validity is specific to the algorithm), or
182032b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(),
182132b31808SJens Wiklander * or the state of \p output is not valid for
182232b31808SJens Wiklander * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
182332b31808SJens Wiklander * step is out of order or the application has done this step already
182432b31808SJens Wiklander * and it may not be repeated.
182532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
182632b31808SJens Wiklander * results in this error code.
182732b31808SJens Wiklander */
182832b31808SJens Wiklander psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
182932b31808SJens Wiklander psa_key_derivation_operation_t *output);
183032b31808SJens Wiklander
183132b31808SJens Wiklander /** Abort a PAKE operation.
183232b31808SJens Wiklander *
183332b31808SJens Wiklander * Aborting an operation frees all associated resources except for the \c
183432b31808SJens Wiklander * operation structure itself. Once aborted, the operation object can be reused
183532b31808SJens Wiklander * for another operation by calling psa_pake_setup() again.
183632b31808SJens Wiklander *
183732b31808SJens Wiklander * This function may be called at any time after the operation
183832b31808SJens Wiklander * object has been initialized as described in #psa_pake_operation_t.
183932b31808SJens Wiklander *
184032b31808SJens Wiklander * In particular, calling psa_pake_abort() after the operation has been
184132b31808SJens Wiklander * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
184232b31808SJens Wiklander * is safe and has no effect.
184332b31808SJens Wiklander *
184432b31808SJens Wiklander * \param[in,out] operation The operation to abort.
184532b31808SJens Wiklander *
184632b31808SJens Wiklander * \retval #PSA_SUCCESS
184732b31808SJens Wiklander * Success.
184832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
184932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
185032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE
185132b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init().
185232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize
185332b31808SJens Wiklander * results in this error code.
185432b31808SJens Wiklander */
185532b31808SJens Wiklander psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
185632b31808SJens Wiklander
185732b31808SJens Wiklander /**@}*/
185832b31808SJens Wiklander
psa_pake_cs_get_algorithm(const psa_pake_cipher_suite_t * cipher_suite)185932b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_algorithm(
186032b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite)
186132b31808SJens Wiklander {
186232b31808SJens Wiklander return cipher_suite->algorithm;
186332b31808SJens Wiklander }
186432b31808SJens Wiklander
psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t * cipher_suite,psa_algorithm_t algorithm)186532b31808SJens Wiklander static inline void psa_pake_cs_set_algorithm(
186632b31808SJens Wiklander psa_pake_cipher_suite_t *cipher_suite,
186732b31808SJens Wiklander psa_algorithm_t algorithm)
186832b31808SJens Wiklander {
186932b31808SJens Wiklander if (!PSA_ALG_IS_PAKE(algorithm)) {
187032b31808SJens Wiklander cipher_suite->algorithm = 0;
187132b31808SJens Wiklander } else {
187232b31808SJens Wiklander cipher_suite->algorithm = algorithm;
187332b31808SJens Wiklander }
187432b31808SJens Wiklander }
187532b31808SJens Wiklander
psa_pake_cs_get_primitive(const psa_pake_cipher_suite_t * cipher_suite)187632b31808SJens Wiklander static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
187732b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite)
187832b31808SJens Wiklander {
187932b31808SJens Wiklander return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
188032b31808SJens Wiklander cipher_suite->bits);
188132b31808SJens Wiklander }
188232b31808SJens Wiklander
psa_pake_cs_set_primitive(psa_pake_cipher_suite_t * cipher_suite,psa_pake_primitive_t primitive)188332b31808SJens Wiklander static inline void psa_pake_cs_set_primitive(
188432b31808SJens Wiklander psa_pake_cipher_suite_t *cipher_suite,
188532b31808SJens Wiklander psa_pake_primitive_t primitive)
188632b31808SJens Wiklander {
188732b31808SJens Wiklander cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
188832b31808SJens Wiklander cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
188932b31808SJens Wiklander cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
189032b31808SJens Wiklander }
189132b31808SJens Wiklander
psa_pake_cs_get_family(const psa_pake_cipher_suite_t * cipher_suite)189232b31808SJens Wiklander static inline psa_pake_family_t psa_pake_cs_get_family(
189332b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite)
189432b31808SJens Wiklander {
189532b31808SJens Wiklander return cipher_suite->family;
189632b31808SJens Wiklander }
189732b31808SJens Wiklander
psa_pake_cs_get_bits(const psa_pake_cipher_suite_t * cipher_suite)189832b31808SJens Wiklander static inline uint16_t psa_pake_cs_get_bits(
189932b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite)
190032b31808SJens Wiklander {
190132b31808SJens Wiklander return cipher_suite->bits;
190232b31808SJens Wiklander }
190332b31808SJens Wiklander
psa_pake_cs_get_hash(const psa_pake_cipher_suite_t * cipher_suite)190432b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_hash(
190532b31808SJens Wiklander const psa_pake_cipher_suite_t *cipher_suite)
190632b31808SJens Wiklander {
190732b31808SJens Wiklander return cipher_suite->hash;
190832b31808SJens Wiklander }
190932b31808SJens Wiklander
psa_pake_cs_set_hash(psa_pake_cipher_suite_t * cipher_suite,psa_algorithm_t hash)191032b31808SJens Wiklander static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
191132b31808SJens Wiklander psa_algorithm_t hash)
191232b31808SJens Wiklander {
191332b31808SJens Wiklander if (!PSA_ALG_IS_HASH(hash)) {
191432b31808SJens Wiklander cipher_suite->hash = 0;
191532b31808SJens Wiklander } else {
191632b31808SJens Wiklander cipher_suite->hash = hash;
191732b31808SJens Wiklander }
191832b31808SJens Wiklander }
191932b31808SJens Wiklander
psa_pake_cipher_suite_init(void)192032b31808SJens Wiklander static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
192132b31808SJens Wiklander {
192232b31808SJens Wiklander const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
192332b31808SJens Wiklander return v;
192432b31808SJens Wiklander }
192532b31808SJens Wiklander
psa_pake_operation_init(void)192632b31808SJens Wiklander static inline struct psa_pake_operation_s psa_pake_operation_init(void)
192732b31808SJens Wiklander {
192832b31808SJens Wiklander const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
192932b31808SJens Wiklander return v;
193032b31808SJens Wiklander }
193132b31808SJens Wiklander
193232b31808SJens Wiklander #ifdef __cplusplus
193332b31808SJens Wiklander }
193432b31808SJens Wiklander #endif
193532b31808SJens Wiklander
193632b31808SJens Wiklander #endif /* PSA_CRYPTO_EXTRA_H */
1937