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