Path: blob/master/thirdparty/mbedtls/include/psa/crypto_extra.h
21733 views
/**1* \file psa/crypto_extra.h2*3* \brief PSA cryptography module: Mbed TLS vendor extensions4*5* \note This file may not be included directly. Applications must6* include psa/crypto.h.7*8* This file is reserved for vendor-specific definitions.9*/10/*11* Copyright The Mbed TLS Contributors12* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later13*/1415#ifndef PSA_CRYPTO_EXTRA_H16#define PSA_CRYPTO_EXTRA_H17#include "mbedtls/private_access.h"1819#include "crypto_types.h"20#include "crypto_compat.h"2122#ifdef __cplusplus23extern "C" {24#endif2526/* UID for secure storage seed */27#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF522829/* See mbedtls_config.h for definition */30#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)31#define MBEDTLS_PSA_KEY_SLOT_COUNT 3232#endif3334/* If the size of static key slots is not explicitly defined by the user, then35* set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE and36* PSA_CIPHER_MAX_KEY_LENGTH.37* See mbedtls_config.h for the definition. */38#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)39#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE \40((PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > PSA_CIPHER_MAX_KEY_LENGTH) ? \41PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE : PSA_CIPHER_MAX_KEY_LENGTH)42#endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/4344/** \addtogroup attributes45* @{46*/4748/** \brief Declare the enrollment algorithm for a key.49*50* An operation on a key may indifferently use the algorithm set with51* psa_set_key_algorithm() or with this function.52*53* \param[out] attributes The attribute structure to write to.54* \param alg2 A second algorithm that the key may be used55* for, in addition to the algorithm set with56* psa_set_key_algorithm().57*58* \warning Setting an enrollment algorithm is not recommended, because59* using the same key with different algorithms can allow some60* attacks based on arithmetic relations between different61* computations made with the same key, or can escalate harmless62* side channels into exploitable ones. Use this function only63* if it is necessary to support a protocol for which it has been64* verified that the usage of the key with multiple algorithms65* is safe.66*/67static inline void psa_set_key_enrollment_algorithm(68psa_key_attributes_t *attributes,69psa_algorithm_t alg2)70{71attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;72}7374/** Retrieve the enrollment algorithm policy from key attributes.75*76* \param[in] attributes The key attribute structure to query.77*78* \return The enrollment algorithm stored in the attribute structure.79*/80static inline psa_algorithm_t psa_get_key_enrollment_algorithm(81const psa_key_attributes_t *attributes)82{83return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);84}8586#if defined(MBEDTLS_PSA_CRYPTO_SE_C)8788/** Retrieve the slot number where a key is stored.89*90* A slot number is only defined for keys that are stored in a secure91* element.92*93* This information is only useful if the secure element is not entirely94* managed through the PSA Cryptography API. It is up to the secure95* element driver to decide how PSA slot numbers map to any other interface96* that the secure element may have.97*98* \param[in] attributes The key attribute structure to query.99* \param[out] slot_number On success, the slot number containing the key.100*101* \retval #PSA_SUCCESS102* The key is located in a secure element, and \p *slot_number103* indicates the slot number that contains it.104* \retval #PSA_ERROR_NOT_PERMITTED105* The caller is not permitted to query the slot number.106* Mbed TLS currently does not return this error.107* \retval #PSA_ERROR_INVALID_ARGUMENT108* The key is not located in a secure element.109*/110psa_status_t psa_get_key_slot_number(111const psa_key_attributes_t *attributes,112psa_key_slot_number_t *slot_number);113114/** Choose the slot number where a key is stored.115*116* This function declares a slot number in the specified attribute117* structure.118*119* A slot number is only meaningful for keys that are stored in a secure120* element. It is up to the secure element driver to decide how PSA slot121* numbers map to any other interface that the secure element may have.122*123* \note Setting a slot number in key attributes for a key creation can124* cause the following errors when creating the key:125* - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does126* not support choosing a specific slot number.127* - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to128* choose slot numbers in general or to choose this specific slot.129* - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not130* valid in general or not valid for this specific key.131* - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the132* selected slot.133*134* \param[out] attributes The attribute structure to write to.135* \param slot_number The slot number to set.136*/137static inline void psa_set_key_slot_number(138psa_key_attributes_t *attributes,139psa_key_slot_number_t slot_number)140{141attributes->MBEDTLS_PRIVATE(has_slot_number) = 1;142attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;143}144145/** Remove the slot number attribute from a key attribute structure.146*147* This function undoes the action of psa_set_key_slot_number().148*149* \param[out] attributes The attribute structure to write to.150*/151static inline void psa_clear_key_slot_number(152psa_key_attributes_t *attributes)153{154attributes->MBEDTLS_PRIVATE(has_slot_number) = 0;155}156157/** Register a key that is already present in a secure element.158*159* The key must be located in a secure element designated by the160* lifetime field in \p attributes, in the slot set with161* psa_set_key_slot_number() in the attribute structure.162* This function makes the key available through the key identifier163* specified in \p attributes.164*165* \param[in] attributes The attributes of the existing key.166* - The lifetime must be a persistent lifetime167* in a secure element. Volatile lifetimes are168* not currently supported.169* - The key identifier must be in the valid170* range for persistent keys.171* - The key type and size must be specified and172* must be consistent with the key material173* in the secure element.174*175* \retval #PSA_SUCCESS176* The key was successfully registered.177* Note that depending on the design of the driver, this may or may178* not guarantee that a key actually exists in the designated slot179* and is compatible with the specified attributes.180* \retval #PSA_ERROR_ALREADY_EXISTS181* There is already a key with the identifier specified in182* \p attributes.183* \retval #PSA_ERROR_NOT_SUPPORTED184* The secure element driver for the specified lifetime does not185* support registering a key.186* \retval #PSA_ERROR_INVALID_ARGUMENT187* The identifier in \p attributes is invalid, namely the identifier is188* not in the user range, or189* \p attributes specifies a lifetime which is not located190* in a secure element, or no slot number is specified in \p attributes,191* or the specified slot number is not valid.192* \retval #PSA_ERROR_NOT_PERMITTED193* The caller is not authorized to register the specified key slot.194* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription195* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription196* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription197* \retval #PSA_ERROR_DATA_INVALID \emptydescription198* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription199* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription200* \retval #PSA_ERROR_BAD_STATE201* The library has not been previously initialized by psa_crypto_init().202* It is implementation-dependent whether a failure to initialize203* results in this error code.204*/205psa_status_t mbedtls_psa_register_se_key(206const psa_key_attributes_t *attributes);207208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */209210/**@}*/211212/**213* \brief Library deinitialization.214*215* This function clears all data associated with the PSA layer,216* including the whole key store.217* This function is not thread safe, it wipes every key slot regardless of218* state and reader count. It should only be called when no slot is in use.219*220* This is an Mbed TLS extension.221*/222void mbedtls_psa_crypto_free(void);223224/** \brief Statistics about225* resource consumption related to the PSA keystore.226*227* \note The content of this structure is not part of the stable API and ABI228* of Mbed TLS and may change arbitrarily from version to version.229*/230typedef struct mbedtls_psa_stats_s {231/** Number of slots containing key material for a volatile key. */232size_t MBEDTLS_PRIVATE(volatile_slots);233/** Number of slots containing key material for a key which is in234* internal persistent storage. */235size_t MBEDTLS_PRIVATE(persistent_slots);236/** Number of slots containing a reference to a key in a237* secure element. */238size_t MBEDTLS_PRIVATE(external_slots);239/** Number of slots which are occupied, but do not contain240* key material yet. */241size_t MBEDTLS_PRIVATE(half_filled_slots);242/** Number of slots that contain cache data. */243size_t MBEDTLS_PRIVATE(cache_slots);244/** Number of slots that are not used for anything. */245size_t MBEDTLS_PRIVATE(empty_slots);246/** Number of slots that are locked. */247size_t MBEDTLS_PRIVATE(locked_slots);248/** Largest key id value among open keys in internal persistent storage. */249psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);250/** Largest key id value among open keys in secure elements. */251psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);252} mbedtls_psa_stats_t;253254/** \brief Get statistics about255* resource consumption related to the PSA keystore.256*257* \note When Mbed TLS is built as part of a service, with isolation258* between the application and the keystore, the service may or259* may not expose this function.260*/261void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);262263/**264* \brief Inject an initial entropy seed for the random generator into265* secure storage.266*267* This function injects data to be used as a seed for the random generator268* used by the PSA Crypto implementation. On devices that lack a trusted269* entropy source (preferably a hardware random number generator),270* the Mbed PSA Crypto implementation uses this value to seed its271* random generator.272*273* On devices without a trusted entropy source, this function must be274* called exactly once in the lifetime of the device. On devices with275* a trusted entropy source, calling this function is optional.276* In all cases, this function may only be called before calling any277* other function in the PSA Crypto API, including psa_crypto_init().278*279* When this function returns successfully, it populates a file in280* persistent storage. Once the file has been created, this function281* can no longer succeed.282*283* If any error occurs, this function does not change the system state.284* You can call this function again after correcting the reason for the285* error if possible.286*287* \warning This function **can** fail! Callers MUST check the return status.288*289* \warning If you use this function, you should use it as part of a290* factory provisioning process. The value of the injected seed291* is critical to the security of the device. It must be292* *secret*, *unpredictable* and (statistically) *unique per device*.293* You should be generate it randomly using a cryptographically294* secure random generator seeded from trusted entropy sources.295* You should transmit it securely to the device and ensure296* that its value is not leaked or stored anywhere beyond the297* needs of transmitting it from the point of generation to298* the call of this function, and erase all copies of the value299* once this function returns.300*301* This is an Mbed TLS extension.302*303* \note This function is only available on the following platforms:304* * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.305* Note that you must provide compatible implementations of306* mbedtls_nv_seed_read and mbedtls_nv_seed_write.307* * In a client-server integration of PSA Cryptography, on the client side,308* if the server supports this feature.309* \param[in] seed Buffer containing the seed value to inject.310* \param[in] seed_size Size of the \p seed buffer.311* The size of the seed in bytes must be greater312* or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE313* and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM314* in `library/entropy_poll.h` in the Mbed TLS source315* code.316* It must be less or equal to317* #MBEDTLS_ENTROPY_MAX_SEED_SIZE.318*319* \retval #PSA_SUCCESS320* The seed value was injected successfully. The random generator321* of the PSA Crypto implementation is now ready for use.322* You may now call psa_crypto_init() and use the PSA Crypto323* implementation.324* \retval #PSA_ERROR_INVALID_ARGUMENT325* \p seed_size is out of range.326* \retval #PSA_ERROR_STORAGE_FAILURE327* There was a failure reading or writing from storage.328* \retval #PSA_ERROR_NOT_PERMITTED329* The library has already been initialized. It is no longer330* possible to call this function.331*/332psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,333size_t seed_size);334335/** \addtogroup crypto_types336* @{337*/338339/** DSA public key.340*341* The import and export format is the342* representation of the public key `y = g^x mod p` as a big-endian byte343* string. The length of the byte string is the length of the base prime `p`344* in bytes.345*/346#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)347348/** DSA key pair (private and public key).349*350* The import and export format is the351* representation of the private key `x` as a big-endian byte string. The352* length of the byte string is the private key size in bytes (leading zeroes353* are not stripped).354*355* Deterministic DSA key derivation with psa_generate_derived_key follows356* FIPS 186-4 §B.1.2: interpret the byte string as integer357* in big-endian order. Discard it if it is not in the range358* [0, *N* - 2] where *N* is the boundary of the private key domain359* (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,360* or the order of the curve's base point for ECC).361* Add 1 to the resulting integer and use this as the private key *x*.362*363*/364#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)365366/** Whether a key type is a DSA key (pair or public-only). */367#define PSA_KEY_TYPE_IS_DSA(type) \368(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)369370#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)371/** DSA signature with hashing.372*373* This is the signature scheme defined by FIPS 186-4,374* with a random per-message secret number (*k*).375*376* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that377* #PSA_ALG_IS_HASH(\p hash_alg) is true).378* This includes #PSA_ALG_ANY_HASH379* when specifying the algorithm in a usage policy.380*381* \return The corresponding DSA signature algorithm.382* \return Unspecified if \p hash_alg is not a supported383* hash algorithm.384*/385#define PSA_ALG_DSA(hash_alg) \386(PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))387#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)388#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG389/** Deterministic DSA signature with hashing.390*391* This is the deterministic variant defined by RFC 6979 of392* the signature scheme defined by FIPS 186-4.393*394* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that395* #PSA_ALG_IS_HASH(\p hash_alg) is true).396* This includes #PSA_ALG_ANY_HASH397* when specifying the algorithm in a usage policy.398*399* \return The corresponding DSA signature algorithm.400* \return Unspecified if \p hash_alg is not a supported401* hash algorithm.402*/403#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \404(PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))405#define PSA_ALG_IS_DSA(alg) \406(((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \407PSA_ALG_DSA_BASE)408#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \409(((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)410#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \411(PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))412#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \413(PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))414415416/* We need to expand the sample definition of this macro from417* the API definition. */418#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN419#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \420PSA_ALG_IS_DSA(alg)421422/**@}*/423424/** \addtogroup attributes425* @{426*/427428/** PAKE operation stages. */429#define PSA_PAKE_OPERATION_STAGE_SETUP 0430#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1431#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2432433/**@}*/434435436/** \defgroup psa_external_rng External random generator437* @{438*/439440#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)441/** External random generator function, implemented by the platform.442*443* When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,444* this function replaces Mbed TLS's entropy and DRBG modules for all445* random generation triggered via PSA crypto interfaces.446*447* \note This random generator must deliver random numbers with cryptographic448* quality and high performance. It must supply unpredictable numbers449* with a uniform distribution. The implementation of this function450* is responsible for ensuring that the random generator is seeded451* with sufficient entropy. If you have a hardware TRNG which is slow452* or delivers non-uniform output, declare it as an entropy source453* with mbedtls_entropy_add_source() instead of enabling this option.454*455* \param[in,out] context Pointer to the random generator context.456* This is all-bits-zero on the first call457* and preserved between successive calls.458* \param[out] output Output buffer. On success, this buffer459* contains random data with a uniform460* distribution.461* \param output_size The size of the \p output buffer in bytes.462* \param[out] output_length On success, set this value to \p output_size.463*464* \retval #PSA_SUCCESS465* Success. The output buffer contains \p output_size bytes of466* cryptographic-quality random data, and \c *output_length is467* set to \p output_size.468* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY469* The random generator requires extra entropy and there is no470* way to obtain entropy under current environment conditions.471* This error should not happen under normal circumstances since472* this function is responsible for obtaining as much entropy as473* it needs. However implementations of this function may return474* #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain475* entropy without blocking indefinitely.476* \retval #PSA_ERROR_HARDWARE_FAILURE477* A failure of the random generator hardware that isn't covered478* by #PSA_ERROR_INSUFFICIENT_ENTROPY.479*/480psa_status_t mbedtls_psa_external_get_random(481mbedtls_psa_external_random_context_t *context,482uint8_t *output, size_t output_size, size_t *output_length);483#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */484485/**@}*/486487/** \defgroup psa_builtin_keys Built-in keys488* @{489*/490491/** The minimum value for a key identifier that is built into the492* implementation.493*494* The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN495* to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from496* #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect497* with any other set of implementation-chosen key identifiers.498*499* This value is part of the library's API since changing it would invalidate500* the values of built-in key identifiers in applications.501*/502#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)503504/** The maximum value for a key identifier that is built into the505* implementation.506*507* See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.508*/509#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)510511/** A slot number identifying a key in a driver.512*513* Values of this type are used to identify built-in keys.514*/515typedef uint64_t psa_drv_slot_number_t;516517#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)518/** Test whether a key identifier belongs to the builtin key range.519*520* \param key_id Key identifier to test.521*522* \retval 1523* The key identifier is a builtin key identifier.524* \retval 0525* The key identifier is not a builtin key identifier.526*/527static inline int psa_key_id_is_builtin(psa_key_id_t key_id)528{529return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&530(key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);531}532533/** Platform function to obtain the location and slot number of a built-in key.534*535* An application-specific implementation of this function must be provided if536* #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided537* as part of a platform's system image.538*539* #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from540* #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.541*542* In a multi-application configuration543* (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),544* this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)545* is allowed to use the given key.546*547* \param key_id The key ID for which to retrieve the548* location and slot attributes.549* \param[out] lifetime On success, the lifetime associated with the key550* corresponding to \p key_id. Lifetime is a551* combination of which driver contains the key,552* and with what persistence level the key is553* intended to be used. If the platform554* implementation does not contain specific555* information about the intended key persistence556* level, the persistence level may be reported as557* #PSA_KEY_PERSISTENCE_DEFAULT.558* \param[out] slot_number On success, the slot number known to the driver559* registered at the lifetime location reported560* through \p lifetime which corresponds to the561* requested built-in key.562*563* \retval #PSA_SUCCESS564* The requested key identifier designates a built-in key.565* In a multi-application configuration, the requested owner566* is allowed to access it.567* \retval #PSA_ERROR_DOES_NOT_EXIST568* The requested key identifier is not a built-in key which is known569* to this function. If a key exists in the key storage with this570* identifier, the data from the storage will be used.571* \return (any other error)572* Any other error is propagated to the function that requested the key.573* Common errors include:574* - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner575* is not allowed to access it.576*/577psa_status_t mbedtls_psa_platform_get_builtin_key(578mbedtls_svc_key_id_t key_id,579psa_key_lifetime_t *lifetime,580psa_drv_slot_number_t *slot_number);581#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */582583/** @} */584585/** \defgroup psa_crypto_client Functions defined by a client provider586*587* The functions in this group are meant to be implemented by providers of588* the PSA Crypto client interface. They are provided by the library when589* #MBEDTLS_PSA_CRYPTO_C is enabled.590*591* \note All functions in this group are experimental, as using592* alternative client interface providers is experimental.593*594* @{595*/596597/** Check if PSA is capable of handling the specified hash algorithm.598*599* This means that PSA core was built with the corresponding PSA_WANT_ALG_xxx600* set and that psa_crypto_init has already been called.601*602* \note When using the built-in version of the PSA core (i.e.603* #MBEDTLS_PSA_CRYPTO_C is set), for now, this function only checks604* the state of the driver subsystem, not the algorithm.605* This might be improved in the future.606*607* \param hash_alg The hash algorithm.608*609* \return 1 if the PSA can handle \p hash_alg, 0 otherwise.610*/611int psa_can_do_hash(psa_algorithm_t hash_alg);612613/**614* Tell if PSA is ready for this cipher.615*616* \note When using the built-in version of the PSA core (i.e.617* #MBEDTLS_PSA_CRYPTO_C is set), for now, this function only checks618* the state of the driver subsystem, not the key type and algorithm.619* This might be improved in the future.620*621* \param key_type The key type.622* \param cipher_alg The cipher algorithm.623*624* \return 1 if the PSA can handle \p cipher_alg, 0 otherwise.625*/626int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg);627628/**@}*/629630/** \addtogroup crypto_types631* @{632*/633634#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)635636/** Whether the specified algorithm is a password-authenticated key exchange.637*638* \param alg An algorithm identifier (value of type #psa_algorithm_t).639*640* \return 1 if \p alg is a password-authenticated key exchange (PAKE)641* algorithm, 0 otherwise.642* This macro may return either 0 or 1 if \p alg is not a supported643* algorithm identifier.644*/645#define PSA_ALG_IS_PAKE(alg) \646(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)647648/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.649*650* This is J-PAKE as defined by RFC 8236, instantiated with the following651* parameters:652*653* - The group can be either an elliptic curve or defined over a finite field.654* - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the655* J-PAKE algorithm.656* - A cryptographic hash function.657*658* To select these parameters and set up the cipher suite, call these functions659* in any order:660*661* \code662* psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);663* psa_pake_cs_set_primitive(cipher_suite,664* PSA_PAKE_PRIMITIVE(type, family, bits));665* psa_pake_cs_set_hash(cipher_suite, hash);666* \endcode667*668* For more information on how to set a specific curve or field, refer to the669* documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.670*671* After initializing a J-PAKE operation, call672*673* \code674* psa_pake_setup(operation, cipher_suite);675* psa_pake_set_user(operation, ...);676* psa_pake_set_peer(operation, ...);677* psa_pake_set_password_key(operation, ...);678* \endcode679*680* The password is provided as a key. This can be the password text itself,681* in an agreed character encoding, or some value derived from the password682* as required by a higher level protocol.683*684* (The implementation converts the key material to a number as described in685* Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_686* (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here687* \c q is order of the group defined by the primitive set in the cipher suite.688* The \c psa_pake_set_password_key() function returns an error if the result689* of the reduction is 0.)690*691* The key exchange flow for J-PAKE is as follows:692* -# To get the first round data that needs to be sent to the peer, call693* \code694* // Get g1695* psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);696* // Get the ZKP public key for x1697* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);698* // Get the ZKP proof for x1699* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);700* // Get g2701* psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);702* // Get the ZKP public key for x2703* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);704* // Get the ZKP proof for x2705* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);706* \endcode707* -# To provide the first round data received from the peer to the operation,708* call709* \code710* // Set g3711* psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);712* // Set the ZKP public key for x3713* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);714* // Set the ZKP proof for x3715* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);716* // Set g4717* psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);718* // Set the ZKP public key for x4719* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);720* // Set the ZKP proof for x4721* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);722* \endcode723* -# To get the second round data that needs to be sent to the peer, call724* \code725* // Get A726* psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);727* // Get ZKP public key for x2*s728* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);729* // Get ZKP proof for x2*s730* psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);731* \endcode732* -# To provide the second round data received from the peer to the operation,733* call734* \code735* // Set B736* psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);737* // Set ZKP public key for x4*s738* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);739* // Set ZKP proof for x4*s740* psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);741* \endcode742* -# To access the shared secret call743* \code744* // Get Ka=Kb=K745* psa_pake_get_implicit_key()746* \endcode747*748* For more information consult the documentation of the individual749* \c PSA_PAKE_STEP_XXX constants.750*751* At this point there is a cryptographic guarantee that only the authenticated752* party who used the same password is able to compute the key. But there is no753* guarantee that the peer is the party it claims to be and was able to do so.754*755* That is, the authentication is only implicit (the peer is not authenticated756* at this point, and no action should be taken that assume that they are - like757* for example accessing restricted files).758*759* To make the authentication explicit there are various methods, see Section 5760* of RFC 8236 for two examples.761*762* \note The JPAKE implementation has the following limitations:763* - The only supported primitive is ECC on the curve secp256r1, i.e.764* `PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,765* PSA_ECC_FAMILY_SECP_R1, 256)`.766* - The only supported hash algorithm is SHA-256, i.e.767* `PSA_ALG_SHA_256`.768* - When using the built-in implementation, the user ID and the peer ID769* must be `"client"` (6-byte string) and `"server"` (6-byte string),770* or the other way round.771* Third-party drivers may or may not have this limitation.772*773*/774#define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)775776/** @} */777778/** \defgroup pake Password-authenticated key exchange (PAKE)779*780* This is a proposed PAKE interface for the PSA Crypto API. It is not part of781* the official PSA Crypto API yet.782*783* \note The content of this section is not part of the stable API and ABI784* of Mbed TLS and may change arbitrarily from version to version.785* Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and786* #PSA_ALG_JPAKE.787* @{788*/789790/** \brief Encoding of the application role of PAKE791*792* Encodes the application's role in the algorithm is being executed. For more793* information see the documentation of individual \c PSA_PAKE_ROLE_XXX794* constants.795*/796typedef uint8_t psa_pake_role_t;797798/** Encoding of input and output indicators for PAKE.799*800* Some PAKE algorithms need to exchange more data than just a single key share.801* This type is for encoding additional input and output data for such802* algorithms.803*/804typedef uint8_t psa_pake_step_t;805806/** Encoding of the type of the PAKE's primitive.807*808* Values defined by this standard will never be in the range 0x80-0xff.809* Vendors who define additional types must use an encoding in this range.810*811* For more information see the documentation of individual812* \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.813*/814typedef uint8_t psa_pake_primitive_type_t;815816/** \brief Encoding of the family of the primitive associated with the PAKE.817*818* For more information see the documentation of individual819* \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.820*/821typedef uint8_t psa_pake_family_t;822823/** \brief Encoding of the primitive associated with the PAKE.824*825* For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.826*/827typedef uint32_t psa_pake_primitive_t;828829/** A value to indicate no role in a PAKE algorithm.830* This value can be used in a call to psa_pake_set_role() for symmetric PAKE831* algorithms which do not assign roles.832*/833#define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)834835/** The first peer in a balanced PAKE.836*837* Although balanced PAKE algorithms are symmetric, some of them needs an838* ordering of peers for the transcript calculations. If the algorithm does not839* need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are840* accepted.841*/842#define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)843844/** The second peer in a balanced PAKE.845*846* Although balanced PAKE algorithms are symmetric, some of them needs an847* ordering of peers for the transcript calculations. If the algorithm does not848* need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are849* accepted.850*/851#define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)852853/** The client in an augmented PAKE.854*855* Augmented PAKE algorithms need to differentiate between client and server.856*/857#define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)858859/** The server in an augmented PAKE.860*861* Augmented PAKE algorithms need to differentiate between client and server.862*/863#define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)864865/** The PAKE primitive type indicating the use of elliptic curves.866*867* The values of the \c family and \c bits fields of the cipher suite identify a868* specific elliptic curve, using the same mapping that is used for ECC869* (::psa_ecc_family_t) keys.870*871* (Here \c family means the value returned by psa_pake_cs_get_family() and872* \c bits means the value returned by psa_pake_cs_get_bits().)873*874* Input and output during the operation can involve group elements and scalar875* values:876* -# The format for group elements is the same as for public keys on the877* specific curve would be. For more information, consult the documentation of878* psa_export_public_key().879* -# The format for scalars is the same as for private keys on the specific880* curve would be. For more information, consult the documentation of881* psa_export_key().882*/883#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)884885/** The PAKE primitive type indicating the use of Diffie-Hellman groups.886*887* The values of the \c family and \c bits fields of the cipher suite identify888* a specific Diffie-Hellman group, using the same mapping that is used for889* Diffie-Hellman (::psa_dh_family_t) keys.890*891* (Here \c family means the value returned by psa_pake_cs_get_family() and892* \c bits means the value returned by psa_pake_cs_get_bits().)893*894* Input and output during the operation can involve group elements and scalar895* values:896* -# The format for group elements is the same as for public keys on the897* specific group would be. For more information, consult the documentation of898* psa_export_public_key().899* -# The format for scalars is the same as for private keys on the specific900* group would be. For more information, consult the documentation of901* psa_export_key().902*/903#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)904905/** Construct a PAKE primitive from type, family and bit-size.906*907* \param pake_type The type of the primitive908* (value of type ::psa_pake_primitive_type_t).909* \param pake_family The family of the primitive910* (the type and interpretation of this parameter depends911* on \p pake_type, for more information consult the912* documentation of individual ::psa_pake_primitive_type_t913* constants).914* \param pake_bits The bit-size of the primitive915* (Value of type \c size_t. The interpretation916* of this parameter depends on \p pake_family, for more917* information consult the documentation of individual918* ::psa_pake_primitive_type_t constants).919*920* \return The constructed primitive value of type ::psa_pake_primitive_t.921* Return 0 if the requested primitive can't be encoded as922* ::psa_pake_primitive_t.923*/924#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \925((pake_bits & 0xFFFF) != pake_bits) ? 0 : \926((psa_pake_primitive_t) (((pake_type) << 24 | \927(pake_family) << 16) | (pake_bits)))928929/** The key share being sent to or received from the peer.930*931* The format for both input and output at this step is the same as for public932* keys on the group determined by the primitive (::psa_pake_primitive_t) would933* be.934*935* For more information on the format, consult the documentation of936* psa_export_public_key().937*938* For information regarding how the group is determined, consult the939* documentation #PSA_PAKE_PRIMITIVE.940*/941#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)942943/** A Schnorr NIZKP public key.944*945* This is the ephemeral public key in the Schnorr Non-Interactive946* Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).947*948* The format for both input and output at this step is the same as for public949* keys on the group determined by the primitive (::psa_pake_primitive_t) would950* be.951*952* For more information on the format, consult the documentation of953* psa_export_public_key().954*955* For information regarding how the group is determined, consult the956* documentation #PSA_PAKE_PRIMITIVE.957*/958#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)959960/** A Schnorr NIZKP proof.961*962* This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the963* value denoted by the letter 'r' in RFC 8235).964*965* Both for input and output, the value at this step is an integer less than966* the order of the group selected in the cipher suite. The format depends on967* the group as well:968*969* - For Montgomery curves, the encoding is little endian.970* - For everything else the encoding is big endian (see Section 2.3.8 of971* _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).972*973* In both cases leading zeroes are allowed as long as the length in bytes does974* not exceed the byte length of the group order.975*976* For information regarding how the group is determined, consult the977* documentation #PSA_PAKE_PRIMITIVE.978*/979#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)980981/**@}*/982983/** A sufficient output buffer size for psa_pake_output().984*985* If the size of the output buffer is at least this large, it is guaranteed986* that psa_pake_output() will not fail due to an insufficient output buffer987* size. The actual size of the output might be smaller in any given call.988*989* See also #PSA_PAKE_OUTPUT_MAX_SIZE990*991* \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that992* #PSA_ALG_IS_PAKE(\p alg) is true).993* \param primitive A primitive of type ::psa_pake_primitive_t that is994* compatible with algorithm \p alg.995* \param output_step A value of type ::psa_pake_step_t that is valid for the996* algorithm \p alg.997* \return A sufficient output buffer size for the specified998* PAKE algorithm, primitive, and output step. If the999* PAKE algorithm, primitive, or output step is not1000* recognized, or the parameters are incompatible,1001* return 0.1002*/1003#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \1004(alg == PSA_ALG_JPAKE && \1005primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \1006PSA_ECC_FAMILY_SECP_R1, 256) ? \1007( \1008output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \1009output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \101032 \1011) : \10120)10131014/** A sufficient input buffer size for psa_pake_input().1015*1016* The value returned by this macro is guaranteed to be large enough for any1017* valid input to psa_pake_input() in an operation with the specified1018* parameters.1019*1020* See also #PSA_PAKE_INPUT_MAX_SIZE1021*1022* \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that1023* #PSA_ALG_IS_PAKE(\p alg) is true).1024* \param primitive A primitive of type ::psa_pake_primitive_t that is1025* compatible with algorithm \p alg.1026* \param input_step A value of type ::psa_pake_step_t that is valid for the1027* algorithm \p alg.1028* \return A sufficient input buffer size for the specified1029* input, cipher suite and algorithm. If the cipher suite,1030* the input type or PAKE algorithm is not recognized, or1031* the parameters are incompatible, return 0.1032*/1033#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \1034(alg == PSA_ALG_JPAKE && \1035primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \1036PSA_ECC_FAMILY_SECP_R1, 256) ? \1037( \1038input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \1039input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \104032 \1041) : \10420)10431044/** Output buffer size for psa_pake_output() for any of the supported PAKE1045* algorithm and primitive suites and output step.1046*1047* This macro must expand to a compile-time constant integer.1048*1049* The value of this macro must be at least as large as the largest value1050* returned by PSA_PAKE_OUTPUT_SIZE()1051*1052* See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p output_step).1053*/1054#define PSA_PAKE_OUTPUT_MAX_SIZE 6510551056/** Input buffer size for psa_pake_input() for any of the supported PAKE1057* algorithm and primitive suites and input step.1058*1059* This macro must expand to a compile-time constant integer.1060*1061* The value of this macro must be at least as large as the largest value1062* returned by PSA_PAKE_INPUT_SIZE()1063*1064* See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p output_step).1065*/1066#define PSA_PAKE_INPUT_MAX_SIZE 6510671068/** Returns a suitable initializer for a PAKE cipher suite object of type1069* psa_pake_cipher_suite_t.1070*/1071#define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }10721073/** Returns a suitable initializer for a PAKE operation object of type1074* psa_pake_operation_t.1075*/1076#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)1077#define PSA_PAKE_OPERATION_INIT { 0 }1078#else1079#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, 0, PSA_PAKE_OPERATION_STAGE_SETUP, \1080{ 0 }, { { 0 } } }1081#endif10821083struct psa_pake_cipher_suite_s {1084psa_algorithm_t algorithm;1085psa_pake_primitive_type_t type;1086psa_pake_family_t family;1087uint16_t bits;1088psa_algorithm_t hash;1089};10901091struct psa_crypto_driver_pake_inputs_s {1092uint8_t *MBEDTLS_PRIVATE(password);1093size_t MBEDTLS_PRIVATE(password_len);1094uint8_t *MBEDTLS_PRIVATE(user);1095size_t MBEDTLS_PRIVATE(user_len);1096uint8_t *MBEDTLS_PRIVATE(peer);1097size_t MBEDTLS_PRIVATE(peer_len);1098psa_key_attributes_t MBEDTLS_PRIVATE(attributes);1099struct psa_pake_cipher_suite_s MBEDTLS_PRIVATE(cipher_suite);1100};11011102typedef enum psa_crypto_driver_pake_step {1103PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */1104PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/1105PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */1106PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */1107PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/1108PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */1109PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */1110PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */1111PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */1112PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */1113PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */1114PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */1115PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */1116} psa_crypto_driver_pake_step_t;11171118typedef enum psa_jpake_round {1119PSA_JPAKE_FIRST = 0,1120PSA_JPAKE_SECOND = 1,1121PSA_JPAKE_FINISHED = 21122} psa_jpake_round_t;11231124typedef enum psa_jpake_io_mode {1125PSA_JPAKE_INPUT = 0,1126PSA_JPAKE_OUTPUT = 11127} psa_jpake_io_mode_t;11281129struct psa_jpake_computation_stage_s {1130/* The J-PAKE round we are currently on */1131psa_jpake_round_t MBEDTLS_PRIVATE(round);1132/* The 'mode' we are currently in (inputting or outputting) */1133psa_jpake_io_mode_t MBEDTLS_PRIVATE(io_mode);1134/* The number of completed inputs so far this round */1135uint8_t MBEDTLS_PRIVATE(inputs);1136/* The number of completed outputs so far this round */1137uint8_t MBEDTLS_PRIVATE(outputs);1138/* The next expected step (KEY_SHARE, ZK_PUBLIC or ZK_PROOF) */1139psa_pake_step_t MBEDTLS_PRIVATE(step);1140};11411142#define PSA_JPAKE_EXPECTED_INPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \1143((round) == PSA_JPAKE_FIRST ? 2 : 1))1144#define PSA_JPAKE_EXPECTED_OUTPUTS(round) ((round) == PSA_JPAKE_FINISHED ? 0 : \1145((round) == PSA_JPAKE_FIRST ? 2 : 1))11461147struct psa_pake_operation_s {1148#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)1149mbedtls_psa_client_handle_t handle;1150#else1151/** Unique ID indicating which driver got assigned to do the1152* operation. Since driver contexts are driver-specific, swapping1153* drivers halfway through the operation is not supported.1154* ID values are auto-generated in psa_crypto_driver_wrappers.h1155* ID value zero means the context is not valid or not assigned to1156* any driver (i.e. none of the driver contexts are active). */1157unsigned int MBEDTLS_PRIVATE(id);1158/* Algorithm of the PAKE operation */1159psa_algorithm_t MBEDTLS_PRIVATE(alg);1160/* A primitive of type compatible with algorithm */1161psa_pake_primitive_t MBEDTLS_PRIVATE(primitive);1162/* Stage of the PAKE operation: waiting for the setup, collecting inputs1163* or computing. */1164uint8_t MBEDTLS_PRIVATE(stage);1165/* Holds computation stage of the PAKE algorithms. */1166union {1167uint8_t MBEDTLS_PRIVATE(dummy);1168#if defined(PSA_WANT_ALG_JPAKE)1169struct psa_jpake_computation_stage_s MBEDTLS_PRIVATE(jpake);1170#endif1171} MBEDTLS_PRIVATE(computation_stage);1172union {1173psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);1174struct psa_crypto_driver_pake_inputs_s MBEDTLS_PRIVATE(inputs);1175} MBEDTLS_PRIVATE(data);1176#endif1177};11781179/** \addtogroup pake1180* @{1181*/11821183/** The type of the data structure for PAKE cipher suites.1184*1185* This is an implementation-defined \c struct. Applications should not1186* make any assumptions about the content of this structure.1187* Implementation details can change in future versions without notice.1188*/1189typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;11901191/** Return an initial value for a PAKE cipher suite object.1192*/1193#if !(defined(__cplusplus) && defined(_MSC_VER))1194static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);1195#endif11961197/** Retrieve the PAKE algorithm from a PAKE cipher suite.1198*1199* \param[in] cipher_suite The cipher suite structure to query.1200*1201* \return The PAKE algorithm stored in the cipher suite structure.1202*/1203static psa_algorithm_t psa_pake_cs_get_algorithm(1204const psa_pake_cipher_suite_t *cipher_suite);12051206/** Declare the PAKE algorithm for the cipher suite.1207*1208* This function overwrites any PAKE algorithm1209* previously set in \p cipher_suite.1210*1211* \note For #PSA_ALG_JPAKE, the only supported hash algorithm is SHA-256.1212*1213* \param[out] cipher_suite The cipher suite structure to write to.1214* \param algorithm The PAKE algorithm to write.1215* (`PSA_ALG_XXX` values of type ::psa_algorithm_t1216* such that #PSA_ALG_IS_PAKE(\c alg) is true.)1217* If this is 0, the PAKE algorithm in1218* \p cipher_suite becomes unspecified.1219*/1220static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,1221psa_algorithm_t algorithm);12221223/** Retrieve the primitive from a PAKE cipher suite.1224*1225* \param[in] cipher_suite The cipher suite structure to query.1226*1227* \return The primitive stored in the cipher suite structure.1228*/1229static psa_pake_primitive_t psa_pake_cs_get_primitive(1230const psa_pake_cipher_suite_t *cipher_suite);12311232/** Declare the primitive for a PAKE cipher suite.1233*1234* This function overwrites any primitive previously set in \p cipher_suite.1235*1236* \note For #PSA_ALG_JPAKE, the only supported primitive is ECC on the curve1237* secp256r1, i.e. `PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,1238* PSA_ECC_FAMILY_SECP_R1, 256)`.1239*1240* \param[out] cipher_suite The cipher suite structure to write to.1241* \param primitive The primitive to write. If this is 0, the1242* primitive type in \p cipher_suite becomes1243* unspecified.1244*/1245static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,1246psa_pake_primitive_t primitive);12471248/** Retrieve the PAKE family from a PAKE cipher suite.1249*1250* \param[in] cipher_suite The cipher suite structure to query.1251*1252* \return The PAKE family stored in the cipher suite structure.1253*/1254static psa_pake_family_t psa_pake_cs_get_family(1255const psa_pake_cipher_suite_t *cipher_suite);12561257/** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.1258*1259* \param[in] cipher_suite The cipher suite structure to query.1260*1261* \return The PAKE primitive bit-size stored in the cipher suite structure.1262*/1263static uint16_t psa_pake_cs_get_bits(1264const psa_pake_cipher_suite_t *cipher_suite);12651266/** Retrieve the hash algorithm from a PAKE cipher suite.1267*1268* \param[in] cipher_suite The cipher suite structure to query.1269*1270* \return The hash algorithm stored in the cipher suite structure. The return1271* value is 0 if the PAKE is not parametrised by a hash algorithm or if1272* the hash algorithm is not set.1273*/1274static psa_algorithm_t psa_pake_cs_get_hash(1275const psa_pake_cipher_suite_t *cipher_suite);12761277/** Declare the hash algorithm for a PAKE cipher suite.1278*1279* This function overwrites any hash algorithm1280* previously set in \p cipher_suite.1281*1282* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1283* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1284* for more information.1285*1286* \param[out] cipher_suite The cipher suite structure to write to.1287* \param hash The hash involved in the cipher suite.1288* (`PSA_ALG_XXX` values of type ::psa_algorithm_t1289* such that #PSA_ALG_IS_HASH(\c alg) is true.)1290* If this is 0, the hash algorithm in1291* \p cipher_suite becomes unspecified.1292*/1293static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,1294psa_algorithm_t hash);12951296/** The type of the state data structure for PAKE operations.1297*1298* Before calling any function on a PAKE operation object, the application1299* must initialize it by any of the following means:1300* - Set the structure to all-bits-zero, for example:1301* \code1302* psa_pake_operation_t operation;1303* memset(&operation, 0, sizeof(operation));1304* \endcode1305* - Initialize the structure to logical zero values, for example:1306* \code1307* psa_pake_operation_t operation = {0};1308* \endcode1309* - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,1310* for example:1311* \code1312* psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;1313* \endcode1314* - Assign the result of the function psa_pake_operation_init()1315* to the structure, for example:1316* \code1317* psa_pake_operation_t operation;1318* operation = psa_pake_operation_init();1319* \endcode1320*1321* This is an implementation-defined \c struct. Applications should not1322* make any assumptions about the content of this structure.1323* Implementation details can change in future versions without notice. */1324typedef struct psa_pake_operation_s psa_pake_operation_t;13251326/** The type of input values for PAKE operations. */1327typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;13281329/** The type of computation stage for J-PAKE operations. */1330typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;13311332/** Return an initial value for a PAKE operation object.1333*/1334#if !(defined(__cplusplus) && defined(_MSC_VER))1335static psa_pake_operation_t psa_pake_operation_init(void);1336#endif13371338/** Get the length of the password in bytes from given inputs.1339*1340* \param[in] inputs Operation inputs.1341* \param[out] password_len Password length.1342*1343* \retval #PSA_SUCCESS1344* Success.1345* \retval #PSA_ERROR_BAD_STATE1346* Password hasn't been set yet.1347*/1348psa_status_t psa_crypto_driver_pake_get_password_len(1349const psa_crypto_driver_pake_inputs_t *inputs,1350size_t *password_len);13511352/** Get the password from given inputs.1353*1354* \param[in] inputs Operation inputs.1355* \param[out] buffer Return buffer for password.1356* \param buffer_size Size of the return buffer in bytes.1357* \param[out] buffer_length Actual size of the password in bytes.1358*1359* \retval #PSA_SUCCESS1360* Success.1361* \retval #PSA_ERROR_BAD_STATE1362* Password hasn't been set yet.1363*/1364psa_status_t psa_crypto_driver_pake_get_password(1365const psa_crypto_driver_pake_inputs_t *inputs,1366uint8_t *buffer, size_t buffer_size, size_t *buffer_length);13671368/** Get the length of the user id in bytes from given inputs.1369*1370* \param[in] inputs Operation inputs.1371* \param[out] user_len User id length.1372*1373* \retval #PSA_SUCCESS1374* Success.1375* \retval #PSA_ERROR_BAD_STATE1376* User id hasn't been set yet.1377*/1378psa_status_t psa_crypto_driver_pake_get_user_len(1379const psa_crypto_driver_pake_inputs_t *inputs,1380size_t *user_len);13811382/** Get the length of the peer id in bytes from given inputs.1383*1384* \param[in] inputs Operation inputs.1385* \param[out] peer_len Peer id length.1386*1387* \retval #PSA_SUCCESS1388* Success.1389* \retval #PSA_ERROR_BAD_STATE1390* Peer id hasn't been set yet.1391*/1392psa_status_t psa_crypto_driver_pake_get_peer_len(1393const psa_crypto_driver_pake_inputs_t *inputs,1394size_t *peer_len);13951396/** Get the user id from given inputs.1397*1398* \param[in] inputs Operation inputs.1399* \param[out] user_id User id.1400* \param user_id_size Size of \p user_id in bytes.1401* \param[out] user_id_len Size of the user id in bytes.1402*1403* \retval #PSA_SUCCESS1404* Success.1405* \retval #PSA_ERROR_BAD_STATE1406* User id hasn't been set yet.1407* \retval #PSA_ERROR_BUFFER_TOO_SMALL1408* The size of the \p user_id is too small.1409*/1410psa_status_t psa_crypto_driver_pake_get_user(1411const psa_crypto_driver_pake_inputs_t *inputs,1412uint8_t *user_id, size_t user_id_size, size_t *user_id_len);14131414/** Get the peer id from given inputs.1415*1416* \param[in] inputs Operation inputs.1417* \param[out] peer_id Peer id.1418* \param peer_id_size Size of \p peer_id in bytes.1419* \param[out] peer_id_length Size of the peer id in bytes.1420*1421* \retval #PSA_SUCCESS1422* Success.1423* \retval #PSA_ERROR_BAD_STATE1424* Peer id hasn't been set yet.1425* \retval #PSA_ERROR_BUFFER_TOO_SMALL1426* The size of the \p peer_id is too small.1427*/1428psa_status_t psa_crypto_driver_pake_get_peer(1429const psa_crypto_driver_pake_inputs_t *inputs,1430uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);14311432/** Get the cipher suite from given inputs.1433*1434* \param[in] inputs Operation inputs.1435* \param[out] cipher_suite Return buffer for role.1436*1437* \retval #PSA_SUCCESS1438* Success.1439* \retval #PSA_ERROR_BAD_STATE1440* Cipher_suite hasn't been set yet.1441*/1442psa_status_t psa_crypto_driver_pake_get_cipher_suite(1443const psa_crypto_driver_pake_inputs_t *inputs,1444psa_pake_cipher_suite_t *cipher_suite);14451446/** Set the session information for a password-authenticated key exchange.1447*1448* The sequence of operations to set up a password-authenticated key exchange1449* is as follows:1450* -# Allocate an operation object which will be passed to all the functions1451* listed here.1452* -# Initialize the operation object with one of the methods described in the1453* documentation for #psa_pake_operation_t, e.g.1454* #PSA_PAKE_OPERATION_INIT.1455* -# Call psa_pake_setup() to specify the cipher suite.1456* -# Call \c psa_pake_set_xxx() functions on the operation to complete the1457* setup. The exact sequence of \c psa_pake_set_xxx() functions that needs1458* to be called depends on the algorithm in use.1459*1460* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1461* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1462* for more information.1463*1464* A typical sequence of calls to perform a password-authenticated key1465* exchange:1466* -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the1467* key share that needs to be sent to the peer.1468* -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide1469* the key share that was received from the peer.1470* -# Depending on the algorithm additional calls to psa_pake_output() and1471* psa_pake_input() might be necessary.1472* -# Call psa_pake_get_implicit_key() for accessing the shared secret.1473*1474* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1475* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1476* for more information.1477*1478* If an error occurs at any step after a call to psa_pake_setup(),1479* the operation will need to be reset by a call to psa_pake_abort(). The1480* application may call psa_pake_abort() at any time after the operation1481* has been initialized.1482*1483* After a successful call to psa_pake_setup(), the application must1484* eventually terminate the operation. The following events terminate an1485* operation:1486* - A call to psa_pake_abort().1487* - A successful call to psa_pake_get_implicit_key().1488*1489* \param[in,out] operation The operation object to set up. It must have1490* been initialized but not set up yet.1491* \param[in] cipher_suite The cipher suite to use. (A cipher suite fully1492* characterizes a PAKE algorithm and determines1493* the algorithm as well.)1494*1495* \retval #PSA_SUCCESS1496* Success.1497* \retval #PSA_ERROR_INVALID_ARGUMENT1498* The algorithm in \p cipher_suite is not a PAKE algorithm, or the1499* PAKE primitive in \p cipher_suite is not compatible with the1500* PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid1501* or not compatible with the PAKE algorithm and primitive.1502* \retval #PSA_ERROR_NOT_SUPPORTED1503* The algorithm in \p cipher_suite is not a supported PAKE algorithm,1504* or the PAKE primitive in \p cipher_suite is not supported or not1505* compatible with the PAKE algorithm, or the hash algorithm in1506* \p cipher_suite is not supported or not compatible with the PAKE1507* algorithm and primitive.1508* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1509* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1510* \retval #PSA_ERROR_BAD_STATE1511* The operation state is not valid, or1512* the library has not been previously initialized by psa_crypto_init().1513* It is implementation-dependent whether a failure to initialize1514* results in this error code.1515*/1516psa_status_t psa_pake_setup(psa_pake_operation_t *operation,1517const psa_pake_cipher_suite_t *cipher_suite);15181519/** Set the password for a password-authenticated key exchange from key ID.1520*1521* Call this function when the password, or a value derived from the password,1522* is already present in the key store.1523*1524* \param[in,out] operation The operation object to set the password for. It1525* must have been set up by psa_pake_setup() and1526* not yet in use (neither psa_pake_output() nor1527* psa_pake_input() has been called yet). It must1528* be on operation for which the password hasn't1529* been set yet (psa_pake_set_password_key()1530* hasn't been called yet).1531* \param password Identifier of the key holding the password or a1532* value derived from the password (eg. by a1533* memory-hard function). It must remain valid1534* until the operation terminates. It must be of1535* type #PSA_KEY_TYPE_PASSWORD or1536* #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow1537* the usage #PSA_KEY_USAGE_DERIVE.1538*1539* \retval #PSA_SUCCESS1540* Success.1541* \retval #PSA_ERROR_INVALID_HANDLE1542* \p password is not a valid key identifier.1543* \retval #PSA_ERROR_NOT_PERMITTED1544* The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not1545* permit the \p operation's algorithm.1546* \retval #PSA_ERROR_INVALID_ARGUMENT1547* The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or1548* #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with1549* the \p operation's cipher suite.1550* \retval #PSA_ERROR_NOT_SUPPORTED1551* The key type or key size of \p password is not supported with the1552* \p operation's cipher suite.1553* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1554* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1555* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1556* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription1557* \retval #PSA_ERROR_DATA_INVALID \emptydescription1558* \retval #PSA_ERROR_BAD_STATE1559* The operation state is not valid (it must have been set up.), or1560* the library has not been previously initialized by psa_crypto_init().1561* It is implementation-dependent whether a failure to initialize1562* results in this error code.1563*/1564psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,1565mbedtls_svc_key_id_t password);15661567/** Set the user ID for a password-authenticated key exchange.1568*1569* Call this function to set the user ID. For PAKE algorithms that associate a1570* user identifier with each side of the session you need to call1571* psa_pake_set_peer() as well. For PAKE algorithms that associate a single1572* user identifier with the session, call psa_pake_set_user() only.1573*1574* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1575* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1576* for more information.1577*1578* \note When using the built-in implementation of #PSA_ALG_JPAKE, the user ID1579* must be `"client"` (6-byte string) or `"server"` (6-byte string).1580* Third-party drivers may or may not have this limitation.1581*1582* \param[in,out] operation The operation object to set the user ID for. It1583* must have been set up by psa_pake_setup() and1584* not yet in use (neither psa_pake_output() nor1585* psa_pake_input() has been called yet). It must1586* be on operation for which the user ID hasn't1587* been set (psa_pake_set_user() hasn't been1588* called yet).1589* \param[in] user_id The user ID to authenticate with.1590* \param user_id_len Size of the \p user_id buffer in bytes.1591*1592* \retval #PSA_SUCCESS1593* Success.1594* \retval #PSA_ERROR_INVALID_ARGUMENT1595* \p user_id is not valid for the \p operation's algorithm and cipher1596* suite.1597* \retval #PSA_ERROR_NOT_SUPPORTED1598* The value of \p user_id is not supported by the implementation.1599* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1600* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1601* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1602* \retval #PSA_ERROR_BAD_STATE1603* The operation state is not valid, or1604* the library has not been previously initialized by psa_crypto_init().1605* It is implementation-dependent whether a failure to initialize1606* results in this error code.1607*/1608psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,1609const uint8_t *user_id,1610size_t user_id_len);16111612/** Set the peer ID for a password-authenticated key exchange.1613*1614* Call this function in addition to psa_pake_set_user() for PAKE algorithms1615* that associate a user identifier with each side of the session. For PAKE1616* algorithms that associate a single user identifier with the session, call1617* psa_pake_set_user() only.1618*1619* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1620* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1621* for more information.1622*1623* \note When using the built-in implementation of #PSA_ALG_JPAKE, the peer ID1624* must be `"client"` (6-byte string) or `"server"` (6-byte string).1625* Third-party drivers may or may not have this limitation.1626*1627* \param[in,out] operation The operation object to set the peer ID for. It1628* must have been set up by psa_pake_setup() and1629* not yet in use (neither psa_pake_output() nor1630* psa_pake_input() has been called yet). It must1631* be on operation for which the peer ID hasn't1632* been set (psa_pake_set_peer() hasn't been1633* called yet).1634* \param[in] peer_id The peer's ID to authenticate.1635* \param peer_id_len Size of the \p peer_id buffer in bytes.1636*1637* \retval #PSA_SUCCESS1638* Success.1639* \retval #PSA_ERROR_INVALID_ARGUMENT1640* \p peer_id is not valid for the \p operation's algorithm and cipher1641* suite.1642* \retval #PSA_ERROR_NOT_SUPPORTED1643* The algorithm doesn't associate a second identity with the session.1644* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1645* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1646* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1647* \retval #PSA_ERROR_BAD_STATE1648* Calling psa_pake_set_peer() is invalid with the \p operation's1649* algorithm, the operation state is not valid, or the library has not1650* been previously initialized by psa_crypto_init().1651* It is implementation-dependent whether a failure to initialize1652* results in this error code.1653*/1654psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,1655const uint8_t *peer_id,1656size_t peer_id_len);16571658/** Set the application role for a password-authenticated key exchange.1659*1660* Not all PAKE algorithms need to differentiate the communicating entities.1661* It is optional to call this function for PAKEs that don't require a role1662* to be specified. For such PAKEs the application role parameter is ignored,1663* or #PSA_PAKE_ROLE_NONE can be passed as \c role.1664*1665* Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`1666* values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)1667* for more information.1668*1669* \param[in,out] operation The operation object to specify the1670* application's role for. It must have been set up1671* by psa_pake_setup() and not yet in use (neither1672* psa_pake_output() nor psa_pake_input() has been1673* called yet). It must be on operation for which1674* the application's role hasn't been specified1675* (psa_pake_set_role() hasn't been called yet).1676* \param role A value of type ::psa_pake_role_t indicating the1677* application's role in the PAKE the algorithm1678* that is being set up. For more information see1679* the documentation of \c PSA_PAKE_ROLE_XXX1680* constants.1681*1682* \retval #PSA_SUCCESS1683* Success.1684* \retval #PSA_ERROR_INVALID_ARGUMENT1685* The \p role is not a valid PAKE role in the \p operation’s algorithm.1686* \retval #PSA_ERROR_NOT_SUPPORTED1687* The \p role for this algorithm is not supported or is not valid.1688* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1689* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1690* \retval #PSA_ERROR_BAD_STATE1691* The operation state is not valid, or1692* the library has not been previously initialized by psa_crypto_init().1693* It is implementation-dependent whether a failure to initialize1694* results in this error code.1695*/1696psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,1697psa_pake_role_t role);16981699/** Get output for a step of a password-authenticated key exchange.1700*1701* Depending on the algorithm being executed, you might need to call this1702* function several times or you might not need to call this at all.1703*1704* The exact sequence of calls to perform a password-authenticated key1705* exchange depends on the algorithm in use. Refer to the documentation of1706* individual PAKE algorithm types (`PSA_ALG_XXX` values of type1707* ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more1708* information.1709*1710* If this function returns an error status, the operation enters an error1711* state and must be aborted by calling psa_pake_abort().1712*1713* \param[in,out] operation Active PAKE operation.1714* \param step The step of the algorithm for which the output is1715* requested.1716* \param[out] output Buffer where the output is to be written in the1717* format appropriate for this \p step. Refer to1718* the documentation of the individual1719* \c PSA_PAKE_STEP_XXX constants for more1720* information.1721* \param output_size Size of the \p output buffer in bytes. This must1722* be at least #PSA_PAKE_OUTPUT_SIZE(\c alg, \c1723* primitive, \p output_step) where \c alg and1724* \p primitive are the PAKE algorithm and primitive1725* in the operation's cipher suite, and \p step is1726* the output step.1727*1728* \param[out] output_length On success, the number of bytes of the returned1729* output.1730*1731* \retval #PSA_SUCCESS1732* Success.1733* \retval #PSA_ERROR_BUFFER_TOO_SMALL1734* The size of the \p output buffer is too small.1735* \retval #PSA_ERROR_INVALID_ARGUMENT1736* \p step is not compatible with the operation's algorithm.1737* \retval #PSA_ERROR_NOT_SUPPORTED1738* \p step is not supported with the operation's algorithm.1739* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription1740* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1741* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1742* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1743* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1744* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription1745* \retval #PSA_ERROR_DATA_INVALID \emptydescription1746* \retval #PSA_ERROR_BAD_STATE1747* The operation state is not valid (it must be active, and fully set1748* up, and this call must conform to the algorithm's requirements1749* for ordering of input and output steps), or1750* the library has not been previously initialized by psa_crypto_init().1751* It is implementation-dependent whether a failure to initialize1752* results in this error code.1753*/1754psa_status_t psa_pake_output(psa_pake_operation_t *operation,1755psa_pake_step_t step,1756uint8_t *output,1757size_t output_size,1758size_t *output_length);17591760/** Provide input for a step of a password-authenticated key exchange.1761*1762* Depending on the algorithm being executed, you might need to call this1763* function several times or you might not need to call this at all.1764*1765* The exact sequence of calls to perform a password-authenticated key1766* exchange depends on the algorithm in use. Refer to the documentation of1767* individual PAKE algorithm types (`PSA_ALG_XXX` values of type1768* ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more1769* information.1770*1771* If this function returns an error status, the operation enters an error1772* state and must be aborted by calling psa_pake_abort().1773*1774* \param[in,out] operation Active PAKE operation.1775* \param step The step for which the input is provided.1776* \param[in] input Buffer containing the input in the format1777* appropriate for this \p step. Refer to the1778* documentation of the individual1779* \c PSA_PAKE_STEP_XXX constants for more1780* information.1781* \param input_length Size of the \p input buffer in bytes.1782*1783* \retval #PSA_SUCCESS1784* Success.1785* \retval #PSA_ERROR_INVALID_SIGNATURE1786* The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.1787* \retval #PSA_ERROR_INVALID_ARGUMENT1788* \p input_length is not compatible with the \p operation’s algorithm,1789* or the \p input is not valid for the \p operation's algorithm,1790* cipher suite or \p step.1791* \retval #PSA_ERROR_NOT_SUPPORTED1792* \p step p is not supported with the \p operation's algorithm, or the1793* \p input is not supported for the \p operation's algorithm, cipher1794* suite or \p step.1795* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1796* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1797* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1798* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1799* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription1800* \retval #PSA_ERROR_DATA_INVALID \emptydescription1801* \retval #PSA_ERROR_BAD_STATE1802* The operation state is not valid (it must be active, and fully set1803* up, and this call must conform to the algorithm's requirements1804* for ordering of input and output steps), or1805* the library has not been previously initialized by psa_crypto_init().1806* It is implementation-dependent whether a failure to initialize1807* results in this error code.1808*/1809psa_status_t psa_pake_input(psa_pake_operation_t *operation,1810psa_pake_step_t step,1811const uint8_t *input,1812size_t input_length);18131814/** Get implicitly confirmed shared secret from a PAKE.1815*1816* At this point there is a cryptographic guarantee that only the authenticated1817* party who used the same password is able to compute the key. But there is no1818* guarantee that the peer is the party it claims to be and was able to do so.1819*1820* That is, the authentication is only implicit. Since the peer is not1821* authenticated yet, no action should be taken yet that assumes that the peer1822* is who it claims to be. For example, do not access restricted files on the1823* peer's behalf until an explicit authentication has succeeded.1824*1825* This function can be called after the key exchange phase of the operation1826* has completed. It imports the shared secret output of the PAKE into the1827* provided derivation operation. The input step1828* #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key1829* material in the key derivation operation.1830*1831* The exact sequence of calls to perform a password-authenticated key1832* exchange depends on the algorithm in use. Refer to the documentation of1833* individual PAKE algorithm types (`PSA_ALG_XXX` values of type1834* ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more1835* information.1836*1837* When this function returns successfully, \p operation becomes inactive.1838* If this function returns an error status, both \p operation1839* and \c key_derivation operations enter an error state and must be aborted by1840* calling psa_pake_abort() and psa_key_derivation_abort() respectively.1841*1842* \param[in,out] operation Active PAKE operation.1843* \param[out] output A key derivation operation that is ready1844* for an input step of type1845* #PSA_KEY_DERIVATION_INPUT_SECRET.1846*1847* \retval #PSA_SUCCESS1848* Success.1849* \retval #PSA_ERROR_INVALID_ARGUMENT1850* #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the1851* algorithm in the \p output key derivation operation.1852* \retval #PSA_ERROR_NOT_SUPPORTED1853* Input from a PAKE is not supported by the algorithm in the \p output1854* key derivation operation.1855* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1856* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1857* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1858* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1859* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription1860* \retval #PSA_ERROR_DATA_INVALID \emptydescription1861* \retval #PSA_ERROR_BAD_STATE1862* The PAKE operation state is not valid (it must be active, but beyond1863* that validity is specific to the algorithm), or1864* the library has not been previously initialized by psa_crypto_init(),1865* or the state of \p output is not valid for1866* the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the1867* step is out of order or the application has done this step already1868* and it may not be repeated.1869* It is implementation-dependent whether a failure to initialize1870* results in this error code.1871*/1872psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,1873psa_key_derivation_operation_t *output);18741875/** Abort a PAKE operation.1876*1877* Aborting an operation frees all associated resources except for the \c1878* operation structure itself. Once aborted, the operation object can be reused1879* for another operation by calling psa_pake_setup() again.1880*1881* This function may be called at any time after the operation1882* object has been initialized as described in #psa_pake_operation_t.1883*1884* In particular, calling psa_pake_abort() after the operation has been1885* terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()1886* is safe and has no effect.1887*1888* \param[in,out] operation The operation to abort.1889*1890* \retval #PSA_SUCCESS1891* Success.1892* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1893* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1894* \retval #PSA_ERROR_BAD_STATE1895* The library has not been previously initialized by psa_crypto_init().1896* It is implementation-dependent whether a failure to initialize1897* results in this error code.1898*/1899psa_status_t psa_pake_abort(psa_pake_operation_t *operation);19001901/**@}*/19021903static inline psa_algorithm_t psa_pake_cs_get_algorithm(1904const psa_pake_cipher_suite_t *cipher_suite)1905{1906return cipher_suite->algorithm;1907}19081909static inline void psa_pake_cs_set_algorithm(1910psa_pake_cipher_suite_t *cipher_suite,1911psa_algorithm_t algorithm)1912{1913if (!PSA_ALG_IS_PAKE(algorithm)) {1914cipher_suite->algorithm = 0;1915} else {1916cipher_suite->algorithm = algorithm;1917}1918}19191920static inline psa_pake_primitive_t psa_pake_cs_get_primitive(1921const psa_pake_cipher_suite_t *cipher_suite)1922{1923return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,1924cipher_suite->bits);1925}19261927static inline void psa_pake_cs_set_primitive(1928psa_pake_cipher_suite_t *cipher_suite,1929psa_pake_primitive_t primitive)1930{1931cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);1932cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));1933cipher_suite->bits = (uint16_t) (0xFFFF & primitive);1934}19351936static inline psa_pake_family_t psa_pake_cs_get_family(1937const psa_pake_cipher_suite_t *cipher_suite)1938{1939return cipher_suite->family;1940}19411942static inline uint16_t psa_pake_cs_get_bits(1943const psa_pake_cipher_suite_t *cipher_suite)1944{1945return cipher_suite->bits;1946}19471948static inline psa_algorithm_t psa_pake_cs_get_hash(1949const psa_pake_cipher_suite_t *cipher_suite)1950{1951return cipher_suite->hash;1952}19531954static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,1955psa_algorithm_t hash)1956{1957if (!PSA_ALG_IS_HASH(hash)) {1958cipher_suite->hash = 0;1959} else {1960cipher_suite->hash = hash;1961}1962}19631964static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)1965{1966const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;1967return v;1968}19691970static inline struct psa_pake_operation_s psa_pake_operation_init(void)1971{1972const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;1973return v;1974}19751976#ifdef __cplusplus1977}1978#endif19791980#endif /* PSA_CRYPTO_EXTRA_H */198119821983