Path: blob/master/thirdparty/mbedtls/include/psa/crypto.h
9904 views
/**1* \file psa/crypto.h2* \brief Platform Security Architecture cryptography module3*/4/*5* Copyright The Mbed TLS Contributors6* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later7*/89#ifndef PSA_CRYPTO_H10#define PSA_CRYPTO_H1112#if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)13#include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE14#else15#include "crypto_platform.h"16#endif1718#include <stddef.h>1920#ifdef __DOXYGEN_ONLY__21/* This __DOXYGEN_ONLY__ block contains mock definitions for things that22* must be defined in the crypto_platform.h header. These mock definitions23* are present in this file as a convenience to generate pretty-printed24* documentation that includes those definitions. */2526/** \defgroup platform Implementation-specific definitions27* @{28*/2930/**@}*/31#endif /* __DOXYGEN_ONLY__ */3233#ifdef __cplusplus34extern "C" {35#endif3637/* The file "crypto_types.h" declares types that encode errors,38* algorithms, key types, policies, etc. */39#include "crypto_types.h"4041/** \defgroup version API version42* @{43*/4445/**46* The major version of this implementation of the PSA Crypto API47*/48#define PSA_CRYPTO_API_VERSION_MAJOR 14950/**51* The minor version of this implementation of the PSA Crypto API52*/53#define PSA_CRYPTO_API_VERSION_MINOR 05455/**@}*/5657/* The file "crypto_values.h" declares macros to build and analyze values58* of integral types defined in "crypto_types.h". */59#include "crypto_values.h"6061/* The file "crypto_sizes.h" contains definitions for size calculation62* macros whose definitions are implementation-specific. */63#include "crypto_sizes.h"6465/* The file "crypto_struct.h" contains definitions for66* implementation-specific structs that are declared above. */67#if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)68#include MBEDTLS_PSA_CRYPTO_STRUCT_FILE69#else70#include "crypto_struct.h"71#endif7273/** \defgroup initialization Library initialization74* @{75*/7677/**78* \brief Library initialization.79*80* Applications must call this function before calling any other81* function in this module.82*83* Applications may call this function more than once. Once a call84* succeeds, subsequent calls are guaranteed to succeed.85*86* If the application calls other functions before calling psa_crypto_init(),87* the behavior is undefined. Implementations are encouraged to either perform88* the operation as if the library had been initialized or to return89* #PSA_ERROR_BAD_STATE or some other applicable error. In particular,90* implementations should not return a success status if the lack of91* initialization may have security implications, for example due to improper92* seeding of the random number generator.93*94* \retval #PSA_SUCCESS \emptydescription95* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription96* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription97* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription98* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription99* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription100* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription101* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription102* \retval #PSA_ERROR_DATA_INVALID \emptydescription103* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription104*/105psa_status_t psa_crypto_init(void);106107/**@}*/108109/** \addtogroup attributes110* @{111*/112113/** \def PSA_KEY_ATTRIBUTES_INIT114*115* This macro returns a suitable initializer for a key attribute structure116* of type #psa_key_attributes_t.117*/118119/** Return an initial value for a key attributes structure.120*/121#if !(defined(__cplusplus) && defined(_MSC_VER))122static psa_key_attributes_t psa_key_attributes_init(void);123#endif124125/** Declare a key as persistent and set its key identifier.126*127* If the attribute structure currently declares the key as volatile (which128* is the default content of an attribute structure), this function sets129* the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.130*131* This function does not access storage, it merely stores the given132* value in the structure.133* The persistent key will be written to storage when the attribute134* structure is passed to a key creation function such as135* psa_import_key(), psa_generate_key(), psa_generate_key_custom(),136* psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()137* or psa_copy_key().138*139* This function may be declared as `static` (i.e. without external140* linkage). This function may be provided as a function-like macro,141* but in this case it must evaluate each of its arguments exactly once.142*143* \param[out] attributes The attribute structure to write to.144* \param key The persistent identifier for the key.145* This can be any value in the range from146* #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX147* inclusive.148*/149static void psa_set_key_id(psa_key_attributes_t *attributes,150mbedtls_svc_key_id_t key);151152#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER153/** Set the owner identifier of a key.154*155* When key identifiers encode key owner identifiers, psa_set_key_id() does156* not allow to define in key attributes the owner of volatile keys as157* psa_set_key_id() enforces the key to be persistent.158*159* This function allows to set in key attributes the owner identifier of a160* key. It is intended to be used for volatile keys. For persistent keys,161* it is recommended to use the PSA Cryptography API psa_set_key_id() to define162* the owner of a key.163*164* \param[out] attributes The attribute structure to write to.165* \param owner The key owner identifier.166*/167static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,168mbedtls_key_owner_id_t owner);169#endif170171/** Set the location of a persistent key.172*173* To make a key persistent, you must give it a persistent key identifier174* with psa_set_key_id(). By default, a key that has a persistent identifier175* is stored in the default storage area identifier by176* #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage177* area, or to explicitly declare the key as volatile.178*179* This function does not access storage, it merely stores the given180* value in the structure.181* The persistent key will be written to storage when the attribute182* structure is passed to a key creation function such as183* psa_import_key(), psa_generate_key(), psa_generate_key_custom(),184* psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()185* or psa_copy_key().186*187* This function may be declared as `static` (i.e. without external188* linkage). This function may be provided as a function-like macro,189* but in this case it must evaluate each of its arguments exactly once.190*191* \param[out] attributes The attribute structure to write to.192* \param lifetime The lifetime for the key.193* If this is #PSA_KEY_LIFETIME_VOLATILE, the194* key will be volatile, and the key identifier195* attribute is reset to 0.196*/197static void psa_set_key_lifetime(psa_key_attributes_t *attributes,198psa_key_lifetime_t lifetime);199200/** Retrieve the key identifier from key attributes.201*202* This function may be declared as `static` (i.e. without external203* linkage). This function may be provided as a function-like macro,204* but in this case it must evaluate its argument exactly once.205*206* \param[in] attributes The key attribute structure to query.207*208* \return The persistent identifier stored in the attribute structure.209* This value is unspecified if the attribute structure declares210* the key as volatile.211*/212static mbedtls_svc_key_id_t psa_get_key_id(213const psa_key_attributes_t *attributes);214215/** Retrieve the lifetime from key attributes.216*217* This function may be declared as `static` (i.e. without external218* linkage). This function may be provided as a function-like macro,219* but in this case it must evaluate its argument exactly once.220*221* \param[in] attributes The key attribute structure to query.222*223* \return The lifetime value stored in the attribute structure.224*/225static psa_key_lifetime_t psa_get_key_lifetime(226const psa_key_attributes_t *attributes);227228/** Declare usage flags for a key.229*230* Usage flags are part of a key's usage policy. They encode what231* kind of operations are permitted on the key. For more details,232* refer to the documentation of the type #psa_key_usage_t.233*234* This function overwrites any usage flags235* previously set in \p attributes.236*237* This function may be declared as `static` (i.e. without external238* linkage). This function may be provided as a function-like macro,239* but in this case it must evaluate each of its arguments exactly once.240*241* \param[out] attributes The attribute structure to write to.242* \param usage_flags The usage flags to write.243*/244static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,245psa_key_usage_t usage_flags);246247/** Retrieve the usage flags from key attributes.248*249* This function may be declared as `static` (i.e. without external250* linkage). This function may be provided as a function-like macro,251* but in this case it must evaluate its argument exactly once.252*253* \param[in] attributes The key attribute structure to query.254*255* \return The usage flags stored in the attribute structure.256*/257static psa_key_usage_t psa_get_key_usage_flags(258const psa_key_attributes_t *attributes);259260/** Declare the permitted algorithm policy for a key.261*262* The permitted algorithm policy of a key encodes which algorithm or263* algorithms are permitted to be used with this key. The following264* algorithm policies are supported:265* - 0 does not allow any cryptographic operation with the key. The key266* may be used for non-cryptographic actions such as exporting (if267* permitted by the usage flags).268* - An algorithm value permits this particular algorithm.269* - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified270* signature scheme with any hash algorithm.271* - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows272* any MAC algorithm from the same base class (e.g. CMAC) which273* generates/verifies a MAC length greater than or equal to the length274* encoded in the wildcard algorithm.275* - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG276* allows any AEAD algorithm from the same base class (e.g. CCM) which277* generates/verifies a tag length greater than or equal to the length278* encoded in the wildcard algorithm.279*280* This function overwrites any algorithm policy281* previously set in \p attributes.282*283* This function may be declared as `static` (i.e. without external284* linkage). This function may be provided as a function-like macro,285* but in this case it must evaluate each of its arguments exactly once.286*287* \param[out] attributes The attribute structure to write to.288* \param alg The permitted algorithm policy to write.289*/290static void psa_set_key_algorithm(psa_key_attributes_t *attributes,291psa_algorithm_t alg);292293294/** Retrieve the algorithm policy from key attributes.295*296* This function may be declared as `static` (i.e. without external297* linkage). This function may be provided as a function-like macro,298* but in this case it must evaluate its argument exactly once.299*300* \param[in] attributes The key attribute structure to query.301*302* \return The algorithm stored in the attribute structure.303*/304static psa_algorithm_t psa_get_key_algorithm(305const psa_key_attributes_t *attributes);306307/** Declare the type of a key.308*309* This function overwrites any key type310* previously set in \p attributes.311*312* This function may be declared as `static` (i.e. without external313* linkage). This function may be provided as a function-like macro,314* but in this case it must evaluate each of its arguments exactly once.315*316* \param[out] attributes The attribute structure to write to.317* \param type The key type to write.318* If this is 0, the key type in \p attributes319* becomes unspecified.320*/321static void psa_set_key_type(psa_key_attributes_t *attributes,322psa_key_type_t type);323324325/** Declare the size of a key.326*327* This function overwrites any key size previously set in \p attributes.328*329* This function may be declared as `static` (i.e. without external330* linkage). This function may be provided as a function-like macro,331* but in this case it must evaluate each of its arguments exactly once.332*333* \param[out] attributes The attribute structure to write to.334* \param bits The key size in bits.335* If this is 0, the key size in \p attributes336* becomes unspecified. Keys of size 0 are337* not supported.338*/339static void psa_set_key_bits(psa_key_attributes_t *attributes,340size_t bits);341342/** Retrieve the key type from key attributes.343*344* This function may be declared as `static` (i.e. without external345* linkage). This function may be provided as a function-like macro,346* but in this case it must evaluate its argument exactly once.347*348* \param[in] attributes The key attribute structure to query.349*350* \return The key type stored in the attribute structure.351*/352#if !(defined(__cplusplus) && defined(_MSC_VER))353static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);354#endif355356/** Retrieve the key size from key attributes.357*358* This function may be declared as `static` (i.e. without external359* linkage). This function may be provided as a function-like macro,360* but in this case it must evaluate its argument exactly once.361*362* \param[in] attributes The key attribute structure to query.363*364* \return The key size stored in the attribute structure, in bits.365*/366static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);367368/** Retrieve the attributes of a key.369*370* This function first resets the attribute structure as with371* psa_reset_key_attributes(). It then copies the attributes of372* the given key into the given attribute structure.373*374* \note This function may allocate memory or other resources.375* Once you have called this function on an attribute structure,376* you must call psa_reset_key_attributes() to free these resources.377*378* \param[in] key Identifier of the key to query.379* \param[in,out] attributes On success, the attributes of the key.380* On failure, equivalent to a381* freshly-initialized structure.382*383* \retval #PSA_SUCCESS \emptydescription384* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription385* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription386* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription387* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription388* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription389* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription390* \retval #PSA_ERROR_DATA_INVALID \emptydescription391* \retval #PSA_ERROR_BAD_STATE392* The library has not been previously initialized by psa_crypto_init().393* It is implementation-dependent whether a failure to initialize394* results in this error code.395*/396psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,397psa_key_attributes_t *attributes);398399/** Reset a key attribute structure to a freshly initialized state.400*401* You must initialize the attribute structure as described in the402* documentation of the type #psa_key_attributes_t before calling this403* function. Once the structure has been initialized, you may call this404* function at any time.405*406* This function frees any auxiliary resources that the structure407* may contain.408*409* \param[in,out] attributes The attribute structure to reset.410*/411void psa_reset_key_attributes(psa_key_attributes_t *attributes);412413/**@}*/414415/** \defgroup key_management Key management416* @{417*/418419/** Remove non-essential copies of key material from memory.420*421* If the key identifier designates a volatile key, this functions does not do422* anything and returns successfully.423*424* If the key identifier designates a persistent key, then this function will425* free all resources associated with the key in volatile memory. The key426* data in persistent storage is not affected and the key can still be used.427*428* \param key Identifier of the key to purge.429*430* \retval #PSA_SUCCESS431* The key material will have been removed from memory if it is not432* currently required.433* \retval #PSA_ERROR_INVALID_ARGUMENT434* \p key is not a valid key identifier.435* \retval #PSA_ERROR_BAD_STATE436* The library has not been previously initialized by psa_crypto_init().437* It is implementation-dependent whether a failure to initialize438* results in this error code.439*/440psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);441442/** Make a copy of a key.443*444* Copy key material from one location to another.445*446* This function is primarily useful to copy a key from one location447* to another, since it populates a key using the material from448* another key which may have a different lifetime.449*450* This function may be used to share a key with a different party,451* subject to implementation-defined restrictions on key sharing.452*453* The policy on the source key must have the usage flag454* #PSA_KEY_USAGE_COPY set.455* This flag is sufficient to permit the copy if the key has the lifetime456* #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.457* Some secure elements do not provide a way to copy a key without458* making it extractable from the secure element. If a key is located459* in such a secure element, then the key must have both usage flags460* #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make461* a copy of the key outside the secure element.462*463* The resulting key may only be used in a way that conforms to464* both the policy of the original key and the policy specified in465* the \p attributes parameter:466* - The usage flags on the resulting key are the bitwise-and of the467* usage flags on the source policy and the usage flags in \p attributes.468* - If both allow the same algorithm or wildcard-based469* algorithm policy, the resulting key has the same algorithm policy.470* - If either of the policies allows an algorithm and the other policy471* allows a wildcard-based algorithm policy that includes this algorithm,472* the resulting key allows the same algorithm.473* - If the policies do not allow any algorithm in common, this function474* fails with the status #PSA_ERROR_INVALID_ARGUMENT.475*476* The effect of this function on implementation-defined attributes is477* implementation-defined.478*479* \param source_key The key to copy. It must allow the usage480* #PSA_KEY_USAGE_COPY. If a private or secret key is481* being copied outside of a secure element it must482* also allow #PSA_KEY_USAGE_EXPORT.483* \param[in] attributes The attributes for the new key.484* They are used as follows:485* - The key type and size may be 0. If either is486* nonzero, it must match the corresponding487* attribute of the source key.488* - The key location (the lifetime and, for489* persistent keys, the key identifier) is490* used directly.491* - The policy constraints (usage flags and492* algorithm policy) are combined from493* the source key and \p attributes so that494* both sets of restrictions apply, as495* described in the documentation of this function.496* \param[out] target_key On success, an identifier for the newly created497* key. For persistent keys, this is the key498* identifier defined in \p attributes.499* \c 0 on failure.500*501* \retval #PSA_SUCCESS \emptydescription502* \retval #PSA_ERROR_INVALID_HANDLE503* \p source_key is invalid.504* \retval #PSA_ERROR_ALREADY_EXISTS505* This is an attempt to create a persistent key, and there is506* already a persistent key with the given identifier.507* \retval #PSA_ERROR_INVALID_ARGUMENT508* The lifetime or identifier in \p attributes are invalid, or509* the policy constraints on the source and specified in510* \p attributes are incompatible, or511* \p attributes specifies a key type or key size512* which does not match the attributes of the source key.513* \retval #PSA_ERROR_NOT_PERMITTED514* The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or515* the source key is not exportable and its lifetime does not516* allow copying it to the target's lifetime.517* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription518* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription519* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription520* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription521* \retval #PSA_ERROR_DATA_INVALID \emptydescription522* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription523* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription524* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription525* \retval #PSA_ERROR_BAD_STATE526* The library has not been previously initialized by psa_crypto_init().527* It is implementation-dependent whether a failure to initialize528* results in this error code.529*/530psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,531const psa_key_attributes_t *attributes,532mbedtls_svc_key_id_t *target_key);533534535/**536* \brief Destroy a key.537*538* This function destroys a key from both volatile539* memory and, if applicable, non-volatile storage. Implementations shall540* make a best effort to ensure that the key material cannot be recovered.541*542* This function also erases any metadata such as policies and frees543* resources associated with the key.544*545* If a key is currently in use in a multipart operation, then destroying the546* key will cause the multipart operation to fail.547*548* \warning We can only guarantee that the the key material will549* eventually be wiped from memory. With threading enabled550* and during concurrent execution, copies of the key material may551* still exist until all threads have finished using the key.552*553* \param key Identifier of the key to erase. If this is \c 0, do nothing and554* return #PSA_SUCCESS.555*556* \retval #PSA_SUCCESS557* \p key was a valid identifier and the key material that it558* referred to has been erased. Alternatively, \p key is \c 0.559* \retval #PSA_ERROR_NOT_PERMITTED560* The key cannot be erased because it is561* read-only, either due to a policy or due to physical restrictions.562* \retval #PSA_ERROR_INVALID_HANDLE563* \p key is not a valid identifier nor \c 0.564* \retval #PSA_ERROR_COMMUNICATION_FAILURE565* There was a failure in communication with the cryptoprocessor.566* The key material may still be present in the cryptoprocessor.567* \retval #PSA_ERROR_DATA_INVALID568* This error is typically a result of either storage corruption on a569* cleartext storage backend, or an attempt to read data that was570* written by an incompatible version of the library.571* \retval #PSA_ERROR_STORAGE_FAILURE572* The storage is corrupted. Implementations shall make a best effort573* to erase key material even in this stage, however applications574* should be aware that it may be impossible to guarantee that the575* key material is not recoverable in such cases.576* \retval #PSA_ERROR_CORRUPTION_DETECTED577* An unexpected condition which is not a storage corruption or578* a communication failure occurred. The cryptoprocessor may have579* been compromised.580* \retval #PSA_ERROR_BAD_STATE581* The library has not been previously initialized by psa_crypto_init().582* It is implementation-dependent whether a failure to initialize583* results in this error code.584*/585psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);586587/**@}*/588589/** \defgroup import_export Key import and export590* @{591*/592593/**594* \brief Import a key in binary format.595*596* This function supports any output from psa_export_key(). Refer to the597* documentation of psa_export_public_key() for the format of public keys598* and to the documentation of psa_export_key() for the format for599* other key types.600*601* The key data determines the key size. The attributes may optionally602* specify a key size; in this case it must match the size determined603* from the key data. A key size of 0 in \p attributes indicates that604* the key size is solely determined by the key data.605*606* Implementations must reject an attempt to import a key of size 0.607*608* This specification supports a single format for each key type.609* Implementations may support other formats as long as the standard610* format is supported. Implementations that support other formats611* should ensure that the formats are clearly unambiguous so as to612* minimize the risk that an invalid input is accidentally interpreted613* according to a different format.614*615* \param[in] attributes The attributes for the new key.616* The key size is always determined from the617* \p data buffer.618* If the key size in \p attributes is nonzero,619* it must be equal to the size from \p data.620* \param[out] key On success, an identifier to the newly created key.621* For persistent keys, this is the key identifier622* defined in \p attributes.623* \c 0 on failure.624* \param[in] data Buffer containing the key data. The content of this625* buffer is interpreted according to the type declared626* in \p attributes.627* All implementations must support at least the format628* described in the documentation629* of psa_export_key() or psa_export_public_key() for630* the chosen type. Implementations may allow other631* formats, but should be conservative: implementations632* should err on the side of rejecting content if it633* may be erroneous (e.g. wrong type or truncated data).634* \param data_length Size of the \p data buffer in bytes.635*636* \retval #PSA_SUCCESS637* Success.638* If the key is persistent, the key material and the key's metadata639* have been saved to persistent storage.640* \retval #PSA_ERROR_ALREADY_EXISTS641* This is an attempt to create a persistent key, and there is642* already a persistent key with the given identifier.643* \retval #PSA_ERROR_NOT_SUPPORTED644* The key type or key size is not supported, either by the645* implementation in general or in this particular persistent location.646* \retval #PSA_ERROR_INVALID_ARGUMENT647* The key attributes, as a whole, are invalid, or648* the key data is not correctly formatted, or649* the size in \p attributes is nonzero and does not match the size650* of the key data.651* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription652* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription653* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription654* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription655* \retval #PSA_ERROR_DATA_INVALID \emptydescription656* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription657* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription658* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription659* \retval #PSA_ERROR_BAD_STATE660* The library has not been previously initialized by psa_crypto_init().661* It is implementation-dependent whether a failure to initialize662* results in this error code.663*/664psa_status_t psa_import_key(const psa_key_attributes_t *attributes,665const uint8_t *data,666size_t data_length,667mbedtls_svc_key_id_t *key);668669670671/**672* \brief Export a key in binary format.673*674* The output of this function can be passed to psa_import_key() to675* create an equivalent object.676*677* If the implementation of psa_import_key() supports other formats678* beyond the format specified here, the output from psa_export_key()679* must use the representation specified here, not the original680* representation.681*682* For standard key types, the output format is as follows:683*684* - For symmetric keys (including MAC keys), the format is the685* raw bytes of the key.686* - For DES, the key data consists of 8 bytes. The parity bits must be687* correct.688* - For Triple-DES, the format is the concatenation of the689* two or three DES keys.690* - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format691* is the non-encrypted DER encoding of the representation defined by692* PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.693* ```694* RSAPrivateKey ::= SEQUENCE {695* version INTEGER, -- must be 0696* modulus INTEGER, -- n697* publicExponent INTEGER, -- e698* privateExponent INTEGER, -- d699* prime1 INTEGER, -- p700* prime2 INTEGER, -- q701* exponent1 INTEGER, -- d mod (p-1)702* exponent2 INTEGER, -- d mod (q-1)703* coefficient INTEGER, -- (inverse of q) mod p704* }705* ```706* - For elliptic curve key pairs (key types for which707* #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is708* a representation of the private value as a `ceiling(m/8)`-byte string709* where `m` is the bit size associated with the curve, i.e. the bit size710* of the order of the curve's coordinate field. This byte string is711* in little-endian order for Montgomery curves (curve types712* `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass713* curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`714* and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).715* For Weierstrass curves, this is the content of the `privateKey` field of716* the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,717* the format is defined by RFC 7748, and output is masked according to §5.718* For twisted Edwards curves, the private key is as defined by RFC 8032719* (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).720* - For Diffie-Hellman key exchange key pairs (key types for which721* #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the722* format is the representation of the private key `x` as a big-endian byte723* string. The length of the byte string is the private key size in bytes724* (leading zeroes are not stripped).725* - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is726* true), the format is the same as for psa_export_public_key().727*728* The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.729*730* \param key Identifier of the key to export. It must allow the731* usage #PSA_KEY_USAGE_EXPORT, unless it is a public732* key.733* \param[out] data Buffer where the key data is to be written.734* \param data_size Size of the \p data buffer in bytes.735* \param[out] data_length On success, the number of bytes736* that make up the key data.737*738* \retval #PSA_SUCCESS \emptydescription739* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription740* \retval #PSA_ERROR_NOT_PERMITTED741* The key does not have the #PSA_KEY_USAGE_EXPORT flag.742* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription743* \retval #PSA_ERROR_BUFFER_TOO_SMALL744* The size of the \p data buffer is too small. You can determine a745* sufficient buffer size by calling746* #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)747* where \c type is the key type748* and \c bits is the key size in bits.749* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription750* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription751* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription752* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription753* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription754* \retval #PSA_ERROR_BAD_STATE755* The library has not been previously initialized by psa_crypto_init().756* It is implementation-dependent whether a failure to initialize757* results in this error code.758*/759psa_status_t psa_export_key(mbedtls_svc_key_id_t key,760uint8_t *data,761size_t data_size,762size_t *data_length);763764/**765* \brief Export a public key or the public part of a key pair in binary format.766*767* The output of this function can be passed to psa_import_key() to768* create an object that is equivalent to the public key.769*770* This specification supports a single format for each key type.771* Implementations may support other formats as long as the standard772* format is supported. Implementations that support other formats773* should ensure that the formats are clearly unambiguous so as to774* minimize the risk that an invalid input is accidentally interpreted775* according to a different format.776*777* For standard key types, the output format is as follows:778* - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of779* the representation defined by RFC 3279 §2.3.1 as `RSAPublicKey`.780* ```781* RSAPublicKey ::= SEQUENCE {782* modulus INTEGER, -- n783* publicExponent INTEGER } -- e784* ```785* - For elliptic curve keys on a twisted Edwards curve (key types for which786* #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY787* returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined788* by RFC 8032789* (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).790* - For other elliptic curve public keys (key types for which791* #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed792* representation defined by SEC1 §2.3.3 as the content of an ECPoint.793* Let `m` be the bit size associated with the curve, i.e. the bit size of794* `q` for a curve over `F_q`. The representation consists of:795* - The byte 0x04;796* - `x_P` as a `ceiling(m/8)`-byte string, big-endian;797* - `y_P` as a `ceiling(m/8)`-byte string, big-endian.798* - For Diffie-Hellman key exchange public keys (key types for which799* #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),800* the format is the representation of the public key `y = g^x mod p` as a801* big-endian byte string. The length of the byte string is the length of the802* base prime `p` in bytes.803*804* Exporting a public key object or the public part of a key pair is805* always permitted, regardless of the key's usage flags.806*807* \param key Identifier of the key to export.808* \param[out] data Buffer where the key data is to be written.809* \param data_size Size of the \p data buffer in bytes.810* \param[out] data_length On success, the number of bytes811* that make up the key data.812*813* \retval #PSA_SUCCESS \emptydescription814* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription815* \retval #PSA_ERROR_INVALID_ARGUMENT816* The key is neither a public key nor a key pair.817* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription818* \retval #PSA_ERROR_BUFFER_TOO_SMALL819* The size of the \p data buffer is too small. You can determine a820* sufficient buffer size by calling821* #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)822* where \c type is the key type823* and \c bits is the key size in bits.824* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription825* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription826* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription827* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription828* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription829* \retval #PSA_ERROR_BAD_STATE830* The library has not been previously initialized by psa_crypto_init().831* It is implementation-dependent whether a failure to initialize832* results in this error code.833*/834psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,835uint8_t *data,836size_t data_size,837size_t *data_length);838839840841/**@}*/842843/** \defgroup hash Message digests844* @{845*/846847/** Calculate the hash (digest) of a message.848*849* \note To verify the hash of a message against an850* expected value, use psa_hash_compare() instead.851*852* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value853* such that #PSA_ALG_IS_HASH(\p alg) is true).854* \param[in] input Buffer containing the message to hash.855* \param input_length Size of the \p input buffer in bytes.856* \param[out] hash Buffer where the hash is to be written.857* \param hash_size Size of the \p hash buffer in bytes.858* \param[out] hash_length On success, the number of bytes859* that make up the hash value. This is always860* #PSA_HASH_LENGTH(\p alg).861*862* \retval #PSA_SUCCESS863* Success.864* \retval #PSA_ERROR_NOT_SUPPORTED865* \p alg is not supported or is not a hash algorithm.866* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription867* \retval #PSA_ERROR_BUFFER_TOO_SMALL868* \p hash_size is too small869* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription870* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription871* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription872* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription873* \retval #PSA_ERROR_BAD_STATE874* The library has not been previously initialized by psa_crypto_init().875* It is implementation-dependent whether a failure to initialize876* results in this error code.877*/878psa_status_t psa_hash_compute(psa_algorithm_t alg,879const uint8_t *input,880size_t input_length,881uint8_t *hash,882size_t hash_size,883size_t *hash_length);884885/** Calculate the hash (digest) of a message and compare it with a886* reference value.887*888* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value889* such that #PSA_ALG_IS_HASH(\p alg) is true).890* \param[in] input Buffer containing the message to hash.891* \param input_length Size of the \p input buffer in bytes.892* \param[in] hash Buffer containing the expected hash value.893* \param hash_length Size of the \p hash buffer in bytes.894*895* \retval #PSA_SUCCESS896* The expected hash is identical to the actual hash of the input.897* \retval #PSA_ERROR_INVALID_SIGNATURE898* The hash of the message was calculated successfully, but it899* differs from the expected hash.900* \retval #PSA_ERROR_NOT_SUPPORTED901* \p alg is not supported or is not a hash algorithm.902* \retval #PSA_ERROR_INVALID_ARGUMENT903* \p input_length or \p hash_length do not match the hash size for \p alg904* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription905* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription906* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription907* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription908* \retval #PSA_ERROR_BAD_STATE909* The library has not been previously initialized by psa_crypto_init().910* It is implementation-dependent whether a failure to initialize911* results in this error code.912*/913psa_status_t psa_hash_compare(psa_algorithm_t alg,914const uint8_t *input,915size_t input_length,916const uint8_t *hash,917size_t hash_length);918919/** The type of the state data structure for multipart hash operations.920*921* Before calling any function on a hash operation object, the application must922* initialize it by any of the following means:923* - Set the structure to all-bits-zero, for example:924* \code925* psa_hash_operation_t operation;926* memset(&operation, 0, sizeof(operation));927* \endcode928* - Initialize the structure to logical zero values, for example:929* \code930* psa_hash_operation_t operation = {0};931* \endcode932* - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,933* for example:934* \code935* psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;936* \endcode937* - Assign the result of the function psa_hash_operation_init()938* to the structure, for example:939* \code940* psa_hash_operation_t operation;941* operation = psa_hash_operation_init();942* \endcode943*944* This is an implementation-defined \c struct. Applications should not945* make any assumptions about the content of this structure.946* Implementation details can change in future versions without notice. */947typedef struct psa_hash_operation_s psa_hash_operation_t;948949/** \def PSA_HASH_OPERATION_INIT950*951* This macro returns a suitable initializer for a hash operation object952* of type #psa_hash_operation_t.953*/954955/** Return an initial value for a hash operation object.956*/957#if !(defined(__cplusplus) && defined(_MSC_VER))958static psa_hash_operation_t psa_hash_operation_init(void);959#endif960961/** Set up a multipart hash operation.962*963* The sequence of operations to calculate a hash (message digest)964* is as follows:965* -# Allocate an operation object which will be passed to all the functions966* listed here.967* -# Initialize the operation object with one of the methods described in the968* documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.969* -# Call psa_hash_setup() to specify the algorithm.970* -# Call psa_hash_update() zero, one or more times, passing a fragment971* of the message each time. The hash that is calculated is the hash972* of the concatenation of these messages in order.973* -# To calculate the hash, call psa_hash_finish().974* To compare the hash with an expected value, call psa_hash_verify().975*976* If an error occurs at any step after a call to psa_hash_setup(), the977* operation will need to be reset by a call to psa_hash_abort(). The978* application may call psa_hash_abort() at any time after the operation979* has been initialized.980*981* After a successful call to psa_hash_setup(), the application must982* eventually terminate the operation. The following events terminate an983* operation:984* - A successful call to psa_hash_finish() or psa_hash_verify().985* - A call to psa_hash_abort().986*987* \param[in,out] operation The operation object to set up. It must have988* been initialized as per the documentation for989* #psa_hash_operation_t and not yet in use.990* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value991* such that #PSA_ALG_IS_HASH(\p alg) is true).992*993* \retval #PSA_SUCCESS994* Success.995* \retval #PSA_ERROR_NOT_SUPPORTED996* \p alg is not a supported hash algorithm.997* \retval #PSA_ERROR_INVALID_ARGUMENT998* \p alg is not a hash algorithm.999* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1000* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1001* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1002* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1003* \retval #PSA_ERROR_BAD_STATE1004* The operation state is not valid (it must be inactive), or1005* the library has not been previously initialized by psa_crypto_init().1006* It is implementation-dependent whether a failure to initialize1007* results in this error code.1008*/1009psa_status_t psa_hash_setup(psa_hash_operation_t *operation,1010psa_algorithm_t alg);10111012/** Add a message fragment to a multipart hash operation.1013*1014* The application must call psa_hash_setup() before calling this function.1015*1016* If this function returns an error status, the operation enters an error1017* state and must be aborted by calling psa_hash_abort().1018*1019* \param[in,out] operation Active hash operation.1020* \param[in] input Buffer containing the message fragment to hash.1021* \param input_length Size of the \p input buffer in bytes.1022*1023* \retval #PSA_SUCCESS1024* Success.1025* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1026* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1027* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1028* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1029* \retval #PSA_ERROR_BAD_STATE1030* The operation state is not valid (it must be active), or1031* the library has not been previously initialized by psa_crypto_init().1032* It is implementation-dependent whether a failure to initialize1033* results in this error code.1034*/1035psa_status_t psa_hash_update(psa_hash_operation_t *operation,1036const uint8_t *input,1037size_t input_length);10381039/** Finish the calculation of the hash of a message.1040*1041* The application must call psa_hash_setup() before calling this function.1042* This function calculates the hash of the message formed by concatenating1043* the inputs passed to preceding calls to psa_hash_update().1044*1045* When this function returns successfully, the operation becomes inactive.1046* If this function returns an error status, the operation enters an error1047* state and must be aborted by calling psa_hash_abort().1048*1049* \warning Applications should not call this function if they expect1050* a specific value for the hash. Call psa_hash_verify() instead.1051* Beware that comparing integrity or authenticity data such as1052* hash values with a function such as \c memcmp is risky1053* because the time taken by the comparison may leak information1054* about the hashed data which could allow an attacker to guess1055* a valid hash and thereby bypass security controls.1056*1057* \param[in,out] operation Active hash operation.1058* \param[out] hash Buffer where the hash is to be written.1059* \param hash_size Size of the \p hash buffer in bytes.1060* \param[out] hash_length On success, the number of bytes1061* that make up the hash value. This is always1062* #PSA_HASH_LENGTH(\c alg) where \c alg is the1063* hash algorithm that is calculated.1064*1065* \retval #PSA_SUCCESS1066* Success.1067* \retval #PSA_ERROR_BUFFER_TOO_SMALL1068* The size of the \p hash buffer is too small. You can determine a1069* sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)1070* where \c alg is the hash algorithm that is calculated.1071* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1072* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1073* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1074* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1075* \retval #PSA_ERROR_BAD_STATE1076* The operation state is not valid (it must be active), or1077* the library has not been previously initialized by psa_crypto_init().1078* It is implementation-dependent whether a failure to initialize1079* results in this error code.1080*/1081psa_status_t psa_hash_finish(psa_hash_operation_t *operation,1082uint8_t *hash,1083size_t hash_size,1084size_t *hash_length);10851086/** Finish the calculation of the hash of a message and compare it with1087* an expected value.1088*1089* The application must call psa_hash_setup() before calling this function.1090* This function calculates the hash of the message formed by concatenating1091* the inputs passed to preceding calls to psa_hash_update(). It then1092* compares the calculated hash with the expected hash passed as a1093* parameter to this function.1094*1095* When this function returns successfully, the operation becomes inactive.1096* If this function returns an error status, the operation enters an error1097* state and must be aborted by calling psa_hash_abort().1098*1099* \note Implementations shall make the best effort to ensure that the1100* comparison between the actual hash and the expected hash is performed1101* in constant time.1102*1103* \param[in,out] operation Active hash operation.1104* \param[in] hash Buffer containing the expected hash value.1105* \param hash_length Size of the \p hash buffer in bytes.1106*1107* \retval #PSA_SUCCESS1108* The expected hash is identical to the actual hash of the message.1109* \retval #PSA_ERROR_INVALID_SIGNATURE1110* The hash of the message was calculated successfully, but it1111* differs from the expected hash.1112* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1113* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1114* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1115* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1116* \retval #PSA_ERROR_BAD_STATE1117* The operation state is not valid (it must be active), or1118* the library has not been previously initialized by psa_crypto_init().1119* It is implementation-dependent whether a failure to initialize1120* results in this error code.1121*/1122psa_status_t psa_hash_verify(psa_hash_operation_t *operation,1123const uint8_t *hash,1124size_t hash_length);11251126/** Abort a hash operation.1127*1128* Aborting an operation frees all associated resources except for the1129* \p operation structure itself. Once aborted, the operation object1130* can be reused for another operation by calling1131* psa_hash_setup() again.1132*1133* You may call this function any time after the operation object has1134* been initialized by one of the methods described in #psa_hash_operation_t.1135*1136* In particular, calling psa_hash_abort() after the operation has been1137* terminated by a call to psa_hash_abort(), psa_hash_finish() or1138* psa_hash_verify() is safe and has no effect.1139*1140* \param[in,out] operation Initialized hash operation.1141*1142* \retval #PSA_SUCCESS \emptydescription1143* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1144* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1145* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1146* \retval #PSA_ERROR_BAD_STATE1147* The library has not been previously initialized by psa_crypto_init().1148* It is implementation-dependent whether a failure to initialize1149* results in this error code.1150*/1151psa_status_t psa_hash_abort(psa_hash_operation_t *operation);11521153/** Clone a hash operation.1154*1155* This function copies the state of an ongoing hash operation to1156* a new operation object. In other words, this function is equivalent1157* to calling psa_hash_setup() on \p target_operation with the same1158* algorithm that \p source_operation was set up for, then1159* psa_hash_update() on \p target_operation with the same input that1160* that was passed to \p source_operation. After this function returns, the1161* two objects are independent, i.e. subsequent calls involving one of1162* the objects do not affect the other object.1163*1164* \param[in] source_operation The active hash operation to clone.1165* \param[in,out] target_operation The operation object to set up.1166* It must be initialized but not active.1167*1168* \retval #PSA_SUCCESS \emptydescription1169* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1170* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1171* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1172* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1173* \retval #PSA_ERROR_BAD_STATE1174* The \p source_operation state is not valid (it must be active), or1175* the \p target_operation state is not valid (it must be inactive), or1176* the library has not been previously initialized by psa_crypto_init().1177* It is implementation-dependent whether a failure to initialize1178* results in this error code.1179*/1180psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,1181psa_hash_operation_t *target_operation);11821183/**@}*/11841185/** \defgroup MAC Message authentication codes1186* @{1187*/11881189/** Calculate the MAC (message authentication code) of a message.1190*1191* \note To verify the MAC of a message against an1192* expected value, use psa_mac_verify() instead.1193* Beware that comparing integrity or authenticity data such as1194* MAC values with a function such as \c memcmp is risky1195* because the time taken by the comparison may leak information1196* about the MAC value which could allow an attacker to guess1197* a valid MAC and thereby bypass security controls.1198*1199* \param key Identifier of the key to use for the operation. It1200* must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.1201* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value1202* such that #PSA_ALG_IS_MAC(\p alg) is true).1203* \param[in] input Buffer containing the input message.1204* \param input_length Size of the \p input buffer in bytes.1205* \param[out] mac Buffer where the MAC value is to be written.1206* \param mac_size Size of the \p mac buffer in bytes.1207* \param[out] mac_length On success, the number of bytes1208* that make up the MAC value.1209*1210* \retval #PSA_SUCCESS1211* Success.1212* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1213* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1214* \retval #PSA_ERROR_INVALID_ARGUMENT1215* \p key is not compatible with \p alg.1216* \retval #PSA_ERROR_NOT_SUPPORTED1217* \p alg is not supported or is not a MAC algorithm.1218* \retval #PSA_ERROR_BUFFER_TOO_SMALL1219* \p mac_size is too small1220* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1221* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1222* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1223* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1224* \retval #PSA_ERROR_STORAGE_FAILURE1225* The key could not be retrieved from storage.1226* \retval #PSA_ERROR_BAD_STATE1227* The library has not been previously initialized by psa_crypto_init().1228* It is implementation-dependent whether a failure to initialize1229* results in this error code.1230*/1231psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,1232psa_algorithm_t alg,1233const uint8_t *input,1234size_t input_length,1235uint8_t *mac,1236size_t mac_size,1237size_t *mac_length);12381239/** Calculate the MAC of a message and compare it with a reference value.1240*1241* \param key Identifier of the key to use for the operation. It1242* must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.1243* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value1244* such that #PSA_ALG_IS_MAC(\p alg) is true).1245* \param[in] input Buffer containing the input message.1246* \param input_length Size of the \p input buffer in bytes.1247* \param[in] mac Buffer containing the expected MAC value.1248* \param mac_length Size of the \p mac buffer in bytes.1249*1250* \retval #PSA_SUCCESS1251* The expected MAC is identical to the actual MAC of the input.1252* \retval #PSA_ERROR_INVALID_SIGNATURE1253* The MAC of the message was calculated successfully, but it1254* differs from the expected value.1255* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1256* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1257* \retval #PSA_ERROR_INVALID_ARGUMENT1258* \p key is not compatible with \p alg.1259* \retval #PSA_ERROR_NOT_SUPPORTED1260* \p alg is not supported or is not a MAC algorithm.1261* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1262* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1263* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1264* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1265* \retval #PSA_ERROR_STORAGE_FAILURE1266* The key could not be retrieved from storage.1267* \retval #PSA_ERROR_BAD_STATE1268* The library has not been previously initialized by psa_crypto_init().1269* It is implementation-dependent whether a failure to initialize1270* results in this error code.1271*/1272psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,1273psa_algorithm_t alg,1274const uint8_t *input,1275size_t input_length,1276const uint8_t *mac,1277size_t mac_length);12781279/** The type of the state data structure for multipart MAC operations.1280*1281* Before calling any function on a MAC operation object, the application must1282* initialize it by any of the following means:1283* - Set the structure to all-bits-zero, for example:1284* \code1285* psa_mac_operation_t operation;1286* memset(&operation, 0, sizeof(operation));1287* \endcode1288* - Initialize the structure to logical zero values, for example:1289* \code1290* psa_mac_operation_t operation = {0};1291* \endcode1292* - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,1293* for example:1294* \code1295* psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;1296* \endcode1297* - Assign the result of the function psa_mac_operation_init()1298* to the structure, for example:1299* \code1300* psa_mac_operation_t operation;1301* operation = psa_mac_operation_init();1302* \endcode1303*1304*1305* This is an implementation-defined \c struct. Applications should not1306* make any assumptions about the content of this structure.1307* Implementation details can change in future versions without notice. */1308typedef struct psa_mac_operation_s psa_mac_operation_t;13091310/** \def PSA_MAC_OPERATION_INIT1311*1312* This macro returns a suitable initializer for a MAC operation object of type1313* #psa_mac_operation_t.1314*/13151316/** Return an initial value for a MAC operation object.1317*/1318#if !(defined(__cplusplus) && defined(_MSC_VER))1319static psa_mac_operation_t psa_mac_operation_init(void);1320#endif13211322/** Set up a multipart MAC calculation operation.1323*1324* This function sets up the calculation of the MAC1325* (message authentication code) of a byte string.1326* To verify the MAC of a message against an1327* expected value, use psa_mac_verify_setup() instead.1328*1329* The sequence of operations to calculate a MAC is as follows:1330* -# Allocate an operation object which will be passed to all the functions1331* listed here.1332* -# Initialize the operation object with one of the methods described in the1333* documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.1334* -# Call psa_mac_sign_setup() to specify the algorithm and key.1335* -# Call psa_mac_update() zero, one or more times, passing a fragment1336* of the message each time. The MAC that is calculated is the MAC1337* of the concatenation of these messages in order.1338* -# At the end of the message, call psa_mac_sign_finish() to finish1339* calculating the MAC value and retrieve it.1340*1341* If an error occurs at any step after a call to psa_mac_sign_setup(), the1342* operation will need to be reset by a call to psa_mac_abort(). The1343* application may call psa_mac_abort() at any time after the operation1344* has been initialized.1345*1346* After a successful call to psa_mac_sign_setup(), the application must1347* eventually terminate the operation through one of the following methods:1348* - A successful call to psa_mac_sign_finish().1349* - A call to psa_mac_abort().1350*1351* \param[in,out] operation The operation object to set up. It must have1352* been initialized as per the documentation for1353* #psa_mac_operation_t and not yet in use.1354* \param key Identifier of the key to use for the operation. It1355* must remain valid until the operation terminates.1356* It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.1357* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value1358* such that #PSA_ALG_IS_MAC(\p alg) is true).1359*1360* \retval #PSA_SUCCESS1361* Success.1362* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1363* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1364* \retval #PSA_ERROR_INVALID_ARGUMENT1365* \p key is not compatible with \p alg.1366* \retval #PSA_ERROR_NOT_SUPPORTED1367* \p alg is not supported or is not a MAC algorithm.1368* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1369* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1370* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1371* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1372* \retval #PSA_ERROR_STORAGE_FAILURE1373* The key could not be retrieved from storage.1374* \retval #PSA_ERROR_BAD_STATE1375* The operation state is not valid (it must be inactive), or1376* the library has not been previously initialized by psa_crypto_init().1377* It is implementation-dependent whether a failure to initialize1378* results in this error code.1379*/1380psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,1381mbedtls_svc_key_id_t key,1382psa_algorithm_t alg);13831384/** Set up a multipart MAC verification operation.1385*1386* This function sets up the verification of the MAC1387* (message authentication code) of a byte string against an expected value.1388*1389* The sequence of operations to verify a MAC is as follows:1390* -# Allocate an operation object which will be passed to all the functions1391* listed here.1392* -# Initialize the operation object with one of the methods described in the1393* documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.1394* -# Call psa_mac_verify_setup() to specify the algorithm and key.1395* -# Call psa_mac_update() zero, one or more times, passing a fragment1396* of the message each time. The MAC that is calculated is the MAC1397* of the concatenation of these messages in order.1398* -# At the end of the message, call psa_mac_verify_finish() to finish1399* calculating the actual MAC of the message and verify it against1400* the expected value.1401*1402* If an error occurs at any step after a call to psa_mac_verify_setup(), the1403* operation will need to be reset by a call to psa_mac_abort(). The1404* application may call psa_mac_abort() at any time after the operation1405* has been initialized.1406*1407* After a successful call to psa_mac_verify_setup(), the application must1408* eventually terminate the operation through one of the following methods:1409* - A successful call to psa_mac_verify_finish().1410* - A call to psa_mac_abort().1411*1412* \param[in,out] operation The operation object to set up. It must have1413* been initialized as per the documentation for1414* #psa_mac_operation_t and not yet in use.1415* \param key Identifier of the key to use for the operation. It1416* must remain valid until the operation terminates.1417* It must allow the usage1418* PSA_KEY_USAGE_VERIFY_MESSAGE.1419* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value1420* such that #PSA_ALG_IS_MAC(\p alg) is true).1421*1422* \retval #PSA_SUCCESS1423* Success.1424* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1425* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1426* \retval #PSA_ERROR_INVALID_ARGUMENT1427* \c key is not compatible with \c alg.1428* \retval #PSA_ERROR_NOT_SUPPORTED1429* \c alg is not supported or is not a MAC algorithm.1430* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1431* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1432* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1433* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1434* \retval #PSA_ERROR_STORAGE_FAILURE1435* The key could not be retrieved from storage.1436* \retval #PSA_ERROR_BAD_STATE1437* The operation state is not valid (it must be inactive), or1438* the library has not been previously initialized by psa_crypto_init().1439* It is implementation-dependent whether a failure to initialize1440* results in this error code.1441*/1442psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,1443mbedtls_svc_key_id_t key,1444psa_algorithm_t alg);14451446/** Add a message fragment to a multipart MAC operation.1447*1448* The application must call psa_mac_sign_setup() or psa_mac_verify_setup()1449* before calling this function.1450*1451* If this function returns an error status, the operation enters an error1452* state and must be aborted by calling psa_mac_abort().1453*1454* \param[in,out] operation Active MAC operation.1455* \param[in] input Buffer containing the message fragment to add to1456* the MAC calculation.1457* \param input_length Size of the \p input buffer in bytes.1458*1459* \retval #PSA_SUCCESS1460* Success.1461* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1462* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1463* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1464* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1465* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1466* \retval #PSA_ERROR_BAD_STATE1467* The operation state is not valid (it must be active), or1468* the library has not been previously initialized by psa_crypto_init().1469* It is implementation-dependent whether a failure to initialize1470* results in this error code.1471*/1472psa_status_t psa_mac_update(psa_mac_operation_t *operation,1473const uint8_t *input,1474size_t input_length);14751476/** Finish the calculation of the MAC of a message.1477*1478* The application must call psa_mac_sign_setup() before calling this function.1479* This function calculates the MAC of the message formed by concatenating1480* the inputs passed to preceding calls to psa_mac_update().1481*1482* When this function returns successfully, the operation becomes inactive.1483* If this function returns an error status, the operation enters an error1484* state and must be aborted by calling psa_mac_abort().1485*1486* \warning Applications should not call this function if they expect1487* a specific value for the MAC. Call psa_mac_verify_finish() instead.1488* Beware that comparing integrity or authenticity data such as1489* MAC values with a function such as \c memcmp is risky1490* because the time taken by the comparison may leak information1491* about the MAC value which could allow an attacker to guess1492* a valid MAC and thereby bypass security controls.1493*1494* \param[in,out] operation Active MAC operation.1495* \param[out] mac Buffer where the MAC value is to be written.1496* \param mac_size Size of the \p mac buffer in bytes.1497* \param[out] mac_length On success, the number of bytes1498* that make up the MAC value. This is always1499* #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)1500* where \c key_type and \c key_bits are the type and1501* bit-size respectively of the key and \c alg is the1502* MAC algorithm that is calculated.1503*1504* \retval #PSA_SUCCESS1505* Success.1506* \retval #PSA_ERROR_BUFFER_TOO_SMALL1507* The size of the \p mac buffer is too small. You can determine a1508* sufficient buffer size by calling PSA_MAC_LENGTH().1509* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1510* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1511* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1512* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1513* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1514* \retval #PSA_ERROR_BAD_STATE1515* The operation state is not valid (it must be an active mac sign1516* operation), or the library has not been previously initialized1517* by psa_crypto_init().1518* It is implementation-dependent whether a failure to initialize1519* results in this error code.1520*/1521psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,1522uint8_t *mac,1523size_t mac_size,1524size_t *mac_length);15251526/** Finish the calculation of the MAC of a message and compare it with1527* an expected value.1528*1529* The application must call psa_mac_verify_setup() before calling this function.1530* This function calculates the MAC of the message formed by concatenating1531* the inputs passed to preceding calls to psa_mac_update(). It then1532* compares the calculated MAC with the expected MAC passed as a1533* parameter to this function.1534*1535* When this function returns successfully, the operation becomes inactive.1536* If this function returns an error status, the operation enters an error1537* state and must be aborted by calling psa_mac_abort().1538*1539* \note Implementations shall make the best effort to ensure that the1540* comparison between the actual MAC and the expected MAC is performed1541* in constant time.1542*1543* \param[in,out] operation Active MAC operation.1544* \param[in] mac Buffer containing the expected MAC value.1545* \param mac_length Size of the \p mac buffer in bytes.1546*1547* \retval #PSA_SUCCESS1548* The expected MAC is identical to the actual MAC of the message.1549* \retval #PSA_ERROR_INVALID_SIGNATURE1550* The MAC of the message was calculated successfully, but it1551* differs from the expected MAC.1552* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1553* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1554* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1555* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1556* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1557* \retval #PSA_ERROR_BAD_STATE1558* The operation state is not valid (it must be an active mac verify1559* operation), or the library has not been previously initialized1560* by psa_crypto_init().1561* It is implementation-dependent whether a failure to initialize1562* results in this error code.1563*/1564psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,1565const uint8_t *mac,1566size_t mac_length);15671568/** Abort a MAC operation.1569*1570* Aborting an operation frees all associated resources except for the1571* \p operation structure itself. Once aborted, the operation object1572* can be reused for another operation by calling1573* psa_mac_sign_setup() or psa_mac_verify_setup() again.1574*1575* You may call this function any time after the operation object has1576* been initialized by one of the methods described in #psa_mac_operation_t.1577*1578* In particular, calling psa_mac_abort() after the operation has been1579* terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or1580* psa_mac_verify_finish() is safe and has no effect.1581*1582* \param[in,out] operation Initialized MAC operation.1583*1584* \retval #PSA_SUCCESS \emptydescription1585* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1586* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1587* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1588* \retval #PSA_ERROR_BAD_STATE1589* The library has not been previously initialized by psa_crypto_init().1590* It is implementation-dependent whether a failure to initialize1591* results in this error code.1592*/1593psa_status_t psa_mac_abort(psa_mac_operation_t *operation);15941595/**@}*/15961597/** \defgroup cipher Symmetric ciphers1598* @{1599*/16001601/** Encrypt a message using a symmetric cipher.1602*1603* This function encrypts a message with a random IV (initialization1604* vector). Use the multipart operation interface with a1605* #psa_cipher_operation_t object to provide other forms of IV.1606*1607* \param key Identifier of the key to use for the operation.1608* It must allow the usage #PSA_KEY_USAGE_ENCRYPT.1609* \param alg The cipher algorithm to compute1610* (\c PSA_ALG_XXX value such that1611* #PSA_ALG_IS_CIPHER(\p alg) is true).1612* \param[in] input Buffer containing the message to encrypt.1613* \param input_length Size of the \p input buffer in bytes.1614* \param[out] output Buffer where the output is to be written.1615* The output contains the IV followed by1616* the ciphertext proper.1617* \param output_size Size of the \p output buffer in bytes.1618* \param[out] output_length On success, the number of bytes1619* that make up the output.1620*1621* \retval #PSA_SUCCESS1622* Success.1623* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1624* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1625* \retval #PSA_ERROR_INVALID_ARGUMENT1626* \p key is not compatible with \p alg.1627* \retval #PSA_ERROR_NOT_SUPPORTED1628* \p alg is not supported or is not a cipher algorithm.1629* \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription1630* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1631* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1632* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1633* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1634* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1635* \retval #PSA_ERROR_BAD_STATE1636* The library has not been previously initialized by psa_crypto_init().1637* It is implementation-dependent whether a failure to initialize1638* results in this error code.1639*/1640psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,1641psa_algorithm_t alg,1642const uint8_t *input,1643size_t input_length,1644uint8_t *output,1645size_t output_size,1646size_t *output_length);16471648/** Decrypt a message using a symmetric cipher.1649*1650* This function decrypts a message encrypted with a symmetric cipher.1651*1652* \param key Identifier of the key to use for the operation.1653* It must remain valid until the operation1654* terminates. It must allow the usage1655* #PSA_KEY_USAGE_DECRYPT.1656* \param alg The cipher algorithm to compute1657* (\c PSA_ALG_XXX value such that1658* #PSA_ALG_IS_CIPHER(\p alg) is true).1659* \param[in] input Buffer containing the message to decrypt.1660* This consists of the IV followed by the1661* ciphertext proper.1662* \param input_length Size of the \p input buffer in bytes.1663* \param[out] output Buffer where the plaintext is to be written.1664* \param output_size Size of the \p output buffer in bytes.1665* \param[out] output_length On success, the number of bytes1666* that make up the output.1667*1668* \retval #PSA_SUCCESS1669* Success.1670* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1671* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1672* \retval #PSA_ERROR_INVALID_ARGUMENT1673* \p key is not compatible with \p alg.1674* \retval #PSA_ERROR_NOT_SUPPORTED1675* \p alg is not supported or is not a cipher algorithm.1676* \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription1677* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1678* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1679* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1680* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1681* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1682* \retval #PSA_ERROR_BAD_STATE1683* The library has not been previously initialized by psa_crypto_init().1684* It is implementation-dependent whether a failure to initialize1685* results in this error code.1686*/1687psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,1688psa_algorithm_t alg,1689const uint8_t *input,1690size_t input_length,1691uint8_t *output,1692size_t output_size,1693size_t *output_length);16941695/** The type of the state data structure for multipart cipher operations.1696*1697* Before calling any function on a cipher operation object, the application1698* must initialize it by any of the following means:1699* - Set the structure to all-bits-zero, for example:1700* \code1701* psa_cipher_operation_t operation;1702* memset(&operation, 0, sizeof(operation));1703* \endcode1704* - Initialize the structure to logical zero values, for example:1705* \code1706* psa_cipher_operation_t operation = {0};1707* \endcode1708* - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,1709* for example:1710* \code1711* psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;1712* \endcode1713* - Assign the result of the function psa_cipher_operation_init()1714* to the structure, for example:1715* \code1716* psa_cipher_operation_t operation;1717* operation = psa_cipher_operation_init();1718* \endcode1719*1720* This is an implementation-defined \c struct. Applications should not1721* make any assumptions about the content of this structure.1722* Implementation details can change in future versions without notice. */1723typedef struct psa_cipher_operation_s psa_cipher_operation_t;17241725/** \def PSA_CIPHER_OPERATION_INIT1726*1727* This macro returns a suitable initializer for a cipher operation object of1728* type #psa_cipher_operation_t.1729*/17301731/** Return an initial value for a cipher operation object.1732*/1733#if !(defined(__cplusplus) && defined(_MSC_VER))1734static psa_cipher_operation_t psa_cipher_operation_init(void);1735#endif17361737/** Set the key for a multipart symmetric encryption operation.1738*1739* The sequence of operations to encrypt a message with a symmetric cipher1740* is as follows:1741* -# Allocate an operation object which will be passed to all the functions1742* listed here.1743* -# Initialize the operation object with one of the methods described in the1744* documentation for #psa_cipher_operation_t, e.g.1745* #PSA_CIPHER_OPERATION_INIT.1746* -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.1747* -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to1748* generate or set the IV (initialization vector). You should use1749* psa_cipher_generate_iv() unless the protocol you are implementing1750* requires a specific IV value.1751* -# Call psa_cipher_update() zero, one or more times, passing a fragment1752* of the message each time.1753* -# Call psa_cipher_finish().1754*1755* If an error occurs at any step after a call to psa_cipher_encrypt_setup(),1756* the operation will need to be reset by a call to psa_cipher_abort(). The1757* application may call psa_cipher_abort() at any time after the operation1758* has been initialized.1759*1760* After a successful call to psa_cipher_encrypt_setup(), the application must1761* eventually terminate the operation. The following events terminate an1762* operation:1763* - A successful call to psa_cipher_finish().1764* - A call to psa_cipher_abort().1765*1766* \param[in,out] operation The operation object to set up. It must have1767* been initialized as per the documentation for1768* #psa_cipher_operation_t and not yet in use.1769* \param key Identifier of the key to use for the operation.1770* It must remain valid until the operation1771* terminates. It must allow the usage1772* #PSA_KEY_USAGE_ENCRYPT.1773* \param alg The cipher algorithm to compute1774* (\c PSA_ALG_XXX value such that1775* #PSA_ALG_IS_CIPHER(\p alg) is true).1776*1777* \retval #PSA_SUCCESS1778* Success.1779* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1780* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1781* \retval #PSA_ERROR_INVALID_ARGUMENT1782* \p key is not compatible with \p alg.1783* \retval #PSA_ERROR_NOT_SUPPORTED1784* \p alg is not supported or is not a cipher algorithm.1785* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1786* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1787* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1788* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1789* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1790* \retval #PSA_ERROR_BAD_STATE1791* The operation state is not valid (it must be inactive), or1792* the library has not been previously initialized by psa_crypto_init().1793* It is implementation-dependent whether a failure to initialize1794* results in this error code.1795*/1796psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,1797mbedtls_svc_key_id_t key,1798psa_algorithm_t alg);17991800/** Set the key for a multipart symmetric decryption operation.1801*1802* The sequence of operations to decrypt a message with a symmetric cipher1803* is as follows:1804* -# Allocate an operation object which will be passed to all the functions1805* listed here.1806* -# Initialize the operation object with one of the methods described in the1807* documentation for #psa_cipher_operation_t, e.g.1808* #PSA_CIPHER_OPERATION_INIT.1809* -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.1810* -# Call psa_cipher_set_iv() with the IV (initialization vector) for the1811* decryption. If the IV is prepended to the ciphertext, you can call1812* psa_cipher_update() on a buffer containing the IV followed by the1813* beginning of the message.1814* -# Call psa_cipher_update() zero, one or more times, passing a fragment1815* of the message each time.1816* -# Call psa_cipher_finish().1817*1818* If an error occurs at any step after a call to psa_cipher_decrypt_setup(),1819* the operation will need to be reset by a call to psa_cipher_abort(). The1820* application may call psa_cipher_abort() at any time after the operation1821* has been initialized.1822*1823* After a successful call to psa_cipher_decrypt_setup(), the application must1824* eventually terminate the operation. The following events terminate an1825* operation:1826* - A successful call to psa_cipher_finish().1827* - A call to psa_cipher_abort().1828*1829* \param[in,out] operation The operation object to set up. It must have1830* been initialized as per the documentation for1831* #psa_cipher_operation_t and not yet in use.1832* \param key Identifier of the key to use for the operation.1833* It must remain valid until the operation1834* terminates. It must allow the usage1835* #PSA_KEY_USAGE_DECRYPT.1836* \param alg The cipher algorithm to compute1837* (\c PSA_ALG_XXX value such that1838* #PSA_ALG_IS_CIPHER(\p alg) is true).1839*1840* \retval #PSA_SUCCESS1841* Success.1842* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription1843* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription1844* \retval #PSA_ERROR_INVALID_ARGUMENT1845* \p key is not compatible with \p alg.1846* \retval #PSA_ERROR_NOT_SUPPORTED1847* \p alg is not supported or is not a cipher algorithm.1848* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1849* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1850* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1851* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1852* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1853* \retval #PSA_ERROR_BAD_STATE1854* The operation state is not valid (it must be inactive), or1855* the library has not been previously initialized by psa_crypto_init().1856* It is implementation-dependent whether a failure to initialize1857* results in this error code.1858*/1859psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,1860mbedtls_svc_key_id_t key,1861psa_algorithm_t alg);18621863/** Generate an IV for a symmetric encryption operation.1864*1865* This function generates a random IV (initialization vector), nonce1866* or initial counter value for the encryption operation as appropriate1867* for the chosen algorithm, key type and key size.1868*1869* The application must call psa_cipher_encrypt_setup() before1870* calling this function.1871*1872* If this function returns an error status, the operation enters an error1873* state and must be aborted by calling psa_cipher_abort().1874*1875* \param[in,out] operation Active cipher operation.1876* \param[out] iv Buffer where the generated IV is to be written.1877* \param iv_size Size of the \p iv buffer in bytes.1878* \param[out] iv_length On success, the number of bytes of the1879* generated IV.1880*1881* \retval #PSA_SUCCESS1882* Success.1883* \retval #PSA_ERROR_BUFFER_TOO_SMALL1884* The size of the \p iv buffer is too small.1885* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1886* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1887* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1888* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1889* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1890* \retval #PSA_ERROR_BAD_STATE1891* The operation state is not valid (it must be active, with no IV set),1892* or the library has not been previously initialized1893* by psa_crypto_init().1894* It is implementation-dependent whether a failure to initialize1895* results in this error code.1896*/1897psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,1898uint8_t *iv,1899size_t iv_size,1900size_t *iv_length);19011902/** Set the IV for a symmetric encryption or decryption operation.1903*1904* This function sets the IV (initialization vector), nonce1905* or initial counter value for the encryption or decryption operation.1906*1907* The application must call psa_cipher_encrypt_setup() before1908* calling this function.1909*1910* If this function returns an error status, the operation enters an error1911* state and must be aborted by calling psa_cipher_abort().1912*1913* \note When encrypting, applications should use psa_cipher_generate_iv()1914* instead of this function, unless implementing a protocol that requires1915* a non-random IV.1916*1917* \param[in,out] operation Active cipher operation.1918* \param[in] iv Buffer containing the IV to use.1919* \param iv_length Size of the IV in bytes.1920*1921* \retval #PSA_SUCCESS1922* Success.1923* \retval #PSA_ERROR_INVALID_ARGUMENT1924* The size of \p iv is not acceptable for the chosen algorithm,1925* or the chosen algorithm does not use an IV.1926* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1927* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1928* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1929* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1930* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1931* \retval #PSA_ERROR_BAD_STATE1932* The operation state is not valid (it must be an active cipher1933* encrypt operation, with no IV set), or the library has not been1934* previously initialized by psa_crypto_init().1935* It is implementation-dependent whether a failure to initialize1936* results in this error code.1937*/1938psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,1939const uint8_t *iv,1940size_t iv_length);19411942/** Encrypt or decrypt a message fragment in an active cipher operation.1943*1944* Before calling this function, you must:1945* 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().1946* The choice of setup function determines whether this function1947* encrypts or decrypts its input.1948* 2. If the algorithm requires an IV, call psa_cipher_generate_iv()1949* (recommended when encrypting) or psa_cipher_set_iv().1950*1951* If this function returns an error status, the operation enters an error1952* state and must be aborted by calling psa_cipher_abort().1953*1954* \param[in,out] operation Active cipher operation.1955* \param[in] input Buffer containing the message fragment to1956* encrypt or decrypt.1957* \param input_length Size of the \p input buffer in bytes.1958* \param[out] output Buffer where the output is to be written.1959* \param output_size Size of the \p output buffer in bytes.1960* \param[out] output_length On success, the number of bytes1961* that make up the returned output.1962*1963* \retval #PSA_SUCCESS1964* Success.1965* \retval #PSA_ERROR_BUFFER_TOO_SMALL1966* The size of the \p output buffer is too small.1967* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription1968* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription1969* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription1970* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription1971* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription1972* \retval #PSA_ERROR_BAD_STATE1973* The operation state is not valid (it must be active, with an IV set1974* if required for the algorithm), or the library has not been1975* previously initialized by psa_crypto_init().1976* It is implementation-dependent whether a failure to initialize1977* results in this error code.1978*/1979psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,1980const uint8_t *input,1981size_t input_length,1982uint8_t *output,1983size_t output_size,1984size_t *output_length);19851986/** Finish encrypting or decrypting a message in a cipher operation.1987*1988* The application must call psa_cipher_encrypt_setup() or1989* psa_cipher_decrypt_setup() before calling this function. The choice1990* of setup function determines whether this function encrypts or1991* decrypts its input.1992*1993* This function finishes the encryption or decryption of the message1994* formed by concatenating the inputs passed to preceding calls to1995* psa_cipher_update().1996*1997* When this function returns successfully, the operation becomes inactive.1998* If this function returns an error status, the operation enters an error1999* state and must be aborted by calling psa_cipher_abort().2000*2001* \param[in,out] operation Active cipher operation.2002* \param[out] output Buffer where the output is to be written.2003* \param output_size Size of the \p output buffer in bytes.2004* \param[out] output_length On success, the number of bytes2005* that make up the returned output.2006*2007* \retval #PSA_SUCCESS2008* Success.2009* \retval #PSA_ERROR_INVALID_ARGUMENT2010* The total input size passed to this operation is not valid for2011* this particular algorithm. For example, the algorithm is a based2012* on block cipher and requires a whole number of blocks, but the2013* total input size is not a multiple of the block size.2014* \retval #PSA_ERROR_INVALID_PADDING2015* This is a decryption operation for an algorithm that includes2016* padding, and the ciphertext does not contain valid padding.2017* \retval #PSA_ERROR_BUFFER_TOO_SMALL2018* The size of the \p output buffer is too small.2019* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2020* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2021* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2022* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2023* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2024* \retval #PSA_ERROR_BAD_STATE2025* The operation state is not valid (it must be active, with an IV set2026* if required for the algorithm), or the library has not been2027* previously initialized by psa_crypto_init().2028* It is implementation-dependent whether a failure to initialize2029* results in this error code.2030*/2031psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,2032uint8_t *output,2033size_t output_size,2034size_t *output_length);20352036/** Abort a cipher operation.2037*2038* Aborting an operation frees all associated resources except for the2039* \p operation structure itself. Once aborted, the operation object2040* can be reused for another operation by calling2041* psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.2042*2043* You may call this function any time after the operation object has2044* been initialized as described in #psa_cipher_operation_t.2045*2046* In particular, calling psa_cipher_abort() after the operation has been2047* terminated by a call to psa_cipher_abort() or psa_cipher_finish()2048* is safe and has no effect.2049*2050* \param[in,out] operation Initialized cipher operation.2051*2052* \retval #PSA_SUCCESS \emptydescription2053* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2054* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2055* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2056* \retval #PSA_ERROR_BAD_STATE2057* The library has not been previously initialized by psa_crypto_init().2058* It is implementation-dependent whether a failure to initialize2059* results in this error code.2060*/2061psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);20622063/**@}*/20642065/** \defgroup aead Authenticated encryption with associated data (AEAD)2066* @{2067*/20682069/** Process an authenticated encryption operation.2070*2071* \param key Identifier of the key to use for the2072* operation. It must allow the usage2073* #PSA_KEY_USAGE_ENCRYPT.2074* \param alg The AEAD algorithm to compute2075* (\c PSA_ALG_XXX value such that2076* #PSA_ALG_IS_AEAD(\p alg) is true).2077* \param[in] nonce Nonce or IV to use.2078* \param nonce_length Size of the \p nonce buffer in bytes.2079* \param[in] additional_data Additional data that will be authenticated2080* but not encrypted.2081* \param additional_data_length Size of \p additional_data in bytes.2082* \param[in] plaintext Data that will be authenticated and2083* encrypted.2084* \param plaintext_length Size of \p plaintext in bytes.2085* \param[out] ciphertext Output buffer for the authenticated and2086* encrypted data. The additional data is not2087* part of this output. For algorithms where the2088* encrypted data and the authentication tag2089* are defined as separate outputs, the2090* authentication tag is appended to the2091* encrypted data.2092* \param ciphertext_size Size of the \p ciphertext buffer in bytes.2093* This must be appropriate for the selected2094* algorithm and key:2095* - A sufficient output size is2096* #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,2097* \p alg, \p plaintext_length) where2098* \c key_type is the type of \p key.2099* - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p2100* plaintext_length) evaluates to the maximum2101* ciphertext size of any supported AEAD2102* encryption.2103* \param[out] ciphertext_length On success, the size of the output2104* in the \p ciphertext buffer.2105*2106* \retval #PSA_SUCCESS2107* Success.2108* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2109* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription2110* \retval #PSA_ERROR_INVALID_ARGUMENT2111* \p key is not compatible with \p alg.2112* \retval #PSA_ERROR_NOT_SUPPORTED2113* \p alg is not supported or is not an AEAD algorithm.2114* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2115* \retval #PSA_ERROR_BUFFER_TOO_SMALL2116* \p ciphertext_size is too small.2117* #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,2118* \p plaintext_length) or2119* #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to2120* determine the required buffer size.2121* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2122* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2123* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2124* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2125* \retval #PSA_ERROR_BAD_STATE2126* The library has not been previously initialized by psa_crypto_init().2127* It is implementation-dependent whether a failure to initialize2128* results in this error code.2129*/2130psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,2131psa_algorithm_t alg,2132const uint8_t *nonce,2133size_t nonce_length,2134const uint8_t *additional_data,2135size_t additional_data_length,2136const uint8_t *plaintext,2137size_t plaintext_length,2138uint8_t *ciphertext,2139size_t ciphertext_size,2140size_t *ciphertext_length);21412142/** Process an authenticated decryption operation.2143*2144* \param key Identifier of the key to use for the2145* operation. It must allow the usage2146* #PSA_KEY_USAGE_DECRYPT.2147* \param alg The AEAD algorithm to compute2148* (\c PSA_ALG_XXX value such that2149* #PSA_ALG_IS_AEAD(\p alg) is true).2150* \param[in] nonce Nonce or IV to use.2151* \param nonce_length Size of the \p nonce buffer in bytes.2152* \param[in] additional_data Additional data that has been authenticated2153* but not encrypted.2154* \param additional_data_length Size of \p additional_data in bytes.2155* \param[in] ciphertext Data that has been authenticated and2156* encrypted. For algorithms where the2157* encrypted data and the authentication tag2158* are defined as separate inputs, the buffer2159* must contain the encrypted data followed2160* by the authentication tag.2161* \param ciphertext_length Size of \p ciphertext in bytes.2162* \param[out] plaintext Output buffer for the decrypted data.2163* \param plaintext_size Size of the \p plaintext buffer in bytes.2164* This must be appropriate for the selected2165* algorithm and key:2166* - A sufficient output size is2167* #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,2168* \p alg, \p ciphertext_length) where2169* \c key_type is the type of \p key.2170* - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p2171* ciphertext_length) evaluates to the maximum2172* plaintext size of any supported AEAD2173* decryption.2174* \param[out] plaintext_length On success, the size of the output2175* in the \p plaintext buffer.2176*2177* \retval #PSA_SUCCESS2178* Success.2179* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2180* \retval #PSA_ERROR_INVALID_SIGNATURE2181* The ciphertext is not authentic.2182* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription2183* \retval #PSA_ERROR_INVALID_ARGUMENT2184* \p key is not compatible with \p alg.2185* \retval #PSA_ERROR_NOT_SUPPORTED2186* \p alg is not supported or is not an AEAD algorithm.2187* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2188* \retval #PSA_ERROR_BUFFER_TOO_SMALL2189* \p plaintext_size is too small.2190* #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,2191* \p ciphertext_length) or2192* #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used2193* to determine the required buffer size.2194* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2195* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2196* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2197* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2198* \retval #PSA_ERROR_BAD_STATE2199* The library has not been previously initialized by psa_crypto_init().2200* It is implementation-dependent whether a failure to initialize2201* results in this error code.2202*/2203psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,2204psa_algorithm_t alg,2205const uint8_t *nonce,2206size_t nonce_length,2207const uint8_t *additional_data,2208size_t additional_data_length,2209const uint8_t *ciphertext,2210size_t ciphertext_length,2211uint8_t *plaintext,2212size_t plaintext_size,2213size_t *plaintext_length);22142215/** The type of the state data structure for multipart AEAD operations.2216*2217* Before calling any function on an AEAD operation object, the application2218* must initialize it by any of the following means:2219* - Set the structure to all-bits-zero, for example:2220* \code2221* psa_aead_operation_t operation;2222* memset(&operation, 0, sizeof(operation));2223* \endcode2224* - Initialize the structure to logical zero values, for example:2225* \code2226* psa_aead_operation_t operation = {0};2227* \endcode2228* - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,2229* for example:2230* \code2231* psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;2232* \endcode2233* - Assign the result of the function psa_aead_operation_init()2234* to the structure, for example:2235* \code2236* psa_aead_operation_t operation;2237* operation = psa_aead_operation_init();2238* \endcode2239*2240* This is an implementation-defined \c struct. Applications should not2241* make any assumptions about the content of this structure.2242* Implementation details can change in future versions without notice. */2243typedef struct psa_aead_operation_s psa_aead_operation_t;22442245/** \def PSA_AEAD_OPERATION_INIT2246*2247* This macro returns a suitable initializer for an AEAD operation object of2248* type #psa_aead_operation_t.2249*/22502251/** Return an initial value for an AEAD operation object.2252*/2253#if !(defined(__cplusplus) && defined(_MSC_VER))2254static psa_aead_operation_t psa_aead_operation_init(void);2255#endif22562257/** Set the key for a multipart authenticated encryption operation.2258*2259* The sequence of operations to encrypt a message with authentication2260* is as follows:2261* -# Allocate an operation object which will be passed to all the functions2262* listed here.2263* -# Initialize the operation object with one of the methods described in the2264* documentation for #psa_aead_operation_t, e.g.2265* #PSA_AEAD_OPERATION_INIT.2266* -# Call psa_aead_encrypt_setup() to specify the algorithm and key.2267* -# If needed, call psa_aead_set_lengths() to specify the length of the2268* inputs to the subsequent calls to psa_aead_update_ad() and2269* psa_aead_update(). See the documentation of psa_aead_set_lengths()2270* for details.2271* -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to2272* generate or set the nonce. You should use2273* psa_aead_generate_nonce() unless the protocol you are implementing2274* requires a specific nonce value.2275* -# Call psa_aead_update_ad() zero, one or more times, passing a fragment2276* of the non-encrypted additional authenticated data each time.2277* -# Call psa_aead_update() zero, one or more times, passing a fragment2278* of the message to encrypt each time.2279* -# Call psa_aead_finish().2280*2281* If an error occurs at any step after a call to psa_aead_encrypt_setup(),2282* the operation will need to be reset by a call to psa_aead_abort(). The2283* application may call psa_aead_abort() at any time after the operation2284* has been initialized.2285*2286* After a successful call to psa_aead_encrypt_setup(), the application must2287* eventually terminate the operation. The following events terminate an2288* operation:2289* - A successful call to psa_aead_finish().2290* - A call to psa_aead_abort().2291*2292* \param[in,out] operation The operation object to set up. It must have2293* been initialized as per the documentation for2294* #psa_aead_operation_t and not yet in use.2295* \param key Identifier of the key to use for the operation.2296* It must remain valid until the operation2297* terminates. It must allow the usage2298* #PSA_KEY_USAGE_ENCRYPT.2299* \param alg The AEAD algorithm to compute2300* (\c PSA_ALG_XXX value such that2301* #PSA_ALG_IS_AEAD(\p alg) is true).2302*2303* \retval #PSA_SUCCESS2304* Success.2305* \retval #PSA_ERROR_BAD_STATE2306* The operation state is not valid (it must be inactive), or2307* the library has not been previously initialized by psa_crypto_init().2308* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2309* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription2310* \retval #PSA_ERROR_INVALID_ARGUMENT2311* \p key is not compatible with \p alg.2312* \retval #PSA_ERROR_NOT_SUPPORTED2313* \p alg is not supported or is not an AEAD algorithm.2314* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2315* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2316* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2317* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2318* \retval #PSA_ERROR_STORAGE_FAILURE2319* The library has not been previously initialized by psa_crypto_init().2320* It is implementation-dependent whether a failure to initialize2321* results in this error code.2322*/2323psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,2324mbedtls_svc_key_id_t key,2325psa_algorithm_t alg);23262327/** Set the key for a multipart authenticated decryption operation.2328*2329* The sequence of operations to decrypt a message with authentication2330* is as follows:2331* -# Allocate an operation object which will be passed to all the functions2332* listed here.2333* -# Initialize the operation object with one of the methods described in the2334* documentation for #psa_aead_operation_t, e.g.2335* #PSA_AEAD_OPERATION_INIT.2336* -# Call psa_aead_decrypt_setup() to specify the algorithm and key.2337* -# If needed, call psa_aead_set_lengths() to specify the length of the2338* inputs to the subsequent calls to psa_aead_update_ad() and2339* psa_aead_update(). See the documentation of psa_aead_set_lengths()2340* for details.2341* -# Call psa_aead_set_nonce() with the nonce for the decryption.2342* -# Call psa_aead_update_ad() zero, one or more times, passing a fragment2343* of the non-encrypted additional authenticated data each time.2344* -# Call psa_aead_update() zero, one or more times, passing a fragment2345* of the ciphertext to decrypt each time.2346* -# Call psa_aead_verify().2347*2348* If an error occurs at any step after a call to psa_aead_decrypt_setup(),2349* the operation will need to be reset by a call to psa_aead_abort(). The2350* application may call psa_aead_abort() at any time after the operation2351* has been initialized.2352*2353* After a successful call to psa_aead_decrypt_setup(), the application must2354* eventually terminate the operation. The following events terminate an2355* operation:2356* - A successful call to psa_aead_verify().2357* - A call to psa_aead_abort().2358*2359* \param[in,out] operation The operation object to set up. It must have2360* been initialized as per the documentation for2361* #psa_aead_operation_t and not yet in use.2362* \param key Identifier of the key to use for the operation.2363* It must remain valid until the operation2364* terminates. It must allow the usage2365* #PSA_KEY_USAGE_DECRYPT.2366* \param alg The AEAD algorithm to compute2367* (\c PSA_ALG_XXX value such that2368* #PSA_ALG_IS_AEAD(\p alg) is true).2369*2370* \retval #PSA_SUCCESS2371* Success.2372* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2373* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription2374* \retval #PSA_ERROR_INVALID_ARGUMENT2375* \p key is not compatible with \p alg.2376* \retval #PSA_ERROR_NOT_SUPPORTED2377* \p alg is not supported or is not an AEAD algorithm.2378* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2379* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2380* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2381* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2382* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2383* \retval #PSA_ERROR_BAD_STATE2384* The operation state is not valid (it must be inactive), or the2385* library has not been previously initialized by psa_crypto_init().2386* It is implementation-dependent whether a failure to initialize2387* results in this error code.2388*/2389psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,2390mbedtls_svc_key_id_t key,2391psa_algorithm_t alg);23922393/** Generate a random nonce for an authenticated encryption operation.2394*2395* This function generates a random nonce for the authenticated encryption2396* operation with an appropriate size for the chosen algorithm, key type2397* and key size.2398*2399* The application must call psa_aead_encrypt_setup() before2400* calling this function.2401*2402* If this function returns an error status, the operation enters an error2403* state and must be aborted by calling psa_aead_abort().2404*2405* \param[in,out] operation Active AEAD operation.2406* \param[out] nonce Buffer where the generated nonce is to be2407* written.2408* \param nonce_size Size of the \p nonce buffer in bytes.2409* \param[out] nonce_length On success, the number of bytes of the2410* generated nonce.2411*2412* \retval #PSA_SUCCESS2413* Success.2414* \retval #PSA_ERROR_BUFFER_TOO_SMALL2415* The size of the \p nonce buffer is too small.2416* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2417* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2418* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2419* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2420* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2421* \retval #PSA_ERROR_BAD_STATE2422* The operation state is not valid (it must be an active aead encrypt2423* operation, with no nonce set), or the library has not been2424* previously initialized by psa_crypto_init().2425* It is implementation-dependent whether a failure to initialize2426* results in this error code.2427*/2428psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,2429uint8_t *nonce,2430size_t nonce_size,2431size_t *nonce_length);24322433/** Set the nonce for an authenticated encryption or decryption operation.2434*2435* This function sets the nonce for the authenticated2436* encryption or decryption operation.2437*2438* The application must call psa_aead_encrypt_setup() or2439* psa_aead_decrypt_setup() before calling this function.2440*2441* If this function returns an error status, the operation enters an error2442* state and must be aborted by calling psa_aead_abort().2443*2444* \note When encrypting, applications should use psa_aead_generate_nonce()2445* instead of this function, unless implementing a protocol that requires2446* a non-random IV.2447*2448* \param[in,out] operation Active AEAD operation.2449* \param[in] nonce Buffer containing the nonce to use.2450* \param nonce_length Size of the nonce in bytes.2451*2452* \retval #PSA_SUCCESS2453* Success.2454* \retval #PSA_ERROR_INVALID_ARGUMENT2455* The size of \p nonce is not acceptable for the chosen algorithm.2456* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2457* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2458* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2459* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2460* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2461* \retval #PSA_ERROR_BAD_STATE2462* The operation state is not valid (it must be active, with no nonce2463* set), or the library has not been previously initialized2464* by psa_crypto_init().2465* It is implementation-dependent whether a failure to initialize2466* results in this error code.2467*/2468psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,2469const uint8_t *nonce,2470size_t nonce_length);24712472/** Declare the lengths of the message and additional data for AEAD.2473*2474* The application must call this function before calling2475* psa_aead_update_ad() or psa_aead_update() if the algorithm for2476* the operation requires it. If the algorithm does not require it,2477* calling this function is optional, but if this function is called2478* then the implementation must enforce the lengths.2479*2480* You may call this function before or after setting the nonce with2481* psa_aead_set_nonce() or psa_aead_generate_nonce().2482*2483* - For #PSA_ALG_CCM, calling this function is required.2484* - For the other AEAD algorithms defined in this specification, calling2485* this function is not required.2486* - For vendor-defined algorithm, refer to the vendor documentation.2487*2488* If this function returns an error status, the operation enters an error2489* state and must be aborted by calling psa_aead_abort().2490*2491* \param[in,out] operation Active AEAD operation.2492* \param ad_length Size of the non-encrypted additional2493* authenticated data in bytes.2494* \param plaintext_length Size of the plaintext to encrypt in bytes.2495*2496* \retval #PSA_SUCCESS2497* Success.2498* \retval #PSA_ERROR_INVALID_ARGUMENT2499* At least one of the lengths is not acceptable for the chosen2500* algorithm.2501* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2502* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2503* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2504* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2505* \retval #PSA_ERROR_BAD_STATE2506* The operation state is not valid (it must be active, and2507* psa_aead_update_ad() and psa_aead_update() must not have been2508* called yet), or the library has not been previously initialized2509* by psa_crypto_init().2510* It is implementation-dependent whether a failure to initialize2511* results in this error code.2512*/2513psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,2514size_t ad_length,2515size_t plaintext_length);25162517/** Pass additional data to an active AEAD operation.2518*2519* Additional data is authenticated, but not encrypted.2520*2521* You may call this function multiple times to pass successive fragments2522* of the additional data. You may not call this function after passing2523* data to encrypt or decrypt with psa_aead_update().2524*2525* Before calling this function, you must:2526* 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().2527* 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().2528*2529* If this function returns an error status, the operation enters an error2530* state and must be aborted by calling psa_aead_abort().2531*2532* \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,2533* there is no guarantee that the input is valid. Therefore, until2534* you have called psa_aead_verify() and it has returned #PSA_SUCCESS,2535* treat the input as untrusted and prepare to undo any action that2536* depends on the input if psa_aead_verify() returns an error status.2537*2538* \param[in,out] operation Active AEAD operation.2539* \param[in] input Buffer containing the fragment of2540* additional data.2541* \param input_length Size of the \p input buffer in bytes.2542*2543* \retval #PSA_SUCCESS2544* Success.2545* \retval #PSA_ERROR_INVALID_ARGUMENT2546* The total input length overflows the additional data length that2547* was previously specified with psa_aead_set_lengths().2548* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2549* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2550* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2551* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2552* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2553* \retval #PSA_ERROR_BAD_STATE2554* The operation state is not valid (it must be active, have a nonce2555* set, have lengths set if required by the algorithm, and2556* psa_aead_update() must not have been called yet), or the library2557* has not been previously initialized by psa_crypto_init().2558* It is implementation-dependent whether a failure to initialize2559* results in this error code.2560*/2561psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,2562const uint8_t *input,2563size_t input_length);25642565/** Encrypt or decrypt a message fragment in an active AEAD operation.2566*2567* Before calling this function, you must:2568* 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().2569* The choice of setup function determines whether this function2570* encrypts or decrypts its input.2571* 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().2572* 3. Call psa_aead_update_ad() to pass all the additional data.2573*2574* If this function returns an error status, the operation enters an error2575* state and must be aborted by calling psa_aead_abort().2576*2577* \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,2578* there is no guarantee that the input is valid. Therefore, until2579* you have called psa_aead_verify() and it has returned #PSA_SUCCESS:2580* - Do not use the output in any way other than storing it in a2581* confidential location. If you take any action that depends2582* on the tentative decrypted data, this action will need to be2583* undone if the input turns out not to be valid. Furthermore,2584* if an adversary can observe that this action took place2585* (for example through timing), they may be able to use this2586* fact as an oracle to decrypt any message encrypted with the2587* same key.2588* - In particular, do not copy the output anywhere but to a2589* memory or storage space that you have exclusive access to.2590*2591* This function does not require the input to be aligned to any2592* particular block boundary. If the implementation can only process2593* a whole block at a time, it must consume all the input provided, but2594* it may delay the end of the corresponding output until a subsequent2595* call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()2596* provides sufficient input. The amount of data that can be delayed2597* in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.2598*2599* \param[in,out] operation Active AEAD operation.2600* \param[in] input Buffer containing the message fragment to2601* encrypt or decrypt.2602* \param input_length Size of the \p input buffer in bytes.2603* \param[out] output Buffer where the output is to be written.2604* \param output_size Size of the \p output buffer in bytes.2605* This must be appropriate for the selected2606* algorithm and key:2607* - A sufficient output size is2608* #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,2609* \c alg, \p input_length) where2610* \c key_type is the type of key and \c alg is2611* the algorithm that were used to set up the2612* operation.2613* - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p2614* input_length) evaluates to the maximum2615* output size of any supported AEAD2616* algorithm.2617* \param[out] output_length On success, the number of bytes2618* that make up the returned output.2619*2620* \retval #PSA_SUCCESS2621* Success.2622* \retval #PSA_ERROR_BUFFER_TOO_SMALL2623* The size of the \p output buffer is too small.2624* #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or2625* #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to2626* determine the required buffer size.2627* \retval #PSA_ERROR_INVALID_ARGUMENT2628* The total length of input to psa_aead_update_ad() so far is2629* less than the additional data length that was previously2630* specified with psa_aead_set_lengths(), or2631* the total input length overflows the plaintext length that2632* was previously specified with psa_aead_set_lengths().2633* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2634* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2635* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2636* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2637* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2638* \retval #PSA_ERROR_BAD_STATE2639* The operation state is not valid (it must be active, have a nonce2640* set, and have lengths set if required by the algorithm), or the2641* library has not been previously initialized by psa_crypto_init().2642* It is implementation-dependent whether a failure to initialize2643* results in this error code.2644*/2645psa_status_t psa_aead_update(psa_aead_operation_t *operation,2646const uint8_t *input,2647size_t input_length,2648uint8_t *output,2649size_t output_size,2650size_t *output_length);26512652/** Finish encrypting a message in an AEAD operation.2653*2654* The operation must have been set up with psa_aead_encrypt_setup().2655*2656* This function finishes the authentication of the additional data2657* formed by concatenating the inputs passed to preceding calls to2658* psa_aead_update_ad() with the plaintext formed by concatenating the2659* inputs passed to preceding calls to psa_aead_update().2660*2661* This function has two output buffers:2662* - \p ciphertext contains trailing ciphertext that was buffered from2663* preceding calls to psa_aead_update().2664* - \p tag contains the authentication tag.2665*2666* When this function returns successfully, the operation becomes inactive.2667* If this function returns an error status, the operation enters an error2668* state and must be aborted by calling psa_aead_abort().2669*2670* \param[in,out] operation Active AEAD operation.2671* \param[out] ciphertext Buffer where the last part of the ciphertext2672* is to be written.2673* \param ciphertext_size Size of the \p ciphertext buffer in bytes.2674* This must be appropriate for the selected2675* algorithm and key:2676* - A sufficient output size is2677* #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,2678* \c alg) where \c key_type is the type of key2679* and \c alg is the algorithm that were used to2680* set up the operation.2681* - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to2682* the maximum output size of any supported AEAD2683* algorithm.2684* \param[out] ciphertext_length On success, the number of bytes of2685* returned ciphertext.2686* \param[out] tag Buffer where the authentication tag is2687* to be written.2688* \param tag_size Size of the \p tag buffer in bytes.2689* This must be appropriate for the selected2690* algorithm and key:2691* - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c2692* key_type, \c key_bits, \c alg) where2693* \c key_type and \c key_bits are the type and2694* bit-size of the key, and \c alg is the2695* algorithm that were used in the call to2696* psa_aead_encrypt_setup().2697* - #PSA_AEAD_TAG_MAX_SIZE evaluates to the2698* maximum tag size of any supported AEAD2699* algorithm.2700* \param[out] tag_length On success, the number of bytes2701* that make up the returned tag.2702*2703* \retval #PSA_SUCCESS2704* Success.2705* \retval #PSA_ERROR_BUFFER_TOO_SMALL2706* The size of the \p ciphertext or \p tag buffer is too small.2707* #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or2708* #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the2709* required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,2710* \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to2711* determine the required \p tag buffer size.2712* \retval #PSA_ERROR_INVALID_ARGUMENT2713* The total length of input to psa_aead_update_ad() so far is2714* less than the additional data length that was previously2715* specified with psa_aead_set_lengths(), or2716* the total length of input to psa_aead_update() so far is2717* less than the plaintext length that was previously2718* specified with psa_aead_set_lengths().2719* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2720* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2721* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2722* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2723* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2724* \retval #PSA_ERROR_BAD_STATE2725* The operation state is not valid (it must be an active encryption2726* operation with a nonce set), or the library has not been previously2727* initialized by psa_crypto_init().2728* It is implementation-dependent whether a failure to initialize2729* results in this error code.2730*/2731psa_status_t psa_aead_finish(psa_aead_operation_t *operation,2732uint8_t *ciphertext,2733size_t ciphertext_size,2734size_t *ciphertext_length,2735uint8_t *tag,2736size_t tag_size,2737size_t *tag_length);27382739/** Finish authenticating and decrypting a message in an AEAD operation.2740*2741* The operation must have been set up with psa_aead_decrypt_setup().2742*2743* This function finishes the authenticated decryption of the message2744* components:2745*2746* - The additional data consisting of the concatenation of the inputs2747* passed to preceding calls to psa_aead_update_ad().2748* - The ciphertext consisting of the concatenation of the inputs passed to2749* preceding calls to psa_aead_update().2750* - The tag passed to this function call.2751*2752* If the authentication tag is correct, this function outputs any remaining2753* plaintext and reports success. If the authentication tag is not correct,2754* this function returns #PSA_ERROR_INVALID_SIGNATURE.2755*2756* When this function returns successfully, the operation becomes inactive.2757* If this function returns an error status, the operation enters an error2758* state and must be aborted by calling psa_aead_abort().2759*2760* \note Implementations shall make the best effort to ensure that the2761* comparison between the actual tag and the expected tag is performed2762* in constant time.2763*2764* \param[in,out] operation Active AEAD operation.2765* \param[out] plaintext Buffer where the last part of the plaintext2766* is to be written. This is the remaining data2767* from previous calls to psa_aead_update()2768* that could not be processed until the end2769* of the input.2770* \param plaintext_size Size of the \p plaintext buffer in bytes.2771* This must be appropriate for the selected algorithm and key:2772* - A sufficient output size is2773* #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,2774* \c alg) where \c key_type is the type of key2775* and \c alg is the algorithm that were used to2776* set up the operation.2777* - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to2778* the maximum output size of any supported AEAD2779* algorithm.2780* \param[out] plaintext_length On success, the number of bytes of2781* returned plaintext.2782* \param[in] tag Buffer containing the authentication tag.2783* \param tag_length Size of the \p tag buffer in bytes.2784*2785* \retval #PSA_SUCCESS2786* Success.2787* \retval #PSA_ERROR_INVALID_SIGNATURE2788* The calculations were successful, but the authentication tag is2789* not correct.2790* \retval #PSA_ERROR_BUFFER_TOO_SMALL2791* The size of the \p plaintext buffer is too small.2792* #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or2793* #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the2794* required buffer size.2795* \retval #PSA_ERROR_INVALID_ARGUMENT2796* The total length of input to psa_aead_update_ad() so far is2797* less than the additional data length that was previously2798* specified with psa_aead_set_lengths(), or2799* the total length of input to psa_aead_update() so far is2800* less than the plaintext length that was previously2801* specified with psa_aead_set_lengths().2802* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2803* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2804* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2805* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2806* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2807* \retval #PSA_ERROR_BAD_STATE2808* The operation state is not valid (it must be an active decryption2809* operation with a nonce set), or the library has not been previously2810* initialized by psa_crypto_init().2811* It is implementation-dependent whether a failure to initialize2812* results in this error code.2813*/2814psa_status_t psa_aead_verify(psa_aead_operation_t *operation,2815uint8_t *plaintext,2816size_t plaintext_size,2817size_t *plaintext_length,2818const uint8_t *tag,2819size_t tag_length);28202821/** Abort an AEAD operation.2822*2823* Aborting an operation frees all associated resources except for the2824* \p operation structure itself. Once aborted, the operation object2825* can be reused for another operation by calling2826* psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.2827*2828* You may call this function any time after the operation object has2829* been initialized as described in #psa_aead_operation_t.2830*2831* In particular, calling psa_aead_abort() after the operation has been2832* terminated by a call to psa_aead_abort(), psa_aead_finish() or2833* psa_aead_verify() is safe and has no effect.2834*2835* \param[in,out] operation Initialized AEAD operation.2836*2837* \retval #PSA_SUCCESS \emptydescription2838* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2839* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2840* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2841* \retval #PSA_ERROR_BAD_STATE2842* The library has not been previously initialized by psa_crypto_init().2843* It is implementation-dependent whether a failure to initialize2844* results in this error code.2845*/2846psa_status_t psa_aead_abort(psa_aead_operation_t *operation);28472848/**@}*/28492850/** \defgroup asymmetric Asymmetric cryptography2851* @{2852*/28532854/**2855* \brief Sign a message with a private key. For hash-and-sign algorithms,2856* this includes the hashing step.2857*2858* \note To perform a multi-part hash-and-sign signature algorithm, first use2859* a multi-part hash operation and then pass the resulting hash to2860* psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the2861* hash algorithm to use.2862*2863* \param[in] key Identifier of the key to use for the operation.2864* It must be an asymmetric key pair. The key must2865* allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.2866* \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX2867* value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)2868* is true), that is compatible with the type of2869* \p key.2870* \param[in] input The input message to sign.2871* \param[in] input_length Size of the \p input buffer in bytes.2872* \param[out] signature Buffer where the signature is to be written.2873* \param[in] signature_size Size of the \p signature buffer in bytes. This2874* must be appropriate for the selected2875* algorithm and key:2876* - The required signature size is2877* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)2878* where \c key_type and \c key_bits are the type and2879* bit-size respectively of key.2880* - #PSA_SIGNATURE_MAX_SIZE evaluates to the2881* maximum signature size of any supported2882* signature algorithm.2883* \param[out] signature_length On success, the number of bytes that make up2884* the returned signature value.2885*2886* \retval #PSA_SUCCESS \emptydescription2887* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2888* \retval #PSA_ERROR_NOT_PERMITTED2889* The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,2890* or it does not permit the requested algorithm.2891* \retval #PSA_ERROR_BUFFER_TOO_SMALL2892* The size of the \p signature buffer is too small. You can2893* determine a sufficient buffer size by calling2894* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)2895* where \c key_type and \c key_bits are the type and bit-size2896* respectively of \p key.2897* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription2898* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription2899* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2900* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2901* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2902* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2903* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2904* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription2905* \retval #PSA_ERROR_DATA_INVALID \emptydescription2906* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription2907* \retval #PSA_ERROR_BAD_STATE2908* The library has not been previously initialized by psa_crypto_init().2909* It is implementation-dependent whether a failure to initialize2910* results in this error code.2911*/2912psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,2913psa_algorithm_t alg,2914const uint8_t *input,2915size_t input_length,2916uint8_t *signature,2917size_t signature_size,2918size_t *signature_length);29192920/** \brief Verify the signature of a message with a public key, using2921* a hash-and-sign verification algorithm.2922*2923* \note To perform a multi-part hash-and-sign signature verification2924* algorithm, first use a multi-part hash operation to hash the message2925* and then pass the resulting hash to psa_verify_hash().2926* PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm2927* to use.2928*2929* \param[in] key Identifier of the key to use for the operation.2930* It must be a public key or an asymmetric key2931* pair. The key must allow the usage2932* #PSA_KEY_USAGE_VERIFY_MESSAGE.2933* \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX2934* value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)2935* is true), that is compatible with the type of2936* \p key.2937* \param[in] input The message whose signature is to be verified.2938* \param[in] input_length Size of the \p input buffer in bytes.2939* \param[in] signature Buffer containing the signature to verify.2940* \param[in] signature_length Size of the \p signature buffer in bytes.2941*2942* \retval #PSA_SUCCESS \emptydescription2943* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2944* \retval #PSA_ERROR_NOT_PERMITTED2945* The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,2946* or it does not permit the requested algorithm.2947* \retval #PSA_ERROR_INVALID_SIGNATURE2948* The calculation was performed successfully, but the passed signature2949* is not a valid signature.2950* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription2951* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription2952* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription2953* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription2954* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription2955* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription2956* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription2957* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription2958* \retval #PSA_ERROR_DATA_INVALID \emptydescription2959* \retval #PSA_ERROR_BAD_STATE2960* The library has not been previously initialized by psa_crypto_init().2961* It is implementation-dependent whether a failure to initialize2962* results in this error code.2963*/2964psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,2965psa_algorithm_t alg,2966const uint8_t *input,2967size_t input_length,2968const uint8_t *signature,2969size_t signature_length);29702971/**2972* \brief Sign a hash or short message with a private key.2973*2974* Note that to perform a hash-and-sign signature algorithm, you must2975* first calculate the hash by calling psa_hash_setup(), psa_hash_update()2976* and psa_hash_finish(), or alternatively by calling psa_hash_compute().2977* Then pass the resulting hash as the \p hash2978* parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)2979* to determine the hash algorithm to use.2980*2981* \param key Identifier of the key to use for the operation.2982* It must be an asymmetric key pair. The key must2983* allow the usage #PSA_KEY_USAGE_SIGN_HASH.2984* \param alg A signature algorithm (PSA_ALG_XXX2985* value such that #PSA_ALG_IS_SIGN_HASH(\p alg)2986* is true), that is compatible with2987* the type of \p key.2988* \param[in] hash The hash or message to sign.2989* \param hash_length Size of the \p hash buffer in bytes.2990* \param[out] signature Buffer where the signature is to be written.2991* \param signature_size Size of the \p signature buffer in bytes.2992* \param[out] signature_length On success, the number of bytes2993* that make up the returned signature value.2994*2995* \retval #PSA_SUCCESS \emptydescription2996* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription2997* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription2998* \retval #PSA_ERROR_BUFFER_TOO_SMALL2999* The size of the \p signature buffer is too small. You can3000* determine a sufficient buffer size by calling3001* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)3002* where \c key_type and \c key_bits are the type and bit-size3003* respectively of \p key.3004* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription3005* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription3006* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3007* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3008* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3009* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3010* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3011* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription3012* \retval #PSA_ERROR_BAD_STATE3013* The library has not been previously initialized by psa_crypto_init().3014* It is implementation-dependent whether a failure to initialize3015* results in this error code.3016*/3017psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,3018psa_algorithm_t alg,3019const uint8_t *hash,3020size_t hash_length,3021uint8_t *signature,3022size_t signature_size,3023size_t *signature_length);30243025/**3026* \brief Verify the signature of a hash or short message using a public key.3027*3028* Note that to perform a hash-and-sign signature algorithm, you must3029* first calculate the hash by calling psa_hash_setup(), psa_hash_update()3030* and psa_hash_finish(), or alternatively by calling psa_hash_compute().3031* Then pass the resulting hash as the \p hash3032* parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)3033* to determine the hash algorithm to use.3034*3035* \param key Identifier of the key to use for the operation. It3036* must be a public key or an asymmetric key pair. The3037* key must allow the usage3038* #PSA_KEY_USAGE_VERIFY_HASH.3039* \param alg A signature algorithm (PSA_ALG_XXX3040* value such that #PSA_ALG_IS_SIGN_HASH(\p alg)3041* is true), that is compatible with3042* the type of \p key.3043* \param[in] hash The hash or message whose signature is to be3044* verified.3045* \param hash_length Size of the \p hash buffer in bytes.3046* \param[in] signature Buffer containing the signature to verify.3047* \param signature_length Size of the \p signature buffer in bytes.3048*3049* \retval #PSA_SUCCESS3050* The signature is valid.3051* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription3052* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription3053* \retval #PSA_ERROR_INVALID_SIGNATURE3054* The calculation was performed successfully, but the passed3055* signature is not a valid signature.3056* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription3057* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription3058* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3059* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3060* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3061* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3062* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3063* \retval #PSA_ERROR_BAD_STATE3064* The library has not been previously initialized by psa_crypto_init().3065* It is implementation-dependent whether a failure to initialize3066* results in this error code.3067*/3068psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,3069psa_algorithm_t alg,3070const uint8_t *hash,3071size_t hash_length,3072const uint8_t *signature,3073size_t signature_length);30743075/**3076* \brief Encrypt a short message with a public key.3077*3078* \param key Identifier of the key to use for the operation.3079* It must be a public key or an asymmetric key3080* pair. It must allow the usage3081* #PSA_KEY_USAGE_ENCRYPT.3082* \param alg An asymmetric encryption algorithm that is3083* compatible with the type of \p key.3084* \param[in] input The message to encrypt.3085* \param input_length Size of the \p input buffer in bytes.3086* \param[in] salt A salt or label, if supported by the3087* encryption algorithm.3088* If the algorithm does not support a3089* salt, pass \c NULL.3090* If the algorithm supports an optional3091* salt and you do not want to pass a salt,3092* pass \c NULL.3093*3094* - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is3095* supported.3096* \param salt_length Size of the \p salt buffer in bytes.3097* If \p salt is \c NULL, pass 0.3098* \param[out] output Buffer where the encrypted message is to3099* be written.3100* \param output_size Size of the \p output buffer in bytes.3101* \param[out] output_length On success, the number of bytes3102* that make up the returned output.3103*3104* \retval #PSA_SUCCESS \emptydescription3105* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription3106* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription3107* \retval #PSA_ERROR_BUFFER_TOO_SMALL3108* The size of the \p output buffer is too small. You can3109* determine a sufficient buffer size by calling3110* #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)3111* where \c key_type and \c key_bits are the type and bit-size3112* respectively of \p key.3113* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription3114* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription3115* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3116* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3117* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3118* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3119* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3120* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription3121* \retval #PSA_ERROR_BAD_STATE3122* The library has not been previously initialized by psa_crypto_init().3123* It is implementation-dependent whether a failure to initialize3124* results in this error code.3125*/3126psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,3127psa_algorithm_t alg,3128const uint8_t *input,3129size_t input_length,3130const uint8_t *salt,3131size_t salt_length,3132uint8_t *output,3133size_t output_size,3134size_t *output_length);31353136/**3137* \brief Decrypt a short message with a private key.3138*3139* \param key Identifier of the key to use for the operation.3140* It must be an asymmetric key pair. It must3141* allow the usage #PSA_KEY_USAGE_DECRYPT.3142* \param alg An asymmetric encryption algorithm that is3143* compatible with the type of \p key.3144* \param[in] input The message to decrypt.3145* \param input_length Size of the \p input buffer in bytes.3146* \param[in] salt A salt or label, if supported by the3147* encryption algorithm.3148* If the algorithm does not support a3149* salt, pass \c NULL.3150* If the algorithm supports an optional3151* salt and you do not want to pass a salt,3152* pass \c NULL.3153*3154* - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is3155* supported.3156* \param salt_length Size of the \p salt buffer in bytes.3157* If \p salt is \c NULL, pass 0.3158* \param[out] output Buffer where the decrypted message is to3159* be written.3160* \param output_size Size of the \c output buffer in bytes.3161* \param[out] output_length On success, the number of bytes3162* that make up the returned output.3163*3164* \retval #PSA_SUCCESS \emptydescription3165* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription3166* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription3167* \retval #PSA_ERROR_BUFFER_TOO_SMALL3168* The size of the \p output buffer is too small. You can3169* determine a sufficient buffer size by calling3170* #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)3171* where \c key_type and \c key_bits are the type and bit-size3172* respectively of \p key.3173* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription3174* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription3175* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3176* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3177* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3178* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3179* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3180* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription3181* \retval #PSA_ERROR_INVALID_PADDING \emptydescription3182* \retval #PSA_ERROR_BAD_STATE3183* The library has not been previously initialized by psa_crypto_init().3184* It is implementation-dependent whether a failure to initialize3185* results in this error code.3186*/3187psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,3188psa_algorithm_t alg,3189const uint8_t *input,3190size_t input_length,3191const uint8_t *salt,3192size_t salt_length,3193uint8_t *output,3194size_t output_size,3195size_t *output_length);31963197/**@}*/31983199/** \defgroup key_derivation Key derivation and pseudorandom generation3200* @{3201*/32023203/** The type of the state data structure for key derivation operations.3204*3205* Before calling any function on a key derivation operation object, the3206* application must initialize it by any of the following means:3207* - Set the structure to all-bits-zero, for example:3208* \code3209* psa_key_derivation_operation_t operation;3210* memset(&operation, 0, sizeof(operation));3211* \endcode3212* - Initialize the structure to logical zero values, for example:3213* \code3214* psa_key_derivation_operation_t operation = {0};3215* \endcode3216* - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,3217* for example:3218* \code3219* psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;3220* \endcode3221* - Assign the result of the function psa_key_derivation_operation_init()3222* to the structure, for example:3223* \code3224* psa_key_derivation_operation_t operation;3225* operation = psa_key_derivation_operation_init();3226* \endcode3227*3228* This is an implementation-defined \c struct. Applications should not3229* make any assumptions about the content of this structure.3230* Implementation details can change in future versions without notice.3231*/3232typedef struct psa_key_derivation_s psa_key_derivation_operation_t;32333234/** \def PSA_KEY_DERIVATION_OPERATION_INIT3235*3236* This macro returns a suitable initializer for a key derivation operation3237* object of type #psa_key_derivation_operation_t.3238*/32393240/** Return an initial value for a key derivation operation object.3241*/3242#if !(defined(__cplusplus) && defined(_MSC_VER))3243static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);3244#endif32453246/** Set up a key derivation operation.3247*3248* A key derivation algorithm takes some inputs and uses them to generate3249* a byte stream in a deterministic way.3250* This byte stream can be used to produce keys and other3251* cryptographic material.3252*3253* To derive a key:3254* -# Start with an initialized object of type #psa_key_derivation_operation_t.3255* -# Call psa_key_derivation_setup() to select the algorithm.3256* -# Provide the inputs for the key derivation by calling3257* psa_key_derivation_input_bytes() or psa_key_derivation_input_key()3258* as appropriate. Which inputs are needed, in what order, and whether3259* they may be keys and if so of what type depends on the algorithm.3260* -# Optionally set the operation's maximum capacity with3261* psa_key_derivation_set_capacity(). You may do this before, in the middle3262* of or after providing inputs. For some algorithms, this step is mandatory3263* because the output depends on the maximum capacity.3264* -# To derive a key, call psa_key_derivation_output_key() or3265* psa_key_derivation_output_key_custom().3266* To derive a byte string for a different purpose, call3267* psa_key_derivation_output_bytes().3268* Successive calls to these functions use successive output bytes3269* calculated by the key derivation algorithm.3270* -# Clean up the key derivation operation object with3271* psa_key_derivation_abort().3272*3273* If this function returns an error, the key derivation operation object is3274* not changed.3275*3276* If an error occurs at any step after a call to psa_key_derivation_setup(),3277* the operation will need to be reset by a call to psa_key_derivation_abort().3278*3279* Implementations must reject an attempt to derive a key of size 0.3280*3281* \param[in,out] operation The key derivation operation object3282* to set up. It must3283* have been initialized but not set up yet.3284* \param alg The key derivation algorithm to compute3285* (\c PSA_ALG_XXX value such that3286* #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).3287*3288* \retval #PSA_SUCCESS3289* Success.3290* \retval #PSA_ERROR_INVALID_ARGUMENT3291* \c alg is not a key derivation algorithm.3292* \retval #PSA_ERROR_NOT_SUPPORTED3293* \c alg is not supported or is not a key derivation algorithm.3294* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3295* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3296* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3297* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3298* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3299* \retval #PSA_ERROR_BAD_STATE3300* The operation state is not valid (it must be inactive), or3301* the library has not been previously initialized by psa_crypto_init().3302* It is implementation-dependent whether a failure to initialize3303* results in this error code.3304*/3305psa_status_t psa_key_derivation_setup(3306psa_key_derivation_operation_t *operation,3307psa_algorithm_t alg);33083309/** Retrieve the current capacity of a key derivation operation.3310*3311* The capacity of a key derivation is the maximum number of bytes that it can3312* return. When you get *N* bytes of output from a key derivation operation,3313* this reduces its capacity by *N*.3314*3315* \param[in] operation The operation to query.3316* \param[out] capacity On success, the capacity of the operation.3317*3318* \retval #PSA_SUCCESS \emptydescription3319* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3320* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3321* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3322* \retval #PSA_ERROR_BAD_STATE3323* The operation state is not valid (it must be active), or3324* the library has not been previously initialized by psa_crypto_init().3325* It is implementation-dependent whether a failure to initialize3326* results in this error code.3327*/3328psa_status_t psa_key_derivation_get_capacity(3329const psa_key_derivation_operation_t *operation,3330size_t *capacity);33313332/** Set the maximum capacity of a key derivation operation.3333*3334* The capacity of a key derivation operation is the maximum number of bytes3335* that the key derivation operation can return from this point onwards.3336*3337* \param[in,out] operation The key derivation operation object to modify.3338* \param capacity The new capacity of the operation.3339* It must be less or equal to the operation's3340* current capacity.3341*3342* \retval #PSA_SUCCESS \emptydescription3343* \retval #PSA_ERROR_INVALID_ARGUMENT3344* \p capacity is larger than the operation's current capacity.3345* In this case, the operation object remains valid and its capacity3346* remains unchanged.3347* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3348* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3349* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3350* \retval #PSA_ERROR_BAD_STATE3351* The operation state is not valid (it must be active), or the3352* library has not been previously initialized by psa_crypto_init().3353* It is implementation-dependent whether a failure to initialize3354* results in this error code.3355*/3356psa_status_t psa_key_derivation_set_capacity(3357psa_key_derivation_operation_t *operation,3358size_t capacity);33593360/** Use the maximum possible capacity for a key derivation operation.3361*3362* Use this value as the capacity argument when setting up a key derivation3363* to indicate that the operation should have the maximum possible capacity.3364* The value of the maximum possible capacity depends on the key derivation3365* algorithm.3366*/3367#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))33683369/** Provide an input for key derivation or key agreement.3370*3371* Which inputs are required and in what order depends on the algorithm.3372* Refer to the documentation of each key derivation or key agreement3373* algorithm for information.3374*3375* This function passes direct inputs, which is usually correct for3376* non-secret inputs. To pass a secret input, which should be in a key3377* object, call psa_key_derivation_input_key() instead of this function.3378* Refer to the documentation of individual step types3379* (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)3380* for more information.3381*3382* If this function returns an error status, the operation enters an error3383* state and must be aborted by calling psa_key_derivation_abort().3384*3385* \param[in,out] operation The key derivation operation object to use.3386* It must have been set up with3387* psa_key_derivation_setup() and must not3388* have produced any output yet.3389* \param step Which step the input data is for.3390* \param[in] data Input data to use.3391* \param data_length Size of the \p data buffer in bytes.3392*3393* \retval #PSA_SUCCESS3394* Success.3395* \retval #PSA_ERROR_INVALID_ARGUMENT3396* \c step is not compatible with the operation's algorithm, or3397* \c step does not allow direct inputs.3398* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3399* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3400* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3401* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3402* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3403* \retval #PSA_ERROR_BAD_STATE3404* The operation state is not valid for this input \p step, or3405* the library has not been previously initialized by psa_crypto_init().3406* It is implementation-dependent whether a failure to initialize3407* results in this error code.3408*/3409psa_status_t psa_key_derivation_input_bytes(3410psa_key_derivation_operation_t *operation,3411psa_key_derivation_step_t step,3412const uint8_t *data,3413size_t data_length);34143415/** Provide a numeric input for key derivation or key agreement.3416*3417* Which inputs are required and in what order depends on the algorithm.3418* However, when an algorithm requires a particular order, numeric inputs3419* usually come first as they tend to be configuration parameters.3420* Refer to the documentation of each key derivation or key agreement3421* algorithm for information.3422*3423* This function is used for inputs which are fixed-size non-negative3424* integers.3425*3426* If this function returns an error status, the operation enters an error3427* state and must be aborted by calling psa_key_derivation_abort().3428*3429* \param[in,out] operation The key derivation operation object to use.3430* It must have been set up with3431* psa_key_derivation_setup() and must not3432* have produced any output yet.3433* \param step Which step the input data is for.3434* \param[in] value The value of the numeric input.3435*3436* \retval #PSA_SUCCESS3437* Success.3438* \retval #PSA_ERROR_INVALID_ARGUMENT3439* \c step is not compatible with the operation's algorithm, or3440* \c step does not allow numeric inputs.3441* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3442* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3443* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3444* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3445* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3446* \retval #PSA_ERROR_BAD_STATE3447* The operation state is not valid for this input \p step, or3448* the library has not been previously initialized by psa_crypto_init().3449* It is implementation-dependent whether a failure to initialize3450* results in this error code.3451*/3452psa_status_t psa_key_derivation_input_integer(3453psa_key_derivation_operation_t *operation,3454psa_key_derivation_step_t step,3455uint64_t value);34563457/** Provide an input for key derivation in the form of a key.3458*3459* Which inputs are required and in what order depends on the algorithm.3460* Refer to the documentation of each key derivation or key agreement3461* algorithm for information.3462*3463* This function obtains input from a key object, which is usually correct for3464* secret inputs or for non-secret personalization strings kept in the key3465* store. To pass a non-secret parameter which is not in the key store,3466* call psa_key_derivation_input_bytes() instead of this function.3467* Refer to the documentation of individual step types3468* (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)3469* for more information.3470*3471* If this function returns an error status, the operation enters an error3472* state and must be aborted by calling psa_key_derivation_abort().3473*3474* \param[in,out] operation The key derivation operation object to use.3475* It must have been set up with3476* psa_key_derivation_setup() and must not3477* have produced any output yet.3478* \param step Which step the input data is for.3479* \param key Identifier of the key. It must have an3480* appropriate type for step and must allow the3481* usage #PSA_KEY_USAGE_DERIVE or3482* #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)3483* and the algorithm used by the operation.3484*3485* \note Once all inputs steps are completed, the operations will allow:3486* - psa_key_derivation_output_bytes() if each input was either a direct input3487* or a key with #PSA_KEY_USAGE_DERIVE set;3488* - psa_key_derivation_output_key() or psa_key_derivation_output_key_custom()3489* if the input for step3490* #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD3491* was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was3492* either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;3493* - psa_key_derivation_verify_bytes() if each input was either a direct input3494* or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;3495* - psa_key_derivation_verify_key() under the same conditions as3496* psa_key_derivation_verify_bytes().3497*3498* \retval #PSA_SUCCESS3499* Success.3500* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription3501* \retval #PSA_ERROR_NOT_PERMITTED3502* The key allows neither #PSA_KEY_USAGE_DERIVE nor3503* #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this3504* algorithm.3505* \retval #PSA_ERROR_INVALID_ARGUMENT3506* \c step is not compatible with the operation's algorithm, or3507* \c step does not allow key inputs of the given type3508* or does not allow key inputs at all.3509* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3510* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3511* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3512* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3513* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3514* \retval #PSA_ERROR_BAD_STATE3515* The operation state is not valid for this input \p step, or3516* the library has not been previously initialized by psa_crypto_init().3517* It is implementation-dependent whether a failure to initialize3518* results in this error code.3519*/3520psa_status_t psa_key_derivation_input_key(3521psa_key_derivation_operation_t *operation,3522psa_key_derivation_step_t step,3523mbedtls_svc_key_id_t key);35243525/** Perform a key agreement and use the shared secret as input to a key3526* derivation.3527*3528* A key agreement algorithm takes two inputs: a private key \p private_key3529* a public key \p peer_key.3530* The result of this function is passed as input to a key derivation.3531* The output of this key derivation can be extracted by reading from the3532* resulting operation to produce keys and other cryptographic material.3533*3534* If this function returns an error status, the operation enters an error3535* state and must be aborted by calling psa_key_derivation_abort().3536*3537* \param[in,out] operation The key derivation operation object to use.3538* It must have been set up with3539* psa_key_derivation_setup() with a3540* key agreement and derivation algorithm3541* \c alg (\c PSA_ALG_XXX value such that3542* #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true3543* and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)3544* is false).3545* The operation must be ready for an3546* input of the type given by \p step.3547* \param step Which step the input data is for.3548* \param private_key Identifier of the private key to use. It must3549* allow the usage #PSA_KEY_USAGE_DERIVE.3550* \param[in] peer_key Public key of the peer. The peer key must be in the3551* same format that psa_import_key() accepts for the3552* public key type corresponding to the type of3553* private_key. That is, this function performs the3554* equivalent of3555* #psa_import_key(...,3556* `peer_key`, `peer_key_length`) where3557* with key attributes indicating the public key3558* type corresponding to the type of `private_key`.3559* For example, for EC keys, this means that peer_key3560* is interpreted as a point on the curve that the3561* private key is on. The standard formats for public3562* keys are documented in the documentation of3563* psa_export_public_key().3564* \param peer_key_length Size of \p peer_key in bytes.3565*3566* \retval #PSA_SUCCESS3567* Success.3568* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription3569* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription3570* \retval #PSA_ERROR_INVALID_ARGUMENT3571* \c private_key is not compatible with \c alg,3572* or \p peer_key is not valid for \c alg or not compatible with3573* \c private_key, or \c step does not allow an input resulting3574* from a key agreement.3575* \retval #PSA_ERROR_NOT_SUPPORTED3576* \c alg is not supported or is not a key derivation algorithm.3577* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3578* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3579* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3580* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3581* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3582* \retval #PSA_ERROR_BAD_STATE3583* The operation state is not valid for this key agreement \p step,3584* or the library has not been previously initialized by psa_crypto_init().3585* It is implementation-dependent whether a failure to initialize3586* results in this error code.3587*/3588psa_status_t psa_key_derivation_key_agreement(3589psa_key_derivation_operation_t *operation,3590psa_key_derivation_step_t step,3591mbedtls_svc_key_id_t private_key,3592const uint8_t *peer_key,3593size_t peer_key_length);35943595/** Read some data from a key derivation operation.3596*3597* This function calculates output bytes from a key derivation algorithm and3598* return those bytes.3599* If you view the key derivation's output as a stream of bytes, this3600* function destructively reads the requested number of bytes from the3601* stream.3602* The operation's capacity decreases by the number of bytes read.3603*3604* If this function returns an error status other than3605* #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error3606* state and must be aborted by calling psa_key_derivation_abort().3607*3608* \param[in,out] operation The key derivation operation object to read from.3609* \param[out] output Buffer where the output will be written.3610* \param output_length Number of bytes to output.3611*3612* \retval #PSA_SUCCESS \emptydescription3613* \retval #PSA_ERROR_NOT_PERMITTED3614* One of the inputs was a key whose policy didn't allow3615* #PSA_KEY_USAGE_DERIVE.3616* \retval #PSA_ERROR_INSUFFICIENT_DATA3617* The operation's capacity was less than3618* \p output_length bytes. Note that in this case,3619* no output is written to the output buffer.3620* The operation's capacity is set to 0, thus3621* subsequent calls to this function will not3622* succeed, even with a smaller output buffer.3623* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3624* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3625* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3626* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3627* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3628* \retval #PSA_ERROR_BAD_STATE3629* The operation state is not valid (it must be active and completed3630* all required input steps), or the library has not been previously3631* initialized by psa_crypto_init().3632* It is implementation-dependent whether a failure to initialize3633* results in this error code.3634*/3635psa_status_t psa_key_derivation_output_bytes(3636psa_key_derivation_operation_t *operation,3637uint8_t *output,3638size_t output_length);36393640/** Derive a key from an ongoing key derivation operation.3641*3642* This function calculates output bytes from a key derivation algorithm3643* and uses those bytes to generate a key deterministically.3644* The key's location, usage policy, type and size are taken from3645* \p attributes.3646*3647* If you view the key derivation's output as a stream of bytes, this3648* function destructively reads as many bytes as required from the3649* stream.3650* The operation's capacity decreases by the number of bytes read.3651*3652* If this function returns an error status other than3653* #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error3654* state and must be aborted by calling psa_key_derivation_abort().3655*3656* How much output is produced and consumed from the operation, and how3657* the key is derived, depends on the key type and on the key size3658* (denoted \c bits below):3659*3660* - For key types for which the key is an arbitrary sequence of bytes3661* of a given size, this function is functionally equivalent to3662* calling #psa_key_derivation_output_bytes3663* and passing the resulting output to #psa_import_key.3664* However, this function has a security benefit:3665* if the implementation provides an isolation boundary then3666* the key material is not exposed outside the isolation boundary.3667* As a consequence, for these key types, this function always consumes3668* exactly (\c bits / 8) bytes from the operation.3669* The following key types defined in this specification follow this scheme:3670*3671* - #PSA_KEY_TYPE_AES;3672* - #PSA_KEY_TYPE_ARIA;3673* - #PSA_KEY_TYPE_CAMELLIA;3674* - #PSA_KEY_TYPE_DERIVE;3675* - #PSA_KEY_TYPE_HMAC;3676* - #PSA_KEY_TYPE_PASSWORD_HASH.3677*3678* - For ECC keys on a Montgomery elliptic curve3679* (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a3680* Montgomery curve), this function always draws a byte string whose3681* length is determined by the curve, and sets the mandatory bits3682* accordingly. That is:3683*3684* - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte3685* string and process it as specified in RFC 7748 §5.3686* - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte3687* string and process it as specified in RFC 7748 §5.3688*3689* - For key types for which the key is represented by a single sequence of3690* \c bits bits with constraints as to which bit sequences are acceptable,3691* this function draws a byte string of length (\c bits / 8) bytes rounded3692* up to the nearest whole number of bytes. If the resulting byte string3693* is acceptable, it becomes the key, otherwise the drawn bytes are discarded.3694* This process is repeated until an acceptable byte string is drawn.3695* The byte string drawn from the operation is interpreted as specified3696* for the output produced by psa_export_key().3697* The following key types defined in this specification follow this scheme:3698*3699* - #PSA_KEY_TYPE_DES.3700* Force-set the parity bits, but discard forbidden weak keys.3701* For 2-key and 3-key triple-DES, the three keys are generated3702* successively (for example, for 3-key triple-DES,3703* if the first 8 bytes specify a weak key and the next 8 bytes do not,3704* discard the first 8 bytes, use the next 8 bytes as the first key,3705* and continue reading output from the operation to derive the other3706* two keys).3707* - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)3708* where \c group designates any Diffie-Hellman group) and3709* ECC keys on a Weierstrass elliptic curve3710* (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a3711* Weierstrass curve).3712* For these key types, interpret the byte string as integer3713* in big-endian order. Discard it if it is not in the range3714* [0, *N* - 2] where *N* is the boundary of the private key domain3715* (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,3716* or the order of the curve's base point for ECC).3717* Add 1 to the resulting integer and use this as the private key *x*.3718* This method allows compliance to NIST standards, specifically3719* the methods titled "key-pair generation by testing candidates"3720* in NIST SP 800-56A §5.6.1.1.4 for Diffie-Hellman,3721* in FIPS 186-4 §B.1.2 for DSA, and3722* in NIST SP 800-56A §5.6.1.2.2 or3723* FIPS 186-4 §B.4.2 for elliptic curve keys.3724*3725* - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,3726* the way in which the operation output is consumed is3727* implementation-defined.3728*3729* In all cases, the data that is read is discarded from the operation.3730* The operation's capacity is decreased by the number of bytes read.3731*3732* For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,3733* the input to that step must be provided with psa_key_derivation_input_key().3734* Future versions of this specification may include additional restrictions3735* on the derived key based on the attributes and strength of the secret key.3736*3737* \note This function is equivalent to calling3738* psa_key_derivation_output_key_custom()3739* with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT3740* and `custom_data_length == 0` (i.e. `custom_data` is empty).3741*3742* \param[in] attributes The attributes for the new key.3743* If the key type to be created is3744* #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in3745* the policy must be the same as in the current3746* operation.3747* \param[in,out] operation The key derivation operation object to read from.3748* \param[out] key On success, an identifier for the newly created3749* key. For persistent keys, this is the key3750* identifier defined in \p attributes.3751* \c 0 on failure.3752*3753* \retval #PSA_SUCCESS3754* Success.3755* If the key is persistent, the key material and the key's metadata3756* have been saved to persistent storage.3757* \retval #PSA_ERROR_ALREADY_EXISTS3758* This is an attempt to create a persistent key, and there is3759* already a persistent key with the given identifier.3760* \retval #PSA_ERROR_INSUFFICIENT_DATA3761* There was not enough data to create the desired key.3762* Note that in this case, no output is written to the output buffer.3763* The operation's capacity is set to 0, thus subsequent calls to3764* this function will not succeed, even with a smaller output buffer.3765* \retval #PSA_ERROR_NOT_SUPPORTED3766* The key type or key size is not supported, either by the3767* implementation in general or in this particular location.3768* \retval #PSA_ERROR_INVALID_ARGUMENT3769* The provided key attributes are not valid for the operation.3770* \retval #PSA_ERROR_NOT_PERMITTED3771* The #PSA_KEY_DERIVATION_INPUT_SECRET or3772* #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a3773* key; or one of the inputs was a key whose policy didn't allow3774* #PSA_KEY_USAGE_DERIVE.3775* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3776* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription3777* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3778* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3779* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3780* \retval #PSA_ERROR_DATA_INVALID \emptydescription3781* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription3782* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3783* \retval #PSA_ERROR_BAD_STATE3784* The operation state is not valid (it must be active and completed3785* all required input steps), or the library has not been previously3786* initialized by psa_crypto_init().3787* It is implementation-dependent whether a failure to initialize3788* results in this error code.3789*/3790psa_status_t psa_key_derivation_output_key(3791const psa_key_attributes_t *attributes,3792psa_key_derivation_operation_t *operation,3793mbedtls_svc_key_id_t *key);37943795/** Derive a key from an ongoing key derivation operation with custom3796* production parameters.3797*3798* See the description of psa_key_derivation_out_key() for the operation of3799* this function with the default production parameters.3800* Mbed TLS currently does not currently support any non-default production3801* parameters.3802*3803* \note This function is experimental and may change in future minor3804* versions of Mbed TLS.3805*3806* \param[in] attributes The attributes for the new key.3807* If the key type to be created is3808* #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in3809* the policy must be the same as in the current3810* operation.3811* \param[in,out] operation The key derivation operation object to read from.3812* \param[in] custom Customization parameters for the key generation.3813* When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT3814* with \p custom_data_length = 0,3815* this function is equivalent to3816* psa_key_derivation_output_key().3817* \param[in] custom_data Variable-length data associated with \c custom.3818* \param custom_data_length3819* Length of `custom_data` in bytes.3820* \param[out] key On success, an identifier for the newly created3821* key. For persistent keys, this is the key3822* identifier defined in \p attributes.3823* \c 0 on failure.3824*3825* \retval #PSA_SUCCESS3826* Success.3827* If the key is persistent, the key material and the key's metadata3828* have been saved to persistent storage.3829* \retval #PSA_ERROR_ALREADY_EXISTS3830* This is an attempt to create a persistent key, and there is3831* already a persistent key with the given identifier.3832* \retval #PSA_ERROR_INSUFFICIENT_DATA3833* There was not enough data to create the desired key.3834* Note that in this case, no output is written to the output buffer.3835* The operation's capacity is set to 0, thus subsequent calls to3836* this function will not succeed, even with a smaller output buffer.3837* \retval #PSA_ERROR_NOT_SUPPORTED3838* The key type or key size is not supported, either by the3839* implementation in general or in this particular location.3840* \retval #PSA_ERROR_INVALID_ARGUMENT3841* The provided key attributes are not valid for the operation.3842* \retval #PSA_ERROR_NOT_PERMITTED3843* The #PSA_KEY_DERIVATION_INPUT_SECRET or3844* #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a3845* key; or one of the inputs was a key whose policy didn't allow3846* #PSA_KEY_USAGE_DERIVE.3847* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3848* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription3849* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3850* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3851* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3852* \retval #PSA_ERROR_DATA_INVALID \emptydescription3853* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription3854* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3855* \retval #PSA_ERROR_BAD_STATE3856* The operation state is not valid (it must be active and completed3857* all required input steps), or the library has not been previously3858* initialized by psa_crypto_init().3859* It is implementation-dependent whether a failure to initialize3860* results in this error code.3861*/3862psa_status_t psa_key_derivation_output_key_custom(3863const psa_key_attributes_t *attributes,3864psa_key_derivation_operation_t *operation,3865const psa_custom_key_parameters_t *custom,3866const uint8_t *custom_data,3867size_t custom_data_length,3868mbedtls_svc_key_id_t *key);38693870#ifndef __cplusplus3871/* Omitted when compiling in C++, because one of the parameters is a3872* pointer to a struct with a flexible array member, and that is not3873* standard C++.3874* https://github.com/Mbed-TLS/mbedtls/issues/90203875*/3876/** Derive a key from an ongoing key derivation operation with custom3877* production parameters.3878*3879* \note3880* This is a deprecated variant of psa_key_derivation_output_key_custom().3881* It is equivalent except that the associated variable-length data3882* is passed in `params->data` instead of a separate parameter.3883* This function will be removed in a future version of Mbed TLS.3884*3885* \param[in] attributes The attributes for the new key.3886* If the key type to be created is3887* #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in3888* the policy must be the same as in the current3889* operation.3890* \param[in,out] operation The key derivation operation object to read from.3891* \param[in] params Customization parameters for the key derivation.3892* When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT3893* with \p params_data_length = 0,3894* this function is equivalent to3895* psa_key_derivation_output_key().3896* Mbed TLS currently only supports the default3897* production parameters, i.e.3898* #PSA_KEY_PRODUCTION_PARAMETERS_INIT,3899* for all key types.3900* \param params_data_length3901* Length of `params->data` in bytes.3902* \param[out] key On success, an identifier for the newly created3903* key. For persistent keys, this is the key3904* identifier defined in \p attributes.3905* \c 0 on failure.3906*3907* \retval #PSA_SUCCESS3908* Success.3909* If the key is persistent, the key material and the key's metadata3910* have been saved to persistent storage.3911* \retval #PSA_ERROR_ALREADY_EXISTS3912* This is an attempt to create a persistent key, and there is3913* already a persistent key with the given identifier.3914* \retval #PSA_ERROR_INSUFFICIENT_DATA3915* There was not enough data to create the desired key.3916* Note that in this case, no output is written to the output buffer.3917* The operation's capacity is set to 0, thus subsequent calls to3918* this function will not succeed, even with a smaller output buffer.3919* \retval #PSA_ERROR_NOT_SUPPORTED3920* The key type or key size is not supported, either by the3921* implementation in general or in this particular location.3922* \retval #PSA_ERROR_INVALID_ARGUMENT3923* The provided key attributes are not valid for the operation.3924* \retval #PSA_ERROR_NOT_PERMITTED3925* The #PSA_KEY_DERIVATION_INPUT_SECRET or3926* #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a3927* key; or one of the inputs was a key whose policy didn't allow3928* #PSA_KEY_USAGE_DERIVE.3929* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3930* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription3931* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3932* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3933* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3934* \retval #PSA_ERROR_DATA_INVALID \emptydescription3935* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription3936* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3937* \retval #PSA_ERROR_BAD_STATE3938* The operation state is not valid (it must be active and completed3939* all required input steps), or the library has not been previously3940* initialized by psa_crypto_init().3941* It is implementation-dependent whether a failure to initialize3942* results in this error code.3943*/3944psa_status_t psa_key_derivation_output_key_ext(3945const psa_key_attributes_t *attributes,3946psa_key_derivation_operation_t *operation,3947const psa_key_production_parameters_t *params,3948size_t params_data_length,3949mbedtls_svc_key_id_t *key);3950#endif /* !__cplusplus */39513952/** Compare output data from a key derivation operation to an expected value.3953*3954* This function calculates output bytes from a key derivation algorithm and3955* compares those bytes to an expected value in constant time.3956* If you view the key derivation's output as a stream of bytes, this3957* function destructively reads the expected number of bytes from the3958* stream before comparing them.3959* The operation's capacity decreases by the number of bytes read.3960*3961* This is functionally equivalent to the following code:3962* \code3963* psa_key_derivation_output_bytes(operation, tmp, output_length);3964* if (memcmp(output, tmp, output_length) != 0)3965* return PSA_ERROR_INVALID_SIGNATURE;3966* \endcode3967* except (1) it works even if the key's policy does not allow outputting the3968* bytes, and (2) the comparison will be done in constant time.3969*3970* If this function returns an error status other than3971* #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,3972* the operation enters an error state and must be aborted by calling3973* psa_key_derivation_abort().3974*3975* \param[in,out] operation The key derivation operation object to read from.3976* \param[in] expected Buffer containing the expected derivation output.3977* \param expected_length Length of the expected output; this is also the3978* number of bytes that will be read.3979*3980* \retval #PSA_SUCCESS \emptydescription3981* \retval #PSA_ERROR_INVALID_SIGNATURE3982* The output was read successfully, but it differs from the expected3983* output.3984* \retval #PSA_ERROR_NOT_PERMITTED3985* One of the inputs was a key whose policy didn't allow3986* #PSA_KEY_USAGE_VERIFY_DERIVATION.3987* \retval #PSA_ERROR_INSUFFICIENT_DATA3988* The operation's capacity was less than3989* \p output_length bytes. Note that in this case,3990* the operation's capacity is set to 0, thus3991* subsequent calls to this function will not3992* succeed, even with a smaller expected output.3993* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription3994* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription3995* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription3996* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription3997* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription3998* \retval #PSA_ERROR_BAD_STATE3999* The operation state is not valid (it must be active and completed4000* all required input steps), or the library has not been previously4001* initialized by psa_crypto_init().4002* It is implementation-dependent whether a failure to initialize4003* results in this error code.4004*/4005psa_status_t psa_key_derivation_verify_bytes(4006psa_key_derivation_operation_t *operation,4007const uint8_t *expected,4008size_t expected_length);40094010/** Compare output data from a key derivation operation to an expected value4011* stored in a key object.4012*4013* This function calculates output bytes from a key derivation algorithm and4014* compares those bytes to an expected value, provided as key of type4015* #PSA_KEY_TYPE_PASSWORD_HASH.4016* If you view the key derivation's output as a stream of bytes, this4017* function destructively reads the number of bytes corresponding to the4018* length of the expected value from the stream before comparing them.4019* The operation's capacity decreases by the number of bytes read.4020*4021* This is functionally equivalent to exporting the key and calling4022* psa_key_derivation_verify_bytes() on the result, except that it4023* works even if the key cannot be exported.4024*4025* If this function returns an error status other than4026* #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,4027* the operation enters an error state and must be aborted by calling4028* psa_key_derivation_abort().4029*4030* \param[in,out] operation The key derivation operation object to read from.4031* \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH4032* containing the expected output. Its policy must4033* include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag4034* and the permitted algorithm must match the4035* operation. The value of this key was likely4036* computed by a previous call to4037* psa_key_derivation_output_key() or4038* psa_key_derivation_output_key_custom().4039*4040* \retval #PSA_SUCCESS \emptydescription4041* \retval #PSA_ERROR_INVALID_SIGNATURE4042* The output was read successfully, but if differs from the expected4043* output.4044* \retval #PSA_ERROR_INVALID_HANDLE4045* The key passed as the expected value does not exist.4046* \retval #PSA_ERROR_INVALID_ARGUMENT4047* The key passed as the expected value has an invalid type.4048* \retval #PSA_ERROR_NOT_PERMITTED4049* The key passed as the expected value does not allow this usage or4050* this algorithm; or one of the inputs was a key whose policy didn't4051* allow #PSA_KEY_USAGE_VERIFY_DERIVATION.4052* \retval #PSA_ERROR_INSUFFICIENT_DATA4053* The operation's capacity was less than4054* the length of the expected value. In this case,4055* the operation's capacity is set to 0, thus4056* subsequent calls to this function will not4057* succeed, even with a smaller expected output.4058* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4059* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4060* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4061* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4062* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4063* \retval #PSA_ERROR_BAD_STATE4064* The operation state is not valid (it must be active and completed4065* all required input steps), or the library has not been previously4066* initialized by psa_crypto_init().4067* It is implementation-dependent whether a failure to initialize4068* results in this error code.4069*/4070psa_status_t psa_key_derivation_verify_key(4071psa_key_derivation_operation_t *operation,4072psa_key_id_t expected);40734074/** Abort a key derivation operation.4075*4076* Aborting an operation frees all associated resources except for the \c4077* operation structure itself. Once aborted, the operation object can be reused4078* for another operation by calling psa_key_derivation_setup() again.4079*4080* This function may be called at any time after the operation4081* object has been initialized as described in #psa_key_derivation_operation_t.4082*4083* In particular, it is valid to call psa_key_derivation_abort() twice, or to4084* call psa_key_derivation_abort() on an operation that has not been set up.4085*4086* \param[in,out] operation The operation to abort.4087*4088* \retval #PSA_SUCCESS \emptydescription4089* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4090* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4091* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4092* \retval #PSA_ERROR_BAD_STATE4093* The library has not been previously initialized by psa_crypto_init().4094* It is implementation-dependent whether a failure to initialize4095* results in this error code.4096*/4097psa_status_t psa_key_derivation_abort(4098psa_key_derivation_operation_t *operation);40994100/** Perform a key agreement and return the raw shared secret.4101*4102* \warning The raw result of a key agreement algorithm such as finite-field4103* Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should4104* not be used directly as key material. It should instead be passed as4105* input to a key derivation algorithm. To chain a key agreement with4106* a key derivation, use psa_key_derivation_key_agreement() and other4107* functions from the key derivation interface.4108*4109* \param alg The key agreement algorithm to compute4110* (\c PSA_ALG_XXX value such that4111* #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)4112* is true).4113* \param private_key Identifier of the private key to use. It must4114* allow the usage #PSA_KEY_USAGE_DERIVE.4115* \param[in] peer_key Public key of the peer. It must be4116* in the same format that psa_import_key()4117* accepts. The standard formats for public4118* keys are documented in the documentation4119* of psa_export_public_key().4120* \param peer_key_length Size of \p peer_key in bytes.4121* \param[out] output Buffer where the decrypted message is to4122* be written.4123* \param output_size Size of the \c output buffer in bytes.4124* \param[out] output_length On success, the number of bytes4125* that make up the returned output.4126*4127* \retval #PSA_SUCCESS4128* Success.4129* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription4130* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription4131* \retval #PSA_ERROR_INVALID_ARGUMENT4132* \p alg is not a key agreement algorithm, or4133* \p private_key is not compatible with \p alg,4134* or \p peer_key is not valid for \p alg or not compatible with4135* \p private_key.4136* \retval #PSA_ERROR_BUFFER_TOO_SMALL4137* \p output_size is too small4138* \retval #PSA_ERROR_NOT_SUPPORTED4139* \p alg is not a supported key agreement algorithm.4140* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4141* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4142* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4143* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4144* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4145* \retval #PSA_ERROR_BAD_STATE4146* The library has not been previously initialized by psa_crypto_init().4147* It is implementation-dependent whether a failure to initialize4148* results in this error code.4149*/4150psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,4151mbedtls_svc_key_id_t private_key,4152const uint8_t *peer_key,4153size_t peer_key_length,4154uint8_t *output,4155size_t output_size,4156size_t *output_length);41574158/**@}*/41594160/** \defgroup random Random generation4161* @{4162*/41634164/**4165* \brief Generate random bytes.4166*4167* \warning This function **can** fail! Callers MUST check the return status4168* and MUST NOT use the content of the output buffer if the return4169* status is not #PSA_SUCCESS.4170*4171* \note To generate a key, use psa_generate_key() instead.4172*4173* \param[out] output Output buffer for the generated data.4174* \param output_size Number of bytes to generate and output.4175*4176* \retval #PSA_SUCCESS \emptydescription4177* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4178* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4179* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4180* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4181* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4182* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4183* \retval #PSA_ERROR_BAD_STATE4184* The library has not been previously initialized by psa_crypto_init().4185* It is implementation-dependent whether a failure to initialize4186* results in this error code.4187*/4188psa_status_t psa_generate_random(uint8_t *output,4189size_t output_size);41904191/**4192* \brief Generate a key or key pair.4193*4194* The key is generated randomly.4195* Its location, usage policy, type and size are taken from \p attributes.4196*4197* Implementations must reject an attempt to generate a key of size 0.4198*4199* The following type-specific considerations apply:4200* - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),4201* the public exponent is 65537.4202* The modulus is a product of two probabilistic primes4203* between 2^{n-1} and 2^n where n is the bit size specified in the4204* attributes.4205*4206* \note This function is equivalent to calling psa_generate_key_custom()4207* with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT4208* and `custom_data_length == 0` (i.e. `custom_data` is empty).4209*4210* \param[in] attributes The attributes for the new key.4211* \param[out] key On success, an identifier for the newly created4212* key. For persistent keys, this is the key4213* identifier defined in \p attributes.4214* \c 0 on failure.4215*4216* \retval #PSA_SUCCESS4217* Success.4218* If the key is persistent, the key material and the key's metadata4219* have been saved to persistent storage.4220* \retval #PSA_ERROR_ALREADY_EXISTS4221* This is an attempt to create a persistent key, and there is4222* already a persistent key with the given identifier.4223* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4224* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4225* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4226* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4227* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4228* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4229* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4230* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription4231* \retval #PSA_ERROR_DATA_INVALID \emptydescription4232* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4233* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4234* \retval #PSA_ERROR_BAD_STATE4235* The library has not been previously initialized by psa_crypto_init().4236* It is implementation-dependent whether a failure to initialize4237* results in this error code.4238*/4239psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,4240mbedtls_svc_key_id_t *key);42414242/**4243* \brief Generate a key or key pair using custom production parameters.4244*4245* See the description of psa_generate_key() for the operation of this4246* function with the default production parameters. In addition, this function4247* supports the following production customizations, described in more detail4248* in the documentation of ::psa_custom_key_parameters_t:4249*4250* - RSA keys: generation with a custom public exponent.4251*4252* \note This function is experimental and may change in future minor4253* versions of Mbed TLS.4254*4255* \param[in] attributes The attributes for the new key.4256* \param[in] custom Customization parameters for the key generation.4257* When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT4258* with \p custom_data_length = 0,4259* this function is equivalent to4260* psa_generate_key().4261* \param[in] custom_data Variable-length data associated with \c custom.4262* \param custom_data_length4263* Length of `custom_data` in bytes.4264* \param[out] key On success, an identifier for the newly created4265* key. For persistent keys, this is the key4266* identifier defined in \p attributes.4267* \c 0 on failure.4268*4269* \retval #PSA_SUCCESS4270* Success.4271* If the key is persistent, the key material and the key's metadata4272* have been saved to persistent storage.4273* \retval #PSA_ERROR_ALREADY_EXISTS4274* This is an attempt to create a persistent key, and there is4275* already a persistent key with the given identifier.4276* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4277* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4278* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4279* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4280* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4281* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4282* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4283* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription4284* \retval #PSA_ERROR_DATA_INVALID \emptydescription4285* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4286* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4287* \retval #PSA_ERROR_BAD_STATE4288* The library has not been previously initialized by psa_crypto_init().4289* It is implementation-dependent whether a failure to initialize4290* results in this error code.4291*/4292psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,4293const psa_custom_key_parameters_t *custom,4294const uint8_t *custom_data,4295size_t custom_data_length,4296mbedtls_svc_key_id_t *key);42974298#ifndef __cplusplus4299/* Omitted when compiling in C++, because one of the parameters is a4300* pointer to a struct with a flexible array member, and that is not4301* standard C++.4302* https://github.com/Mbed-TLS/mbedtls/issues/90204303*/4304/**4305* \brief Generate a key or key pair using custom production parameters.4306*4307* \note4308* This is a deprecated variant of psa_key_derivation_output_key_custom().4309* It is equivalent except that the associated variable-length data4310* is passed in `params->data` instead of a separate parameter.4311* This function will be removed in a future version of Mbed TLS.4312*4313* \param[in] attributes The attributes for the new key.4314* \param[in] params Customization parameters for the key generation.4315* When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT4316* with \p params_data_length = 0,4317* this function is equivalent to4318* psa_generate_key().4319* \param params_data_length4320* Length of `params->data` in bytes.4321* \param[out] key On success, an identifier for the newly created4322* key. For persistent keys, this is the key4323* identifier defined in \p attributes.4324* \c 0 on failure.4325*4326* \retval #PSA_SUCCESS4327* Success.4328* If the key is persistent, the key material and the key's metadata4329* have been saved to persistent storage.4330* \retval #PSA_ERROR_ALREADY_EXISTS4331* This is an attempt to create a persistent key, and there is4332* already a persistent key with the given identifier.4333* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4334* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4335* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4336* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4337* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4338* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4339* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4340* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription4341* \retval #PSA_ERROR_DATA_INVALID \emptydescription4342* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4343* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4344* \retval #PSA_ERROR_BAD_STATE4345* The library has not been previously initialized by psa_crypto_init().4346* It is implementation-dependent whether a failure to initialize4347* results in this error code.4348*/4349psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,4350const psa_key_production_parameters_t *params,4351size_t params_data_length,4352mbedtls_svc_key_id_t *key);4353#endif /* !__cplusplus */43544355/**@}*/43564357/** \defgroup interruptible_hash Interruptible sign/verify hash4358* @{4359*/43604361/** The type of the state data structure for interruptible hash4362* signing operations.4363*4364* Before calling any function on a sign hash operation object, the4365* application must initialize it by any of the following means:4366* - Set the structure to all-bits-zero, for example:4367* \code4368* psa_sign_hash_interruptible_operation_t operation;4369* memset(&operation, 0, sizeof(operation));4370* \endcode4371* - Initialize the structure to logical zero values, for example:4372* \code4373* psa_sign_hash_interruptible_operation_t operation = {0};4374* \endcode4375* - Initialize the structure to the initializer4376* #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:4377* \code4378* psa_sign_hash_interruptible_operation_t operation =4379* PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;4380* \endcode4381* - Assign the result of the function4382* psa_sign_hash_interruptible_operation_init() to the structure, for4383* example:4384* \code4385* psa_sign_hash_interruptible_operation_t operation;4386* operation = psa_sign_hash_interruptible_operation_init();4387* \endcode4388*4389* This is an implementation-defined \c struct. Applications should not4390* make any assumptions about the content of this structure.4391* Implementation details can change in future versions without notice. */4392typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;43934394/** The type of the state data structure for interruptible hash4395* verification operations.4396*4397* Before calling any function on a sign hash operation object, the4398* application must initialize it by any of the following means:4399* - Set the structure to all-bits-zero, for example:4400* \code4401* psa_verify_hash_interruptible_operation_t operation;4402* memset(&operation, 0, sizeof(operation));4403* \endcode4404* - Initialize the structure to logical zero values, for example:4405* \code4406* psa_verify_hash_interruptible_operation_t operation = {0};4407* \endcode4408* - Initialize the structure to the initializer4409* #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:4410* \code4411* psa_verify_hash_interruptible_operation_t operation =4412* PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;4413* \endcode4414* - Assign the result of the function4415* psa_verify_hash_interruptible_operation_init() to the structure, for4416* example:4417* \code4418* psa_verify_hash_interruptible_operation_t operation;4419* operation = psa_verify_hash_interruptible_operation_init();4420* \endcode4421*4422* This is an implementation-defined \c struct. Applications should not4423* make any assumptions about the content of this structure.4424* Implementation details can change in future versions without notice. */4425typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;44264427/**4428* \brief Set the maximum number of ops allowed to be4429* executed by an interruptible function in a4430* single call.4431*4432* \warning This is a beta API, and thus subject to change4433* at any point. It is not bound by the usual4434* interface stability promises.4435*4436* \note The time taken to execute a single op is4437* implementation specific and depends on4438* software, hardware, the algorithm, key type and4439* curve chosen. Even within a single operation,4440* successive ops can take differing amounts of4441* time. The only guarantee is that lower values4442* for \p max_ops means functions will block for a4443* lesser maximum amount of time. The functions4444* \c psa_sign_interruptible_get_num_ops() and4445* \c psa_verify_interruptible_get_num_ops() are4446* provided to help with tuning this value.4447*4448* \note This value defaults to4449* #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which4450* means the whole operation will be done in one4451* go, regardless of the number of ops required.4452*4453* \note If more ops are needed to complete a4454* computation, #PSA_OPERATION_INCOMPLETE will be4455* returned by the function performing the4456* computation. It is then the caller's4457* responsibility to either call again with the4458* same operation context until it returns 0 or an4459* error code; or to call the relevant abort4460* function if the answer is no longer required.4461*4462* \note The interpretation of \p max_ops is also4463* implementation defined. On a hard real time4464* system, this can indicate a hard deadline, as a4465* real-time system needs a guarantee of not4466* spending more than X time, however care must be4467* taken in such an implementation to avoid the4468* situation whereby calls just return, not being4469* able to do any actual work within the allotted4470* time. On a non-real-time system, the4471* implementation can be more relaxed, but again4472* whether this number should be interpreted as as4473* hard or soft limit or even whether a less than4474* or equals as regards to ops executed in a4475* single call is implementation defined.4476*4477* \note For keys in local storage when no accelerator4478* driver applies, please see also the4479* documentation for \c mbedtls_ecp_set_max_ops(),4480* which is the internal implementation in these4481* cases.4482*4483* \warning With implementations that interpret this number4484* as a hard limit, setting this number too small4485* may result in an infinite loop, whereby each4486* call results in immediate return with no ops4487* done (as there is not enough time to execute4488* any), and thus no result will ever be achieved.4489*4490* \note This only applies to functions whose4491* documentation mentions they may return4492* #PSA_OPERATION_INCOMPLETE.4493*4494* \param max_ops The maximum number of ops to be executed in a4495* single call. This can be a number from 0 to4496* #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 04497* is the least amount of work done per call.4498*/4499void psa_interruptible_set_max_ops(uint32_t max_ops);45004501/**4502* \brief Get the maximum number of ops allowed to be4503* executed by an interruptible function in a4504* single call. This will return the last4505* value set by4506* \c psa_interruptible_set_max_ops() or4507* #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if4508* that function has never been called.4509*4510* \warning This is a beta API, and thus subject to change4511* at any point. It is not bound by the usual4512* interface stability promises.4513*4514* \return Maximum number of ops allowed to be4515* executed by an interruptible function in a4516* single call.4517*/4518uint32_t psa_interruptible_get_max_ops(void);45194520/**4521* \brief Get the number of ops that a hash signing4522* operation has taken so far. If the operation4523* has completed, then this will represent the4524* number of ops required for the entire4525* operation. After initialization or calling4526* \c psa_sign_hash_interruptible_abort() on4527* the operation, a value of 0 will be returned.4528*4529* \note This interface is guaranteed re-entrant and4530* thus may be called from driver code.4531*4532* \warning This is a beta API, and thus subject to change4533* at any point. It is not bound by the usual4534* interface stability promises.4535*4536* This is a helper provided to help you tune the4537* value passed to \c4538* psa_interruptible_set_max_ops().4539*4540* \param operation The \c psa_sign_hash_interruptible_operation_t4541* to use. This must be initialized first.4542*4543* \return Number of ops that the operation has taken so4544* far.4545*/4546uint32_t psa_sign_hash_get_num_ops(4547const psa_sign_hash_interruptible_operation_t *operation);45484549/**4550* \brief Get the number of ops that a hash verification4551* operation has taken so far. If the operation4552* has completed, then this will represent the4553* number of ops required for the entire4554* operation. After initialization or calling \c4555* psa_verify_hash_interruptible_abort() on the4556* operation, a value of 0 will be returned.4557*4558* \warning This is a beta API, and thus subject to change4559* at any point. It is not bound by the usual4560* interface stability promises.4561*4562* This is a helper provided to help you tune the4563* value passed to \c4564* psa_interruptible_set_max_ops().4565*4566* \param operation The \c4567* psa_verify_hash_interruptible_operation_t to4568* use. This must be initialized first.4569*4570* \return Number of ops that the operation has taken so4571* far.4572*/4573uint32_t psa_verify_hash_get_num_ops(4574const psa_verify_hash_interruptible_operation_t *operation);45754576/**4577* \brief Start signing a hash or short message with a4578* private key, in an interruptible manner.4579*4580* \see \c psa_sign_hash_complete()4581*4582* \warning This is a beta API, and thus subject to change4583* at any point. It is not bound by the usual4584* interface stability promises.4585*4586* \note This function combined with \c4587* psa_sign_hash_complete() is equivalent to4588* \c psa_sign_hash() but4589* \c psa_sign_hash_complete() can return early and4590* resume according to the limit set with \c4591* psa_interruptible_set_max_ops() to reduce the4592* maximum time spent in a function call.4593*4594* \note Users should call \c psa_sign_hash_complete()4595* repeatedly on the same context after a4596* successful call to this function until \c4597* psa_sign_hash_complete() either returns 0 or an4598* error. \c psa_sign_hash_complete() will return4599* #PSA_OPERATION_INCOMPLETE if there is more work4600* to do. Alternatively users can call4601* \c psa_sign_hash_abort() at any point if they no4602* longer want the result.4603*4604* \note If this function returns an error status, the4605* operation enters an error state and must be4606* aborted by calling \c psa_sign_hash_abort().4607*4608* \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t4609* to use. This must be initialized first.4610*4611* \param key Identifier of the key to use for the operation.4612* It must be an asymmetric key pair. The key must4613* allow the usage #PSA_KEY_USAGE_SIGN_HASH.4614* \param alg A signature algorithm (\c PSA_ALG_XXX4615* value such that #PSA_ALG_IS_SIGN_HASH(\p alg)4616* is true), that is compatible with4617* the type of \p key.4618* \param[in] hash The hash or message to sign.4619* \param hash_length Size of the \p hash buffer in bytes.4620*4621* \retval #PSA_SUCCESS4622* The operation started successfully - call \c psa_sign_hash_complete()4623* with the same context to complete the operation4624*4625* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription4626* \retval #PSA_ERROR_NOT_PERMITTED4627* The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does4628* not permit the requested algorithm.4629* \retval #PSA_ERROR_BAD_STATE4630* An operation has previously been started on this context, and is4631* still in progress.4632* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4633* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4634* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4635* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4636* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4637* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4638* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4639* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4640* \retval #PSA_ERROR_DATA_INVALID \emptydescription4641* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4642* \retval #PSA_ERROR_BAD_STATE4643* The library has not been previously initialized by psa_crypto_init().4644* It is implementation-dependent whether a failure to initialize4645* results in this error code.4646*/4647psa_status_t psa_sign_hash_start(4648psa_sign_hash_interruptible_operation_t *operation,4649mbedtls_svc_key_id_t key, psa_algorithm_t alg,4650const uint8_t *hash, size_t hash_length);46514652/**4653* \brief Continue and eventually complete the action of4654* signing a hash or short message with a private4655* key, in an interruptible manner.4656*4657* \see \c psa_sign_hash_start()4658*4659* \warning This is a beta API, and thus subject to change4660* at any point. It is not bound by the usual4661* interface stability promises.4662*4663* \note This function combined with \c4664* psa_sign_hash_start() is equivalent to4665* \c psa_sign_hash() but this function can return4666* early and resume according to the limit set with4667* \c psa_interruptible_set_max_ops() to reduce the4668* maximum time spent in a function call.4669*4670* \note Users should call this function on the same4671* operation object repeatedly until it either4672* returns 0 or an error. This function will return4673* #PSA_OPERATION_INCOMPLETE if there is more work4674* to do. Alternatively users can call4675* \c psa_sign_hash_abort() at any point if they no4676* longer want the result.4677*4678* \note When this function returns successfully, the4679* operation becomes inactive. If this function4680* returns an error status, the operation enters an4681* error state and must be aborted by calling4682* \c psa_sign_hash_abort().4683*4684* \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t4685* to use. This must be initialized first, and have4686* had \c psa_sign_hash_start() called with it4687* first.4688*4689* \param[out] signature Buffer where the signature is to be written.4690* \param signature_size Size of the \p signature buffer in bytes. This4691* must be appropriate for the selected4692* algorithm and key:4693* - The required signature size is4694* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c4695* key_bits, \c alg) where \c key_type and \c4696* key_bits are the type and bit-size4697* respectively of key.4698* - #PSA_SIGNATURE_MAX_SIZE evaluates to the4699* maximum signature size of any supported4700* signature algorithm.4701* \param[out] signature_length On success, the number of bytes that make up4702* the returned signature value.4703*4704* \retval #PSA_SUCCESS4705* Operation completed successfully4706*4707* \retval #PSA_OPERATION_INCOMPLETE4708* Operation was interrupted due to the setting of \c4709* psa_interruptible_set_max_ops(). There is still work to be done.4710* Call this function again with the same operation object.4711*4712* \retval #PSA_ERROR_BUFFER_TOO_SMALL4713* The size of the \p signature buffer is too small. You can4714* determine a sufficient buffer size by calling4715* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)4716* where \c key_type and \c key_bits are the type and bit-size4717* respectively of \c key.4718*4719* \retval #PSA_ERROR_BAD_STATE4720* An operation was not previously started on this context via4721* \c psa_sign_hash_start().4722*4723* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4724* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4725* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4726* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4727* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4728* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4729* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4730* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4731* \retval #PSA_ERROR_DATA_INVALID \emptydescription4732* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4733* \retval #PSA_ERROR_BAD_STATE4734* The library has either not been previously initialized by4735* psa_crypto_init() or you did not previously call4736* psa_sign_hash_start() with this operation object. It is4737* implementation-dependent whether a failure to initialize results in4738* this error code.4739*/4740psa_status_t psa_sign_hash_complete(4741psa_sign_hash_interruptible_operation_t *operation,4742uint8_t *signature, size_t signature_size,4743size_t *signature_length);47444745/**4746* \brief Abort a sign hash operation.4747*4748* \warning This is a beta API, and thus subject to change4749* at any point. It is not bound by the usual4750* interface stability promises.4751*4752* \note This function is the only function that clears4753* the number of ops completed as part of the4754* operation. Please ensure you copy this value via4755* \c psa_sign_hash_get_num_ops() if required4756* before calling.4757*4758* \note Aborting an operation frees all associated4759* resources except for the \p operation structure4760* itself. Once aborted, the operation object can4761* be reused for another operation by calling \c4762* psa_sign_hash_start() again.4763*4764* \note You may call this function any time after the4765* operation object has been initialized. In4766* particular, calling \c psa_sign_hash_abort()4767* after the operation has already been terminated4768* by a call to \c psa_sign_hash_abort() or4769* psa_sign_hash_complete() is safe.4770*4771* \param[in,out] operation Initialized sign hash operation.4772*4773* \retval #PSA_SUCCESS4774* The operation was aborted successfully.4775*4776* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4777* \retval #PSA_ERROR_BAD_STATE4778* The library has not been previously initialized by psa_crypto_init().4779* It is implementation-dependent whether a failure to initialize4780* results in this error code.4781*/4782psa_status_t psa_sign_hash_abort(4783psa_sign_hash_interruptible_operation_t *operation);47844785/**4786* \brief Start reading and verifying a hash or short4787* message, in an interruptible manner.4788*4789* \see \c psa_verify_hash_complete()4790*4791* \warning This is a beta API, and thus subject to change4792* at any point. It is not bound by the usual4793* interface stability promises.4794*4795* \note This function combined with \c4796* psa_verify_hash_complete() is equivalent to4797* \c psa_verify_hash() but \c4798* psa_verify_hash_complete() can return early and4799* resume according to the limit set with \c4800* psa_interruptible_set_max_ops() to reduce the4801* maximum time spent in a function.4802*4803* \note Users should call \c psa_verify_hash_complete()4804* repeatedly on the same operation object after a4805* successful call to this function until \c4806* psa_verify_hash_complete() either returns 0 or4807* an error. \c psa_verify_hash_complete() will4808* return #PSA_OPERATION_INCOMPLETE if there is4809* more work to do. Alternatively users can call4810* \c psa_verify_hash_abort() at any point if they4811* no longer want the result.4812*4813* \note If this function returns an error status, the4814* operation enters an error state and must be4815* aborted by calling \c psa_verify_hash_abort().4816*4817* \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t4818* to use. This must be initialized first.4819*4820* \param key Identifier of the key to use for the operation.4821* The key must allow the usage4822* #PSA_KEY_USAGE_VERIFY_HASH.4823* \param alg A signature algorithm (\c PSA_ALG_XXX4824* value such that #PSA_ALG_IS_SIGN_HASH(\p alg)4825* is true), that is compatible with4826* the type of \p key.4827* \param[in] hash The hash whose signature is to be verified.4828* \param hash_length Size of the \p hash buffer in bytes.4829* \param[in] signature Buffer containing the signature to verify.4830* \param signature_length Size of the \p signature buffer in bytes.4831*4832* \retval #PSA_SUCCESS4833* The operation started successfully - please call \c4834* psa_verify_hash_complete() with the same context to complete the4835* operation.4836*4837* \retval #PSA_ERROR_BAD_STATE4838* Another operation has already been started on this context, and is4839* still in progress.4840*4841* \retval #PSA_ERROR_NOT_PERMITTED4842* The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does4843* not permit the requested algorithm.4844*4845* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4846* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4847* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4848* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4849* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4850* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4851* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4852* \retval PSA_ERROR_DATA_CORRUPT \emptydescription4853* \retval PSA_ERROR_DATA_INVALID \emptydescription4854* \retval #PSA_ERROR_BAD_STATE4855* The library has not been previously initialized by psa_crypto_init().4856* It is implementation-dependent whether a failure to initialize4857* results in this error code.4858*/4859psa_status_t psa_verify_hash_start(4860psa_verify_hash_interruptible_operation_t *operation,4861mbedtls_svc_key_id_t key, psa_algorithm_t alg,4862const uint8_t *hash, size_t hash_length,4863const uint8_t *signature, size_t signature_length);48644865/**4866* \brief Continue and eventually complete the action of4867* reading and verifying a hash or short message4868* signed with a private key, in an interruptible4869* manner.4870*4871* \see \c psa_verify_hash_start()4872*4873* \warning This is a beta API, and thus subject to change4874* at any point. It is not bound by the usual4875* interface stability promises.4876*4877* \note This function combined with \c4878* psa_verify_hash_start() is equivalent to4879* \c psa_verify_hash() but this function can4880* return early and resume according to the limit4881* set with \c psa_interruptible_set_max_ops() to4882* reduce the maximum time spent in a function4883* call.4884*4885* \note Users should call this function on the same4886* operation object repeatedly until it either4887* returns 0 or an error. This function will return4888* #PSA_OPERATION_INCOMPLETE if there is more work4889* to do. Alternatively users can call4890* \c psa_verify_hash_abort() at any point if they4891* no longer want the result.4892*4893* \note When this function returns successfully, the4894* operation becomes inactive. If this function4895* returns an error status, the operation enters an4896* error state and must be aborted by calling4897* \c psa_verify_hash_abort().4898*4899* \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t4900* to use. This must be initialized first, and have4901* had \c psa_verify_hash_start() called with it4902* first.4903*4904* \retval #PSA_SUCCESS4905* Operation completed successfully, and the passed signature is valid.4906*4907* \retval #PSA_OPERATION_INCOMPLETE4908* Operation was interrupted due to the setting of \c4909* psa_interruptible_set_max_ops(). There is still work to be done.4910* Call this function again with the same operation object.4911*4912* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription4913* \retval #PSA_ERROR_INVALID_SIGNATURE4914* The calculation was performed successfully, but the passed4915* signature is not a valid signature.4916* \retval #PSA_ERROR_BAD_STATE4917* An operation was not previously started on this context via4918* \c psa_verify_hash_start().4919* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4920* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription4921* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription4922* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription4923* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription4924* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription4925* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription4926* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription4927* \retval #PSA_ERROR_DATA_INVALID \emptydescription4928* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription4929* \retval #PSA_ERROR_BAD_STATE4930* The library has either not been previously initialized by4931* psa_crypto_init() or you did not previously call4932* psa_verify_hash_start() on this object. It is4933* implementation-dependent whether a failure to initialize results in4934* this error code.4935*/4936psa_status_t psa_verify_hash_complete(4937psa_verify_hash_interruptible_operation_t *operation);49384939/**4940* \brief Abort a verify hash operation.4941*4942* \warning This is a beta API, and thus subject to change at4943* any point. It is not bound by the usual interface4944* stability promises.4945*4946* \note This function is the only function that clears the4947* number of ops completed as part of the operation.4948* Please ensure you copy this value via4949* \c psa_verify_hash_get_num_ops() if required4950* before calling.4951*4952* \note Aborting an operation frees all associated4953* resources except for the operation structure4954* itself. Once aborted, the operation object can be4955* reused for another operation by calling \c4956* psa_verify_hash_start() again.4957*4958* \note You may call this function any time after the4959* operation object has been initialized.4960* In particular, calling \c psa_verify_hash_abort()4961* after the operation has already been terminated by4962* a call to \c psa_verify_hash_abort() or4963* psa_verify_hash_complete() is safe.4964*4965* \param[in,out] operation Initialized verify hash operation.4966*4967* \retval #PSA_SUCCESS4968* The operation was aborted successfully.4969*4970* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription4971* \retval #PSA_ERROR_BAD_STATE4972* The library has not been previously initialized by psa_crypto_init().4973* It is implementation-dependent whether a failure to initialize4974* results in this error code.4975*/4976psa_status_t psa_verify_hash_abort(4977psa_verify_hash_interruptible_operation_t *operation);497849794980/**@}*/49814982#ifdef __cplusplus4983}4984#endif49854986/* The file "crypto_extra.h" contains vendor-specific definitions. This4987* can include vendor-defined algorithms, extra functions, etc. */4988#include "crypto_extra.h"49894990#endif /* PSA_CRYPTO_H */499149924993