xref: /optee_os/lib/libmbedtls/mbedtls/include/psa/crypto_extra.h (revision b0563631928755fe864b97785160fb3088e9efdc)
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
13*b0563631STom 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 
3532b31808SJens Wiklander /** \addtogroup attributes
3632b31808SJens Wiklander  * @{
3732b31808SJens Wiklander  */
3832b31808SJens Wiklander 
3932b31808SJens Wiklander /** \brief Declare the enrollment algorithm for a key.
4032b31808SJens Wiklander  *
4132b31808SJens Wiklander  * An operation on a key may indifferently use the algorithm set with
4232b31808SJens Wiklander  * psa_set_key_algorithm() or with this function.
4332b31808SJens Wiklander  *
4432b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
4532b31808SJens Wiklander  * \param alg2                  A second algorithm that the key may be used
4632b31808SJens Wiklander  *                              for, in addition to the algorithm set with
4732b31808SJens Wiklander  *                              psa_set_key_algorithm().
4832b31808SJens Wiklander  *
4932b31808SJens Wiklander  * \warning Setting an enrollment algorithm is not recommended, because
5032b31808SJens Wiklander  *          using the same key with different algorithms can allow some
5132b31808SJens Wiklander  *          attacks based on arithmetic relations between different
5232b31808SJens Wiklander  *          computations made with the same key, or can escalate harmless
5332b31808SJens Wiklander  *          side channels into exploitable ones. Use this function only
5432b31808SJens Wiklander  *          if it is necessary to support a protocol for which it has been
5532b31808SJens Wiklander  *          verified that the usage of the key with multiple algorithms
5632b31808SJens Wiklander  *          is safe.
5732b31808SJens Wiklander  */
5832b31808SJens Wiklander static inline void psa_set_key_enrollment_algorithm(
5932b31808SJens Wiklander     psa_key_attributes_t *attributes,
6032b31808SJens Wiklander     psa_algorithm_t alg2)
6132b31808SJens Wiklander {
62*b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
6332b31808SJens Wiklander }
6432b31808SJens Wiklander 
6532b31808SJens Wiklander /** Retrieve the enrollment algorithm policy from key attributes.
6632b31808SJens Wiklander  *
6732b31808SJens Wiklander  * \param[in] attributes        The key attribute structure to query.
6832b31808SJens Wiklander  *
6932b31808SJens Wiklander  * \return The enrollment algorithm stored in the attribute structure.
7032b31808SJens Wiklander  */
7132b31808SJens Wiklander static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
7232b31808SJens Wiklander     const psa_key_attributes_t *attributes)
7332b31808SJens Wiklander {
74*b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
7532b31808SJens Wiklander }
7632b31808SJens Wiklander 
7732b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
7832b31808SJens Wiklander 
7932b31808SJens Wiklander /** Retrieve the slot number where a key is stored.
8032b31808SJens Wiklander  *
8132b31808SJens Wiklander  * A slot number is only defined for keys that are stored in a secure
8232b31808SJens Wiklander  * element.
8332b31808SJens Wiklander  *
8432b31808SJens Wiklander  * This information is only useful if the secure element is not entirely
8532b31808SJens Wiklander  * managed through the PSA Cryptography API. It is up to the secure
8632b31808SJens Wiklander  * element driver to decide how PSA slot numbers map to any other interface
8732b31808SJens Wiklander  * that the secure element may have.
8832b31808SJens Wiklander  *
8932b31808SJens Wiklander  * \param[in] attributes        The key attribute structure to query.
9032b31808SJens Wiklander  * \param[out] slot_number      On success, the slot number containing the key.
9132b31808SJens Wiklander  *
9232b31808SJens Wiklander  * \retval #PSA_SUCCESS
9332b31808SJens Wiklander  *         The key is located in a secure element, and \p *slot_number
9432b31808SJens Wiklander  *         indicates the slot number that contains it.
9532b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
9632b31808SJens Wiklander  *         The caller is not permitted to query the slot number.
97*b0563631STom Van Eyck  *         Mbed TLS currently does not return this error.
9832b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
9932b31808SJens Wiklander  *         The key is not located in a secure element.
10032b31808SJens Wiklander  */
10132b31808SJens Wiklander psa_status_t psa_get_key_slot_number(
10232b31808SJens Wiklander     const psa_key_attributes_t *attributes,
10332b31808SJens Wiklander     psa_key_slot_number_t *slot_number);
10432b31808SJens Wiklander 
10532b31808SJens Wiklander /** Choose the slot number where a key is stored.
10632b31808SJens Wiklander  *
10732b31808SJens Wiklander  * This function declares a slot number in the specified attribute
10832b31808SJens Wiklander  * structure.
10932b31808SJens Wiklander  *
11032b31808SJens Wiklander  * A slot number is only meaningful for keys that are stored in a secure
11132b31808SJens Wiklander  * element. It is up to the secure element driver to decide how PSA slot
11232b31808SJens Wiklander  * numbers map to any other interface that the secure element may have.
11332b31808SJens Wiklander  *
11432b31808SJens Wiklander  * \note Setting a slot number in key attributes for a key creation can
11532b31808SJens Wiklander  *       cause the following errors when creating the key:
11632b31808SJens Wiklander  *       - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
11732b31808SJens Wiklander  *         not support choosing a specific slot number.
11832b31808SJens Wiklander  *       - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
11932b31808SJens Wiklander  *         choose slot numbers in general or to choose this specific slot.
12032b31808SJens Wiklander  *       - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
12132b31808SJens Wiklander  *         valid in general or not valid for this specific key.
12232b31808SJens Wiklander  *       - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
12332b31808SJens Wiklander  *         selected slot.
12432b31808SJens Wiklander  *
12532b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
12632b31808SJens Wiklander  * \param slot_number           The slot number to set.
12732b31808SJens Wiklander  */
12832b31808SJens Wiklander static inline void psa_set_key_slot_number(
12932b31808SJens Wiklander     psa_key_attributes_t *attributes,
13032b31808SJens Wiklander     psa_key_slot_number_t slot_number)
13132b31808SJens Wiklander {
132*b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;
13332b31808SJens Wiklander     attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
13432b31808SJens Wiklander }
13532b31808SJens Wiklander 
13632b31808SJens Wiklander /** Remove the slot number attribute from a key attribute structure.
13732b31808SJens Wiklander  *
13832b31808SJens Wiklander  * This function undoes the action of psa_set_key_slot_number().
13932b31808SJens Wiklander  *
14032b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
14132b31808SJens Wiklander  */
14232b31808SJens Wiklander static inline void psa_clear_key_slot_number(
14332b31808SJens Wiklander     psa_key_attributes_t *attributes)
14432b31808SJens Wiklander {
145*b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;
14632b31808SJens Wiklander }
14732b31808SJens Wiklander 
14832b31808SJens Wiklander /** Register a key that is already present in a secure element.
14932b31808SJens Wiklander  *
15032b31808SJens Wiklander  * The key must be located in a secure element designated by the
15132b31808SJens Wiklander  * lifetime field in \p attributes, in the slot set with
15232b31808SJens Wiklander  * psa_set_key_slot_number() in the attribute structure.
15332b31808SJens Wiklander  * This function makes the key available through the key identifier
15432b31808SJens Wiklander  * specified in \p attributes.
15532b31808SJens Wiklander  *
15632b31808SJens Wiklander  * \param[in] attributes        The attributes of the existing key.
15732b31808SJens Wiklander  *
15832b31808SJens Wiklander  * \retval #PSA_SUCCESS
15932b31808SJens Wiklander  *         The key was successfully registered.
16032b31808SJens Wiklander  *         Note that depending on the design of the driver, this may or may
16132b31808SJens Wiklander  *         not guarantee that a key actually exists in the designated slot
16232b31808SJens Wiklander  *         and is compatible with the specified attributes.
16332b31808SJens Wiklander  * \retval #PSA_ERROR_ALREADY_EXISTS
16432b31808SJens Wiklander  *         There is already a key with the identifier specified in
16532b31808SJens Wiklander  *         \p attributes.
16632b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
16732b31808SJens Wiklander  *         The secure element driver for the specified lifetime does not
16832b31808SJens Wiklander  *         support registering a key.
16932b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
17032b31808SJens Wiklander  *         The identifier in \p attributes is invalid, namely the identifier is
17132b31808SJens Wiklander  *         not in the user range, or
17232b31808SJens Wiklander  *         \p attributes specifies a lifetime which is not located
17332b31808SJens Wiklander  *         in a secure element, or no slot number is specified in \p attributes,
17432b31808SJens Wiklander  *         or the specified slot number is not valid.
17532b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
17632b31808SJens Wiklander  *         The caller is not authorized to register the specified key slot.
17732b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
17832b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
17932b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
18032b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
18132b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
18232b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
18332b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
18432b31808SJens Wiklander  *         The library has not been previously initialized by psa_crypto_init().
18532b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
18632b31808SJens Wiklander  *         results in this error code.
18732b31808SJens Wiklander  */
18832b31808SJens Wiklander psa_status_t mbedtls_psa_register_se_key(
18932b31808SJens Wiklander     const psa_key_attributes_t *attributes);
19032b31808SJens Wiklander 
19132b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
19232b31808SJens Wiklander 
19332b31808SJens Wiklander /**@}*/
19432b31808SJens Wiklander 
19532b31808SJens Wiklander /**
19632b31808SJens Wiklander  * \brief Library deinitialization.
19732b31808SJens Wiklander  *
19832b31808SJens Wiklander  * This function clears all data associated with the PSA layer,
19932b31808SJens Wiklander  * including the whole key store.
200*b0563631STom Van Eyck  * This function is not thread safe, it wipes every key slot regardless of
201*b0563631STom Van Eyck  * state and reader count. It should only be called when no slot is in use.
20232b31808SJens Wiklander  *
20332b31808SJens Wiklander  * This is an Mbed TLS extension.
20432b31808SJens Wiklander  */
20532b31808SJens Wiklander void mbedtls_psa_crypto_free(void);
20632b31808SJens Wiklander 
20732b31808SJens Wiklander /** \brief Statistics about
20832b31808SJens Wiklander  * resource consumption related to the PSA keystore.
20932b31808SJens Wiklander  *
21032b31808SJens Wiklander  * \note The content of this structure is not part of the stable API and ABI
211*b0563631STom Van Eyck  *       of Mbed TLS and may change arbitrarily from version to version.
21232b31808SJens Wiklander  */
21332b31808SJens Wiklander typedef struct mbedtls_psa_stats_s {
21432b31808SJens Wiklander     /** Number of slots containing key material for a volatile key. */
21532b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(volatile_slots);
21632b31808SJens Wiklander     /** Number of slots containing key material for a key which is in
21732b31808SJens Wiklander      * internal persistent storage. */
21832b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(persistent_slots);
21932b31808SJens Wiklander     /** Number of slots containing a reference to a key in a
22032b31808SJens Wiklander      * secure element. */
22132b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(external_slots);
22232b31808SJens Wiklander     /** Number of slots which are occupied, but do not contain
22332b31808SJens Wiklander      * key material yet. */
22432b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(half_filled_slots);
22532b31808SJens Wiklander     /** Number of slots that contain cache data. */
22632b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(cache_slots);
22732b31808SJens Wiklander     /** Number of slots that are not used for anything. */
22832b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(empty_slots);
22932b31808SJens Wiklander     /** Number of slots that are locked. */
23032b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(locked_slots);
23132b31808SJens Wiklander     /** Largest key id value among open keys in internal persistent storage. */
23232b31808SJens Wiklander     psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
23332b31808SJens Wiklander     /** Largest key id value among open keys in secure elements. */
23432b31808SJens Wiklander     psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
23532b31808SJens Wiklander } mbedtls_psa_stats_t;
23632b31808SJens Wiklander 
23732b31808SJens Wiklander /** \brief Get statistics about
23832b31808SJens Wiklander  * resource consumption related to the PSA keystore.
23932b31808SJens Wiklander  *
240*b0563631STom Van Eyck  * \note When Mbed TLS is built as part of a service, with isolation
24132b31808SJens Wiklander  *       between the application and the keystore, the service may or
24232b31808SJens Wiklander  *       may not expose this function.
24332b31808SJens Wiklander  */
24432b31808SJens Wiklander void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
24532b31808SJens Wiklander 
24632b31808SJens Wiklander /**
24732b31808SJens Wiklander  * \brief Inject an initial entropy seed for the random generator into
24832b31808SJens Wiklander  *        secure storage.
24932b31808SJens Wiklander  *
25032b31808SJens Wiklander  * This function injects data to be used as a seed for the random generator
25132b31808SJens Wiklander  * used by the PSA Crypto implementation. On devices that lack a trusted
25232b31808SJens Wiklander  * entropy source (preferably a hardware random number generator),
25332b31808SJens Wiklander  * the Mbed PSA Crypto implementation uses this value to seed its
25432b31808SJens Wiklander  * random generator.
25532b31808SJens Wiklander  *
25632b31808SJens Wiklander  * On devices without a trusted entropy source, this function must be
25732b31808SJens Wiklander  * called exactly once in the lifetime of the device. On devices with
25832b31808SJens Wiklander  * a trusted entropy source, calling this function is optional.
25932b31808SJens Wiklander  * In all cases, this function may only be called before calling any
26032b31808SJens Wiklander  * other function in the PSA Crypto API, including psa_crypto_init().
26132b31808SJens Wiklander  *
26232b31808SJens Wiklander  * When this function returns successfully, it populates a file in
26332b31808SJens Wiklander  * persistent storage. Once the file has been created, this function
26432b31808SJens Wiklander  * can no longer succeed.
26532b31808SJens Wiklander  *
26632b31808SJens Wiklander  * If any error occurs, this function does not change the system state.
26732b31808SJens Wiklander  * You can call this function again after correcting the reason for the
26832b31808SJens Wiklander  * error if possible.
26932b31808SJens Wiklander  *
27032b31808SJens Wiklander  * \warning This function **can** fail! Callers MUST check the return status.
27132b31808SJens Wiklander  *
27232b31808SJens Wiklander  * \warning If you use this function, you should use it as part of a
27332b31808SJens Wiklander  *          factory provisioning process. The value of the injected seed
27432b31808SJens Wiklander  *          is critical to the security of the device. It must be
27532b31808SJens Wiklander  *          *secret*, *unpredictable* and (statistically) *unique per device*.
27632b31808SJens Wiklander  *          You should be generate it randomly using a cryptographically
27732b31808SJens Wiklander  *          secure random generator seeded from trusted entropy sources.
27832b31808SJens Wiklander  *          You should transmit it securely to the device and ensure
27932b31808SJens Wiklander  *          that its value is not leaked or stored anywhere beyond the
28032b31808SJens Wiklander  *          needs of transmitting it from the point of generation to
28132b31808SJens Wiklander  *          the call of this function, and erase all copies of the value
28232b31808SJens Wiklander  *          once this function returns.
28332b31808SJens Wiklander  *
28432b31808SJens Wiklander  * This is an Mbed TLS extension.
28532b31808SJens Wiklander  *
28632b31808SJens Wiklander  * \note This function is only available on the following platforms:
28732b31808SJens Wiklander  * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
28832b31808SJens Wiklander  *   Note that you must provide compatible implementations of
28932b31808SJens Wiklander  *   mbedtls_nv_seed_read and mbedtls_nv_seed_write.
29032b31808SJens Wiklander  * * In a client-server integration of PSA Cryptography, on the client side,
29132b31808SJens Wiklander  *   if the server supports this feature.
29232b31808SJens Wiklander  * \param[in] seed          Buffer containing the seed value to inject.
29332b31808SJens Wiklander  * \param[in] seed_size     Size of the \p seed buffer.
29432b31808SJens Wiklander  *                          The size of the seed in bytes must be greater
29532b31808SJens Wiklander  *                          or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
29632b31808SJens Wiklander  *                          and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
29732b31808SJens Wiklander  *                          in `library/entropy_poll.h` in the Mbed TLS source
29832b31808SJens Wiklander  *                          code.
29932b31808SJens Wiklander  *                          It must be less or equal to
30032b31808SJens Wiklander  *                          #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
30132b31808SJens Wiklander  *
30232b31808SJens Wiklander  * \retval #PSA_SUCCESS
30332b31808SJens Wiklander  *         The seed value was injected successfully. The random generator
30432b31808SJens Wiklander  *         of the PSA Crypto implementation is now ready for use.
30532b31808SJens Wiklander  *         You may now call psa_crypto_init() and use the PSA Crypto
30632b31808SJens Wiklander  *         implementation.
30732b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
30832b31808SJens Wiklander  *         \p seed_size is out of range.
30932b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE
31032b31808SJens Wiklander  *         There was a failure reading or writing from storage.
31132b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
31232b31808SJens Wiklander  *         The library has already been initialized. It is no longer
31332b31808SJens Wiklander  *         possible to call this function.
31432b31808SJens Wiklander  */
31532b31808SJens Wiklander psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
31632b31808SJens Wiklander                                         size_t seed_size);
31732b31808SJens Wiklander 
31832b31808SJens Wiklander /** \addtogroup crypto_types
31932b31808SJens Wiklander  * @{
32032b31808SJens Wiklander  */
32132b31808SJens Wiklander 
32232b31808SJens Wiklander /** DSA public key.
32332b31808SJens Wiklander  *
32432b31808SJens Wiklander  * The import and export format is the
32532b31808SJens Wiklander  * representation of the public key `y = g^x mod p` as a big-endian byte
32632b31808SJens Wiklander  * string. The length of the byte string is the length of the base prime `p`
32732b31808SJens Wiklander  * in bytes.
32832b31808SJens Wiklander  */
32932b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_PUBLIC_KEY                 ((psa_key_type_t) 0x4002)
33032b31808SJens Wiklander 
33132b31808SJens Wiklander /** DSA key pair (private and public key).
33232b31808SJens Wiklander  *
33332b31808SJens Wiklander  * The import and export format is the
33432b31808SJens Wiklander  * representation of the private key `x` as a big-endian byte string. The
33532b31808SJens Wiklander  * length of the byte string is the private key size in bytes (leading zeroes
33632b31808SJens Wiklander  * are not stripped).
33732b31808SJens Wiklander  *
33832b31808SJens Wiklander  * Deterministic DSA key derivation with psa_generate_derived_key follows
33932b31808SJens Wiklander  * FIPS 186-4 §B.1.2: interpret the byte string as integer
34032b31808SJens Wiklander  * in big-endian order. Discard it if it is not in the range
34132b31808SJens Wiklander  * [0, *N* - 2] where *N* is the boundary of the private key domain
34232b31808SJens Wiklander  * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
34332b31808SJens Wiklander  * or the order of the curve's base point for ECC).
34432b31808SJens Wiklander  * Add 1 to the resulting integer and use this as the private key *x*.
34532b31808SJens Wiklander  *
34632b31808SJens Wiklander  */
34732b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_KEY_PAIR                    ((psa_key_type_t) 0x7002)
34832b31808SJens Wiklander 
34932b31808SJens Wiklander /** Whether a key type is a DSA key (pair or public-only). */
35032b31808SJens Wiklander #define PSA_KEY_TYPE_IS_DSA(type)                                       \
35132b31808SJens Wiklander     (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
35232b31808SJens Wiklander 
35332b31808SJens Wiklander #define PSA_ALG_DSA_BASE                        ((psa_algorithm_t) 0x06000400)
35432b31808SJens Wiklander /** DSA signature with hashing.
35532b31808SJens Wiklander  *
35632b31808SJens Wiklander  * This is the signature scheme defined by FIPS 186-4,
35732b31808SJens Wiklander  * with a random per-message secret number (*k*).
35832b31808SJens Wiklander  *
35932b31808SJens Wiklander  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
36032b31808SJens Wiklander  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
36132b31808SJens Wiklander  *                      This includes #PSA_ALG_ANY_HASH
36232b31808SJens Wiklander  *                      when specifying the algorithm in a usage policy.
36332b31808SJens Wiklander  *
36432b31808SJens Wiklander  * \return              The corresponding DSA signature algorithm.
36532b31808SJens Wiklander  * \return              Unspecified if \p hash_alg is not a supported
36632b31808SJens Wiklander  *                      hash algorithm.
36732b31808SJens Wiklander  */
36832b31808SJens Wiklander #define PSA_ALG_DSA(hash_alg)                             \
36932b31808SJens Wiklander     (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
37032b31808SJens Wiklander #define PSA_ALG_DETERMINISTIC_DSA_BASE          ((psa_algorithm_t) 0x06000500)
37132b31808SJens Wiklander #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
37232b31808SJens Wiklander /** Deterministic DSA signature with hashing.
37332b31808SJens Wiklander  *
37432b31808SJens Wiklander  * This is the deterministic variant defined by RFC 6979 of
37532b31808SJens Wiklander  * the signature scheme defined by FIPS 186-4.
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_DETERMINISTIC_DSA(hash_alg)                             \
38732b31808SJens Wiklander     (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
38832b31808SJens Wiklander #define PSA_ALG_IS_DSA(alg)                                             \
38932b31808SJens Wiklander     (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) ==  \
39032b31808SJens Wiklander      PSA_ALG_DSA_BASE)
39132b31808SJens Wiklander #define PSA_ALG_DSA_IS_DETERMINISTIC(alg)               \
39232b31808SJens Wiklander     (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
39332b31808SJens Wiklander #define PSA_ALG_IS_DETERMINISTIC_DSA(alg)                       \
39432b31808SJens Wiklander     (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
39532b31808SJens Wiklander #define PSA_ALG_IS_RANDOMIZED_DSA(alg)                          \
39632b31808SJens Wiklander     (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
39732b31808SJens Wiklander 
39832b31808SJens Wiklander 
39932b31808SJens Wiklander /* We need to expand the sample definition of this macro from
40032b31808SJens Wiklander  * the API definition. */
40132b31808SJens Wiklander #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
40232b31808SJens Wiklander #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg)    \
40332b31808SJens Wiklander     PSA_ALG_IS_DSA(alg)
40432b31808SJens Wiklander 
40532b31808SJens Wiklander /**@}*/
40632b31808SJens Wiklander 
40732b31808SJens Wiklander /** \addtogroup attributes
40832b31808SJens Wiklander  * @{
40932b31808SJens Wiklander  */
41032b31808SJens Wiklander 
41132b31808SJens Wiklander /** PAKE operation stages. */
41232b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_SETUP 0
41332b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
41432b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
41532b31808SJens Wiklander 
41632b31808SJens Wiklander /**@}*/
41732b31808SJens Wiklander 
41832b31808SJens Wiklander 
41932b31808SJens Wiklander /** \defgroup psa_external_rng External random generator
42032b31808SJens Wiklander  * @{
42132b31808SJens Wiklander  */
42232b31808SJens Wiklander 
42332b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
42432b31808SJens Wiklander /** External random generator function, implemented by the platform.
42532b31808SJens Wiklander  *
42632b31808SJens Wiklander  * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
42732b31808SJens Wiklander  * this function replaces Mbed TLS's entropy and DRBG modules for all
42832b31808SJens Wiklander  * random generation triggered via PSA crypto interfaces.
42932b31808SJens Wiklander  *
43032b31808SJens Wiklander  * \note This random generator must deliver random numbers with cryptographic
43132b31808SJens Wiklander  *       quality and high performance. It must supply unpredictable numbers
43232b31808SJens Wiklander  *       with a uniform distribution. The implementation of this function
43332b31808SJens Wiklander  *       is responsible for ensuring that the random generator is seeded
43432b31808SJens Wiklander  *       with sufficient entropy. If you have a hardware TRNG which is slow
43532b31808SJens Wiklander  *       or delivers non-uniform output, declare it as an entropy source
43632b31808SJens Wiklander  *       with mbedtls_entropy_add_source() instead of enabling this option.
43732b31808SJens Wiklander  *
43832b31808SJens Wiklander  * \param[in,out] context       Pointer to the random generator context.
43932b31808SJens Wiklander  *                              This is all-bits-zero on the first call
44032b31808SJens Wiklander  *                              and preserved between successive calls.
44132b31808SJens Wiklander  * \param[out] output           Output buffer. On success, this buffer
44232b31808SJens Wiklander  *                              contains random data with a uniform
44332b31808SJens Wiklander  *                              distribution.
44432b31808SJens Wiklander  * \param output_size           The size of the \p output buffer in bytes.
44532b31808SJens Wiklander  * \param[out] output_length    On success, set this value to \p output_size.
44632b31808SJens Wiklander  *
44732b31808SJens Wiklander  * \retval #PSA_SUCCESS
44832b31808SJens Wiklander  *         Success. The output buffer contains \p output_size bytes of
44932b31808SJens Wiklander  *         cryptographic-quality random data, and \c *output_length is
45032b31808SJens Wiklander  *         set to \p output_size.
45132b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
45232b31808SJens Wiklander  *         The random generator requires extra entropy and there is no
45332b31808SJens Wiklander  *         way to obtain entropy under current environment conditions.
45432b31808SJens Wiklander  *         This error should not happen under normal circumstances since
45532b31808SJens Wiklander  *         this function is responsible for obtaining as much entropy as
45632b31808SJens Wiklander  *         it needs. However implementations of this function may return
45732b31808SJens Wiklander  *         #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
45832b31808SJens Wiklander  *         entropy without blocking indefinitely.
45932b31808SJens Wiklander  * \retval #PSA_ERROR_HARDWARE_FAILURE
46032b31808SJens Wiklander  *         A failure of the random generator hardware that isn't covered
46132b31808SJens Wiklander  *         by #PSA_ERROR_INSUFFICIENT_ENTROPY.
46232b31808SJens Wiklander  */
46332b31808SJens Wiklander psa_status_t mbedtls_psa_external_get_random(
46432b31808SJens Wiklander     mbedtls_psa_external_random_context_t *context,
46532b31808SJens Wiklander     uint8_t *output, size_t output_size, size_t *output_length);
46632b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
46732b31808SJens Wiklander 
46832b31808SJens Wiklander /**@}*/
46932b31808SJens Wiklander 
47032b31808SJens Wiklander /** \defgroup psa_builtin_keys Built-in keys
47132b31808SJens Wiklander  * @{
47232b31808SJens Wiklander  */
47332b31808SJens Wiklander 
47432b31808SJens Wiklander /** The minimum value for a key identifier that is built into the
47532b31808SJens Wiklander  * implementation.
47632b31808SJens Wiklander  *
47732b31808SJens Wiklander  * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
47832b31808SJens Wiklander  * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
47932b31808SJens Wiklander  * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
48032b31808SJens Wiklander  * with any other set of implementation-chosen key identifiers.
48132b31808SJens Wiklander  *
48232b31808SJens Wiklander  * This value is part of the library's ABI since changing it would invalidate
48332b31808SJens Wiklander  * the values of built-in key identifiers in applications.
48432b31808SJens Wiklander  */
48532b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN          ((psa_key_id_t) 0x7fff0000)
48632b31808SJens Wiklander 
48732b31808SJens Wiklander /** The maximum value for a key identifier that is built into the
48832b31808SJens Wiklander  * implementation.
48932b31808SJens Wiklander  *
49032b31808SJens Wiklander  * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
49132b31808SJens Wiklander  */
49232b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX          ((psa_key_id_t) 0x7fffefff)
49332b31808SJens Wiklander 
49432b31808SJens Wiklander /** A slot number identifying a key in a driver.
49532b31808SJens Wiklander  *
49632b31808SJens Wiklander  * Values of this type are used to identify built-in keys.
49732b31808SJens Wiklander  */
49832b31808SJens Wiklander typedef uint64_t psa_drv_slot_number_t;
49932b31808SJens Wiklander 
50032b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
50132b31808SJens Wiklander /** Test whether a key identifier belongs to the builtin key range.
50232b31808SJens Wiklander  *
50332b31808SJens Wiklander  * \param key_id  Key identifier to test.
50432b31808SJens Wiklander  *
50532b31808SJens Wiklander  * \retval 1
50632b31808SJens Wiklander  *         The key identifier is a builtin key identifier.
50732b31808SJens Wiklander  * \retval 0
50832b31808SJens Wiklander  *         The key identifier is not a builtin key identifier.
50932b31808SJens Wiklander  */
51032b31808SJens Wiklander static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
51132b31808SJens Wiklander {
51232b31808SJens Wiklander     return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
51332b31808SJens Wiklander            (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
51432b31808SJens Wiklander }
51532b31808SJens Wiklander 
51632b31808SJens Wiklander /** Platform function to obtain the location and slot number of a built-in key.
51732b31808SJens Wiklander  *
51832b31808SJens Wiklander  * An application-specific implementation of this function must be provided if
51932b31808SJens Wiklander  * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
52032b31808SJens Wiklander  * as part of a platform's system image.
52132b31808SJens Wiklander  *
52232b31808SJens Wiklander  * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
52332b31808SJens Wiklander  * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
52432b31808SJens Wiklander  *
52532b31808SJens Wiklander  * In a multi-application configuration
52632b31808SJens Wiklander  * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
52732b31808SJens Wiklander  * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
52832b31808SJens Wiklander  * is allowed to use the given key.
52932b31808SJens Wiklander  *
53032b31808SJens Wiklander  * \param key_id                The key ID for which to retrieve the
53132b31808SJens Wiklander  *                              location and slot attributes.
53232b31808SJens Wiklander  * \param[out] lifetime         On success, the lifetime associated with the key
53332b31808SJens Wiklander  *                              corresponding to \p key_id. Lifetime is a
53432b31808SJens Wiklander  *                              combination of which driver contains the key,
53532b31808SJens Wiklander  *                              and with what persistence level the key is
53632b31808SJens Wiklander  *                              intended to be used. If the platform
53732b31808SJens Wiklander  *                              implementation does not contain specific
53832b31808SJens Wiklander  *                              information about the intended key persistence
53932b31808SJens Wiklander  *                              level, the persistence level may be reported as
54032b31808SJens Wiklander  *                              #PSA_KEY_PERSISTENCE_DEFAULT.
54132b31808SJens Wiklander  * \param[out] slot_number      On success, the slot number known to the driver
54232b31808SJens Wiklander  *                              registered at the lifetime location reported
54332b31808SJens Wiklander  *                              through \p lifetime which corresponds to the
54432b31808SJens Wiklander  *                              requested built-in key.
54532b31808SJens Wiklander  *
54632b31808SJens Wiklander  * \retval #PSA_SUCCESS
54732b31808SJens Wiklander  *         The requested key identifier designates a built-in key.
54832b31808SJens Wiklander  *         In a multi-application configuration, the requested owner
54932b31808SJens Wiklander  *         is allowed to access it.
55032b31808SJens Wiklander  * \retval #PSA_ERROR_DOES_NOT_EXIST
55132b31808SJens Wiklander  *         The requested key identifier is not a built-in key which is known
55232b31808SJens Wiklander  *         to this function. If a key exists in the key storage with this
55332b31808SJens Wiklander  *         identifier, the data from the storage will be used.
55432b31808SJens Wiklander  * \return (any other error)
55532b31808SJens Wiklander  *         Any other error is propagated to the function that requested the key.
55632b31808SJens Wiklander  *         Common errors include:
55732b31808SJens Wiklander  *         - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
55832b31808SJens Wiklander  *           is not allowed to access it.
55932b31808SJens Wiklander  */
56032b31808SJens Wiklander psa_status_t mbedtls_psa_platform_get_builtin_key(
56132b31808SJens Wiklander     mbedtls_svc_key_id_t key_id,
56232b31808SJens Wiklander     psa_key_lifetime_t *lifetime,
56332b31808SJens Wiklander     psa_drv_slot_number_t *slot_number);
56432b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
56532b31808SJens Wiklander 
56632b31808SJens Wiklander /** @} */
56732b31808SJens Wiklander 
56832b31808SJens Wiklander /** \addtogroup crypto_types
56932b31808SJens Wiklander  * @{
57032b31808SJens Wiklander  */
57132b31808SJens Wiklander 
57232b31808SJens Wiklander #define PSA_ALG_CATEGORY_PAKE                   ((psa_algorithm_t) 0x0a000000)
57332b31808SJens Wiklander 
57432b31808SJens Wiklander /** Whether the specified algorithm is a password-authenticated key exchange.
57532b31808SJens Wiklander  *
57632b31808SJens Wiklander  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
57732b31808SJens Wiklander  *
57832b31808SJens Wiklander  * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
57932b31808SJens Wiklander  *         algorithm, 0 otherwise.
58032b31808SJens Wiklander  *         This macro may return either 0 or 1 if \p alg is not a supported
58132b31808SJens Wiklander  *         algorithm identifier.
58232b31808SJens Wiklander  */
58332b31808SJens Wiklander #define PSA_ALG_IS_PAKE(alg)                                        \
58432b31808SJens Wiklander     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
58532b31808SJens Wiklander 
58632b31808SJens Wiklander /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
58732b31808SJens Wiklander  *
58832b31808SJens Wiklander  * This is J-PAKE as defined by RFC 8236, instantiated with the following
58932b31808SJens Wiklander  * parameters:
59032b31808SJens Wiklander  *
59132b31808SJens Wiklander  * - The group can be either an elliptic curve or defined over a finite field.
59232b31808SJens Wiklander  * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
59332b31808SJens Wiklander  *   J-PAKE algorithm.
59432b31808SJens Wiklander  * - A cryptographic hash function.
59532b31808SJens Wiklander  *
59632b31808SJens Wiklander  * To select these parameters and set up the cipher suite, call these functions
59732b31808SJens Wiklander  * in any order:
59832b31808SJens Wiklander  *
59932b31808SJens Wiklander  * \code
60032b31808SJens Wiklander  * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
60132b31808SJens Wiklander  * psa_pake_cs_set_primitive(cipher_suite,
60232b31808SJens Wiklander  *                           PSA_PAKE_PRIMITIVE(type, family, bits));
60332b31808SJens Wiklander  * psa_pake_cs_set_hash(cipher_suite, hash);
60432b31808SJens Wiklander  * \endcode
60532b31808SJens Wiklander  *
60632b31808SJens Wiklander  * For more information on how to set a specific curve or field, refer to the
60732b31808SJens Wiklander  * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
60832b31808SJens Wiklander  *
60932b31808SJens Wiklander  * After initializing a J-PAKE operation, call
61032b31808SJens Wiklander  *
61132b31808SJens Wiklander  * \code
61232b31808SJens Wiklander  * psa_pake_setup(operation, cipher_suite);
61332b31808SJens Wiklander  * psa_pake_set_user(operation, ...);
61432b31808SJens Wiklander  * psa_pake_set_peer(operation, ...);
61532b31808SJens Wiklander  * psa_pake_set_password_key(operation, ...);
61632b31808SJens Wiklander  * \endcode
61732b31808SJens Wiklander  *
61832b31808SJens Wiklander  * The password is provided as a key. This can be the password text itself,
61932b31808SJens Wiklander  * in an agreed character encoding, or some value derived from the password
62032b31808SJens Wiklander  * as required by a higher level protocol.
62132b31808SJens Wiklander  *
62232b31808SJens Wiklander  * (The implementation converts the key material to a number as described in
62332b31808SJens Wiklander  * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
62432b31808SJens Wiklander  * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
62532b31808SJens Wiklander  * \c q is order of the group defined by the primitive set in the cipher suite.
62632b31808SJens Wiklander  * The \c psa_pake_set_password_key() function returns an error if the result
62732b31808SJens Wiklander  * of the reduction is 0.)
62832b31808SJens Wiklander  *
62932b31808SJens Wiklander  * The key exchange flow for J-PAKE is as follows:
63032b31808SJens Wiklander  * -# To get the first round data that needs to be sent to the peer, call
63132b31808SJens Wiklander  *    \code
63232b31808SJens Wiklander  *    // Get g1
63332b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
63432b31808SJens Wiklander  *    // Get the ZKP public key for x1
63532b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
63632b31808SJens Wiklander  *    // Get the ZKP proof for x1
63732b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
63832b31808SJens Wiklander  *    // Get g2
63932b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
64032b31808SJens Wiklander  *    // Get the ZKP public key for x2
64132b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
64232b31808SJens Wiklander  *    // Get the ZKP proof for x2
64332b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
64432b31808SJens Wiklander  *    \endcode
64532b31808SJens Wiklander  * -# To provide the first round data received from the peer to the operation,
64632b31808SJens Wiklander  *    call
64732b31808SJens Wiklander  *    \code
64832b31808SJens Wiklander  *    // Set g3
64932b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
65032b31808SJens Wiklander  *    // Set the ZKP public key for x3
65132b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
65232b31808SJens Wiklander  *    // Set the ZKP proof for x3
65332b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
65432b31808SJens Wiklander  *    // Set g4
65532b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
65632b31808SJens Wiklander  *    // Set the ZKP public key for x4
65732b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
65832b31808SJens Wiklander  *    // Set the ZKP proof for x4
65932b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
66032b31808SJens Wiklander  *    \endcode
66132b31808SJens Wiklander  * -# To get the second round data that needs to be sent to the peer, call
66232b31808SJens Wiklander  *    \code
66332b31808SJens Wiklander  *    // Get A
66432b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
66532b31808SJens Wiklander  *    // Get ZKP public key for x2*s
66632b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
66732b31808SJens Wiklander  *    // Get ZKP proof for x2*s
66832b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
66932b31808SJens Wiklander  *    \endcode
67032b31808SJens Wiklander  * -# To provide the second round data received from the peer to the operation,
67132b31808SJens Wiklander  *    call
67232b31808SJens Wiklander  *    \code
67332b31808SJens Wiklander  *    // Set B
67432b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
67532b31808SJens Wiklander  *    // Set ZKP public key for x4*s
67632b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
67732b31808SJens Wiklander  *    // Set ZKP proof for x4*s
67832b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
67932b31808SJens Wiklander  *    \endcode
68032b31808SJens Wiklander  * -# To access the shared secret call
68132b31808SJens Wiklander  *    \code
68232b31808SJens Wiklander  *    // Get Ka=Kb=K
68332b31808SJens Wiklander  *    psa_pake_get_implicit_key()
68432b31808SJens Wiklander  *    \endcode
68532b31808SJens Wiklander  *
68632b31808SJens Wiklander  * For more information consult the documentation of the individual
68732b31808SJens Wiklander  * \c PSA_PAKE_STEP_XXX constants.
68832b31808SJens Wiklander  *
68932b31808SJens Wiklander  * At this point there is a cryptographic guarantee that only the authenticated
69032b31808SJens Wiklander  * party who used the same password is able to compute the key. But there is no
69132b31808SJens Wiklander  * guarantee that the peer is the party it claims to be and was able to do so.
69232b31808SJens Wiklander  *
69332b31808SJens Wiklander  * That is, the authentication is only implicit (the peer is not authenticated
69432b31808SJens Wiklander  * at this point, and no action should be taken that assume that they are - like
69532b31808SJens Wiklander  * for example accessing restricted files).
69632b31808SJens Wiklander  *
69732b31808SJens Wiklander  * To make the authentication explicit there are various methods, see Section 5
69832b31808SJens Wiklander  * of RFC 8236 for two examples.
69932b31808SJens Wiklander  *
70032b31808SJens Wiklander  */
70132b31808SJens Wiklander #define PSA_ALG_JPAKE                   ((psa_algorithm_t) 0x0a000100)
70232b31808SJens Wiklander 
70332b31808SJens Wiklander /** @} */
70432b31808SJens Wiklander 
70532b31808SJens Wiklander /** \defgroup pake Password-authenticated key exchange (PAKE)
70632b31808SJens Wiklander  *
70732b31808SJens Wiklander  * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
70832b31808SJens Wiklander  * the official PSA Crypto API yet.
70932b31808SJens Wiklander  *
71032b31808SJens Wiklander  * \note The content of this section is not part of the stable API and ABI
711*b0563631STom Van Eyck  *       of Mbed TLS and may change arbitrarily from version to version.
71232b31808SJens Wiklander  *       Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
71332b31808SJens Wiklander  *       #PSA_ALG_JPAKE.
71432b31808SJens Wiklander  * @{
71532b31808SJens Wiklander  */
71632b31808SJens Wiklander 
71732b31808SJens Wiklander /** \brief Encoding of the application role of PAKE
71832b31808SJens Wiklander  *
71932b31808SJens Wiklander  * Encodes the application's role in the algorithm is being executed. For more
72032b31808SJens Wiklander  * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
72132b31808SJens Wiklander  * constants.
72232b31808SJens Wiklander  */
72332b31808SJens Wiklander typedef uint8_t psa_pake_role_t;
72432b31808SJens Wiklander 
72532b31808SJens Wiklander /** Encoding of input and output indicators for PAKE.
72632b31808SJens Wiklander  *
72732b31808SJens Wiklander  * Some PAKE algorithms need to exchange more data than just a single key share.
72832b31808SJens Wiklander  * This type is for encoding additional input and output data for such
72932b31808SJens Wiklander  * algorithms.
73032b31808SJens Wiklander  */
73132b31808SJens Wiklander typedef uint8_t psa_pake_step_t;
73232b31808SJens Wiklander 
73332b31808SJens Wiklander /** Encoding of the type of the PAKE's primitive.
73432b31808SJens Wiklander  *
73532b31808SJens Wiklander  * Values defined by this standard will never be in the range 0x80-0xff.
73632b31808SJens Wiklander  * Vendors who define additional types must use an encoding in this range.
73732b31808SJens Wiklander  *
73832b31808SJens Wiklander  * For more information see the documentation of individual
73932b31808SJens Wiklander  * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
74032b31808SJens Wiklander  */
74132b31808SJens Wiklander typedef uint8_t psa_pake_primitive_type_t;
74232b31808SJens Wiklander 
74332b31808SJens Wiklander /** \brief Encoding of the family of the primitive associated with the PAKE.
74432b31808SJens Wiklander  *
74532b31808SJens Wiklander  * For more information see the documentation of individual
74632b31808SJens Wiklander  * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
74732b31808SJens Wiklander  */
74832b31808SJens Wiklander typedef uint8_t psa_pake_family_t;
74932b31808SJens Wiklander 
75032b31808SJens Wiklander /** \brief Encoding of the primitive associated with the PAKE.
75132b31808SJens Wiklander  *
75232b31808SJens Wiklander  * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
75332b31808SJens Wiklander  */
75432b31808SJens Wiklander typedef uint32_t psa_pake_primitive_t;
75532b31808SJens Wiklander 
75632b31808SJens Wiklander /** A value to indicate no role in a PAKE algorithm.
75732b31808SJens Wiklander  * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
75832b31808SJens Wiklander  * algorithms which do not assign roles.
75932b31808SJens Wiklander  */
76032b31808SJens Wiklander #define PSA_PAKE_ROLE_NONE                  ((psa_pake_role_t) 0x00)
76132b31808SJens Wiklander 
76232b31808SJens Wiklander /** The first peer in a balanced PAKE.
76332b31808SJens Wiklander  *
76432b31808SJens Wiklander  * Although balanced PAKE algorithms are symmetric, some of them needs an
76532b31808SJens Wiklander  * ordering of peers for the transcript calculations. If the algorithm does not
76632b31808SJens Wiklander  * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
76732b31808SJens Wiklander  * accepted.
76832b31808SJens Wiklander  */
76932b31808SJens Wiklander #define PSA_PAKE_ROLE_FIRST                ((psa_pake_role_t) 0x01)
77032b31808SJens Wiklander 
77132b31808SJens Wiklander /** The second peer in a balanced PAKE.
77232b31808SJens Wiklander  *
77332b31808SJens Wiklander  * Although balanced PAKE algorithms are symmetric, some of them needs an
77432b31808SJens Wiklander  * ordering of peers for the transcript calculations. If the algorithm does not
77532b31808SJens Wiklander  * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
77632b31808SJens Wiklander  * accepted.
77732b31808SJens Wiklander  */
77832b31808SJens Wiklander #define PSA_PAKE_ROLE_SECOND                ((psa_pake_role_t) 0x02)
77932b31808SJens Wiklander 
78032b31808SJens Wiklander /** The client in an augmented PAKE.
78132b31808SJens Wiklander  *
78232b31808SJens Wiklander  * Augmented PAKE algorithms need to differentiate between client and server.
78332b31808SJens Wiklander  */
78432b31808SJens Wiklander #define PSA_PAKE_ROLE_CLIENT                ((psa_pake_role_t) 0x11)
78532b31808SJens Wiklander 
78632b31808SJens Wiklander /** The server in an augmented PAKE.
78732b31808SJens Wiklander  *
78832b31808SJens Wiklander  * Augmented PAKE algorithms need to differentiate between client and server.
78932b31808SJens Wiklander  */
79032b31808SJens Wiklander #define PSA_PAKE_ROLE_SERVER                ((psa_pake_role_t) 0x12)
79132b31808SJens Wiklander 
79232b31808SJens Wiklander /** The PAKE primitive type indicating the use of elliptic curves.
79332b31808SJens Wiklander  *
79432b31808SJens Wiklander  * The values of the \c family and \c bits fields of the cipher suite identify a
79532b31808SJens Wiklander  * specific elliptic curve, using the same mapping that is used for ECC
79632b31808SJens Wiklander  * (::psa_ecc_family_t) keys.
79732b31808SJens Wiklander  *
79832b31808SJens Wiklander  * (Here \c family means the value returned by psa_pake_cs_get_family() and
79932b31808SJens Wiklander  * \c bits means the value returned by psa_pake_cs_get_bits().)
80032b31808SJens Wiklander  *
80132b31808SJens Wiklander  * Input and output during the operation can involve group elements and scalar
80232b31808SJens Wiklander  * values:
80332b31808SJens Wiklander  * -# The format for group elements is the same as for public keys on the
80432b31808SJens Wiklander  *  specific curve would be. For more information, consult the documentation of
80532b31808SJens Wiklander  *  psa_export_public_key().
80632b31808SJens Wiklander  * -# The format for scalars is the same as for private keys on the specific
80732b31808SJens Wiklander  *  curve would be. For more information, consult the documentation of
80832b31808SJens Wiklander  *  psa_export_key().
80932b31808SJens Wiklander  */
81032b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_ECC       ((psa_pake_primitive_type_t) 0x01)
81132b31808SJens Wiklander 
81232b31808SJens Wiklander /** The PAKE primitive type indicating the use of Diffie-Hellman groups.
81332b31808SJens Wiklander  *
81432b31808SJens Wiklander  * The values of the \c family and \c bits fields of the cipher suite identify
81532b31808SJens Wiklander  * a specific Diffie-Hellman group, using the same mapping that is used for
81632b31808SJens Wiklander  * Diffie-Hellman (::psa_dh_family_t) keys.
81732b31808SJens Wiklander  *
81832b31808SJens Wiklander  * (Here \c family means the value returned by psa_pake_cs_get_family() and
81932b31808SJens Wiklander  * \c bits means the value returned by psa_pake_cs_get_bits().)
82032b31808SJens Wiklander  *
82132b31808SJens Wiklander  * Input and output during the operation can involve group elements and scalar
82232b31808SJens Wiklander  * values:
82332b31808SJens Wiklander  * -# The format for group elements is the same as for public keys on the
82432b31808SJens Wiklander  *  specific group would be. For more information, consult the documentation of
82532b31808SJens Wiklander  *  psa_export_public_key().
82632b31808SJens Wiklander  * -# The format for scalars is the same as for private keys on the specific
82732b31808SJens Wiklander  *  group would be. For more information, consult the documentation of
82832b31808SJens Wiklander  *  psa_export_key().
82932b31808SJens Wiklander  */
83032b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_DH       ((psa_pake_primitive_type_t) 0x02)
83132b31808SJens Wiklander 
83232b31808SJens Wiklander /** Construct a PAKE primitive from type, family and bit-size.
83332b31808SJens Wiklander  *
83432b31808SJens Wiklander  * \param pake_type     The type of the primitive
83532b31808SJens Wiklander  *                      (value of type ::psa_pake_primitive_type_t).
83632b31808SJens Wiklander  * \param pake_family   The family of the primitive
83732b31808SJens Wiklander  *                      (the type and interpretation of this parameter depends
838*b0563631STom Van Eyck  *                      on \p pake_type, for more information consult the
83932b31808SJens Wiklander  *                      documentation of individual ::psa_pake_primitive_type_t
84032b31808SJens Wiklander  *                      constants).
84132b31808SJens Wiklander  * \param pake_bits     The bit-size of the primitive
84232b31808SJens Wiklander  *                      (Value of type \c size_t. The interpretation
843*b0563631STom Van Eyck  *                      of this parameter depends on \p pake_family, for more
84432b31808SJens Wiklander  *                      information consult the documentation of individual
84532b31808SJens Wiklander  *                      ::psa_pake_primitive_type_t constants).
84632b31808SJens Wiklander  *
84732b31808SJens Wiklander  * \return The constructed primitive value of type ::psa_pake_primitive_t.
84832b31808SJens Wiklander  *         Return 0 if the requested primitive can't be encoded as
84932b31808SJens Wiklander  *         ::psa_pake_primitive_t.
85032b31808SJens Wiklander  */
85132b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
85232b31808SJens Wiklander     ((pake_bits & 0xFFFF) != pake_bits) ? 0 :                 \
85332b31808SJens Wiklander     ((psa_pake_primitive_t) (((pake_type) << 24 |             \
85432b31808SJens Wiklander                               (pake_family) << 16) | (pake_bits)))
85532b31808SJens Wiklander 
85632b31808SJens Wiklander /** The key share being sent to or received from the peer.
85732b31808SJens Wiklander  *
85832b31808SJens Wiklander  * The format for both input and output at this step is the same as for public
85932b31808SJens Wiklander  * keys on the group determined by the primitive (::psa_pake_primitive_t) would
86032b31808SJens Wiklander  * be.
86132b31808SJens Wiklander  *
86232b31808SJens Wiklander  * For more information on the format, consult the documentation of
86332b31808SJens Wiklander  * psa_export_public_key().
86432b31808SJens Wiklander  *
86532b31808SJens Wiklander  * For information regarding how the group is determined, consult the
86632b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
86732b31808SJens Wiklander  */
86832b31808SJens Wiklander #define PSA_PAKE_STEP_KEY_SHARE                 ((psa_pake_step_t) 0x01)
86932b31808SJens Wiklander 
87032b31808SJens Wiklander /** A Schnorr NIZKP public key.
87132b31808SJens Wiklander  *
87232b31808SJens Wiklander  * This is the ephemeral public key in the Schnorr Non-Interactive
87332b31808SJens Wiklander  * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
87432b31808SJens Wiklander  *
87532b31808SJens Wiklander  * The format for both input and output at this step is the same as for public
87632b31808SJens Wiklander  * keys on the group determined by the primitive (::psa_pake_primitive_t) would
87732b31808SJens Wiklander  * be.
87832b31808SJens Wiklander  *
87932b31808SJens Wiklander  * For more information on the format, consult the documentation of
88032b31808SJens Wiklander  * psa_export_public_key().
88132b31808SJens Wiklander  *
88232b31808SJens Wiklander  * For information regarding how the group is determined, consult the
88332b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
88432b31808SJens Wiklander  */
88532b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PUBLIC                 ((psa_pake_step_t) 0x02)
88632b31808SJens Wiklander 
88732b31808SJens Wiklander /** A Schnorr NIZKP proof.
88832b31808SJens Wiklander  *
88932b31808SJens Wiklander  * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
89032b31808SJens Wiklander  * value denoted by the letter 'r' in RFC 8235).
89132b31808SJens Wiklander  *
89232b31808SJens Wiklander  * Both for input and output, the value at this step is an integer less than
89332b31808SJens Wiklander  * the order of the group selected in the cipher suite. The format depends on
89432b31808SJens Wiklander  * the group as well:
89532b31808SJens Wiklander  *
89632b31808SJens Wiklander  * - For Montgomery curves, the encoding is little endian.
89732b31808SJens Wiklander  * - For everything else the encoding is big endian (see Section 2.3.8 of
89832b31808SJens Wiklander  *   _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
89932b31808SJens Wiklander  *
90032b31808SJens Wiklander  * In both cases leading zeroes are allowed as long as the length in bytes does
90132b31808SJens Wiklander  * not exceed the byte length of the group order.
90232b31808SJens Wiklander  *
90332b31808SJens Wiklander  * For information regarding how the group is determined, consult the
90432b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
90532b31808SJens Wiklander  */
90632b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PROOF                  ((psa_pake_step_t) 0x03)
90732b31808SJens Wiklander 
90832b31808SJens Wiklander /** The type of the data structure for PAKE cipher suites.
90932b31808SJens Wiklander  *
91032b31808SJens Wiklander  * This is an implementation-defined \c struct. Applications should not
91132b31808SJens Wiklander  * make any assumptions about the content of this structure.
91232b31808SJens Wiklander  * Implementation details can change in future versions without notice.
91332b31808SJens Wiklander  */
91432b31808SJens Wiklander typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
91532b31808SJens Wiklander 
91632b31808SJens Wiklander /** Return an initial value for a PAKE cipher suite object.
91732b31808SJens Wiklander  */
91832b31808SJens Wiklander static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
91932b31808SJens Wiklander 
92032b31808SJens Wiklander /** Retrieve the PAKE algorithm from a PAKE cipher suite.
92132b31808SJens Wiklander  *
92232b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
92332b31808SJens Wiklander  *
92432b31808SJens Wiklander  * \return The PAKE algorithm stored in the cipher suite structure.
92532b31808SJens Wiklander  */
92632b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_algorithm(
92732b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
92832b31808SJens Wiklander 
92932b31808SJens Wiklander /** Declare the PAKE algorithm for the cipher suite.
93032b31808SJens Wiklander  *
93132b31808SJens Wiklander  * This function overwrites any PAKE algorithm
93232b31808SJens Wiklander  * previously set in \p cipher_suite.
93332b31808SJens Wiklander  *
93432b31808SJens Wiklander  * \param[out] cipher_suite    The cipher suite structure to write to.
93532b31808SJens Wiklander  * \param algorithm            The PAKE algorithm to write.
93632b31808SJens Wiklander  *                             (`PSA_ALG_XXX` values of type ::psa_algorithm_t
93732b31808SJens Wiklander  *                             such that #PSA_ALG_IS_PAKE(\c alg) is true.)
93832b31808SJens Wiklander  *                             If this is 0, the PAKE algorithm in
93932b31808SJens Wiklander  *                             \p cipher_suite becomes unspecified.
94032b31808SJens Wiklander  */
94132b31808SJens Wiklander static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
94232b31808SJens Wiklander                                       psa_algorithm_t algorithm);
94332b31808SJens Wiklander 
94432b31808SJens Wiklander /** Retrieve the primitive from a PAKE cipher suite.
94532b31808SJens Wiklander  *
94632b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
94732b31808SJens Wiklander  *
94832b31808SJens Wiklander  * \return The primitive stored in the cipher suite structure.
94932b31808SJens Wiklander  */
95032b31808SJens Wiklander static psa_pake_primitive_t psa_pake_cs_get_primitive(
95132b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
95232b31808SJens Wiklander 
95332b31808SJens Wiklander /** Declare the primitive for a PAKE cipher suite.
95432b31808SJens Wiklander  *
95532b31808SJens Wiklander  * This function overwrites any primitive previously set in \p cipher_suite.
95632b31808SJens Wiklander  *
95732b31808SJens Wiklander  * \param[out] cipher_suite    The cipher suite structure to write to.
95832b31808SJens Wiklander  * \param primitive            The primitive to write. If this is 0, the
95932b31808SJens Wiklander  *                             primitive type in \p cipher_suite becomes
96032b31808SJens Wiklander  *                             unspecified.
96132b31808SJens Wiklander  */
96232b31808SJens Wiklander static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
96332b31808SJens Wiklander                                       psa_pake_primitive_t primitive);
96432b31808SJens Wiklander 
96532b31808SJens Wiklander /** Retrieve the PAKE family from a PAKE cipher suite.
96632b31808SJens Wiklander  *
96732b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
96832b31808SJens Wiklander  *
96932b31808SJens Wiklander  * \return The PAKE family stored in the cipher suite structure.
97032b31808SJens Wiklander  */
97132b31808SJens Wiklander static psa_pake_family_t psa_pake_cs_get_family(
97232b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
97332b31808SJens Wiklander 
97432b31808SJens Wiklander /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
97532b31808SJens Wiklander  *
97632b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
97732b31808SJens Wiklander  *
97832b31808SJens Wiklander  * \return The PAKE primitive bit-size stored in the cipher suite structure.
97932b31808SJens Wiklander  */
98032b31808SJens Wiklander static uint16_t psa_pake_cs_get_bits(
98132b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
98232b31808SJens Wiklander 
98332b31808SJens Wiklander /** Retrieve the hash algorithm from a PAKE cipher suite.
98432b31808SJens Wiklander  *
98532b31808SJens Wiklander  * \param[in] cipher_suite      The cipher suite structure to query.
98632b31808SJens Wiklander  *
98732b31808SJens Wiklander  * \return The hash algorithm stored in the cipher suite structure. The return
98832b31808SJens Wiklander  *         value is 0 if the PAKE is not parametrised by a hash algorithm or if
98932b31808SJens Wiklander  *         the hash algorithm is not set.
99032b31808SJens Wiklander  */
99132b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_hash(
99232b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
99332b31808SJens Wiklander 
99432b31808SJens Wiklander /** Declare the hash algorithm for a PAKE cipher suite.
99532b31808SJens Wiklander  *
99632b31808SJens Wiklander  * This function overwrites any hash algorithm
99732b31808SJens Wiklander  * previously set in \p cipher_suite.
99832b31808SJens Wiklander  *
99932b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
100032b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
100132b31808SJens Wiklander  * for more information.
100232b31808SJens Wiklander  *
100332b31808SJens Wiklander  * \param[out] cipher_suite     The cipher suite structure to write to.
100432b31808SJens Wiklander  * \param hash                  The hash involved in the cipher suite.
100532b31808SJens Wiklander  *                              (`PSA_ALG_XXX` values of type ::psa_algorithm_t
100632b31808SJens Wiklander  *                              such that #PSA_ALG_IS_HASH(\c alg) is true.)
100732b31808SJens Wiklander  *                              If this is 0, the hash algorithm in
100832b31808SJens Wiklander  *                              \p cipher_suite becomes unspecified.
100932b31808SJens Wiklander  */
101032b31808SJens Wiklander static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
101132b31808SJens Wiklander                                  psa_algorithm_t hash);
101232b31808SJens Wiklander 
101332b31808SJens Wiklander /** The type of the state data structure for PAKE operations.
101432b31808SJens Wiklander  *
101532b31808SJens Wiklander  * Before calling any function on a PAKE operation object, the application
101632b31808SJens Wiklander  * must initialize it by any of the following means:
101732b31808SJens Wiklander  * - Set the structure to all-bits-zero, for example:
101832b31808SJens Wiklander  *   \code
101932b31808SJens Wiklander  *   psa_pake_operation_t operation;
102032b31808SJens Wiklander  *   memset(&operation, 0, sizeof(operation));
102132b31808SJens Wiklander  *   \endcode
102232b31808SJens Wiklander  * - Initialize the structure to logical zero values, for example:
102332b31808SJens Wiklander  *   \code
102432b31808SJens Wiklander  *   psa_pake_operation_t operation = {0};
102532b31808SJens Wiklander  *   \endcode
102632b31808SJens Wiklander  * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
102732b31808SJens Wiklander  *   for example:
102832b31808SJens Wiklander  *   \code
102932b31808SJens Wiklander  *   psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
103032b31808SJens Wiklander  *   \endcode
103132b31808SJens Wiklander  * - Assign the result of the function psa_pake_operation_init()
103232b31808SJens Wiklander  *   to the structure, for example:
103332b31808SJens Wiklander  *   \code
103432b31808SJens Wiklander  *   psa_pake_operation_t operation;
103532b31808SJens Wiklander  *   operation = psa_pake_operation_init();
103632b31808SJens Wiklander  *   \endcode
103732b31808SJens Wiklander  *
103832b31808SJens Wiklander  * This is an implementation-defined \c struct. Applications should not
103932b31808SJens Wiklander  * make any assumptions about the content of this structure.
104032b31808SJens Wiklander  * Implementation details can change in future versions without notice. */
104132b31808SJens Wiklander typedef struct psa_pake_operation_s psa_pake_operation_t;
104232b31808SJens Wiklander 
104332b31808SJens Wiklander /** The type of input values for PAKE operations. */
104432b31808SJens Wiklander typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
104532b31808SJens Wiklander 
104632b31808SJens Wiklander /** The type of computation stage for J-PAKE operations. */
104732b31808SJens Wiklander typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
104832b31808SJens Wiklander 
104932b31808SJens Wiklander /** Return an initial value for a PAKE operation object.
105032b31808SJens Wiklander  */
105132b31808SJens Wiklander static psa_pake_operation_t psa_pake_operation_init(void);
105232b31808SJens Wiklander 
105332b31808SJens Wiklander /** Get the length of the password in bytes from given inputs.
105432b31808SJens Wiklander  *
105532b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
105632b31808SJens Wiklander  * \param[out] password_len     Password length.
105732b31808SJens Wiklander  *
105832b31808SJens Wiklander  * \retval #PSA_SUCCESS
105932b31808SJens Wiklander  *         Success.
106032b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
106132b31808SJens Wiklander  *         Password hasn't been set yet.
106232b31808SJens Wiklander  */
106332b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password_len(
106432b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
106532b31808SJens Wiklander     size_t *password_len);
106632b31808SJens Wiklander 
106732b31808SJens Wiklander /** Get the password from given inputs.
106832b31808SJens Wiklander  *
106932b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
107032b31808SJens Wiklander  * \param[out] buffer           Return buffer for password.
107132b31808SJens Wiklander  * \param      buffer_size      Size of the return buffer in bytes.
107232b31808SJens Wiklander  * \param[out] buffer_length    Actual size of the password in bytes.
107332b31808SJens Wiklander  *
107432b31808SJens Wiklander  * \retval #PSA_SUCCESS
107532b31808SJens Wiklander  *         Success.
107632b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
107732b31808SJens Wiklander  *         Password hasn't been set yet.
107832b31808SJens Wiklander  */
107932b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password(
108032b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
108132b31808SJens Wiklander     uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
108232b31808SJens Wiklander 
108332b31808SJens Wiklander /** Get the length of the user id in bytes from given inputs.
108432b31808SJens Wiklander  *
108532b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
108632b31808SJens Wiklander  * \param[out] user_len         User id length.
108732b31808SJens Wiklander  *
108832b31808SJens Wiklander  * \retval #PSA_SUCCESS
108932b31808SJens Wiklander  *         Success.
109032b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
109132b31808SJens Wiklander  *         User id hasn't been set yet.
109232b31808SJens Wiklander  */
109332b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user_len(
109432b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
109532b31808SJens Wiklander     size_t *user_len);
109632b31808SJens Wiklander 
109732b31808SJens Wiklander /** Get the length of the peer id in bytes from given inputs.
109832b31808SJens Wiklander  *
109932b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
110032b31808SJens Wiklander  * \param[out] peer_len         Peer id length.
110132b31808SJens Wiklander  *
110232b31808SJens Wiklander  * \retval #PSA_SUCCESS
110332b31808SJens Wiklander  *         Success.
110432b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
110532b31808SJens Wiklander  *         Peer id hasn't been set yet.
110632b31808SJens Wiklander  */
110732b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer_len(
110832b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
110932b31808SJens Wiklander     size_t *peer_len);
111032b31808SJens Wiklander 
111132b31808SJens Wiklander /** Get the user id from given inputs.
111232b31808SJens Wiklander  *
111332b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
111432b31808SJens Wiklander  * \param[out] user_id          User id.
111532b31808SJens Wiklander  * \param      user_id_size     Size of \p user_id in bytes.
111632b31808SJens Wiklander  * \param[out] user_id_len      Size of the user id in bytes.
111732b31808SJens Wiklander  *
111832b31808SJens Wiklander  * \retval #PSA_SUCCESS
111932b31808SJens Wiklander  *         Success.
112032b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
112132b31808SJens Wiklander  *         User id hasn't been set yet.
112232b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
112332b31808SJens Wiklander  *         The size of the \p user_id is too small.
112432b31808SJens Wiklander  */
112532b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user(
112632b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
112732b31808SJens Wiklander     uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
112832b31808SJens Wiklander 
112932b31808SJens Wiklander /** Get the peer id from given inputs.
113032b31808SJens Wiklander  *
113132b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
113232b31808SJens Wiklander  * \param[out] peer_id          Peer id.
113332b31808SJens Wiklander  * \param      peer_id_size     Size of \p peer_id in bytes.
113432b31808SJens Wiklander  * \param[out] peer_id_length   Size of the peer id in bytes.
113532b31808SJens Wiklander  *
113632b31808SJens Wiklander  * \retval #PSA_SUCCESS
113732b31808SJens Wiklander  *         Success.
113832b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
113932b31808SJens Wiklander  *         Peer id hasn't been set yet.
114032b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
114132b31808SJens Wiklander  *         The size of the \p peer_id is too small.
114232b31808SJens Wiklander  */
114332b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer(
114432b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
114532b31808SJens Wiklander     uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
114632b31808SJens Wiklander 
114732b31808SJens Wiklander /** Get the cipher suite from given inputs.
114832b31808SJens Wiklander  *
114932b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
115032b31808SJens Wiklander  * \param[out] cipher_suite     Return buffer for role.
115132b31808SJens Wiklander  *
115232b31808SJens Wiklander  * \retval #PSA_SUCCESS
115332b31808SJens Wiklander  *         Success.
115432b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
115532b31808SJens Wiklander  *         Cipher_suite hasn't been set yet.
115632b31808SJens Wiklander  */
115732b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_cipher_suite(
115832b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
115932b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite);
116032b31808SJens Wiklander 
116132b31808SJens Wiklander /** Set the session information for a password-authenticated key exchange.
116232b31808SJens Wiklander  *
116332b31808SJens Wiklander  * The sequence of operations to set up a password-authenticated key exchange
116432b31808SJens Wiklander  * is as follows:
116532b31808SJens Wiklander  * -# Allocate an operation object which will be passed to all the functions
116632b31808SJens Wiklander  *    listed here.
116732b31808SJens Wiklander  * -# Initialize the operation object with one of the methods described in the
116832b31808SJens Wiklander  *    documentation for #psa_pake_operation_t, e.g.
116932b31808SJens Wiklander  *    #PSA_PAKE_OPERATION_INIT.
117032b31808SJens Wiklander  * -# Call psa_pake_setup() to specify the cipher suite.
117132b31808SJens Wiklander  * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
117232b31808SJens Wiklander  *    setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
117332b31808SJens Wiklander  *    to be called depends on the algorithm in use.
117432b31808SJens Wiklander  *
117532b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
117632b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
117732b31808SJens Wiklander  * for more information.
117832b31808SJens Wiklander  *
117932b31808SJens Wiklander  * A typical sequence of calls to perform a password-authenticated key
118032b31808SJens Wiklander  * exchange:
118132b31808SJens Wiklander  * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
118232b31808SJens Wiklander  *    key share that needs to be sent to the peer.
118332b31808SJens Wiklander  * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
118432b31808SJens Wiklander  *    the key share that was received from the peer.
118532b31808SJens Wiklander  * -# Depending on the algorithm additional calls to psa_pake_output() and
118632b31808SJens Wiklander  *    psa_pake_input() might be necessary.
118732b31808SJens Wiklander  * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
118832b31808SJens Wiklander  *
118932b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
119032b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
119132b31808SJens Wiklander  * for more information.
119232b31808SJens Wiklander  *
119332b31808SJens Wiklander  * If an error occurs at any step after a call to psa_pake_setup(),
119432b31808SJens Wiklander  * the operation will need to be reset by a call to psa_pake_abort(). The
119532b31808SJens Wiklander  * application may call psa_pake_abort() at any time after the operation
119632b31808SJens Wiklander  * has been initialized.
119732b31808SJens Wiklander  *
119832b31808SJens Wiklander  * After a successful call to psa_pake_setup(), the application must
119932b31808SJens Wiklander  * eventually terminate the operation. The following events terminate an
120032b31808SJens Wiklander  * operation:
120132b31808SJens Wiklander  * - A call to psa_pake_abort().
120232b31808SJens Wiklander  * - A successful call to psa_pake_get_implicit_key().
120332b31808SJens Wiklander  *
120432b31808SJens Wiklander  * \param[in,out] operation     The operation object to set up. It must have
120532b31808SJens Wiklander  *                              been initialized but not set up yet.
120632b31808SJens Wiklander  * \param[in] cipher_suite      The cipher suite to use. (A cipher suite fully
120732b31808SJens Wiklander  *                              characterizes a PAKE algorithm and determines
120832b31808SJens Wiklander  *                              the algorithm as well.)
120932b31808SJens Wiklander  *
121032b31808SJens Wiklander  * \retval #PSA_SUCCESS
121132b31808SJens Wiklander  *         Success.
121232b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
121332b31808SJens Wiklander  *         The algorithm in \p cipher_suite is not a PAKE algorithm, or the
121432b31808SJens Wiklander  *         PAKE primitive in \p cipher_suite is not compatible with the
121532b31808SJens Wiklander  *         PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
121632b31808SJens Wiklander  *         or not compatible with the PAKE algorithm and primitive.
121732b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
121832b31808SJens Wiklander  *         The algorithm in \p cipher_suite is not a supported PAKE algorithm,
121932b31808SJens Wiklander  *         or the PAKE primitive in \p cipher_suite is not supported or not
122032b31808SJens Wiklander  *         compatible with the PAKE algorithm, or the hash algorithm in
122132b31808SJens Wiklander  *         \p cipher_suite is not supported or not compatible with the PAKE
122232b31808SJens Wiklander  *         algorithm and primitive.
122332b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
122432b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
122532b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
122632b31808SJens Wiklander  *         The operation state is not valid, or
122732b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
122832b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
122932b31808SJens Wiklander  *         results in this error code.
123032b31808SJens Wiklander  */
123132b31808SJens Wiklander psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
123232b31808SJens Wiklander                             const psa_pake_cipher_suite_t *cipher_suite);
123332b31808SJens Wiklander 
123432b31808SJens Wiklander /** Set the password for a password-authenticated key exchange from key ID.
123532b31808SJens Wiklander  *
123632b31808SJens Wiklander  * Call this function when the password, or a value derived from the password,
123732b31808SJens Wiklander  * is already present in the key store.
123832b31808SJens Wiklander  *
123932b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the password for. It
124032b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
124132b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
124232b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
124332b31808SJens Wiklander  *                              be on operation for which the password hasn't
124432b31808SJens Wiklander  *                              been set yet (psa_pake_set_password_key()
124532b31808SJens Wiklander  *                              hasn't been called yet).
124632b31808SJens Wiklander  * \param password              Identifier of the key holding the password or a
124732b31808SJens Wiklander  *                              value derived from the password (eg. by a
124832b31808SJens Wiklander  *                              memory-hard function).  It must remain valid
124932b31808SJens Wiklander  *                              until the operation terminates. It must be of
125032b31808SJens Wiklander  *                              type #PSA_KEY_TYPE_PASSWORD or
125132b31808SJens Wiklander  *                              #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
125232b31808SJens Wiklander  *                              the usage #PSA_KEY_USAGE_DERIVE.
125332b31808SJens Wiklander  *
125432b31808SJens Wiklander  * \retval #PSA_SUCCESS
125532b31808SJens Wiklander  *         Success.
125632b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_HANDLE
125732b31808SJens Wiklander  *         \p password is not a valid key identifier.
125832b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
125932b31808SJens Wiklander  *         The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
126032b31808SJens Wiklander  *         permit the \p operation's algorithm.
126132b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
126232b31808SJens Wiklander  *         The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
126332b31808SJens Wiklander  *         #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
126432b31808SJens Wiklander  *         the \p operation's cipher suite.
126532b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
126632b31808SJens Wiklander  *         The key type or key size of \p password is not supported with the
126732b31808SJens Wiklander  *         \p operation's cipher suite.
126832b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
126932b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
127032b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
127132b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
127232b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
127332b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
127432b31808SJens Wiklander  *         The operation state is not valid (it must have been set up.), or
127532b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
127632b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
127732b31808SJens Wiklander  *         results in this error code.
127832b31808SJens Wiklander  */
127932b31808SJens Wiklander psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
128032b31808SJens Wiklander                                        mbedtls_svc_key_id_t password);
128132b31808SJens Wiklander 
128232b31808SJens Wiklander /** Set the user ID for a password-authenticated key exchange.
128332b31808SJens Wiklander  *
128432b31808SJens Wiklander  * Call this function to set the user ID. For PAKE algorithms that associate a
128532b31808SJens Wiklander  * user identifier with each side of the session you need to call
128632b31808SJens Wiklander  * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
128732b31808SJens Wiklander  * user identifier with the session, call psa_pake_set_user() only.
128832b31808SJens Wiklander  *
128932b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
129032b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
129132b31808SJens Wiklander  * for more information.
129232b31808SJens Wiklander  *
129332b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the user ID for. It
129432b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
129532b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
129632b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
129732b31808SJens Wiklander  *                              be on operation for which the user ID hasn't
129832b31808SJens Wiklander  *                              been set (psa_pake_set_user() hasn't been
129932b31808SJens Wiklander  *                              called yet).
130032b31808SJens Wiklander  * \param[in] user_id           The user ID to authenticate with.
130132b31808SJens Wiklander  * \param user_id_len           Size of the \p user_id buffer in bytes.
130232b31808SJens Wiklander  *
130332b31808SJens Wiklander  * \retval #PSA_SUCCESS
130432b31808SJens Wiklander  *         Success.
130532b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
130632b31808SJens Wiklander  *         \p user_id is not valid for the \p operation's algorithm and cipher
130732b31808SJens Wiklander  *         suite.
130832b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
130932b31808SJens Wiklander  *         The value of \p user_id is not supported by the implementation.
131032b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
131132b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
131232b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
131332b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
131432b31808SJens Wiklander  *         The operation state is not valid, or
131532b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
131632b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
131732b31808SJens Wiklander  *         results in this error code.
131832b31808SJens Wiklander  */
131932b31808SJens Wiklander psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
132032b31808SJens Wiklander                                const uint8_t *user_id,
132132b31808SJens Wiklander                                size_t user_id_len);
132232b31808SJens Wiklander 
132332b31808SJens Wiklander /** Set the peer ID for a password-authenticated key exchange.
132432b31808SJens Wiklander  *
132532b31808SJens Wiklander  * Call this function in addition to psa_pake_set_user() for PAKE algorithms
132632b31808SJens Wiklander  * that associate a user identifier with each side of the session. For PAKE
132732b31808SJens Wiklander  * algorithms that associate a single user identifier with the session, call
132832b31808SJens Wiklander  * psa_pake_set_user() only.
132932b31808SJens Wiklander  *
133032b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
133132b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
133232b31808SJens Wiklander  * for more information.
133332b31808SJens Wiklander  *
133432b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the peer ID for. It
133532b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
133632b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
133732b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
133832b31808SJens Wiklander  *                              be on operation for which the peer ID hasn't
133932b31808SJens Wiklander  *                              been set (psa_pake_set_peer() hasn't been
134032b31808SJens Wiklander  *                              called yet).
134132b31808SJens Wiklander  * \param[in] peer_id           The peer's ID to authenticate.
134232b31808SJens Wiklander  * \param peer_id_len           Size of the \p peer_id buffer in bytes.
134332b31808SJens Wiklander  *
134432b31808SJens Wiklander  * \retval #PSA_SUCCESS
134532b31808SJens Wiklander  *         Success.
134632b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1347*b0563631STom Van Eyck  *         \p peer_id is not valid for the \p operation's algorithm and cipher
134832b31808SJens Wiklander  *         suite.
134932b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
135032b31808SJens Wiklander  *         The algorithm doesn't associate a second identity with the session.
135132b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
135232b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
135332b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
135432b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
135532b31808SJens Wiklander  *         Calling psa_pake_set_peer() is invalid with the \p operation's
135632b31808SJens Wiklander  *         algorithm, the operation state is not valid, or the library has not
135732b31808SJens Wiklander  *         been previously initialized by psa_crypto_init().
135832b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
135932b31808SJens Wiklander  *         results in this error code.
136032b31808SJens Wiklander  */
136132b31808SJens Wiklander psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
136232b31808SJens Wiklander                                const uint8_t *peer_id,
136332b31808SJens Wiklander                                size_t peer_id_len);
136432b31808SJens Wiklander 
136532b31808SJens Wiklander /** Set the application role for a password-authenticated key exchange.
136632b31808SJens Wiklander  *
136732b31808SJens Wiklander  * Not all PAKE algorithms need to differentiate the communicating entities.
136832b31808SJens Wiklander  * It is optional to call this function for PAKEs that don't require a role
136932b31808SJens Wiklander  * to be specified. For such PAKEs the application role parameter is ignored,
137032b31808SJens Wiklander  * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
137132b31808SJens Wiklander  *
137232b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
137332b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
137432b31808SJens Wiklander  * for more information.
137532b31808SJens Wiklander  *
137632b31808SJens Wiklander  * \param[in,out] operation     The operation object to specify the
137732b31808SJens Wiklander  *                              application's role for. It must have been set up
137832b31808SJens Wiklander  *                              by psa_pake_setup() and not yet in use (neither
137932b31808SJens Wiklander  *                              psa_pake_output() nor psa_pake_input() has been
138032b31808SJens Wiklander  *                              called yet). It must be on operation for which
138132b31808SJens Wiklander  *                              the application's role hasn't been specified
138232b31808SJens Wiklander  *                              (psa_pake_set_role() hasn't been called yet).
138332b31808SJens Wiklander  * \param role                  A value of type ::psa_pake_role_t indicating the
138432b31808SJens Wiklander  *                              application's role in the PAKE the algorithm
138532b31808SJens Wiklander  *                              that is being set up. For more information see
138632b31808SJens Wiklander  *                              the documentation of \c PSA_PAKE_ROLE_XXX
138732b31808SJens Wiklander  *                              constants.
138832b31808SJens Wiklander  *
138932b31808SJens Wiklander  * \retval #PSA_SUCCESS
139032b31808SJens Wiklander  *         Success.
139132b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
139232b31808SJens Wiklander  *         The \p role is not a valid PAKE role in the \p operation’s algorithm.
139332b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
139432b31808SJens Wiklander  *         The \p role for this algorithm is not supported or is not valid.
139532b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
139632b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
139732b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
139832b31808SJens Wiklander  *         The operation state is not valid, or
139932b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
140032b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
140132b31808SJens Wiklander  *         results in this error code.
140232b31808SJens Wiklander  */
140332b31808SJens Wiklander psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
140432b31808SJens Wiklander                                psa_pake_role_t role);
140532b31808SJens Wiklander 
140632b31808SJens Wiklander /** Get output for a step of a password-authenticated key exchange.
140732b31808SJens Wiklander  *
140832b31808SJens Wiklander  * Depending on the algorithm being executed, you might need to call this
140932b31808SJens Wiklander  * function several times or you might not need to call this at all.
141032b31808SJens Wiklander  *
141132b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
141232b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
141332b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
141432b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
141532b31808SJens Wiklander  * information.
141632b31808SJens Wiklander  *
141732b31808SJens Wiklander  * If this function returns an error status, the operation enters an error
141832b31808SJens Wiklander  * state and must be aborted by calling psa_pake_abort().
141932b31808SJens Wiklander  *
142032b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
142132b31808SJens Wiklander  * \param step                 The step of the algorithm for which the output is
142232b31808SJens Wiklander  *                             requested.
142332b31808SJens Wiklander  * \param[out] output          Buffer where the output is to be written in the
142432b31808SJens Wiklander  *                             format appropriate for this \p step. Refer to
142532b31808SJens Wiklander  *                             the documentation of the individual
142632b31808SJens Wiklander  *                             \c PSA_PAKE_STEP_XXX constants for more
142732b31808SJens Wiklander  *                             information.
142832b31808SJens Wiklander  * \param output_size          Size of the \p output buffer in bytes. This must
1429*b0563631STom Van Eyck  *                             be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c
1430*b0563631STom Van Eyck  *                             primitive, \p output_step) where \c alg and
143132b31808SJens Wiklander  *                             \p primitive are the PAKE algorithm and primitive
143232b31808SJens Wiklander  *                             in the operation's cipher suite, and \p step is
143332b31808SJens Wiklander  *                             the output step.
143432b31808SJens Wiklander  *
143532b31808SJens Wiklander  * \param[out] output_length   On success, the number of bytes of the returned
143632b31808SJens Wiklander  *                             output.
143732b31808SJens Wiklander  *
143832b31808SJens Wiklander  * \retval #PSA_SUCCESS
143932b31808SJens Wiklander  *         Success.
144032b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
144132b31808SJens Wiklander  *         The size of the \p output buffer is too small.
144232b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
144332b31808SJens Wiklander  *         \p step is not compatible with the operation's algorithm.
144432b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
144532b31808SJens Wiklander  *         \p step is not supported with the operation's algorithm.
144632b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
144732b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
144832b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
144932b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
145032b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
145132b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
145232b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
145332b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
145432b31808SJens Wiklander  *         The operation state is not valid (it must be active, and fully set
145532b31808SJens Wiklander  *         up, and this call must conform to the algorithm's requirements
145632b31808SJens Wiklander  *         for ordering of input and output steps), or
145732b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
145832b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
145932b31808SJens Wiklander  *         results in this error code.
146032b31808SJens Wiklander  */
146132b31808SJens Wiklander psa_status_t psa_pake_output(psa_pake_operation_t *operation,
146232b31808SJens Wiklander                              psa_pake_step_t step,
146332b31808SJens Wiklander                              uint8_t *output,
146432b31808SJens Wiklander                              size_t output_size,
146532b31808SJens Wiklander                              size_t *output_length);
146632b31808SJens Wiklander 
146732b31808SJens Wiklander /** Provide input for a step of a password-authenticated key exchange.
146832b31808SJens Wiklander  *
146932b31808SJens Wiklander  * Depending on the algorithm being executed, you might need to call this
147032b31808SJens Wiklander  * function several times or you might not need to call this at all.
147132b31808SJens Wiklander  *
147232b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
147332b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
147432b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
147532b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
147632b31808SJens Wiklander  * information.
147732b31808SJens Wiklander  *
147832b31808SJens Wiklander  * If this function returns an error status, the operation enters an error
147932b31808SJens Wiklander  * state and must be aborted by calling psa_pake_abort().
148032b31808SJens Wiklander  *
148132b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
148232b31808SJens Wiklander  * \param step                 The step for which the input is provided.
148332b31808SJens Wiklander  * \param[in] input            Buffer containing the input in the format
148432b31808SJens Wiklander  *                             appropriate for this \p step. Refer to the
148532b31808SJens Wiklander  *                             documentation of the individual
148632b31808SJens Wiklander  *                             \c PSA_PAKE_STEP_XXX constants for more
148732b31808SJens Wiklander  *                             information.
148832b31808SJens Wiklander  * \param input_length         Size of the \p input buffer in bytes.
148932b31808SJens Wiklander  *
149032b31808SJens Wiklander  * \retval #PSA_SUCCESS
149132b31808SJens Wiklander  *         Success.
149232b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_SIGNATURE
149332b31808SJens Wiklander  *         The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
149432b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1495*b0563631STom Van Eyck  *         \p input_length is not compatible with the \p operation’s algorithm,
1496*b0563631STom Van Eyck  *         or the \p input is not valid for the \p operation's algorithm,
1497*b0563631STom Van Eyck  *         cipher suite or \p step.
149832b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
149932b31808SJens Wiklander  *         \p step p is not supported with the \p operation's algorithm, or the
150032b31808SJens Wiklander  *         \p input is not supported for the \p operation's algorithm, cipher
150132b31808SJens Wiklander  *         suite or \p step.
150232b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
150332b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
150432b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
150532b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
150632b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
150732b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
150832b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
150932b31808SJens Wiklander  *         The operation state is not valid (it must be active, and fully set
151032b31808SJens Wiklander  *         up, and this call must conform to the algorithm's requirements
151132b31808SJens Wiklander  *         for ordering of input and output steps), or
151232b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
151332b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
151432b31808SJens Wiklander  *         results in this error code.
151532b31808SJens Wiklander  */
151632b31808SJens Wiklander psa_status_t psa_pake_input(psa_pake_operation_t *operation,
151732b31808SJens Wiklander                             psa_pake_step_t step,
151832b31808SJens Wiklander                             const uint8_t *input,
151932b31808SJens Wiklander                             size_t input_length);
152032b31808SJens Wiklander 
152132b31808SJens Wiklander /** Get implicitly confirmed shared secret from a PAKE.
152232b31808SJens Wiklander  *
152332b31808SJens Wiklander  * At this point there is a cryptographic guarantee that only the authenticated
152432b31808SJens Wiklander  * party who used the same password is able to compute the key. But there is no
152532b31808SJens Wiklander  * guarantee that the peer is the party it claims to be and was able to do so.
152632b31808SJens Wiklander  *
152732b31808SJens Wiklander  * That is, the authentication is only implicit. Since the peer is not
152832b31808SJens Wiklander  * authenticated yet, no action should be taken yet that assumes that the peer
152932b31808SJens Wiklander  * is who it claims to be. For example, do not access restricted files on the
153032b31808SJens Wiklander  * peer's behalf until an explicit authentication has succeeded.
153132b31808SJens Wiklander  *
153232b31808SJens Wiklander  * This function can be called after the key exchange phase of the operation
153332b31808SJens Wiklander  * has completed. It imports the shared secret output of the PAKE into the
153432b31808SJens Wiklander  * provided derivation operation. The input step
153532b31808SJens Wiklander  * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
153632b31808SJens Wiklander  * material in the key derivation operation.
153732b31808SJens Wiklander  *
153832b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
153932b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
154032b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
154132b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
154232b31808SJens Wiklander  * information.
154332b31808SJens Wiklander  *
154432b31808SJens Wiklander  * When this function returns successfully, \p operation becomes inactive.
154532b31808SJens Wiklander  * If this function returns an error status, both \p operation
1546*b0563631STom Van Eyck  * and \c key_derivation operations enter an error state and must be aborted by
154732b31808SJens Wiklander  * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
154832b31808SJens Wiklander  *
154932b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
155032b31808SJens Wiklander  * \param[out] output          A key derivation operation that is ready
155132b31808SJens Wiklander  *                             for an input step of type
155232b31808SJens Wiklander  *                             #PSA_KEY_DERIVATION_INPUT_SECRET.
155332b31808SJens Wiklander  *
155432b31808SJens Wiklander  * \retval #PSA_SUCCESS
155532b31808SJens Wiklander  *         Success.
155632b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
155732b31808SJens Wiklander  *         #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
155832b31808SJens Wiklander  *         algorithm in the \p output key derivation operation.
155932b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
156032b31808SJens Wiklander  *         Input from a PAKE is not supported by the algorithm in the \p output
156132b31808SJens Wiklander  *         key derivation operation.
156232b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
156332b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
156432b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
156532b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
156632b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
156732b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
156832b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
156932b31808SJens Wiklander  *         The PAKE operation state is not valid (it must be active, but beyond
157032b31808SJens Wiklander  *         that validity is specific to the algorithm), or
157132b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init(),
157232b31808SJens Wiklander  *         or the state of \p output is not valid for
157332b31808SJens Wiklander  *         the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
157432b31808SJens Wiklander  *         step is out of order or the application has done this step already
157532b31808SJens Wiklander  *         and it may not be repeated.
157632b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
157732b31808SJens Wiklander  *         results in this error code.
157832b31808SJens Wiklander  */
157932b31808SJens Wiklander psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
158032b31808SJens Wiklander                                        psa_key_derivation_operation_t *output);
158132b31808SJens Wiklander 
158232b31808SJens Wiklander /** Abort a PAKE operation.
158332b31808SJens Wiklander  *
158432b31808SJens Wiklander  * Aborting an operation frees all associated resources except for the \c
158532b31808SJens Wiklander  * operation structure itself. Once aborted, the operation object can be reused
158632b31808SJens Wiklander  * for another operation by calling psa_pake_setup() again.
158732b31808SJens Wiklander  *
158832b31808SJens Wiklander  * This function may be called at any time after the operation
158932b31808SJens Wiklander  * object has been initialized as described in #psa_pake_operation_t.
159032b31808SJens Wiklander  *
159132b31808SJens Wiklander  * In particular, calling psa_pake_abort() after the operation has been
159232b31808SJens Wiklander  * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
159332b31808SJens Wiklander  * is safe and has no effect.
159432b31808SJens Wiklander  *
159532b31808SJens Wiklander  * \param[in,out] operation    The operation to abort.
159632b31808SJens Wiklander  *
159732b31808SJens Wiklander  * \retval #PSA_SUCCESS
159832b31808SJens Wiklander  *         Success.
159932b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
160032b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
160132b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
160232b31808SJens Wiklander  *         The library has not been previously initialized by psa_crypto_init().
160332b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
160432b31808SJens Wiklander  *         results in this error code.
160532b31808SJens Wiklander  */
160632b31808SJens Wiklander psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
160732b31808SJens Wiklander 
160832b31808SJens Wiklander /**@}*/
160932b31808SJens Wiklander 
161032b31808SJens Wiklander /** A sufficient output buffer size for psa_pake_output().
161132b31808SJens Wiklander  *
161232b31808SJens Wiklander  * If the size of the output buffer is at least this large, it is guaranteed
161332b31808SJens Wiklander  * that psa_pake_output() will not fail due to an insufficient output buffer
161432b31808SJens Wiklander  * size. The actual size of the output might be smaller in any given call.
161532b31808SJens Wiklander  *
161632b31808SJens Wiklander  * See also #PSA_PAKE_OUTPUT_MAX_SIZE
161732b31808SJens Wiklander  *
161832b31808SJens Wiklander  * \param alg           A PAKE algorithm (\c PSA_ALG_XXX value such that
161932b31808SJens Wiklander  *                      #PSA_ALG_IS_PAKE(\p alg) is true).
162032b31808SJens Wiklander  * \param primitive     A primitive of type ::psa_pake_primitive_t that is
162132b31808SJens Wiklander  *                      compatible with algorithm \p alg.
162232b31808SJens Wiklander  * \param output_step   A value of type ::psa_pake_step_t that is valid for the
162332b31808SJens Wiklander  *                      algorithm \p alg.
162432b31808SJens Wiklander  * \return              A sufficient output buffer size for the specified
162532b31808SJens Wiklander  *                      PAKE algorithm, primitive, and output step. If the
162632b31808SJens Wiklander  *                      PAKE algorithm, primitive, or output step is not
162732b31808SJens Wiklander  *                      recognized, or the parameters are incompatible,
162832b31808SJens Wiklander  *                      return 0.
162932b31808SJens Wiklander  */
163032b31808SJens Wiklander #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step)               \
163132b31808SJens Wiklander     (alg == PSA_ALG_JPAKE &&                                           \
163232b31808SJens Wiklander      primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,      \
163332b31808SJens Wiklander                                      PSA_ECC_FAMILY_SECP_R1, 256) ?    \
163432b31808SJens Wiklander      (                                                                 \
163532b31808SJens Wiklander          output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 :                   \
163632b31808SJens Wiklander          output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 :                   \
163732b31808SJens Wiklander          32                                                              \
163832b31808SJens Wiklander      ) :                                                               \
163932b31808SJens Wiklander      0)
164032b31808SJens Wiklander 
164132b31808SJens Wiklander /** A sufficient input buffer size for psa_pake_input().
164232b31808SJens Wiklander  *
164332b31808SJens Wiklander  * The value returned by this macro is guaranteed to be large enough for any
164432b31808SJens Wiklander  * valid input to psa_pake_input() in an operation with the specified
164532b31808SJens Wiklander  * parameters.
164632b31808SJens Wiklander  *
164732b31808SJens Wiklander  * See also #PSA_PAKE_INPUT_MAX_SIZE
164832b31808SJens Wiklander  *
164932b31808SJens Wiklander  * \param alg           A PAKE algorithm (\c PSA_ALG_XXX value such that
165032b31808SJens Wiklander  *                      #PSA_ALG_IS_PAKE(\p alg) is true).
165132b31808SJens Wiklander  * \param primitive     A primitive of type ::psa_pake_primitive_t that is
165232b31808SJens Wiklander  *                      compatible with algorithm \p alg.
165332b31808SJens Wiklander  * \param input_step    A value of type ::psa_pake_step_t that is valid for the
165432b31808SJens Wiklander  *                      algorithm \p alg.
165532b31808SJens Wiklander  * \return              A sufficient input buffer size for the specified
165632b31808SJens Wiklander  *                      input, cipher suite and algorithm. If the cipher suite,
165732b31808SJens Wiklander  *                      the input type or PAKE algorithm is not recognized, or
165832b31808SJens Wiklander  *                      the parameters are incompatible, return 0.
165932b31808SJens Wiklander  */
166032b31808SJens Wiklander #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step)                 \
166132b31808SJens Wiklander     (alg == PSA_ALG_JPAKE &&                                           \
166232b31808SJens Wiklander      primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,      \
166332b31808SJens Wiklander                                      PSA_ECC_FAMILY_SECP_R1, 256) ?    \
166432b31808SJens Wiklander      (                                                                 \
166532b31808SJens Wiklander          input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 :                    \
166632b31808SJens Wiklander          input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 :                    \
166732b31808SJens Wiklander          32                                                              \
166832b31808SJens Wiklander      ) :                                                               \
166932b31808SJens Wiklander      0)
167032b31808SJens Wiklander 
167132b31808SJens Wiklander /** Output buffer size for psa_pake_output() for any of the supported PAKE
167232b31808SJens Wiklander  * algorithm and primitive suites and output step.
167332b31808SJens Wiklander  *
167432b31808SJens Wiklander  * This macro must expand to a compile-time constant integer.
167532b31808SJens Wiklander  *
1676*b0563631STom Van Eyck  * The value of this macro must be at least as large as the largest value
1677*b0563631STom Van Eyck  * returned by PSA_PAKE_OUTPUT_SIZE()
1678*b0563631STom Van Eyck  *
1679*b0563631STom Van Eyck  * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).
168032b31808SJens Wiklander  */
168132b31808SJens Wiklander #define PSA_PAKE_OUTPUT_MAX_SIZE 65
168232b31808SJens Wiklander 
168332b31808SJens Wiklander /** Input buffer size for psa_pake_input() for any of the supported PAKE
168432b31808SJens Wiklander  * algorithm and primitive suites and input step.
168532b31808SJens Wiklander  *
168632b31808SJens Wiklander  * This macro must expand to a compile-time constant integer.
168732b31808SJens Wiklander  *
1688*b0563631STom Van Eyck  * The value of this macro must be at least as large as the largest value
1689*b0563631STom Van Eyck  * returned by PSA_PAKE_INPUT_SIZE()
1690*b0563631STom Van Eyck  *
1691*b0563631STom Van Eyck  * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).
169232b31808SJens Wiklander  */
169332b31808SJens Wiklander #define PSA_PAKE_INPUT_MAX_SIZE 65
169432b31808SJens Wiklander 
169532b31808SJens Wiklander /** Returns a suitable initializer for a PAKE cipher suite object of type
169632b31808SJens Wiklander  * psa_pake_cipher_suite_t.
169732b31808SJens Wiklander  */
169832b31808SJens Wiklander #define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
169932b31808SJens Wiklander 
170032b31808SJens Wiklander /** Returns a suitable initializer for a PAKE operation object of type
170132b31808SJens Wiklander  * psa_pake_operation_t.
170232b31808SJens Wiklander  */
1703*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1704*b0563631STom Van Eyck #define PSA_PAKE_OPERATION_INIT { 0 }
1705*b0563631STom Van Eyck #else
1706*b0563631STom Van Eyck #define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \
170732b31808SJens Wiklander                                   { 0 }, { { 0 } } }
1708*b0563631STom Van Eyck #endif
170932b31808SJens Wiklander 
171032b31808SJens Wiklander struct psa_pake_cipher_suite_s {
171132b31808SJens Wiklander     psa_algorithm_t algorithm;
171232b31808SJens Wiklander     psa_pake_primitive_type_t type;
171332b31808SJens Wiklander     psa_pake_family_t family;
171432b31808SJens Wiklander     uint16_t  bits;
171532b31808SJens Wiklander     psa_algorithm_t hash;
171632b31808SJens Wiklander };
171732b31808SJens Wiklander 
171832b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_algorithm(
171932b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
172032b31808SJens Wiklander {
172132b31808SJens Wiklander     return cipher_suite->algorithm;
172232b31808SJens Wiklander }
172332b31808SJens Wiklander 
172432b31808SJens Wiklander static inline void psa_pake_cs_set_algorithm(
172532b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite,
172632b31808SJens Wiklander     psa_algorithm_t algorithm)
172732b31808SJens Wiklander {
172832b31808SJens Wiklander     if (!PSA_ALG_IS_PAKE(algorithm)) {
172932b31808SJens Wiklander         cipher_suite->algorithm = 0;
173032b31808SJens Wiklander     } else {
173132b31808SJens Wiklander         cipher_suite->algorithm = algorithm;
173232b31808SJens Wiklander     }
173332b31808SJens Wiklander }
173432b31808SJens Wiklander 
173532b31808SJens Wiklander static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
173632b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
173732b31808SJens Wiklander {
173832b31808SJens Wiklander     return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
173932b31808SJens Wiklander                               cipher_suite->bits);
174032b31808SJens Wiklander }
174132b31808SJens Wiklander 
174232b31808SJens Wiklander static inline void psa_pake_cs_set_primitive(
174332b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite,
174432b31808SJens Wiklander     psa_pake_primitive_t primitive)
174532b31808SJens Wiklander {
174632b31808SJens Wiklander     cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
174732b31808SJens Wiklander     cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
174832b31808SJens Wiklander     cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
174932b31808SJens Wiklander }
175032b31808SJens Wiklander 
175132b31808SJens Wiklander static inline psa_pake_family_t psa_pake_cs_get_family(
175232b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
175332b31808SJens Wiklander {
175432b31808SJens Wiklander     return cipher_suite->family;
175532b31808SJens Wiklander }
175632b31808SJens Wiklander 
175732b31808SJens Wiklander static inline uint16_t psa_pake_cs_get_bits(
175832b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
175932b31808SJens Wiklander {
176032b31808SJens Wiklander     return cipher_suite->bits;
176132b31808SJens Wiklander }
176232b31808SJens Wiklander 
176332b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_hash(
176432b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
176532b31808SJens Wiklander {
176632b31808SJens Wiklander     return cipher_suite->hash;
176732b31808SJens Wiklander }
176832b31808SJens Wiklander 
176932b31808SJens Wiklander static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
177032b31808SJens Wiklander                                         psa_algorithm_t hash)
177132b31808SJens Wiklander {
177232b31808SJens Wiklander     if (!PSA_ALG_IS_HASH(hash)) {
177332b31808SJens Wiklander         cipher_suite->hash = 0;
177432b31808SJens Wiklander     } else {
177532b31808SJens Wiklander         cipher_suite->hash = hash;
177632b31808SJens Wiklander     }
177732b31808SJens Wiklander }
177832b31808SJens Wiklander 
177932b31808SJens Wiklander struct psa_crypto_driver_pake_inputs_s {
178032b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(password);
178132b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(password_len);
178232b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(user);
178332b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(user_len);
178432b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(peer);
178532b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(peer_len);
178632b31808SJens Wiklander     psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
178732b31808SJens Wiklander     psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
178832b31808SJens Wiklander };
178932b31808SJens Wiklander 
179032b31808SJens Wiklander typedef enum psa_crypto_driver_pake_step {
179132b31808SJens Wiklander     PSA_JPAKE_STEP_INVALID        = 0,  /* Invalid step */
179232b31808SJens Wiklander     PSA_JPAKE_X1_STEP_KEY_SHARE   = 1,  /* Round 1: input/output key share (for ephemeral private key X1).*/
179332b31808SJens Wiklander     PSA_JPAKE_X1_STEP_ZK_PUBLIC   = 2,  /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
179432b31808SJens Wiklander     PSA_JPAKE_X1_STEP_ZK_PROOF    = 3,  /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
179532b31808SJens Wiklander     PSA_JPAKE_X2_STEP_KEY_SHARE   = 4,  /* Round 1: input/output key share (for ephemeral private key X2).*/
179632b31808SJens Wiklander     PSA_JPAKE_X2_STEP_ZK_PUBLIC   = 5,  /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
179732b31808SJens Wiklander     PSA_JPAKE_X2_STEP_ZK_PROOF    = 6,  /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
179832b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_KEY_SHARE  = 7,  /* Round 2: output X2S key (our key) */
179932b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_ZK_PUBLIC  = 8,  /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
180032b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_ZK_PROOF   = 9,  /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
180132b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_KEY_SHARE  = 10, /* Round 2: input X4S key (from peer) */
180232b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_ZK_PUBLIC  = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
180332b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_ZK_PROOF   = 12  /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
180432b31808SJens Wiklander } psa_crypto_driver_pake_step_t;
180532b31808SJens Wiklander 
1806*b0563631STom Van Eyck typedef enum psa_jpake_round {
1807*b0563631STom Van Eyck     PSA_JPAKE_FIRST = 0,
1808*b0563631STom Van Eyck     PSA_JPAKE_SECOND = 1,
1809*b0563631STom Van Eyck     PSA_JPAKE_FINISHED = 2
1810*b0563631STom Van Eyck } psa_jpake_round_t;
1811*b0563631STom Van Eyck 
1812*b0563631STom Van Eyck typedef enum psa_jpake_io_mode {
1813*b0563631STom Van Eyck     PSA_JPAKE_INPUT = 0,
1814*b0563631STom Van Eyck     PSA_JPAKE_OUTPUT = 1
1815*b0563631STom Van Eyck } psa_jpake_io_mode_t;
181632b31808SJens Wiklander 
181732b31808SJens Wiklander struct psa_jpake_computation_stage_s {
1818*b0563631STom Van Eyck     /* The J-PAKE round we are currently on */
1819*b0563631STom Van Eyck     psa_jpake_round_t MBEDTLS_PRIVATE(round);
1820*b0563631STom Van Eyck     /* The 'mode' we are currently in (inputting or outputting) */
1821*b0563631STom Van Eyck     psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);
1822*b0563631STom Van Eyck     /* The number of completed inputs so far this round */
1823*b0563631STom Van Eyck     uint8_t MBEDTLS_PRIVATE(inputs);
1824*b0563631STom Van Eyck     /* The number of completed outputs so far this round */
1825*b0563631STom Van Eyck     uint8_t MBEDTLS_PRIVATE(outputs);
1826*b0563631STom Van Eyck     /* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */
1827*b0563631STom Van Eyck     psa_pake_step_t MBEDTLS_PRIVATE(step);
182832b31808SJens Wiklander };
182932b31808SJens Wiklander 
1830*b0563631STom Van Eyck #define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1831*b0563631STom Van Eyck                                           ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1832*b0563631STom Van Eyck #define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \
1833*b0563631STom Van Eyck                                            ((round) == PSA_JPAKE_FIRST ? 2 : 1))
1834*b0563631STom Van Eyck 
183532b31808SJens Wiklander struct psa_pake_operation_s {
1836*b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
1837*b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
1838*b0563631STom Van Eyck #else
183932b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
184032b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
184132b31808SJens Wiklander      * drivers halfway through the operation is not supported.
184232b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
184332b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
184432b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
184532b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
184632b31808SJens Wiklander     /* Algorithm of the PAKE operation */
184732b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg);
1848*b0563631STom Van Eyck     /* A primitive of type compatible with algorithm */
1849*b0563631STom Van Eyck     psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);
185032b31808SJens Wiklander     /* Stage of the PAKE operation: waiting for the setup, collecting inputs
185132b31808SJens Wiklander      * or computing. */
185232b31808SJens Wiklander     uint8_t MBEDTLS_PRIVATE(stage);
185332b31808SJens Wiklander     /* Holds computation stage of the PAKE algorithms. */
185432b31808SJens Wiklander     union {
185532b31808SJens Wiklander         uint8_t MBEDTLS_PRIVATE(dummy);
185632b31808SJens Wiklander #if defined(PSA_WANT_ALG_JPAKE)
185732b31808SJens Wiklander         psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
185832b31808SJens Wiklander #endif
185932b31808SJens Wiklander     } MBEDTLS_PRIVATE(computation_stage);
186032b31808SJens Wiklander     union {
186132b31808SJens Wiklander         psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
186232b31808SJens Wiklander         psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
186332b31808SJens Wiklander     } MBEDTLS_PRIVATE(data);
1864*b0563631STom Van Eyck #endif
186532b31808SJens Wiklander };
186632b31808SJens Wiklander 
186732b31808SJens Wiklander static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
186832b31808SJens Wiklander {
186932b31808SJens Wiklander     const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
187032b31808SJens Wiklander     return v;
187132b31808SJens Wiklander }
187232b31808SJens Wiklander 
187332b31808SJens Wiklander static inline struct psa_pake_operation_s psa_pake_operation_init(void)
187432b31808SJens Wiklander {
187532b31808SJens Wiklander     const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
187632b31808SJens Wiklander     return v;
187732b31808SJens Wiklander }
187832b31808SJens Wiklander 
187932b31808SJens Wiklander #ifdef __cplusplus
188032b31808SJens Wiklander }
188132b31808SJens Wiklander #endif
188232b31808SJens Wiklander 
188332b31808SJens Wiklander #endif /* PSA_CRYPTO_EXTRA_H */
1884