132b31808SJens Wiklander /** 232b31808SJens Wiklander * \file psa/crypto.h 332b31808SJens Wiklander * \brief Platform Security Architecture cryptography module 432b31808SJens Wiklander */ 532b31808SJens Wiklander /* 632b31808SJens Wiklander * Copyright The Mbed TLS Contributors 7*b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 832b31808SJens Wiklander */ 932b31808SJens Wiklander 1032b31808SJens Wiklander #ifndef PSA_CRYPTO_H 1132b31808SJens Wiklander #define PSA_CRYPTO_H 1232b31808SJens Wiklander 1332b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE) 1432b31808SJens Wiklander #include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE 1532b31808SJens Wiklander #else 1632b31808SJens Wiklander #include "crypto_platform.h" 1732b31808SJens Wiklander #endif 1832b31808SJens Wiklander 1932b31808SJens Wiklander #include <stddef.h> 2032b31808SJens Wiklander 2132b31808SJens Wiklander #ifdef __DOXYGEN_ONLY__ 2232b31808SJens Wiklander /* This __DOXYGEN_ONLY__ block contains mock definitions for things that 2332b31808SJens Wiklander * must be defined in the crypto_platform.h header. These mock definitions 2432b31808SJens Wiklander * are present in this file as a convenience to generate pretty-printed 2532b31808SJens Wiklander * documentation that includes those definitions. */ 2632b31808SJens Wiklander 2732b31808SJens Wiklander /** \defgroup platform Implementation-specific definitions 2832b31808SJens Wiklander * @{ 2932b31808SJens Wiklander */ 3032b31808SJens Wiklander 3132b31808SJens Wiklander /**@}*/ 3232b31808SJens Wiklander #endif /* __DOXYGEN_ONLY__ */ 3332b31808SJens Wiklander 3432b31808SJens Wiklander #ifdef __cplusplus 3532b31808SJens Wiklander extern "C" { 3632b31808SJens Wiklander #endif 3732b31808SJens Wiklander 3832b31808SJens Wiklander /* The file "crypto_types.h" declares types that encode errors, 3932b31808SJens Wiklander * algorithms, key types, policies, etc. */ 4032b31808SJens Wiklander #include "crypto_types.h" 4132b31808SJens Wiklander 4232b31808SJens Wiklander /** \defgroup version API version 4332b31808SJens Wiklander * @{ 4432b31808SJens Wiklander */ 4532b31808SJens Wiklander 4632b31808SJens Wiklander /** 4732b31808SJens Wiklander * The major version of this implementation of the PSA Crypto API 4832b31808SJens Wiklander */ 4932b31808SJens Wiklander #define PSA_CRYPTO_API_VERSION_MAJOR 1 5032b31808SJens Wiklander 5132b31808SJens Wiklander /** 5232b31808SJens Wiklander * The minor version of this implementation of the PSA Crypto API 5332b31808SJens Wiklander */ 5432b31808SJens Wiklander #define PSA_CRYPTO_API_VERSION_MINOR 0 5532b31808SJens Wiklander 5632b31808SJens Wiklander /**@}*/ 5732b31808SJens Wiklander 5832b31808SJens Wiklander /* The file "crypto_values.h" declares macros to build and analyze values 5932b31808SJens Wiklander * of integral types defined in "crypto_types.h". */ 6032b31808SJens Wiklander #include "crypto_values.h" 6132b31808SJens Wiklander 6232b31808SJens Wiklander /** \defgroup initialization Library initialization 6332b31808SJens Wiklander * @{ 6432b31808SJens Wiklander */ 6532b31808SJens Wiklander 6632b31808SJens Wiklander /** 6732b31808SJens Wiklander * \brief Library initialization. 6832b31808SJens Wiklander * 6932b31808SJens Wiklander * Applications must call this function before calling any other 7032b31808SJens Wiklander * function in this module. 7132b31808SJens Wiklander * 7232b31808SJens Wiklander * Applications may call this function more than once. Once a call 7332b31808SJens Wiklander * succeeds, subsequent calls are guaranteed to succeed. 7432b31808SJens Wiklander * 7532b31808SJens Wiklander * If the application calls other functions before calling psa_crypto_init(), 7632b31808SJens Wiklander * the behavior is undefined. Implementations are encouraged to either perform 7732b31808SJens Wiklander * the operation as if the library had been initialized or to return 7832b31808SJens Wiklander * #PSA_ERROR_BAD_STATE or some other applicable error. In particular, 7932b31808SJens Wiklander * implementations should not return a success status if the lack of 8032b31808SJens Wiklander * initialization may have security implications, for example due to improper 8132b31808SJens Wiklander * seeding of the random number generator. 8232b31808SJens Wiklander * 8332b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 8432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 8532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 8632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 8732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 8832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 8932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 9032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 9132b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 9232b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 9332b31808SJens Wiklander */ 9432b31808SJens Wiklander psa_status_t psa_crypto_init(void); 9532b31808SJens Wiklander 9632b31808SJens Wiklander /**@}*/ 9732b31808SJens Wiklander 9832b31808SJens Wiklander /** \addtogroup attributes 9932b31808SJens Wiklander * @{ 10032b31808SJens Wiklander */ 10132b31808SJens Wiklander 10232b31808SJens Wiklander /** \def PSA_KEY_ATTRIBUTES_INIT 10332b31808SJens Wiklander * 10432b31808SJens Wiklander * This macro returns a suitable initializer for a key attribute structure 10532b31808SJens Wiklander * of type #psa_key_attributes_t. 10632b31808SJens Wiklander */ 10732b31808SJens Wiklander 10832b31808SJens Wiklander /** Return an initial value for a key attributes structure. 10932b31808SJens Wiklander */ 11032b31808SJens Wiklander static psa_key_attributes_t psa_key_attributes_init(void); 11132b31808SJens Wiklander 11232b31808SJens Wiklander /** Declare a key as persistent and set its key identifier. 11332b31808SJens Wiklander * 11432b31808SJens Wiklander * If the attribute structure currently declares the key as volatile (which 11532b31808SJens Wiklander * is the default content of an attribute structure), this function sets 11632b31808SJens Wiklander * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT. 11732b31808SJens Wiklander * 11832b31808SJens Wiklander * This function does not access storage, it merely stores the given 11932b31808SJens Wiklander * value in the structure. 12032b31808SJens Wiklander * The persistent key will be written to storage when the attribute 12132b31808SJens Wiklander * structure is passed to a key creation function such as 122*b0563631STom Van Eyck * psa_import_key(), psa_generate_key(), psa_generate_key_ext(), 123*b0563631STom Van Eyck * psa_key_derivation_output_key(), psa_key_derivation_output_key_ext() 124*b0563631STom Van Eyck * or psa_copy_key(). 12532b31808SJens Wiklander * 12632b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 12732b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 12832b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 12932b31808SJens Wiklander * 13032b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 13132b31808SJens Wiklander * \param key The persistent identifier for the key. 13232b31808SJens Wiklander */ 13332b31808SJens Wiklander static void psa_set_key_id(psa_key_attributes_t *attributes, 13432b31808SJens Wiklander mbedtls_svc_key_id_t key); 13532b31808SJens Wiklander 13632b31808SJens Wiklander #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER 13732b31808SJens Wiklander /** Set the owner identifier of a key. 13832b31808SJens Wiklander * 13932b31808SJens Wiklander * When key identifiers encode key owner identifiers, psa_set_key_id() does 14032b31808SJens Wiklander * not allow to define in key attributes the owner of volatile keys as 14132b31808SJens Wiklander * psa_set_key_id() enforces the key to be persistent. 14232b31808SJens Wiklander * 14332b31808SJens Wiklander * This function allows to set in key attributes the owner identifier of a 14432b31808SJens Wiklander * key. It is intended to be used for volatile keys. For persistent keys, 14532b31808SJens Wiklander * it is recommended to use the PSA Cryptography API psa_set_key_id() to define 14632b31808SJens Wiklander * the owner of a key. 14732b31808SJens Wiklander * 14832b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 14932b31808SJens Wiklander * \param owner The key owner identifier. 15032b31808SJens Wiklander */ 15132b31808SJens Wiklander static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes, 15232b31808SJens Wiklander mbedtls_key_owner_id_t owner); 15332b31808SJens Wiklander #endif 15432b31808SJens Wiklander 15532b31808SJens Wiklander /** Set the location of a persistent key. 15632b31808SJens Wiklander * 15732b31808SJens Wiklander * To make a key persistent, you must give it a persistent key identifier 15832b31808SJens Wiklander * with psa_set_key_id(). By default, a key that has a persistent identifier 15932b31808SJens Wiklander * is stored in the default storage area identifier by 16032b31808SJens Wiklander * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage 16132b31808SJens Wiklander * area, or to explicitly declare the key as volatile. 16232b31808SJens Wiklander * 16332b31808SJens Wiklander * This function does not access storage, it merely stores the given 16432b31808SJens Wiklander * value in the structure. 16532b31808SJens Wiklander * The persistent key will be written to storage when the attribute 16632b31808SJens Wiklander * structure is passed to a key creation function such as 167*b0563631STom Van Eyck * psa_import_key(), psa_generate_key(), psa_generate_key_ext(), 168*b0563631STom Van Eyck * psa_key_derivation_output_key(), psa_key_derivation_output_key_ext() 169*b0563631STom Van Eyck * or psa_copy_key(). 17032b31808SJens Wiklander * 17132b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 17232b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 17332b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 17432b31808SJens Wiklander * 17532b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 17632b31808SJens Wiklander * \param lifetime The lifetime for the key. 17732b31808SJens Wiklander * If this is #PSA_KEY_LIFETIME_VOLATILE, the 17832b31808SJens Wiklander * key will be volatile, and the key identifier 17932b31808SJens Wiklander * attribute is reset to 0. 18032b31808SJens Wiklander */ 18132b31808SJens Wiklander static void psa_set_key_lifetime(psa_key_attributes_t *attributes, 18232b31808SJens Wiklander psa_key_lifetime_t lifetime); 18332b31808SJens Wiklander 18432b31808SJens Wiklander /** Retrieve the key identifier from key attributes. 18532b31808SJens Wiklander * 18632b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 18732b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 18832b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 18932b31808SJens Wiklander * 19032b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 19132b31808SJens Wiklander * 19232b31808SJens Wiklander * \return The persistent identifier stored in the attribute structure. 19332b31808SJens Wiklander * This value is unspecified if the attribute structure declares 19432b31808SJens Wiklander * the key as volatile. 19532b31808SJens Wiklander */ 19632b31808SJens Wiklander static mbedtls_svc_key_id_t psa_get_key_id( 19732b31808SJens Wiklander const psa_key_attributes_t *attributes); 19832b31808SJens Wiklander 19932b31808SJens Wiklander /** Retrieve the lifetime from key attributes. 20032b31808SJens Wiklander * 20132b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 20232b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 20332b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 20432b31808SJens Wiklander * 20532b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 20632b31808SJens Wiklander * 20732b31808SJens Wiklander * \return The lifetime value stored in the attribute structure. 20832b31808SJens Wiklander */ 20932b31808SJens Wiklander static psa_key_lifetime_t psa_get_key_lifetime( 21032b31808SJens Wiklander const psa_key_attributes_t *attributes); 21132b31808SJens Wiklander 21232b31808SJens Wiklander /** Declare usage flags for a key. 21332b31808SJens Wiklander * 21432b31808SJens Wiklander * Usage flags are part of a key's usage policy. They encode what 21532b31808SJens Wiklander * kind of operations are permitted on the key. For more details, 21632b31808SJens Wiklander * refer to the documentation of the type #psa_key_usage_t. 21732b31808SJens Wiklander * 21832b31808SJens Wiklander * This function overwrites any usage flags 21932b31808SJens Wiklander * previously set in \p attributes. 22032b31808SJens Wiklander * 22132b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 22232b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 22332b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 22432b31808SJens Wiklander * 22532b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 22632b31808SJens Wiklander * \param usage_flags The usage flags to write. 22732b31808SJens Wiklander */ 22832b31808SJens Wiklander static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, 22932b31808SJens Wiklander psa_key_usage_t usage_flags); 23032b31808SJens Wiklander 23132b31808SJens Wiklander /** Retrieve the usage flags from key attributes. 23232b31808SJens Wiklander * 23332b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 23432b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 23532b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 23632b31808SJens Wiklander * 23732b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 23832b31808SJens Wiklander * 23932b31808SJens Wiklander * \return The usage flags stored in the attribute structure. 24032b31808SJens Wiklander */ 24132b31808SJens Wiklander static psa_key_usage_t psa_get_key_usage_flags( 24232b31808SJens Wiklander const psa_key_attributes_t *attributes); 24332b31808SJens Wiklander 24432b31808SJens Wiklander /** Declare the permitted algorithm policy for a key. 24532b31808SJens Wiklander * 24632b31808SJens Wiklander * The permitted algorithm policy of a key encodes which algorithm or 24732b31808SJens Wiklander * algorithms are permitted to be used with this key. The following 24832b31808SJens Wiklander * algorithm policies are supported: 24932b31808SJens Wiklander * - 0 does not allow any cryptographic operation with the key. The key 25032b31808SJens Wiklander * may be used for non-cryptographic actions such as exporting (if 25132b31808SJens Wiklander * permitted by the usage flags). 25232b31808SJens Wiklander * - An algorithm value permits this particular algorithm. 25332b31808SJens Wiklander * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified 25432b31808SJens Wiklander * signature scheme with any hash algorithm. 25532b31808SJens Wiklander * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows 25632b31808SJens Wiklander * any MAC algorithm from the same base class (e.g. CMAC) which 25732b31808SJens Wiklander * generates/verifies a MAC length greater than or equal to the length 25832b31808SJens Wiklander * encoded in the wildcard algorithm. 25932b31808SJens Wiklander * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG 26032b31808SJens Wiklander * allows any AEAD algorithm from the same base class (e.g. CCM) which 26132b31808SJens Wiklander * generates/verifies a tag length greater than or equal to the length 26232b31808SJens Wiklander * encoded in the wildcard algorithm. 26332b31808SJens Wiklander * 26432b31808SJens Wiklander * This function overwrites any algorithm policy 26532b31808SJens Wiklander * previously set in \p attributes. 26632b31808SJens Wiklander * 26732b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 26832b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 26932b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 27032b31808SJens Wiklander * 27132b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 27232b31808SJens Wiklander * \param alg The permitted algorithm policy to write. 27332b31808SJens Wiklander */ 27432b31808SJens Wiklander static void psa_set_key_algorithm(psa_key_attributes_t *attributes, 27532b31808SJens Wiklander psa_algorithm_t alg); 27632b31808SJens Wiklander 27732b31808SJens Wiklander 27832b31808SJens Wiklander /** Retrieve the algorithm policy from key attributes. 27932b31808SJens Wiklander * 28032b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 28132b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 28232b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 28332b31808SJens Wiklander * 28432b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 28532b31808SJens Wiklander * 28632b31808SJens Wiklander * \return The algorithm stored in the attribute structure. 28732b31808SJens Wiklander */ 28832b31808SJens Wiklander static psa_algorithm_t psa_get_key_algorithm( 28932b31808SJens Wiklander const psa_key_attributes_t *attributes); 29032b31808SJens Wiklander 29132b31808SJens Wiklander /** Declare the type of a key. 29232b31808SJens Wiklander * 29332b31808SJens Wiklander * This function overwrites any key type 29432b31808SJens Wiklander * previously set in \p attributes. 29532b31808SJens Wiklander * 29632b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 29732b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 29832b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 29932b31808SJens Wiklander * 30032b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 30132b31808SJens Wiklander * \param type The key type to write. 30232b31808SJens Wiklander * If this is 0, the key type in \p attributes 30332b31808SJens Wiklander * becomes unspecified. 30432b31808SJens Wiklander */ 30532b31808SJens Wiklander static void psa_set_key_type(psa_key_attributes_t *attributes, 30632b31808SJens Wiklander psa_key_type_t type); 30732b31808SJens Wiklander 30832b31808SJens Wiklander 30932b31808SJens Wiklander /** Declare the size of a key. 31032b31808SJens Wiklander * 31132b31808SJens Wiklander * This function overwrites any key size previously set in \p attributes. 31232b31808SJens Wiklander * 31332b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 31432b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 31532b31808SJens Wiklander * but in this case it must evaluate each of its arguments exactly once. 31632b31808SJens Wiklander * 31732b31808SJens Wiklander * \param[out] attributes The attribute structure to write to. 31832b31808SJens Wiklander * \param bits The key size in bits. 31932b31808SJens Wiklander * If this is 0, the key size in \p attributes 32032b31808SJens Wiklander * becomes unspecified. Keys of size 0 are 32132b31808SJens Wiklander * not supported. 32232b31808SJens Wiklander */ 32332b31808SJens Wiklander static void psa_set_key_bits(psa_key_attributes_t *attributes, 32432b31808SJens Wiklander size_t bits); 32532b31808SJens Wiklander 32632b31808SJens Wiklander /** Retrieve the key type from key attributes. 32732b31808SJens Wiklander * 32832b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 32932b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 33032b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 33132b31808SJens Wiklander * 33232b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 33332b31808SJens Wiklander * 33432b31808SJens Wiklander * \return The key type stored in the attribute structure. 33532b31808SJens Wiklander */ 33632b31808SJens Wiklander static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes); 33732b31808SJens Wiklander 33832b31808SJens Wiklander /** Retrieve the key size from key attributes. 33932b31808SJens Wiklander * 34032b31808SJens Wiklander * This function may be declared as `static` (i.e. without external 34132b31808SJens Wiklander * linkage). This function may be provided as a function-like macro, 34232b31808SJens Wiklander * but in this case it must evaluate its argument exactly once. 34332b31808SJens Wiklander * 34432b31808SJens Wiklander * \param[in] attributes The key attribute structure to query. 34532b31808SJens Wiklander * 34632b31808SJens Wiklander * \return The key size stored in the attribute structure, in bits. 34732b31808SJens Wiklander */ 34832b31808SJens Wiklander static size_t psa_get_key_bits(const psa_key_attributes_t *attributes); 34932b31808SJens Wiklander 35032b31808SJens Wiklander /** Retrieve the attributes of a key. 35132b31808SJens Wiklander * 35232b31808SJens Wiklander * This function first resets the attribute structure as with 35332b31808SJens Wiklander * psa_reset_key_attributes(). It then copies the attributes of 35432b31808SJens Wiklander * the given key into the given attribute structure. 35532b31808SJens Wiklander * 35632b31808SJens Wiklander * \note This function may allocate memory or other resources. 35732b31808SJens Wiklander * Once you have called this function on an attribute structure, 35832b31808SJens Wiklander * you must call psa_reset_key_attributes() to free these resources. 35932b31808SJens Wiklander * 36032b31808SJens Wiklander * \param[in] key Identifier of the key to query. 36132b31808SJens Wiklander * \param[in,out] attributes On success, the attributes of the key. 36232b31808SJens Wiklander * On failure, equivalent to a 36332b31808SJens Wiklander * freshly-initialized structure. 36432b31808SJens Wiklander * 36532b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 36632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 36732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 36832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 36932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 37032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 37132b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 37232b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 37332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 37432b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 37532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 37632b31808SJens Wiklander * results in this error code. 37732b31808SJens Wiklander */ 37832b31808SJens Wiklander psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, 37932b31808SJens Wiklander psa_key_attributes_t *attributes); 38032b31808SJens Wiklander 38132b31808SJens Wiklander /** Reset a key attribute structure to a freshly initialized state. 38232b31808SJens Wiklander * 38332b31808SJens Wiklander * You must initialize the attribute structure as described in the 38432b31808SJens Wiklander * documentation of the type #psa_key_attributes_t before calling this 38532b31808SJens Wiklander * function. Once the structure has been initialized, you may call this 38632b31808SJens Wiklander * function at any time. 38732b31808SJens Wiklander * 38832b31808SJens Wiklander * This function frees any auxiliary resources that the structure 38932b31808SJens Wiklander * may contain. 39032b31808SJens Wiklander * 39132b31808SJens Wiklander * \param[in,out] attributes The attribute structure to reset. 39232b31808SJens Wiklander */ 39332b31808SJens Wiklander void psa_reset_key_attributes(psa_key_attributes_t *attributes); 39432b31808SJens Wiklander 39532b31808SJens Wiklander /**@}*/ 39632b31808SJens Wiklander 39732b31808SJens Wiklander /** \defgroup key_management Key management 39832b31808SJens Wiklander * @{ 39932b31808SJens Wiklander */ 40032b31808SJens Wiklander 40132b31808SJens Wiklander /** Remove non-essential copies of key material from memory. 40232b31808SJens Wiklander * 40332b31808SJens Wiklander * If the key identifier designates a volatile key, this functions does not do 40432b31808SJens Wiklander * anything and returns successfully. 40532b31808SJens Wiklander * 40632b31808SJens Wiklander * If the key identifier designates a persistent key, then this function will 40732b31808SJens Wiklander * free all resources associated with the key in volatile memory. The key 40832b31808SJens Wiklander * data in persistent storage is not affected and the key can still be used. 40932b31808SJens Wiklander * 41032b31808SJens Wiklander * \param key Identifier of the key to purge. 41132b31808SJens Wiklander * 41232b31808SJens Wiklander * \retval #PSA_SUCCESS 41332b31808SJens Wiklander * The key material will have been removed from memory if it is not 41432b31808SJens Wiklander * currently required. 41532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 41632b31808SJens Wiklander * \p key is not a valid key identifier. 41732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 41832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 41932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 42032b31808SJens Wiklander * results in this error code. 42132b31808SJens Wiklander */ 42232b31808SJens Wiklander psa_status_t psa_purge_key(mbedtls_svc_key_id_t key); 42332b31808SJens Wiklander 42432b31808SJens Wiklander /** Make a copy of a key. 42532b31808SJens Wiklander * 42632b31808SJens Wiklander * Copy key material from one location to another. 42732b31808SJens Wiklander * 42832b31808SJens Wiklander * This function is primarily useful to copy a key from one location 42932b31808SJens Wiklander * to another, since it populates a key using the material from 43032b31808SJens Wiklander * another key which may have a different lifetime. 43132b31808SJens Wiklander * 43232b31808SJens Wiklander * This function may be used to share a key with a different party, 43332b31808SJens Wiklander * subject to implementation-defined restrictions on key sharing. 43432b31808SJens Wiklander * 43532b31808SJens Wiklander * The policy on the source key must have the usage flag 43632b31808SJens Wiklander * #PSA_KEY_USAGE_COPY set. 43732b31808SJens Wiklander * This flag is sufficient to permit the copy if the key has the lifetime 43832b31808SJens Wiklander * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT. 43932b31808SJens Wiklander * Some secure elements do not provide a way to copy a key without 44032b31808SJens Wiklander * making it extractable from the secure element. If a key is located 44132b31808SJens Wiklander * in such a secure element, then the key must have both usage flags 44232b31808SJens Wiklander * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make 44332b31808SJens Wiklander * a copy of the key outside the secure element. 44432b31808SJens Wiklander * 44532b31808SJens Wiklander * The resulting key may only be used in a way that conforms to 44632b31808SJens Wiklander * both the policy of the original key and the policy specified in 44732b31808SJens Wiklander * the \p attributes parameter: 44832b31808SJens Wiklander * - The usage flags on the resulting key are the bitwise-and of the 44932b31808SJens Wiklander * usage flags on the source policy and the usage flags in \p attributes. 45032b31808SJens Wiklander * - If both allow the same algorithm or wildcard-based 45132b31808SJens Wiklander * algorithm policy, the resulting key has the same algorithm policy. 45232b31808SJens Wiklander * - If either of the policies allows an algorithm and the other policy 45332b31808SJens Wiklander * allows a wildcard-based algorithm policy that includes this algorithm, 45432b31808SJens Wiklander * the resulting key allows the same algorithm. 45532b31808SJens Wiklander * - If the policies do not allow any algorithm in common, this function 45632b31808SJens Wiklander * fails with the status #PSA_ERROR_INVALID_ARGUMENT. 45732b31808SJens Wiklander * 45832b31808SJens Wiklander * The effect of this function on implementation-defined attributes is 45932b31808SJens Wiklander * implementation-defined. 46032b31808SJens Wiklander * 46132b31808SJens Wiklander * \param source_key The key to copy. It must allow the usage 46232b31808SJens Wiklander * #PSA_KEY_USAGE_COPY. If a private or secret key is 46332b31808SJens Wiklander * being copied outside of a secure element it must 46432b31808SJens Wiklander * also allow #PSA_KEY_USAGE_EXPORT. 46532b31808SJens Wiklander * \param[in] attributes The attributes for the new key. 46632b31808SJens Wiklander * They are used as follows: 46732b31808SJens Wiklander * - The key type and size may be 0. If either is 46832b31808SJens Wiklander * nonzero, it must match the corresponding 46932b31808SJens Wiklander * attribute of the source key. 47032b31808SJens Wiklander * - The key location (the lifetime and, for 47132b31808SJens Wiklander * persistent keys, the key identifier) is 47232b31808SJens Wiklander * used directly. 47332b31808SJens Wiklander * - The policy constraints (usage flags and 47432b31808SJens Wiklander * algorithm policy) are combined from 47532b31808SJens Wiklander * the source key and \p attributes so that 47632b31808SJens Wiklander * both sets of restrictions apply, as 47732b31808SJens Wiklander * described in the documentation of this function. 47832b31808SJens Wiklander * \param[out] target_key On success, an identifier for the newly created 47932b31808SJens Wiklander * key. For persistent keys, this is the key 48032b31808SJens Wiklander * identifier defined in \p attributes. 48132b31808SJens Wiklander * \c 0 on failure. 48232b31808SJens Wiklander * 48332b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 48432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE 48532b31808SJens Wiklander * \p source_key is invalid. 48632b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS 48732b31808SJens Wiklander * This is an attempt to create a persistent key, and there is 48832b31808SJens Wiklander * already a persistent key with the given identifier. 48932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 49032b31808SJens Wiklander * The lifetime or identifier in \p attributes are invalid, or 49132b31808SJens Wiklander * the policy constraints on the source and specified in 49232b31808SJens Wiklander * \p attributes are incompatible, or 49332b31808SJens Wiklander * \p attributes specifies a key type or key size 49432b31808SJens Wiklander * which does not match the attributes of the source key. 49532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 49632b31808SJens Wiklander * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or 49732b31808SJens Wiklander * the source key is not exportable and its lifetime does not 49832b31808SJens Wiklander * allow copying it to the target's lifetime. 49932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 50032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 50132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 50232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 50332b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 50432b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 50532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 50632b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 50732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 50832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 50932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 51032b31808SJens Wiklander * results in this error code. 51132b31808SJens Wiklander */ 51232b31808SJens Wiklander psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, 51332b31808SJens Wiklander const psa_key_attributes_t *attributes, 51432b31808SJens Wiklander mbedtls_svc_key_id_t *target_key); 51532b31808SJens Wiklander 51632b31808SJens Wiklander 51732b31808SJens Wiklander /** 51832b31808SJens Wiklander * \brief Destroy a key. 51932b31808SJens Wiklander * 52032b31808SJens Wiklander * This function destroys a key from both volatile 52132b31808SJens Wiklander * memory and, if applicable, non-volatile storage. Implementations shall 52232b31808SJens Wiklander * make a best effort to ensure that the key material cannot be recovered. 52332b31808SJens Wiklander * 52432b31808SJens Wiklander * This function also erases any metadata such as policies and frees 52532b31808SJens Wiklander * resources associated with the key. 52632b31808SJens Wiklander * 52732b31808SJens Wiklander * If a key is currently in use in a multipart operation, then destroying the 52832b31808SJens Wiklander * key will cause the multipart operation to fail. 52932b31808SJens Wiklander * 530*b0563631STom Van Eyck * \warning We can only guarantee that the the key material will 531*b0563631STom Van Eyck * eventually be wiped from memory. With threading enabled 532*b0563631STom Van Eyck * and during concurrent execution, copies of the key material may 533*b0563631STom Van Eyck * still exist until all threads have finished using the key. 534*b0563631STom Van Eyck * 53532b31808SJens Wiklander * \param key Identifier of the key to erase. If this is \c 0, do nothing and 53632b31808SJens Wiklander * return #PSA_SUCCESS. 53732b31808SJens Wiklander * 53832b31808SJens Wiklander * \retval #PSA_SUCCESS 53932b31808SJens Wiklander * \p key was a valid identifier and the key material that it 54032b31808SJens Wiklander * referred to has been erased. Alternatively, \p key is \c 0. 54132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 54232b31808SJens Wiklander * The key cannot be erased because it is 54332b31808SJens Wiklander * read-only, either due to a policy or due to physical restrictions. 54432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE 54532b31808SJens Wiklander * \p key is not a valid identifier nor \c 0. 54632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE 54732b31808SJens Wiklander * There was a failure in communication with the cryptoprocessor. 54832b31808SJens Wiklander * The key material may still be present in the cryptoprocessor. 54932b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID 55032b31808SJens Wiklander * This error is typically a result of either storage corruption on a 55132b31808SJens Wiklander * cleartext storage backend, or an attempt to read data that was 55232b31808SJens Wiklander * written by an incompatible version of the library. 55332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 55432b31808SJens Wiklander * The storage is corrupted. Implementations shall make a best effort 55532b31808SJens Wiklander * to erase key material even in this stage, however applications 55632b31808SJens Wiklander * should be aware that it may be impossible to guarantee that the 55732b31808SJens Wiklander * key material is not recoverable in such cases. 55832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED 55932b31808SJens Wiklander * An unexpected condition which is not a storage corruption or 56032b31808SJens Wiklander * a communication failure occurred. The cryptoprocessor may have 56132b31808SJens Wiklander * been compromised. 56232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 56332b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 56432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 56532b31808SJens Wiklander * results in this error code. 56632b31808SJens Wiklander */ 56732b31808SJens Wiklander psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key); 56832b31808SJens Wiklander 56932b31808SJens Wiklander /**@}*/ 57032b31808SJens Wiklander 57132b31808SJens Wiklander /** \defgroup import_export Key import and export 57232b31808SJens Wiklander * @{ 57332b31808SJens Wiklander */ 57432b31808SJens Wiklander 57532b31808SJens Wiklander /** 57632b31808SJens Wiklander * \brief Import a key in binary format. 57732b31808SJens Wiklander * 57832b31808SJens Wiklander * This function supports any output from psa_export_key(). Refer to the 57932b31808SJens Wiklander * documentation of psa_export_public_key() for the format of public keys 58032b31808SJens Wiklander * and to the documentation of psa_export_key() for the format for 58132b31808SJens Wiklander * other key types. 58232b31808SJens Wiklander * 58332b31808SJens Wiklander * The key data determines the key size. The attributes may optionally 58432b31808SJens Wiklander * specify a key size; in this case it must match the size determined 58532b31808SJens Wiklander * from the key data. A key size of 0 in \p attributes indicates that 58632b31808SJens Wiklander * the key size is solely determined by the key data. 58732b31808SJens Wiklander * 58832b31808SJens Wiklander * Implementations must reject an attempt to import a key of size 0. 58932b31808SJens Wiklander * 59032b31808SJens Wiklander * This specification supports a single format for each key type. 59132b31808SJens Wiklander * Implementations may support other formats as long as the standard 59232b31808SJens Wiklander * format is supported. Implementations that support other formats 59332b31808SJens Wiklander * should ensure that the formats are clearly unambiguous so as to 59432b31808SJens Wiklander * minimize the risk that an invalid input is accidentally interpreted 59532b31808SJens Wiklander * according to a different format. 59632b31808SJens Wiklander * 59732b31808SJens Wiklander * \param[in] attributes The attributes for the new key. 59832b31808SJens Wiklander * The key size is always determined from the 59932b31808SJens Wiklander * \p data buffer. 60032b31808SJens Wiklander * If the key size in \p attributes is nonzero, 60132b31808SJens Wiklander * it must be equal to the size from \p data. 60232b31808SJens Wiklander * \param[out] key On success, an identifier to the newly created key. 60332b31808SJens Wiklander * For persistent keys, this is the key identifier 60432b31808SJens Wiklander * defined in \p attributes. 60532b31808SJens Wiklander * \c 0 on failure. 60632b31808SJens Wiklander * \param[in] data Buffer containing the key data. The content of this 60732b31808SJens Wiklander * buffer is interpreted according to the type declared 60832b31808SJens Wiklander * in \p attributes. 60932b31808SJens Wiklander * All implementations must support at least the format 61032b31808SJens Wiklander * described in the documentation 61132b31808SJens Wiklander * of psa_export_key() or psa_export_public_key() for 61232b31808SJens Wiklander * the chosen type. Implementations may allow other 61332b31808SJens Wiklander * formats, but should be conservative: implementations 61432b31808SJens Wiklander * should err on the side of rejecting content if it 61532b31808SJens Wiklander * may be erroneous (e.g. wrong type or truncated data). 61632b31808SJens Wiklander * \param data_length Size of the \p data buffer in bytes. 61732b31808SJens Wiklander * 61832b31808SJens Wiklander * \retval #PSA_SUCCESS 61932b31808SJens Wiklander * Success. 62032b31808SJens Wiklander * If the key is persistent, the key material and the key's metadata 62132b31808SJens Wiklander * have been saved to persistent storage. 62232b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS 62332b31808SJens Wiklander * This is an attempt to create a persistent key, and there is 62432b31808SJens Wiklander * already a persistent key with the given identifier. 62532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 62632b31808SJens Wiklander * The key type or key size is not supported, either by the 62732b31808SJens Wiklander * implementation in general or in this particular persistent location. 62832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 62932b31808SJens Wiklander * The key attributes, as a whole, are invalid, or 63032b31808SJens Wiklander * the key data is not correctly formatted, or 63132b31808SJens Wiklander * the size in \p attributes is nonzero and does not match the size 63232b31808SJens Wiklander * of the key data. 63332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 63432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 63532b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 63632b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 63732b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 63832b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 63932b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 64032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 64132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 64232b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 64332b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 64432b31808SJens Wiklander * results in this error code. 64532b31808SJens Wiklander */ 64632b31808SJens Wiklander psa_status_t psa_import_key(const psa_key_attributes_t *attributes, 64732b31808SJens Wiklander const uint8_t *data, 64832b31808SJens Wiklander size_t data_length, 64932b31808SJens Wiklander mbedtls_svc_key_id_t *key); 65032b31808SJens Wiklander 65132b31808SJens Wiklander 65232b31808SJens Wiklander 65332b31808SJens Wiklander /** 65432b31808SJens Wiklander * \brief Export a key in binary format. 65532b31808SJens Wiklander * 65632b31808SJens Wiklander * The output of this function can be passed to psa_import_key() to 65732b31808SJens Wiklander * create an equivalent object. 65832b31808SJens Wiklander * 65932b31808SJens Wiklander * If the implementation of psa_import_key() supports other formats 66032b31808SJens Wiklander * beyond the format specified here, the output from psa_export_key() 66132b31808SJens Wiklander * must use the representation specified here, not the original 66232b31808SJens Wiklander * representation. 66332b31808SJens Wiklander * 66432b31808SJens Wiklander * For standard key types, the output format is as follows: 66532b31808SJens Wiklander * 66632b31808SJens Wiklander * - For symmetric keys (including MAC keys), the format is the 66732b31808SJens Wiklander * raw bytes of the key. 66832b31808SJens Wiklander * - For DES, the key data consists of 8 bytes. The parity bits must be 66932b31808SJens Wiklander * correct. 67032b31808SJens Wiklander * - For Triple-DES, the format is the concatenation of the 67132b31808SJens Wiklander * two or three DES keys. 67232b31808SJens Wiklander * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format 67332b31808SJens Wiklander * is the non-encrypted DER encoding of the representation defined by 67432b31808SJens Wiklander * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0. 67532b31808SJens Wiklander * ``` 67632b31808SJens Wiklander * RSAPrivateKey ::= SEQUENCE { 67732b31808SJens Wiklander * version INTEGER, -- must be 0 67832b31808SJens Wiklander * modulus INTEGER, -- n 67932b31808SJens Wiklander * publicExponent INTEGER, -- e 68032b31808SJens Wiklander * privateExponent INTEGER, -- d 68132b31808SJens Wiklander * prime1 INTEGER, -- p 68232b31808SJens Wiklander * prime2 INTEGER, -- q 68332b31808SJens Wiklander * exponent1 INTEGER, -- d mod (p-1) 68432b31808SJens Wiklander * exponent2 INTEGER, -- d mod (q-1) 68532b31808SJens Wiklander * coefficient INTEGER, -- (inverse of q) mod p 68632b31808SJens Wiklander * } 68732b31808SJens Wiklander * ``` 68832b31808SJens Wiklander * - For elliptic curve key pairs (key types for which 68932b31808SJens Wiklander * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is 69032b31808SJens Wiklander * a representation of the private value as a `ceiling(m/8)`-byte string 69132b31808SJens Wiklander * where `m` is the bit size associated with the curve, i.e. the bit size 69232b31808SJens Wiklander * of the order of the curve's coordinate field. This byte string is 69332b31808SJens Wiklander * in little-endian order for Montgomery curves (curve types 69432b31808SJens Wiklander * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass 69532b31808SJens Wiklander * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX` 69632b31808SJens Wiklander * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`). 69732b31808SJens Wiklander * For Weierstrass curves, this is the content of the `privateKey` field of 69832b31808SJens Wiklander * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves, 69932b31808SJens Wiklander * the format is defined by RFC 7748, and output is masked according to §5. 70032b31808SJens Wiklander * For twisted Edwards curves, the private key is as defined by RFC 8032 70132b31808SJens Wiklander * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448). 70232b31808SJens Wiklander * - For Diffie-Hellman key exchange key pairs (key types for which 70332b31808SJens Wiklander * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the 70432b31808SJens Wiklander * format is the representation of the private key `x` as a big-endian byte 70532b31808SJens Wiklander * string. The length of the byte string is the private key size in bytes 70632b31808SJens Wiklander * (leading zeroes are not stripped). 70732b31808SJens Wiklander * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is 70832b31808SJens Wiklander * true), the format is the same as for psa_export_public_key(). 70932b31808SJens Wiklander * 71032b31808SJens Wiklander * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set. 71132b31808SJens Wiklander * 71232b31808SJens Wiklander * \param key Identifier of the key to export. It must allow the 71332b31808SJens Wiklander * usage #PSA_KEY_USAGE_EXPORT, unless it is a public 71432b31808SJens Wiklander * key. 71532b31808SJens Wiklander * \param[out] data Buffer where the key data is to be written. 71632b31808SJens Wiklander * \param data_size Size of the \p data buffer in bytes. 71732b31808SJens Wiklander * \param[out] data_length On success, the number of bytes 71832b31808SJens Wiklander * that make up the key data. 71932b31808SJens Wiklander * 72032b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 72132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 72232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 72332b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_EXPORT flag. 72432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 72532b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 72632b31808SJens Wiklander * The size of the \p data buffer is too small. You can determine a 72732b31808SJens Wiklander * sufficient buffer size by calling 72832b31808SJens Wiklander * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits) 72932b31808SJens Wiklander * where \c type is the key type 73032b31808SJens Wiklander * and \c bits is the key size in bits. 73132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 73232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 73332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 73432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 73532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 73632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 73732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 73832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 73932b31808SJens Wiklander * results in this error code. 74032b31808SJens Wiklander */ 74132b31808SJens Wiklander psa_status_t psa_export_key(mbedtls_svc_key_id_t key, 74232b31808SJens Wiklander uint8_t *data, 74332b31808SJens Wiklander size_t data_size, 74432b31808SJens Wiklander size_t *data_length); 74532b31808SJens Wiklander 74632b31808SJens Wiklander /** 74732b31808SJens Wiklander * \brief Export a public key or the public part of a key pair in binary format. 74832b31808SJens Wiklander * 74932b31808SJens Wiklander * The output of this function can be passed to psa_import_key() to 75032b31808SJens Wiklander * create an object that is equivalent to the public key. 75132b31808SJens Wiklander * 75232b31808SJens Wiklander * This specification supports a single format for each key type. 75332b31808SJens Wiklander * Implementations may support other formats as long as the standard 75432b31808SJens Wiklander * format is supported. Implementations that support other formats 75532b31808SJens Wiklander * should ensure that the formats are clearly unambiguous so as to 75632b31808SJens Wiklander * minimize the risk that an invalid input is accidentally interpreted 75732b31808SJens Wiklander * according to a different format. 75832b31808SJens Wiklander * 75932b31808SJens Wiklander * For standard key types, the output format is as follows: 76032b31808SJens Wiklander * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of 76132b31808SJens Wiklander * the representation defined by RFC 3279 §2.3.1 as `RSAPublicKey`. 76232b31808SJens Wiklander * ``` 76332b31808SJens Wiklander * RSAPublicKey ::= SEQUENCE { 76432b31808SJens Wiklander * modulus INTEGER, -- n 76532b31808SJens Wiklander * publicExponent INTEGER } -- e 76632b31808SJens Wiklander * ``` 76732b31808SJens Wiklander * - For elliptic curve keys on a twisted Edwards curve (key types for which 76832b31808SJens Wiklander * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY 76932b31808SJens Wiklander * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined 77032b31808SJens Wiklander * by RFC 8032 77132b31808SJens Wiklander * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448). 77232b31808SJens Wiklander * - For other elliptic curve public keys (key types for which 77332b31808SJens Wiklander * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed 77432b31808SJens Wiklander * representation defined by SEC1 §2.3.3 as the content of an ECPoint. 77532b31808SJens Wiklander * Let `m` be the bit size associated with the curve, i.e. the bit size of 77632b31808SJens Wiklander * `q` for a curve over `F_q`. The representation consists of: 77732b31808SJens Wiklander * - The byte 0x04; 77832b31808SJens Wiklander * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; 77932b31808SJens Wiklander * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. 78032b31808SJens Wiklander * - For Diffie-Hellman key exchange public keys (key types for which 78132b31808SJens Wiklander * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true), 78232b31808SJens Wiklander * the format is the representation of the public key `y = g^x mod p` as a 78332b31808SJens Wiklander * big-endian byte string. The length of the byte string is the length of the 78432b31808SJens Wiklander * base prime `p` in bytes. 78532b31808SJens Wiklander * 78632b31808SJens Wiklander * Exporting a public key object or the public part of a key pair is 78732b31808SJens Wiklander * always permitted, regardless of the key's usage flags. 78832b31808SJens Wiklander * 78932b31808SJens Wiklander * \param key Identifier of the key to export. 79032b31808SJens Wiklander * \param[out] data Buffer where the key data is to be written. 79132b31808SJens Wiklander * \param data_size Size of the \p data buffer in bytes. 79232b31808SJens Wiklander * \param[out] data_length On success, the number of bytes 79332b31808SJens Wiklander * that make up the key data. 79432b31808SJens Wiklander * 79532b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 79632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 79732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 79832b31808SJens Wiklander * The key is neither a public key nor a key pair. 79932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 80032b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 80132b31808SJens Wiklander * The size of the \p data buffer is too small. You can determine a 80232b31808SJens Wiklander * sufficient buffer size by calling 80332b31808SJens Wiklander * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits) 80432b31808SJens Wiklander * where \c type is the key type 80532b31808SJens Wiklander * and \c bits is the key size in bits. 80632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 80732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 80832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 80932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 81032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 81132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 81232b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 81332b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 81432b31808SJens Wiklander * results in this error code. 81532b31808SJens Wiklander */ 81632b31808SJens Wiklander psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, 81732b31808SJens Wiklander uint8_t *data, 81832b31808SJens Wiklander size_t data_size, 81932b31808SJens Wiklander size_t *data_length); 82032b31808SJens Wiklander 82132b31808SJens Wiklander 82232b31808SJens Wiklander 82332b31808SJens Wiklander /**@}*/ 82432b31808SJens Wiklander 82532b31808SJens Wiklander /** \defgroup hash Message digests 82632b31808SJens Wiklander * @{ 82732b31808SJens Wiklander */ 82832b31808SJens Wiklander 82932b31808SJens Wiklander /** Calculate the hash (digest) of a message. 83032b31808SJens Wiklander * 83132b31808SJens Wiklander * \note To verify the hash of a message against an 83232b31808SJens Wiklander * expected value, use psa_hash_compare() instead. 83332b31808SJens Wiklander * 83432b31808SJens Wiklander * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value 83532b31808SJens Wiklander * such that #PSA_ALG_IS_HASH(\p alg) is true). 83632b31808SJens Wiklander * \param[in] input Buffer containing the message to hash. 83732b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 83832b31808SJens Wiklander * \param[out] hash Buffer where the hash is to be written. 83932b31808SJens Wiklander * \param hash_size Size of the \p hash buffer in bytes. 84032b31808SJens Wiklander * \param[out] hash_length On success, the number of bytes 84132b31808SJens Wiklander * that make up the hash value. This is always 84232b31808SJens Wiklander * #PSA_HASH_LENGTH(\p alg). 84332b31808SJens Wiklander * 84432b31808SJens Wiklander * \retval #PSA_SUCCESS 84532b31808SJens Wiklander * Success. 84632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 84732b31808SJens Wiklander * \p alg is not supported or is not a hash algorithm. 84832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 84932b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 85032b31808SJens Wiklander * \p hash_size is too small 85132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 85232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 85332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 85432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 85532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 85632b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 85732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 85832b31808SJens Wiklander * results in this error code. 85932b31808SJens Wiklander */ 86032b31808SJens Wiklander psa_status_t psa_hash_compute(psa_algorithm_t alg, 86132b31808SJens Wiklander const uint8_t *input, 86232b31808SJens Wiklander size_t input_length, 86332b31808SJens Wiklander uint8_t *hash, 86432b31808SJens Wiklander size_t hash_size, 86532b31808SJens Wiklander size_t *hash_length); 86632b31808SJens Wiklander 86732b31808SJens Wiklander /** Calculate the hash (digest) of a message and compare it with a 86832b31808SJens Wiklander * reference value. 86932b31808SJens Wiklander * 87032b31808SJens Wiklander * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value 87132b31808SJens Wiklander * such that #PSA_ALG_IS_HASH(\p alg) is true). 87232b31808SJens Wiklander * \param[in] input Buffer containing the message to hash. 87332b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 87432b31808SJens Wiklander * \param[out] hash Buffer containing the expected hash value. 87532b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 87632b31808SJens Wiklander * 87732b31808SJens Wiklander * \retval #PSA_SUCCESS 87832b31808SJens Wiklander * The expected hash is identical to the actual hash of the input. 87932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 88032b31808SJens Wiklander * The hash of the message was calculated successfully, but it 88132b31808SJens Wiklander * differs from the expected hash. 88232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 88332b31808SJens Wiklander * \p alg is not supported or is not a hash algorithm. 88432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 88532b31808SJens Wiklander * \p input_length or \p hash_length do not match the hash size for \p alg 88632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 88732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 88832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 88932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 89032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 89132b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 89232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 89332b31808SJens Wiklander * results in this error code. 89432b31808SJens Wiklander */ 89532b31808SJens Wiklander psa_status_t psa_hash_compare(psa_algorithm_t alg, 89632b31808SJens Wiklander const uint8_t *input, 89732b31808SJens Wiklander size_t input_length, 89832b31808SJens Wiklander const uint8_t *hash, 89932b31808SJens Wiklander size_t hash_length); 90032b31808SJens Wiklander 90132b31808SJens Wiklander /** The type of the state data structure for multipart hash operations. 90232b31808SJens Wiklander * 90332b31808SJens Wiklander * Before calling any function on a hash operation object, the application must 90432b31808SJens Wiklander * initialize it by any of the following means: 90532b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 90632b31808SJens Wiklander * \code 90732b31808SJens Wiklander * psa_hash_operation_t operation; 90832b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 90932b31808SJens Wiklander * \endcode 91032b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 91132b31808SJens Wiklander * \code 91232b31808SJens Wiklander * psa_hash_operation_t operation = {0}; 91332b31808SJens Wiklander * \endcode 91432b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT, 91532b31808SJens Wiklander * for example: 91632b31808SJens Wiklander * \code 91732b31808SJens Wiklander * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; 91832b31808SJens Wiklander * \endcode 91932b31808SJens Wiklander * - Assign the result of the function psa_hash_operation_init() 92032b31808SJens Wiklander * to the structure, for example: 92132b31808SJens Wiklander * \code 92232b31808SJens Wiklander * psa_hash_operation_t operation; 92332b31808SJens Wiklander * operation = psa_hash_operation_init(); 92432b31808SJens Wiklander * \endcode 92532b31808SJens Wiklander * 92632b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 92732b31808SJens Wiklander * make any assumptions about the content of this structure. 92832b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 92932b31808SJens Wiklander typedef struct psa_hash_operation_s psa_hash_operation_t; 93032b31808SJens Wiklander 93132b31808SJens Wiklander /** \def PSA_HASH_OPERATION_INIT 93232b31808SJens Wiklander * 93332b31808SJens Wiklander * This macro returns a suitable initializer for a hash operation object 93432b31808SJens Wiklander * of type #psa_hash_operation_t. 93532b31808SJens Wiklander */ 93632b31808SJens Wiklander 93732b31808SJens Wiklander /** Return an initial value for a hash operation object. 93832b31808SJens Wiklander */ 93932b31808SJens Wiklander static psa_hash_operation_t psa_hash_operation_init(void); 94032b31808SJens Wiklander 94132b31808SJens Wiklander /** Set up a multipart hash operation. 94232b31808SJens Wiklander * 94332b31808SJens Wiklander * The sequence of operations to calculate a hash (message digest) 94432b31808SJens Wiklander * is as follows: 94532b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 94632b31808SJens Wiklander * listed here. 94732b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 94832b31808SJens Wiklander * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT. 94932b31808SJens Wiklander * -# Call psa_hash_setup() to specify the algorithm. 95032b31808SJens Wiklander * -# Call psa_hash_update() zero, one or more times, passing a fragment 95132b31808SJens Wiklander * of the message each time. The hash that is calculated is the hash 95232b31808SJens Wiklander * of the concatenation of these messages in order. 95332b31808SJens Wiklander * -# To calculate the hash, call psa_hash_finish(). 95432b31808SJens Wiklander * To compare the hash with an expected value, call psa_hash_verify(). 95532b31808SJens Wiklander * 95632b31808SJens Wiklander * If an error occurs at any step after a call to psa_hash_setup(), the 95732b31808SJens Wiklander * operation will need to be reset by a call to psa_hash_abort(). The 95832b31808SJens Wiklander * application may call psa_hash_abort() at any time after the operation 95932b31808SJens Wiklander * has been initialized. 96032b31808SJens Wiklander * 96132b31808SJens Wiklander * After a successful call to psa_hash_setup(), the application must 96232b31808SJens Wiklander * eventually terminate the operation. The following events terminate an 96332b31808SJens Wiklander * operation: 96432b31808SJens Wiklander * - A successful call to psa_hash_finish() or psa_hash_verify(). 96532b31808SJens Wiklander * - A call to psa_hash_abort(). 96632b31808SJens Wiklander * 96732b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 96832b31808SJens Wiklander * been initialized as per the documentation for 96932b31808SJens Wiklander * #psa_hash_operation_t and not yet in use. 97032b31808SJens Wiklander * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value 97132b31808SJens Wiklander * such that #PSA_ALG_IS_HASH(\p alg) is true). 97232b31808SJens Wiklander * 97332b31808SJens Wiklander * \retval #PSA_SUCCESS 97432b31808SJens Wiklander * Success. 97532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 97632b31808SJens Wiklander * \p alg is not a supported hash algorithm. 97732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 97832b31808SJens Wiklander * \p alg is not a hash algorithm. 97932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 98032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 98132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 98232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 98332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 98432b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 98532b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 98632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 98732b31808SJens Wiklander * results in this error code. 98832b31808SJens Wiklander */ 98932b31808SJens Wiklander psa_status_t psa_hash_setup(psa_hash_operation_t *operation, 99032b31808SJens Wiklander psa_algorithm_t alg); 99132b31808SJens Wiklander 99232b31808SJens Wiklander /** Add a message fragment to a multipart hash operation. 99332b31808SJens Wiklander * 99432b31808SJens Wiklander * The application must call psa_hash_setup() before calling this function. 99532b31808SJens Wiklander * 99632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 99732b31808SJens Wiklander * state and must be aborted by calling psa_hash_abort(). 99832b31808SJens Wiklander * 99932b31808SJens Wiklander * \param[in,out] operation Active hash operation. 100032b31808SJens Wiklander * \param[in] input Buffer containing the message fragment to hash. 100132b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 100232b31808SJens Wiklander * 100332b31808SJens Wiklander * \retval #PSA_SUCCESS 100432b31808SJens Wiklander * Success. 100532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 100632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 100732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 100832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 100932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 101032b31808SJens Wiklander * The operation state is not valid (it must be active), or 101132b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 101232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 101332b31808SJens Wiklander * results in this error code. 101432b31808SJens Wiklander */ 101532b31808SJens Wiklander psa_status_t psa_hash_update(psa_hash_operation_t *operation, 101632b31808SJens Wiklander const uint8_t *input, 101732b31808SJens Wiklander size_t input_length); 101832b31808SJens Wiklander 101932b31808SJens Wiklander /** Finish the calculation of the hash of a message. 102032b31808SJens Wiklander * 102132b31808SJens Wiklander * The application must call psa_hash_setup() before calling this function. 102232b31808SJens Wiklander * This function calculates the hash of the message formed by concatenating 102332b31808SJens Wiklander * the inputs passed to preceding calls to psa_hash_update(). 102432b31808SJens Wiklander * 102532b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 102632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 102732b31808SJens Wiklander * state and must be aborted by calling psa_hash_abort(). 102832b31808SJens Wiklander * 102932b31808SJens Wiklander * \warning Applications should not call this function if they expect 103032b31808SJens Wiklander * a specific value for the hash. Call psa_hash_verify() instead. 103132b31808SJens Wiklander * Beware that comparing integrity or authenticity data such as 103232b31808SJens Wiklander * hash values with a function such as \c memcmp is risky 103332b31808SJens Wiklander * because the time taken by the comparison may leak information 103432b31808SJens Wiklander * about the hashed data which could allow an attacker to guess 103532b31808SJens Wiklander * a valid hash and thereby bypass security controls. 103632b31808SJens Wiklander * 103732b31808SJens Wiklander * \param[in,out] operation Active hash operation. 103832b31808SJens Wiklander * \param[out] hash Buffer where the hash is to be written. 103932b31808SJens Wiklander * \param hash_size Size of the \p hash buffer in bytes. 104032b31808SJens Wiklander * \param[out] hash_length On success, the number of bytes 104132b31808SJens Wiklander * that make up the hash value. This is always 104232b31808SJens Wiklander * #PSA_HASH_LENGTH(\c alg) where \c alg is the 104332b31808SJens Wiklander * hash algorithm that is calculated. 104432b31808SJens Wiklander * 104532b31808SJens Wiklander * \retval #PSA_SUCCESS 104632b31808SJens Wiklander * Success. 104732b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 104832b31808SJens Wiklander * The size of the \p hash buffer is too small. You can determine a 104932b31808SJens Wiklander * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg) 105032b31808SJens Wiklander * where \c alg is the hash algorithm that is calculated. 105132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 105232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 105332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 105432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 105532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 105632b31808SJens Wiklander * The operation state is not valid (it must be active), or 105732b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 105832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 105932b31808SJens Wiklander * results in this error code. 106032b31808SJens Wiklander */ 106132b31808SJens Wiklander psa_status_t psa_hash_finish(psa_hash_operation_t *operation, 106232b31808SJens Wiklander uint8_t *hash, 106332b31808SJens Wiklander size_t hash_size, 106432b31808SJens Wiklander size_t *hash_length); 106532b31808SJens Wiklander 106632b31808SJens Wiklander /** Finish the calculation of the hash of a message and compare it with 106732b31808SJens Wiklander * an expected value. 106832b31808SJens Wiklander * 106932b31808SJens Wiklander * The application must call psa_hash_setup() before calling this function. 107032b31808SJens Wiklander * This function calculates the hash of the message formed by concatenating 107132b31808SJens Wiklander * the inputs passed to preceding calls to psa_hash_update(). It then 107232b31808SJens Wiklander * compares the calculated hash with the expected hash passed as a 107332b31808SJens Wiklander * parameter to this function. 107432b31808SJens Wiklander * 107532b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 107632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 107732b31808SJens Wiklander * state and must be aborted by calling psa_hash_abort(). 107832b31808SJens Wiklander * 107932b31808SJens Wiklander * \note Implementations shall make the best effort to ensure that the 108032b31808SJens Wiklander * comparison between the actual hash and the expected hash is performed 108132b31808SJens Wiklander * in constant time. 108232b31808SJens Wiklander * 108332b31808SJens Wiklander * \param[in,out] operation Active hash operation. 108432b31808SJens Wiklander * \param[in] hash Buffer containing the expected hash value. 108532b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 108632b31808SJens Wiklander * 108732b31808SJens Wiklander * \retval #PSA_SUCCESS 108832b31808SJens Wiklander * The expected hash is identical to the actual hash of the message. 108932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 109032b31808SJens Wiklander * The hash of the message was calculated successfully, but it 109132b31808SJens Wiklander * differs from the expected hash. 109232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 109332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 109432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 109532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 109632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 109732b31808SJens Wiklander * The operation state is not valid (it must be active), or 109832b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 109932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 110032b31808SJens Wiklander * results in this error code. 110132b31808SJens Wiklander */ 110232b31808SJens Wiklander psa_status_t psa_hash_verify(psa_hash_operation_t *operation, 110332b31808SJens Wiklander const uint8_t *hash, 110432b31808SJens Wiklander size_t hash_length); 110532b31808SJens Wiklander 110632b31808SJens Wiklander /** Abort a hash operation. 110732b31808SJens Wiklander * 110832b31808SJens Wiklander * Aborting an operation frees all associated resources except for the 110932b31808SJens Wiklander * \p operation structure itself. Once aborted, the operation object 111032b31808SJens Wiklander * can be reused for another operation by calling 111132b31808SJens Wiklander * psa_hash_setup() again. 111232b31808SJens Wiklander * 111332b31808SJens Wiklander * You may call this function any time after the operation object has 111432b31808SJens Wiklander * been initialized by one of the methods described in #psa_hash_operation_t. 111532b31808SJens Wiklander * 111632b31808SJens Wiklander * In particular, calling psa_hash_abort() after the operation has been 111732b31808SJens Wiklander * terminated by a call to psa_hash_abort(), psa_hash_finish() or 111832b31808SJens Wiklander * psa_hash_verify() is safe and has no effect. 111932b31808SJens Wiklander * 112032b31808SJens Wiklander * \param[in,out] operation Initialized hash operation. 112132b31808SJens Wiklander * 112232b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 112332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 112432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 112532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 112632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 112732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 112832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 112932b31808SJens Wiklander * results in this error code. 113032b31808SJens Wiklander */ 113132b31808SJens Wiklander psa_status_t psa_hash_abort(psa_hash_operation_t *operation); 113232b31808SJens Wiklander 113332b31808SJens Wiklander /** Clone a hash operation. 113432b31808SJens Wiklander * 113532b31808SJens Wiklander * This function copies the state of an ongoing hash operation to 113632b31808SJens Wiklander * a new operation object. In other words, this function is equivalent 113732b31808SJens Wiklander * to calling psa_hash_setup() on \p target_operation with the same 113832b31808SJens Wiklander * algorithm that \p source_operation was set up for, then 113932b31808SJens Wiklander * psa_hash_update() on \p target_operation with the same input that 114032b31808SJens Wiklander * that was passed to \p source_operation. After this function returns, the 114132b31808SJens Wiklander * two objects are independent, i.e. subsequent calls involving one of 114232b31808SJens Wiklander * the objects do not affect the other object. 114332b31808SJens Wiklander * 114432b31808SJens Wiklander * \param[in] source_operation The active hash operation to clone. 114532b31808SJens Wiklander * \param[in,out] target_operation The operation object to set up. 114632b31808SJens Wiklander * It must be initialized but not active. 114732b31808SJens Wiklander * 114832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 114932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 115032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 115132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 115232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 115332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 115432b31808SJens Wiklander * The \p source_operation state is not valid (it must be active), or 115532b31808SJens Wiklander * the \p target_operation state is not valid (it must be inactive), or 115632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 115732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 115832b31808SJens Wiklander * results in this error code. 115932b31808SJens Wiklander */ 116032b31808SJens Wiklander psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, 116132b31808SJens Wiklander psa_hash_operation_t *target_operation); 116232b31808SJens Wiklander 116332b31808SJens Wiklander /**@}*/ 116432b31808SJens Wiklander 116532b31808SJens Wiklander /** \defgroup MAC Message authentication codes 116632b31808SJens Wiklander * @{ 116732b31808SJens Wiklander */ 116832b31808SJens Wiklander 116932b31808SJens Wiklander /** Calculate the MAC (message authentication code) of a message. 117032b31808SJens Wiklander * 117132b31808SJens Wiklander * \note To verify the MAC of a message against an 117232b31808SJens Wiklander * expected value, use psa_mac_verify() instead. 117332b31808SJens Wiklander * Beware that comparing integrity or authenticity data such as 117432b31808SJens Wiklander * MAC values with a function such as \c memcmp is risky 117532b31808SJens Wiklander * because the time taken by the comparison may leak information 117632b31808SJens Wiklander * about the MAC value which could allow an attacker to guess 117732b31808SJens Wiklander * a valid MAC and thereby bypass security controls. 117832b31808SJens Wiklander * 117932b31808SJens Wiklander * \param key Identifier of the key to use for the operation. It 118032b31808SJens Wiklander * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE. 118132b31808SJens Wiklander * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value 118232b31808SJens Wiklander * such that #PSA_ALG_IS_MAC(\p alg) is true). 118332b31808SJens Wiklander * \param[in] input Buffer containing the input message. 118432b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 118532b31808SJens Wiklander * \param[out] mac Buffer where the MAC value is to be written. 118632b31808SJens Wiklander * \param mac_size Size of the \p mac buffer in bytes. 118732b31808SJens Wiklander * \param[out] mac_length On success, the number of bytes 118832b31808SJens Wiklander * that make up the MAC value. 118932b31808SJens Wiklander * 119032b31808SJens Wiklander * \retval #PSA_SUCCESS 119132b31808SJens Wiklander * Success. 119232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 119332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 119432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 119532b31808SJens Wiklander * \p key is not compatible with \p alg. 119632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 119732b31808SJens Wiklander * \p alg is not supported or is not a MAC algorithm. 119832b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 119932b31808SJens Wiklander * \p mac_size is too small 120032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 120132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 120232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 120332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 120432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 120532b31808SJens Wiklander * The key could not be retrieved from storage. 120632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 120732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 120832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 120932b31808SJens Wiklander * results in this error code. 121032b31808SJens Wiklander */ 121132b31808SJens Wiklander psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, 121232b31808SJens Wiklander psa_algorithm_t alg, 121332b31808SJens Wiklander const uint8_t *input, 121432b31808SJens Wiklander size_t input_length, 121532b31808SJens Wiklander uint8_t *mac, 121632b31808SJens Wiklander size_t mac_size, 121732b31808SJens Wiklander size_t *mac_length); 121832b31808SJens Wiklander 121932b31808SJens Wiklander /** Calculate the MAC of a message and compare it with a reference value. 122032b31808SJens Wiklander * 122132b31808SJens Wiklander * \param key Identifier of the key to use for the operation. It 122232b31808SJens Wiklander * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE. 122332b31808SJens Wiklander * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value 122432b31808SJens Wiklander * such that #PSA_ALG_IS_MAC(\p alg) is true). 122532b31808SJens Wiklander * \param[in] input Buffer containing the input message. 122632b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 122732b31808SJens Wiklander * \param[out] mac Buffer containing the expected MAC value. 122832b31808SJens Wiklander * \param mac_length Size of the \p mac buffer in bytes. 122932b31808SJens Wiklander * 123032b31808SJens Wiklander * \retval #PSA_SUCCESS 123132b31808SJens Wiklander * The expected MAC is identical to the actual MAC of the input. 123232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 123332b31808SJens Wiklander * The MAC of the message was calculated successfully, but it 123432b31808SJens Wiklander * differs from the expected value. 123532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 123632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 123732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 123832b31808SJens Wiklander * \p key is not compatible with \p alg. 123932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 124032b31808SJens Wiklander * \p alg is not supported or is not a MAC algorithm. 124132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 124232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 124332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 124432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 124532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 124632b31808SJens Wiklander * The key could not be retrieved from storage. 124732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 124832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 124932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 125032b31808SJens Wiklander * results in this error code. 125132b31808SJens Wiklander */ 125232b31808SJens Wiklander psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, 125332b31808SJens Wiklander psa_algorithm_t alg, 125432b31808SJens Wiklander const uint8_t *input, 125532b31808SJens Wiklander size_t input_length, 125632b31808SJens Wiklander const uint8_t *mac, 125732b31808SJens Wiklander size_t mac_length); 125832b31808SJens Wiklander 125932b31808SJens Wiklander /** The type of the state data structure for multipart MAC operations. 126032b31808SJens Wiklander * 126132b31808SJens Wiklander * Before calling any function on a MAC operation object, the application must 126232b31808SJens Wiklander * initialize it by any of the following means: 126332b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 126432b31808SJens Wiklander * \code 126532b31808SJens Wiklander * psa_mac_operation_t operation; 126632b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 126732b31808SJens Wiklander * \endcode 126832b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 126932b31808SJens Wiklander * \code 127032b31808SJens Wiklander * psa_mac_operation_t operation = {0}; 127132b31808SJens Wiklander * \endcode 127232b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT, 127332b31808SJens Wiklander * for example: 127432b31808SJens Wiklander * \code 127532b31808SJens Wiklander * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; 127632b31808SJens Wiklander * \endcode 127732b31808SJens Wiklander * - Assign the result of the function psa_mac_operation_init() 127832b31808SJens Wiklander * to the structure, for example: 127932b31808SJens Wiklander * \code 128032b31808SJens Wiklander * psa_mac_operation_t operation; 128132b31808SJens Wiklander * operation = psa_mac_operation_init(); 128232b31808SJens Wiklander * \endcode 128332b31808SJens Wiklander * 128432b31808SJens Wiklander * 128532b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 128632b31808SJens Wiklander * make any assumptions about the content of this structure. 128732b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 128832b31808SJens Wiklander typedef struct psa_mac_operation_s psa_mac_operation_t; 128932b31808SJens Wiklander 129032b31808SJens Wiklander /** \def PSA_MAC_OPERATION_INIT 129132b31808SJens Wiklander * 129232b31808SJens Wiklander * This macro returns a suitable initializer for a MAC operation object of type 129332b31808SJens Wiklander * #psa_mac_operation_t. 129432b31808SJens Wiklander */ 129532b31808SJens Wiklander 129632b31808SJens Wiklander /** Return an initial value for a MAC operation object. 129732b31808SJens Wiklander */ 129832b31808SJens Wiklander static psa_mac_operation_t psa_mac_operation_init(void); 129932b31808SJens Wiklander 130032b31808SJens Wiklander /** Set up a multipart MAC calculation operation. 130132b31808SJens Wiklander * 130232b31808SJens Wiklander * This function sets up the calculation of the MAC 130332b31808SJens Wiklander * (message authentication code) of a byte string. 130432b31808SJens Wiklander * To verify the MAC of a message against an 130532b31808SJens Wiklander * expected value, use psa_mac_verify_setup() instead. 130632b31808SJens Wiklander * 130732b31808SJens Wiklander * The sequence of operations to calculate a MAC is as follows: 130832b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 130932b31808SJens Wiklander * listed here. 131032b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 131132b31808SJens Wiklander * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT. 131232b31808SJens Wiklander * -# Call psa_mac_sign_setup() to specify the algorithm and key. 131332b31808SJens Wiklander * -# Call psa_mac_update() zero, one or more times, passing a fragment 131432b31808SJens Wiklander * of the message each time. The MAC that is calculated is the MAC 131532b31808SJens Wiklander * of the concatenation of these messages in order. 131632b31808SJens Wiklander * -# At the end of the message, call psa_mac_sign_finish() to finish 131732b31808SJens Wiklander * calculating the MAC value and retrieve it. 131832b31808SJens Wiklander * 131932b31808SJens Wiklander * If an error occurs at any step after a call to psa_mac_sign_setup(), the 132032b31808SJens Wiklander * operation will need to be reset by a call to psa_mac_abort(). The 132132b31808SJens Wiklander * application may call psa_mac_abort() at any time after the operation 132232b31808SJens Wiklander * has been initialized. 132332b31808SJens Wiklander * 132432b31808SJens Wiklander * After a successful call to psa_mac_sign_setup(), the application must 132532b31808SJens Wiklander * eventually terminate the operation through one of the following methods: 132632b31808SJens Wiklander * - A successful call to psa_mac_sign_finish(). 132732b31808SJens Wiklander * - A call to psa_mac_abort(). 132832b31808SJens Wiklander * 132932b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 133032b31808SJens Wiklander * been initialized as per the documentation for 133132b31808SJens Wiklander * #psa_mac_operation_t and not yet in use. 133232b31808SJens Wiklander * \param key Identifier of the key to use for the operation. It 133332b31808SJens Wiklander * must remain valid until the operation terminates. 133432b31808SJens Wiklander * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE. 133532b31808SJens Wiklander * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value 133632b31808SJens Wiklander * such that #PSA_ALG_IS_MAC(\p alg) is true). 133732b31808SJens Wiklander * 133832b31808SJens Wiklander * \retval #PSA_SUCCESS 133932b31808SJens Wiklander * Success. 134032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 134132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 134232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 134332b31808SJens Wiklander * \p key is not compatible with \p alg. 134432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 134532b31808SJens Wiklander * \p alg is not supported or is not a MAC algorithm. 134632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 134732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 134832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 134932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 135032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 135132b31808SJens Wiklander * The key could not be retrieved from storage. 135232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 135332b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 135432b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 135532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 135632b31808SJens Wiklander * results in this error code. 135732b31808SJens Wiklander */ 135832b31808SJens Wiklander psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, 135932b31808SJens Wiklander mbedtls_svc_key_id_t key, 136032b31808SJens Wiklander psa_algorithm_t alg); 136132b31808SJens Wiklander 136232b31808SJens Wiklander /** Set up a multipart MAC verification operation. 136332b31808SJens Wiklander * 136432b31808SJens Wiklander * This function sets up the verification of the MAC 136532b31808SJens Wiklander * (message authentication code) of a byte string against an expected value. 136632b31808SJens Wiklander * 136732b31808SJens Wiklander * The sequence of operations to verify a MAC is as follows: 136832b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 136932b31808SJens Wiklander * listed here. 137032b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 137132b31808SJens Wiklander * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT. 137232b31808SJens Wiklander * -# Call psa_mac_verify_setup() to specify the algorithm and key. 137332b31808SJens Wiklander * -# Call psa_mac_update() zero, one or more times, passing a fragment 137432b31808SJens Wiklander * of the message each time. The MAC that is calculated is the MAC 137532b31808SJens Wiklander * of the concatenation of these messages in order. 137632b31808SJens Wiklander * -# At the end of the message, call psa_mac_verify_finish() to finish 137732b31808SJens Wiklander * calculating the actual MAC of the message and verify it against 137832b31808SJens Wiklander * the expected value. 137932b31808SJens Wiklander * 138032b31808SJens Wiklander * If an error occurs at any step after a call to psa_mac_verify_setup(), the 138132b31808SJens Wiklander * operation will need to be reset by a call to psa_mac_abort(). The 138232b31808SJens Wiklander * application may call psa_mac_abort() at any time after the operation 138332b31808SJens Wiklander * has been initialized. 138432b31808SJens Wiklander * 138532b31808SJens Wiklander * After a successful call to psa_mac_verify_setup(), the application must 138632b31808SJens Wiklander * eventually terminate the operation through one of the following methods: 138732b31808SJens Wiklander * - A successful call to psa_mac_verify_finish(). 138832b31808SJens Wiklander * - A call to psa_mac_abort(). 138932b31808SJens Wiklander * 139032b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 139132b31808SJens Wiklander * been initialized as per the documentation for 139232b31808SJens Wiklander * #psa_mac_operation_t and not yet in use. 139332b31808SJens Wiklander * \param key Identifier of the key to use for the operation. It 139432b31808SJens Wiklander * must remain valid until the operation terminates. 139532b31808SJens Wiklander * It must allow the usage 139632b31808SJens Wiklander * PSA_KEY_USAGE_VERIFY_MESSAGE. 139732b31808SJens Wiklander * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value 139832b31808SJens Wiklander * such that #PSA_ALG_IS_MAC(\p alg) is true). 139932b31808SJens Wiklander * 140032b31808SJens Wiklander * \retval #PSA_SUCCESS 140132b31808SJens Wiklander * Success. 140232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 140332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 140432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 140532b31808SJens Wiklander * \c key is not compatible with \c alg. 140632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 140732b31808SJens Wiklander * \c alg is not supported or is not a MAC algorithm. 140832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 140932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 141032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 141132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 141232b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 141332b31808SJens Wiklander * The key could not be retrieved from storage. 141432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 141532b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 141632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 141732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 141832b31808SJens Wiklander * results in this error code. 141932b31808SJens Wiklander */ 142032b31808SJens Wiklander psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, 142132b31808SJens Wiklander mbedtls_svc_key_id_t key, 142232b31808SJens Wiklander psa_algorithm_t alg); 142332b31808SJens Wiklander 142432b31808SJens Wiklander /** Add a message fragment to a multipart MAC operation. 142532b31808SJens Wiklander * 142632b31808SJens Wiklander * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() 142732b31808SJens Wiklander * before calling this function. 142832b31808SJens Wiklander * 142932b31808SJens Wiklander * If this function returns an error status, the operation enters an error 143032b31808SJens Wiklander * state and must be aborted by calling psa_mac_abort(). 143132b31808SJens Wiklander * 143232b31808SJens Wiklander * \param[in,out] operation Active MAC operation. 143332b31808SJens Wiklander * \param[in] input Buffer containing the message fragment to add to 143432b31808SJens Wiklander * the MAC calculation. 143532b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 143632b31808SJens Wiklander * 143732b31808SJens Wiklander * \retval #PSA_SUCCESS 143832b31808SJens Wiklander * Success. 143932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 144032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 144132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 144232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 144332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 144432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 144532b31808SJens Wiklander * The operation state is not valid (it must be active), or 144632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 144732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 144832b31808SJens Wiklander * results in this error code. 144932b31808SJens Wiklander */ 145032b31808SJens Wiklander psa_status_t psa_mac_update(psa_mac_operation_t *operation, 145132b31808SJens Wiklander const uint8_t *input, 145232b31808SJens Wiklander size_t input_length); 145332b31808SJens Wiklander 145432b31808SJens Wiklander /** Finish the calculation of the MAC of a message. 145532b31808SJens Wiklander * 145632b31808SJens Wiklander * The application must call psa_mac_sign_setup() before calling this function. 145732b31808SJens Wiklander * This function calculates the MAC of the message formed by concatenating 145832b31808SJens Wiklander * the inputs passed to preceding calls to psa_mac_update(). 145932b31808SJens Wiklander * 146032b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 146132b31808SJens Wiklander * If this function returns an error status, the operation enters an error 146232b31808SJens Wiklander * state and must be aborted by calling psa_mac_abort(). 146332b31808SJens Wiklander * 146432b31808SJens Wiklander * \warning Applications should not call this function if they expect 146532b31808SJens Wiklander * a specific value for the MAC. Call psa_mac_verify_finish() instead. 146632b31808SJens Wiklander * Beware that comparing integrity or authenticity data such as 146732b31808SJens Wiklander * MAC values with a function such as \c memcmp is risky 146832b31808SJens Wiklander * because the time taken by the comparison may leak information 146932b31808SJens Wiklander * about the MAC value which could allow an attacker to guess 147032b31808SJens Wiklander * a valid MAC and thereby bypass security controls. 147132b31808SJens Wiklander * 147232b31808SJens Wiklander * \param[in,out] operation Active MAC operation. 147332b31808SJens Wiklander * \param[out] mac Buffer where the MAC value is to be written. 147432b31808SJens Wiklander * \param mac_size Size of the \p mac buffer in bytes. 147532b31808SJens Wiklander * \param[out] mac_length On success, the number of bytes 147632b31808SJens Wiklander * that make up the MAC value. This is always 147732b31808SJens Wiklander * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg) 147832b31808SJens Wiklander * where \c key_type and \c key_bits are the type and 147932b31808SJens Wiklander * bit-size respectively of the key and \c alg is the 148032b31808SJens Wiklander * MAC algorithm that is calculated. 148132b31808SJens Wiklander * 148232b31808SJens Wiklander * \retval #PSA_SUCCESS 148332b31808SJens Wiklander * Success. 148432b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 148532b31808SJens Wiklander * The size of the \p mac buffer is too small. You can determine a 148632b31808SJens Wiklander * sufficient buffer size by calling PSA_MAC_LENGTH(). 148732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 148832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 148932b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 149032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 149132b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 149232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 149332b31808SJens Wiklander * The operation state is not valid (it must be an active mac sign 149432b31808SJens Wiklander * operation), or the library has not been previously initialized 149532b31808SJens Wiklander * by psa_crypto_init(). 149632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 149732b31808SJens Wiklander * results in this error code. 149832b31808SJens Wiklander */ 149932b31808SJens Wiklander psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, 150032b31808SJens Wiklander uint8_t *mac, 150132b31808SJens Wiklander size_t mac_size, 150232b31808SJens Wiklander size_t *mac_length); 150332b31808SJens Wiklander 150432b31808SJens Wiklander /** Finish the calculation of the MAC of a message and compare it with 150532b31808SJens Wiklander * an expected value. 150632b31808SJens Wiklander * 150732b31808SJens Wiklander * The application must call psa_mac_verify_setup() before calling this function. 150832b31808SJens Wiklander * This function calculates the MAC of the message formed by concatenating 150932b31808SJens Wiklander * the inputs passed to preceding calls to psa_mac_update(). It then 151032b31808SJens Wiklander * compares the calculated MAC with the expected MAC passed as a 151132b31808SJens Wiklander * parameter to this function. 151232b31808SJens Wiklander * 151332b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 151432b31808SJens Wiklander * If this function returns an error status, the operation enters an error 151532b31808SJens Wiklander * state and must be aborted by calling psa_mac_abort(). 151632b31808SJens Wiklander * 151732b31808SJens Wiklander * \note Implementations shall make the best effort to ensure that the 151832b31808SJens Wiklander * comparison between the actual MAC and the expected MAC is performed 151932b31808SJens Wiklander * in constant time. 152032b31808SJens Wiklander * 152132b31808SJens Wiklander * \param[in,out] operation Active MAC operation. 152232b31808SJens Wiklander * \param[in] mac Buffer containing the expected MAC value. 152332b31808SJens Wiklander * \param mac_length Size of the \p mac buffer in bytes. 152432b31808SJens Wiklander * 152532b31808SJens Wiklander * \retval #PSA_SUCCESS 152632b31808SJens Wiklander * The expected MAC is identical to the actual MAC of the message. 152732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 152832b31808SJens Wiklander * The MAC of the message was calculated successfully, but it 152932b31808SJens Wiklander * differs from the expected MAC. 153032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 153132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 153232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 153332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 153432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 153532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 153632b31808SJens Wiklander * The operation state is not valid (it must be an active mac verify 153732b31808SJens Wiklander * operation), or the library has not been previously initialized 153832b31808SJens Wiklander * by psa_crypto_init(). 153932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 154032b31808SJens Wiklander * results in this error code. 154132b31808SJens Wiklander */ 154232b31808SJens Wiklander psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, 154332b31808SJens Wiklander const uint8_t *mac, 154432b31808SJens Wiklander size_t mac_length); 154532b31808SJens Wiklander 154632b31808SJens Wiklander /** Abort a MAC operation. 154732b31808SJens Wiklander * 154832b31808SJens Wiklander * Aborting an operation frees all associated resources except for the 154932b31808SJens Wiklander * \p operation structure itself. Once aborted, the operation object 155032b31808SJens Wiklander * can be reused for another operation by calling 155132b31808SJens Wiklander * psa_mac_sign_setup() or psa_mac_verify_setup() again. 155232b31808SJens Wiklander * 155332b31808SJens Wiklander * You may call this function any time after the operation object has 155432b31808SJens Wiklander * been initialized by one of the methods described in #psa_mac_operation_t. 155532b31808SJens Wiklander * 155632b31808SJens Wiklander * In particular, calling psa_mac_abort() after the operation has been 155732b31808SJens Wiklander * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or 155832b31808SJens Wiklander * psa_mac_verify_finish() is safe and has no effect. 155932b31808SJens Wiklander * 156032b31808SJens Wiklander * \param[in,out] operation Initialized MAC operation. 156132b31808SJens Wiklander * 156232b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 156332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 156432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 156532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 156632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 156732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 156832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 156932b31808SJens Wiklander * results in this error code. 157032b31808SJens Wiklander */ 157132b31808SJens Wiklander psa_status_t psa_mac_abort(psa_mac_operation_t *operation); 157232b31808SJens Wiklander 157332b31808SJens Wiklander /**@}*/ 157432b31808SJens Wiklander 157532b31808SJens Wiklander /** \defgroup cipher Symmetric ciphers 157632b31808SJens Wiklander * @{ 157732b31808SJens Wiklander */ 157832b31808SJens Wiklander 157932b31808SJens Wiklander /** Encrypt a message using a symmetric cipher. 158032b31808SJens Wiklander * 158132b31808SJens Wiklander * This function encrypts a message with a random IV (initialization 158232b31808SJens Wiklander * vector). Use the multipart operation interface with a 158332b31808SJens Wiklander * #psa_cipher_operation_t object to provide other forms of IV. 158432b31808SJens Wiklander * 158532b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 158632b31808SJens Wiklander * It must allow the usage #PSA_KEY_USAGE_ENCRYPT. 158732b31808SJens Wiklander * \param alg The cipher algorithm to compute 158832b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 158932b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 159032b31808SJens Wiklander * \param[in] input Buffer containing the message to encrypt. 159132b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 159232b31808SJens Wiklander * \param[out] output Buffer where the output is to be written. 159332b31808SJens Wiklander * The output contains the IV followed by 159432b31808SJens Wiklander * the ciphertext proper. 159532b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 159632b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 159732b31808SJens Wiklander * that make up the output. 159832b31808SJens Wiklander * 159932b31808SJens Wiklander * \retval #PSA_SUCCESS 160032b31808SJens Wiklander * Success. 160132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 160232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 160332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 160432b31808SJens Wiklander * \p key is not compatible with \p alg. 160532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 160632b31808SJens Wiklander * \p alg is not supported or is not a cipher algorithm. 160732b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription 160832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 160932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 161032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 161132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 161232b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 161332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 161432b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 161532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 161632b31808SJens Wiklander * results in this error code. 161732b31808SJens Wiklander */ 161832b31808SJens Wiklander psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, 161932b31808SJens Wiklander psa_algorithm_t alg, 162032b31808SJens Wiklander const uint8_t *input, 162132b31808SJens Wiklander size_t input_length, 162232b31808SJens Wiklander uint8_t *output, 162332b31808SJens Wiklander size_t output_size, 162432b31808SJens Wiklander size_t *output_length); 162532b31808SJens Wiklander 162632b31808SJens Wiklander /** Decrypt a message using a symmetric cipher. 162732b31808SJens Wiklander * 162832b31808SJens Wiklander * This function decrypts a message encrypted with a symmetric cipher. 162932b31808SJens Wiklander * 163032b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 163132b31808SJens Wiklander * It must remain valid until the operation 163232b31808SJens Wiklander * terminates. It must allow the usage 163332b31808SJens Wiklander * #PSA_KEY_USAGE_DECRYPT. 163432b31808SJens Wiklander * \param alg The cipher algorithm to compute 163532b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 163632b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 163732b31808SJens Wiklander * \param[in] input Buffer containing the message to decrypt. 163832b31808SJens Wiklander * This consists of the IV followed by the 163932b31808SJens Wiklander * ciphertext proper. 164032b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 164132b31808SJens Wiklander * \param[out] output Buffer where the plaintext is to be written. 164232b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 164332b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 164432b31808SJens Wiklander * that make up the output. 164532b31808SJens Wiklander * 164632b31808SJens Wiklander * \retval #PSA_SUCCESS 164732b31808SJens Wiklander * Success. 164832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 164932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 165032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 165132b31808SJens Wiklander * \p key is not compatible with \p alg. 165232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 165332b31808SJens Wiklander * \p alg is not supported or is not a cipher algorithm. 165432b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription 165532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 165632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 165732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 165832b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 165932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 166032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 166132b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 166232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 166332b31808SJens Wiklander * results in this error code. 166432b31808SJens Wiklander */ 166532b31808SJens Wiklander psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, 166632b31808SJens Wiklander psa_algorithm_t alg, 166732b31808SJens Wiklander const uint8_t *input, 166832b31808SJens Wiklander size_t input_length, 166932b31808SJens Wiklander uint8_t *output, 167032b31808SJens Wiklander size_t output_size, 167132b31808SJens Wiklander size_t *output_length); 167232b31808SJens Wiklander 167332b31808SJens Wiklander /** The type of the state data structure for multipart cipher operations. 167432b31808SJens Wiklander * 167532b31808SJens Wiklander * Before calling any function on a cipher operation object, the application 167632b31808SJens Wiklander * must initialize it by any of the following means: 167732b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 167832b31808SJens Wiklander * \code 167932b31808SJens Wiklander * psa_cipher_operation_t operation; 168032b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 168132b31808SJens Wiklander * \endcode 168232b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 168332b31808SJens Wiklander * \code 168432b31808SJens Wiklander * psa_cipher_operation_t operation = {0}; 168532b31808SJens Wiklander * \endcode 168632b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT, 168732b31808SJens Wiklander * for example: 168832b31808SJens Wiklander * \code 168932b31808SJens Wiklander * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 169032b31808SJens Wiklander * \endcode 169132b31808SJens Wiklander * - Assign the result of the function psa_cipher_operation_init() 169232b31808SJens Wiklander * to the structure, for example: 169332b31808SJens Wiklander * \code 169432b31808SJens Wiklander * psa_cipher_operation_t operation; 169532b31808SJens Wiklander * operation = psa_cipher_operation_init(); 169632b31808SJens Wiklander * \endcode 169732b31808SJens Wiklander * 169832b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 169932b31808SJens Wiklander * make any assumptions about the content of this structure. 170032b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 170132b31808SJens Wiklander typedef struct psa_cipher_operation_s psa_cipher_operation_t; 170232b31808SJens Wiklander 170332b31808SJens Wiklander /** \def PSA_CIPHER_OPERATION_INIT 170432b31808SJens Wiklander * 170532b31808SJens Wiklander * This macro returns a suitable initializer for a cipher operation object of 170632b31808SJens Wiklander * type #psa_cipher_operation_t. 170732b31808SJens Wiklander */ 170832b31808SJens Wiklander 170932b31808SJens Wiklander /** Return an initial value for a cipher operation object. 171032b31808SJens Wiklander */ 171132b31808SJens Wiklander static psa_cipher_operation_t psa_cipher_operation_init(void); 171232b31808SJens Wiklander 171332b31808SJens Wiklander /** Set the key for a multipart symmetric encryption operation. 171432b31808SJens Wiklander * 171532b31808SJens Wiklander * The sequence of operations to encrypt a message with a symmetric cipher 171632b31808SJens Wiklander * is as follows: 171732b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 171832b31808SJens Wiklander * listed here. 171932b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 172032b31808SJens Wiklander * documentation for #psa_cipher_operation_t, e.g. 172132b31808SJens Wiklander * #PSA_CIPHER_OPERATION_INIT. 172232b31808SJens Wiklander * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. 172332b31808SJens Wiklander * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to 172432b31808SJens Wiklander * generate or set the IV (initialization vector). You should use 172532b31808SJens Wiklander * psa_cipher_generate_iv() unless the protocol you are implementing 172632b31808SJens Wiklander * requires a specific IV value. 172732b31808SJens Wiklander * -# Call psa_cipher_update() zero, one or more times, passing a fragment 172832b31808SJens Wiklander * of the message each time. 172932b31808SJens Wiklander * -# Call psa_cipher_finish(). 173032b31808SJens Wiklander * 173132b31808SJens Wiklander * If an error occurs at any step after a call to psa_cipher_encrypt_setup(), 173232b31808SJens Wiklander * the operation will need to be reset by a call to psa_cipher_abort(). The 173332b31808SJens Wiklander * application may call psa_cipher_abort() at any time after the operation 173432b31808SJens Wiklander * has been initialized. 173532b31808SJens Wiklander * 173632b31808SJens Wiklander * After a successful call to psa_cipher_encrypt_setup(), the application must 173732b31808SJens Wiklander * eventually terminate the operation. The following events terminate an 173832b31808SJens Wiklander * operation: 173932b31808SJens Wiklander * - A successful call to psa_cipher_finish(). 174032b31808SJens Wiklander * - A call to psa_cipher_abort(). 174132b31808SJens Wiklander * 174232b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 174332b31808SJens Wiklander * been initialized as per the documentation for 174432b31808SJens Wiklander * #psa_cipher_operation_t and not yet in use. 174532b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 174632b31808SJens Wiklander * It must remain valid until the operation 174732b31808SJens Wiklander * terminates. It must allow the usage 174832b31808SJens Wiklander * #PSA_KEY_USAGE_ENCRYPT. 174932b31808SJens Wiklander * \param alg The cipher algorithm to compute 175032b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 175132b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 175232b31808SJens Wiklander * 175332b31808SJens Wiklander * \retval #PSA_SUCCESS 175432b31808SJens Wiklander * Success. 175532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 175632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 175732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 175832b31808SJens Wiklander * \p key is not compatible with \p alg. 175932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 176032b31808SJens Wiklander * \p alg is not supported or is not a cipher algorithm. 176132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 176232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 176332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 176432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 176532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 176632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 176732b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 176832b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 176932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 177032b31808SJens Wiklander * results in this error code. 177132b31808SJens Wiklander */ 177232b31808SJens Wiklander psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, 177332b31808SJens Wiklander mbedtls_svc_key_id_t key, 177432b31808SJens Wiklander psa_algorithm_t alg); 177532b31808SJens Wiklander 177632b31808SJens Wiklander /** Set the key for a multipart symmetric decryption operation. 177732b31808SJens Wiklander * 177832b31808SJens Wiklander * The sequence of operations to decrypt a message with a symmetric cipher 177932b31808SJens Wiklander * is as follows: 178032b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 178132b31808SJens Wiklander * listed here. 178232b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 178332b31808SJens Wiklander * documentation for #psa_cipher_operation_t, e.g. 178432b31808SJens Wiklander * #PSA_CIPHER_OPERATION_INIT. 178532b31808SJens Wiklander * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. 178632b31808SJens Wiklander * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the 178732b31808SJens Wiklander * decryption. If the IV is prepended to the ciphertext, you can call 178832b31808SJens Wiklander * psa_cipher_update() on a buffer containing the IV followed by the 178932b31808SJens Wiklander * beginning of the message. 179032b31808SJens Wiklander * -# Call psa_cipher_update() zero, one or more times, passing a fragment 179132b31808SJens Wiklander * of the message each time. 179232b31808SJens Wiklander * -# Call psa_cipher_finish(). 179332b31808SJens Wiklander * 179432b31808SJens Wiklander * If an error occurs at any step after a call to psa_cipher_decrypt_setup(), 179532b31808SJens Wiklander * the operation will need to be reset by a call to psa_cipher_abort(). The 179632b31808SJens Wiklander * application may call psa_cipher_abort() at any time after the operation 179732b31808SJens Wiklander * has been initialized. 179832b31808SJens Wiklander * 179932b31808SJens Wiklander * After a successful call to psa_cipher_decrypt_setup(), the application must 180032b31808SJens Wiklander * eventually terminate the operation. The following events terminate an 180132b31808SJens Wiklander * operation: 180232b31808SJens Wiklander * - A successful call to psa_cipher_finish(). 180332b31808SJens Wiklander * - A call to psa_cipher_abort(). 180432b31808SJens Wiklander * 180532b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 180632b31808SJens Wiklander * been initialized as per the documentation for 180732b31808SJens Wiklander * #psa_cipher_operation_t and not yet in use. 180832b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 180932b31808SJens Wiklander * It must remain valid until the operation 181032b31808SJens Wiklander * terminates. It must allow the usage 181132b31808SJens Wiklander * #PSA_KEY_USAGE_DECRYPT. 181232b31808SJens Wiklander * \param alg The cipher algorithm to compute 181332b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 181432b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 181532b31808SJens Wiklander * 181632b31808SJens Wiklander * \retval #PSA_SUCCESS 181732b31808SJens Wiklander * Success. 181832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 181932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 182032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 182132b31808SJens Wiklander * \p key is not compatible with \p alg. 182232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 182332b31808SJens Wiklander * \p alg is not supported or is not a cipher algorithm. 182432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 182532b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 182632b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 182732b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 182832b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 182932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 183032b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 183132b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 183232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 183332b31808SJens Wiklander * results in this error code. 183432b31808SJens Wiklander */ 183532b31808SJens Wiklander psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, 183632b31808SJens Wiklander mbedtls_svc_key_id_t key, 183732b31808SJens Wiklander psa_algorithm_t alg); 183832b31808SJens Wiklander 183932b31808SJens Wiklander /** Generate an IV for a symmetric encryption operation. 184032b31808SJens Wiklander * 184132b31808SJens Wiklander * This function generates a random IV (initialization vector), nonce 184232b31808SJens Wiklander * or initial counter value for the encryption operation as appropriate 184332b31808SJens Wiklander * for the chosen algorithm, key type and key size. 184432b31808SJens Wiklander * 184532b31808SJens Wiklander * The application must call psa_cipher_encrypt_setup() before 184632b31808SJens Wiklander * calling this function. 184732b31808SJens Wiklander * 184832b31808SJens Wiklander * If this function returns an error status, the operation enters an error 184932b31808SJens Wiklander * state and must be aborted by calling psa_cipher_abort(). 185032b31808SJens Wiklander * 185132b31808SJens Wiklander * \param[in,out] operation Active cipher operation. 185232b31808SJens Wiklander * \param[out] iv Buffer where the generated IV is to be written. 185332b31808SJens Wiklander * \param iv_size Size of the \p iv buffer in bytes. 185432b31808SJens Wiklander * \param[out] iv_length On success, the number of bytes of the 185532b31808SJens Wiklander * generated IV. 185632b31808SJens Wiklander * 185732b31808SJens Wiklander * \retval #PSA_SUCCESS 185832b31808SJens Wiklander * Success. 185932b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 186032b31808SJens Wiklander * The size of the \p iv buffer is too small. 186132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 186232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 186332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 186432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 186532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 186632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 186732b31808SJens Wiklander * The operation state is not valid (it must be active, with no IV set), 186832b31808SJens Wiklander * or the library has not been previously initialized 186932b31808SJens Wiklander * by psa_crypto_init(). 187032b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 187132b31808SJens Wiklander * results in this error code. 187232b31808SJens Wiklander */ 187332b31808SJens Wiklander psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, 187432b31808SJens Wiklander uint8_t *iv, 187532b31808SJens Wiklander size_t iv_size, 187632b31808SJens Wiklander size_t *iv_length); 187732b31808SJens Wiklander 187832b31808SJens Wiklander /** Set the IV for a symmetric encryption or decryption operation. 187932b31808SJens Wiklander * 188032b31808SJens Wiklander * This function sets the IV (initialization vector), nonce 188132b31808SJens Wiklander * or initial counter value for the encryption or decryption operation. 188232b31808SJens Wiklander * 188332b31808SJens Wiklander * The application must call psa_cipher_encrypt_setup() before 188432b31808SJens Wiklander * calling this function. 188532b31808SJens Wiklander * 188632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 188732b31808SJens Wiklander * state and must be aborted by calling psa_cipher_abort(). 188832b31808SJens Wiklander * 188932b31808SJens Wiklander * \note When encrypting, applications should use psa_cipher_generate_iv() 189032b31808SJens Wiklander * instead of this function, unless implementing a protocol that requires 189132b31808SJens Wiklander * a non-random IV. 189232b31808SJens Wiklander * 189332b31808SJens Wiklander * \param[in,out] operation Active cipher operation. 189432b31808SJens Wiklander * \param[in] iv Buffer containing the IV to use. 189532b31808SJens Wiklander * \param iv_length Size of the IV in bytes. 189632b31808SJens Wiklander * 189732b31808SJens Wiklander * \retval #PSA_SUCCESS 189832b31808SJens Wiklander * Success. 189932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 190032b31808SJens Wiklander * The size of \p iv is not acceptable for the chosen algorithm, 190132b31808SJens Wiklander * or the chosen algorithm does not use an IV. 190232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 190332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 190432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 190532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 190632b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 190732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 190832b31808SJens Wiklander * The operation state is not valid (it must be an active cipher 190932b31808SJens Wiklander * encrypt operation, with no IV set), or the library has not been 191032b31808SJens Wiklander * previously initialized by psa_crypto_init(). 191132b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 191232b31808SJens Wiklander * results in this error code. 191332b31808SJens Wiklander */ 191432b31808SJens Wiklander psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, 191532b31808SJens Wiklander const uint8_t *iv, 191632b31808SJens Wiklander size_t iv_length); 191732b31808SJens Wiklander 191832b31808SJens Wiklander /** Encrypt or decrypt a message fragment in an active cipher operation. 191932b31808SJens Wiklander * 192032b31808SJens Wiklander * Before calling this function, you must: 192132b31808SJens Wiklander * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). 192232b31808SJens Wiklander * The choice of setup function determines whether this function 192332b31808SJens Wiklander * encrypts or decrypts its input. 192432b31808SJens Wiklander * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() 192532b31808SJens Wiklander * (recommended when encrypting) or psa_cipher_set_iv(). 192632b31808SJens Wiklander * 192732b31808SJens Wiklander * If this function returns an error status, the operation enters an error 192832b31808SJens Wiklander * state and must be aborted by calling psa_cipher_abort(). 192932b31808SJens Wiklander * 193032b31808SJens Wiklander * \param[in,out] operation Active cipher operation. 193132b31808SJens Wiklander * \param[in] input Buffer containing the message fragment to 193232b31808SJens Wiklander * encrypt or decrypt. 193332b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 193432b31808SJens Wiklander * \param[out] output Buffer where the output is to be written. 193532b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 193632b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 193732b31808SJens Wiklander * that make up the returned output. 193832b31808SJens Wiklander * 193932b31808SJens Wiklander * \retval #PSA_SUCCESS 194032b31808SJens Wiklander * Success. 194132b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 194232b31808SJens Wiklander * The size of the \p output buffer is too small. 194332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 194432b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 194532b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 194632b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 194732b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 194832b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 194932b31808SJens Wiklander * The operation state is not valid (it must be active, with an IV set 195032b31808SJens Wiklander * if required for the algorithm), or the library has not been 195132b31808SJens Wiklander * previously initialized by psa_crypto_init(). 195232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 195332b31808SJens Wiklander * results in this error code. 195432b31808SJens Wiklander */ 195532b31808SJens Wiklander psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, 195632b31808SJens Wiklander const uint8_t *input, 195732b31808SJens Wiklander size_t input_length, 195832b31808SJens Wiklander uint8_t *output, 195932b31808SJens Wiklander size_t output_size, 196032b31808SJens Wiklander size_t *output_length); 196132b31808SJens Wiklander 196232b31808SJens Wiklander /** Finish encrypting or decrypting a message in a cipher operation. 196332b31808SJens Wiklander * 196432b31808SJens Wiklander * The application must call psa_cipher_encrypt_setup() or 196532b31808SJens Wiklander * psa_cipher_decrypt_setup() before calling this function. The choice 196632b31808SJens Wiklander * of setup function determines whether this function encrypts or 196732b31808SJens Wiklander * decrypts its input. 196832b31808SJens Wiklander * 196932b31808SJens Wiklander * This function finishes the encryption or decryption of the message 197032b31808SJens Wiklander * formed by concatenating the inputs passed to preceding calls to 197132b31808SJens Wiklander * psa_cipher_update(). 197232b31808SJens Wiklander * 197332b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 197432b31808SJens Wiklander * If this function returns an error status, the operation enters an error 197532b31808SJens Wiklander * state and must be aborted by calling psa_cipher_abort(). 197632b31808SJens Wiklander * 197732b31808SJens Wiklander * \param[in,out] operation Active cipher operation. 197832b31808SJens Wiklander * \param[out] output Buffer where the output is to be written. 197932b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 198032b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 198132b31808SJens Wiklander * that make up the returned output. 198232b31808SJens Wiklander * 198332b31808SJens Wiklander * \retval #PSA_SUCCESS 198432b31808SJens Wiklander * Success. 198532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 198632b31808SJens Wiklander * The total input size passed to this operation is not valid for 198732b31808SJens Wiklander * this particular algorithm. For example, the algorithm is a based 198832b31808SJens Wiklander * on block cipher and requires a whole number of blocks, but the 198932b31808SJens Wiklander * total input size is not a multiple of the block size. 199032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_PADDING 199132b31808SJens Wiklander * This is a decryption operation for an algorithm that includes 199232b31808SJens Wiklander * padding, and the ciphertext does not contain valid padding. 199332b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 199432b31808SJens Wiklander * The size of the \p output buffer is too small. 199532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 199632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 199732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 199832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 199932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 200032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 200132b31808SJens Wiklander * The operation state is not valid (it must be active, with an IV set 200232b31808SJens Wiklander * if required for the algorithm), or the library has not been 200332b31808SJens Wiklander * previously initialized by psa_crypto_init(). 200432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 200532b31808SJens Wiklander * results in this error code. 200632b31808SJens Wiklander */ 200732b31808SJens Wiklander psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, 200832b31808SJens Wiklander uint8_t *output, 200932b31808SJens Wiklander size_t output_size, 201032b31808SJens Wiklander size_t *output_length); 201132b31808SJens Wiklander 201232b31808SJens Wiklander /** Abort a cipher operation. 201332b31808SJens Wiklander * 201432b31808SJens Wiklander * Aborting an operation frees all associated resources except for the 201532b31808SJens Wiklander * \p operation structure itself. Once aborted, the operation object 201632b31808SJens Wiklander * can be reused for another operation by calling 201732b31808SJens Wiklander * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. 201832b31808SJens Wiklander * 201932b31808SJens Wiklander * You may call this function any time after the operation object has 202032b31808SJens Wiklander * been initialized as described in #psa_cipher_operation_t. 202132b31808SJens Wiklander * 202232b31808SJens Wiklander * In particular, calling psa_cipher_abort() after the operation has been 202332b31808SJens Wiklander * terminated by a call to psa_cipher_abort() or psa_cipher_finish() 202432b31808SJens Wiklander * is safe and has no effect. 202532b31808SJens Wiklander * 202632b31808SJens Wiklander * \param[in,out] operation Initialized cipher operation. 202732b31808SJens Wiklander * 202832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 202932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 203032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 203132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 203232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 203332b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 203432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 203532b31808SJens Wiklander * results in this error code. 203632b31808SJens Wiklander */ 203732b31808SJens Wiklander psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); 203832b31808SJens Wiklander 203932b31808SJens Wiklander /**@}*/ 204032b31808SJens Wiklander 204132b31808SJens Wiklander /** \defgroup aead Authenticated encryption with associated data (AEAD) 204232b31808SJens Wiklander * @{ 204332b31808SJens Wiklander */ 204432b31808SJens Wiklander 204532b31808SJens Wiklander /** Process an authenticated encryption operation. 204632b31808SJens Wiklander * 204732b31808SJens Wiklander * \param key Identifier of the key to use for the 204832b31808SJens Wiklander * operation. It must allow the usage 204932b31808SJens Wiklander * #PSA_KEY_USAGE_ENCRYPT. 205032b31808SJens Wiklander * \param alg The AEAD algorithm to compute 205132b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 205232b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 205332b31808SJens Wiklander * \param[in] nonce Nonce or IV to use. 205432b31808SJens Wiklander * \param nonce_length Size of the \p nonce buffer in bytes. 205532b31808SJens Wiklander * \param[in] additional_data Additional data that will be authenticated 205632b31808SJens Wiklander * but not encrypted. 205732b31808SJens Wiklander * \param additional_data_length Size of \p additional_data in bytes. 205832b31808SJens Wiklander * \param[in] plaintext Data that will be authenticated and 205932b31808SJens Wiklander * encrypted. 206032b31808SJens Wiklander * \param plaintext_length Size of \p plaintext in bytes. 206132b31808SJens Wiklander * \param[out] ciphertext Output buffer for the authenticated and 206232b31808SJens Wiklander * encrypted data. The additional data is not 206332b31808SJens Wiklander * part of this output. For algorithms where the 206432b31808SJens Wiklander * encrypted data and the authentication tag 206532b31808SJens Wiklander * are defined as separate outputs, the 206632b31808SJens Wiklander * authentication tag is appended to the 206732b31808SJens Wiklander * encrypted data. 206832b31808SJens Wiklander * \param ciphertext_size Size of the \p ciphertext buffer in bytes. 206932b31808SJens Wiklander * This must be appropriate for the selected 207032b31808SJens Wiklander * algorithm and key: 207132b31808SJens Wiklander * - A sufficient output size is 207232b31808SJens Wiklander * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, 207332b31808SJens Wiklander * \p alg, \p plaintext_length) where 207432b31808SJens Wiklander * \c key_type is the type of \p key. 207532b31808SJens Wiklander * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p 207632b31808SJens Wiklander * plaintext_length) evaluates to the maximum 207732b31808SJens Wiklander * ciphertext size of any supported AEAD 207832b31808SJens Wiklander * encryption. 207932b31808SJens Wiklander * \param[out] ciphertext_length On success, the size of the output 208032b31808SJens Wiklander * in the \p ciphertext buffer. 208132b31808SJens Wiklander * 208232b31808SJens Wiklander * \retval #PSA_SUCCESS 208332b31808SJens Wiklander * Success. 208432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 208532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 208632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 208732b31808SJens Wiklander * \p key is not compatible with \p alg. 208832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 208932b31808SJens Wiklander * \p alg is not supported or is not an AEAD algorithm. 209032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 209132b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 209232b31808SJens Wiklander * \p ciphertext_size is too small. 209332b31808SJens Wiklander * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg, 209432b31808SJens Wiklander * \p plaintext_length) or 209532b31808SJens Wiklander * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to 209632b31808SJens Wiklander * determine the required buffer size. 209732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 209832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 209932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 210032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 210132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 210232b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 210332b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 210432b31808SJens Wiklander * results in this error code. 210532b31808SJens Wiklander */ 210632b31808SJens Wiklander psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, 210732b31808SJens Wiklander psa_algorithm_t alg, 210832b31808SJens Wiklander const uint8_t *nonce, 210932b31808SJens Wiklander size_t nonce_length, 211032b31808SJens Wiklander const uint8_t *additional_data, 211132b31808SJens Wiklander size_t additional_data_length, 211232b31808SJens Wiklander const uint8_t *plaintext, 211332b31808SJens Wiklander size_t plaintext_length, 211432b31808SJens Wiklander uint8_t *ciphertext, 211532b31808SJens Wiklander size_t ciphertext_size, 211632b31808SJens Wiklander size_t *ciphertext_length); 211732b31808SJens Wiklander 211832b31808SJens Wiklander /** Process an authenticated decryption operation. 211932b31808SJens Wiklander * 212032b31808SJens Wiklander * \param key Identifier of the key to use for the 212132b31808SJens Wiklander * operation. It must allow the usage 212232b31808SJens Wiklander * #PSA_KEY_USAGE_DECRYPT. 212332b31808SJens Wiklander * \param alg The AEAD algorithm to compute 212432b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 212532b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 212632b31808SJens Wiklander * \param[in] nonce Nonce or IV to use. 212732b31808SJens Wiklander * \param nonce_length Size of the \p nonce buffer in bytes. 212832b31808SJens Wiklander * \param[in] additional_data Additional data that has been authenticated 212932b31808SJens Wiklander * but not encrypted. 213032b31808SJens Wiklander * \param additional_data_length Size of \p additional_data in bytes. 213132b31808SJens Wiklander * \param[in] ciphertext Data that has been authenticated and 213232b31808SJens Wiklander * encrypted. For algorithms where the 213332b31808SJens Wiklander * encrypted data and the authentication tag 213432b31808SJens Wiklander * are defined as separate inputs, the buffer 213532b31808SJens Wiklander * must contain the encrypted data followed 213632b31808SJens Wiklander * by the authentication tag. 213732b31808SJens Wiklander * \param ciphertext_length Size of \p ciphertext in bytes. 213832b31808SJens Wiklander * \param[out] plaintext Output buffer for the decrypted data. 213932b31808SJens Wiklander * \param plaintext_size Size of the \p plaintext buffer in bytes. 214032b31808SJens Wiklander * This must be appropriate for the selected 214132b31808SJens Wiklander * algorithm and key: 214232b31808SJens Wiklander * - A sufficient output size is 214332b31808SJens Wiklander * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, 214432b31808SJens Wiklander * \p alg, \p ciphertext_length) where 214532b31808SJens Wiklander * \c key_type is the type of \p key. 214632b31808SJens Wiklander * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p 214732b31808SJens Wiklander * ciphertext_length) evaluates to the maximum 214832b31808SJens Wiklander * plaintext size of any supported AEAD 214932b31808SJens Wiklander * decryption. 215032b31808SJens Wiklander * \param[out] plaintext_length On success, the size of the output 215132b31808SJens Wiklander * in the \p plaintext buffer. 215232b31808SJens Wiklander * 215332b31808SJens Wiklander * \retval #PSA_SUCCESS 215432b31808SJens Wiklander * Success. 215532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 215632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 215732b31808SJens Wiklander * The ciphertext is not authentic. 215832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 215932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 216032b31808SJens Wiklander * \p key is not compatible with \p alg. 216132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 216232b31808SJens Wiklander * \p alg is not supported or is not an AEAD algorithm. 216332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 216432b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 216532b31808SJens Wiklander * \p plaintext_size is too small. 216632b31808SJens Wiklander * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg, 216732b31808SJens Wiklander * \p ciphertext_length) or 216832b31808SJens Wiklander * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used 216932b31808SJens Wiklander * to determine the required buffer size. 217032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 217132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 217232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 217332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 217432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 217532b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 217632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 217732b31808SJens Wiklander * results in this error code. 217832b31808SJens Wiklander */ 217932b31808SJens Wiklander psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, 218032b31808SJens Wiklander psa_algorithm_t alg, 218132b31808SJens Wiklander const uint8_t *nonce, 218232b31808SJens Wiklander size_t nonce_length, 218332b31808SJens Wiklander const uint8_t *additional_data, 218432b31808SJens Wiklander size_t additional_data_length, 218532b31808SJens Wiklander const uint8_t *ciphertext, 218632b31808SJens Wiklander size_t ciphertext_length, 218732b31808SJens Wiklander uint8_t *plaintext, 218832b31808SJens Wiklander size_t plaintext_size, 218932b31808SJens Wiklander size_t *plaintext_length); 219032b31808SJens Wiklander 219132b31808SJens Wiklander /** The type of the state data structure for multipart AEAD operations. 219232b31808SJens Wiklander * 219332b31808SJens Wiklander * Before calling any function on an AEAD operation object, the application 219432b31808SJens Wiklander * must initialize it by any of the following means: 219532b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 219632b31808SJens Wiklander * \code 219732b31808SJens Wiklander * psa_aead_operation_t operation; 219832b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 219932b31808SJens Wiklander * \endcode 220032b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 220132b31808SJens Wiklander * \code 220232b31808SJens Wiklander * psa_aead_operation_t operation = {0}; 220332b31808SJens Wiklander * \endcode 220432b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT, 220532b31808SJens Wiklander * for example: 220632b31808SJens Wiklander * \code 220732b31808SJens Wiklander * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; 220832b31808SJens Wiklander * \endcode 220932b31808SJens Wiklander * - Assign the result of the function psa_aead_operation_init() 221032b31808SJens Wiklander * to the structure, for example: 221132b31808SJens Wiklander * \code 221232b31808SJens Wiklander * psa_aead_operation_t operation; 221332b31808SJens Wiklander * operation = psa_aead_operation_init(); 221432b31808SJens Wiklander * \endcode 221532b31808SJens Wiklander * 221632b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 221732b31808SJens Wiklander * make any assumptions about the content of this structure. 221832b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 221932b31808SJens Wiklander typedef struct psa_aead_operation_s psa_aead_operation_t; 222032b31808SJens Wiklander 222132b31808SJens Wiklander /** \def PSA_AEAD_OPERATION_INIT 222232b31808SJens Wiklander * 222332b31808SJens Wiklander * This macro returns a suitable initializer for an AEAD operation object of 222432b31808SJens Wiklander * type #psa_aead_operation_t. 222532b31808SJens Wiklander */ 222632b31808SJens Wiklander 222732b31808SJens Wiklander /** Return an initial value for an AEAD operation object. 222832b31808SJens Wiklander */ 222932b31808SJens Wiklander static psa_aead_operation_t psa_aead_operation_init(void); 223032b31808SJens Wiklander 223132b31808SJens Wiklander /** Set the key for a multipart authenticated encryption operation. 223232b31808SJens Wiklander * 223332b31808SJens Wiklander * The sequence of operations to encrypt a message with authentication 223432b31808SJens Wiklander * is as follows: 223532b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 223632b31808SJens Wiklander * listed here. 223732b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 223832b31808SJens Wiklander * documentation for #psa_aead_operation_t, e.g. 223932b31808SJens Wiklander * #PSA_AEAD_OPERATION_INIT. 224032b31808SJens Wiklander * -# Call psa_aead_encrypt_setup() to specify the algorithm and key. 224132b31808SJens Wiklander * -# If needed, call psa_aead_set_lengths() to specify the length of the 224232b31808SJens Wiklander * inputs to the subsequent calls to psa_aead_update_ad() and 224332b31808SJens Wiklander * psa_aead_update(). See the documentation of psa_aead_set_lengths() 224432b31808SJens Wiklander * for details. 224532b31808SJens Wiklander * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to 224632b31808SJens Wiklander * generate or set the nonce. You should use 224732b31808SJens Wiklander * psa_aead_generate_nonce() unless the protocol you are implementing 224832b31808SJens Wiklander * requires a specific nonce value. 224932b31808SJens Wiklander * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment 225032b31808SJens Wiklander * of the non-encrypted additional authenticated data each time. 225132b31808SJens Wiklander * -# Call psa_aead_update() zero, one or more times, passing a fragment 225232b31808SJens Wiklander * of the message to encrypt each time. 225332b31808SJens Wiklander * -# Call psa_aead_finish(). 225432b31808SJens Wiklander * 225532b31808SJens Wiklander * If an error occurs at any step after a call to psa_aead_encrypt_setup(), 225632b31808SJens Wiklander * the operation will need to be reset by a call to psa_aead_abort(). The 225732b31808SJens Wiklander * application may call psa_aead_abort() at any time after the operation 225832b31808SJens Wiklander * has been initialized. 225932b31808SJens Wiklander * 226032b31808SJens Wiklander * After a successful call to psa_aead_encrypt_setup(), the application must 226132b31808SJens Wiklander * eventually terminate the operation. The following events terminate an 226232b31808SJens Wiklander * operation: 226332b31808SJens Wiklander * - A successful call to psa_aead_finish(). 226432b31808SJens Wiklander * - A call to psa_aead_abort(). 226532b31808SJens Wiklander * 226632b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 226732b31808SJens Wiklander * been initialized as per the documentation for 226832b31808SJens Wiklander * #psa_aead_operation_t and not yet in use. 226932b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 227032b31808SJens Wiklander * It must remain valid until the operation 227132b31808SJens Wiklander * terminates. It must allow the usage 227232b31808SJens Wiklander * #PSA_KEY_USAGE_ENCRYPT. 227332b31808SJens Wiklander * \param alg The AEAD algorithm to compute 227432b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 227532b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 227632b31808SJens Wiklander * 227732b31808SJens Wiklander * \retval #PSA_SUCCESS 227832b31808SJens Wiklander * Success. 227932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 228032b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 228132b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 228232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 228332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 228432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 228532b31808SJens Wiklander * \p key is not compatible with \p alg. 228632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 228732b31808SJens Wiklander * \p alg is not supported or is not an AEAD algorithm. 228832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 228932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 229032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 229132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 229232b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE 229332b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 229432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 229532b31808SJens Wiklander * results in this error code. 229632b31808SJens Wiklander */ 229732b31808SJens Wiklander psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, 229832b31808SJens Wiklander mbedtls_svc_key_id_t key, 229932b31808SJens Wiklander psa_algorithm_t alg); 230032b31808SJens Wiklander 230132b31808SJens Wiklander /** Set the key for a multipart authenticated decryption operation. 230232b31808SJens Wiklander * 230332b31808SJens Wiklander * The sequence of operations to decrypt a message with authentication 230432b31808SJens Wiklander * is as follows: 230532b31808SJens Wiklander * -# Allocate an operation object which will be passed to all the functions 230632b31808SJens Wiklander * listed here. 230732b31808SJens Wiklander * -# Initialize the operation object with one of the methods described in the 230832b31808SJens Wiklander * documentation for #psa_aead_operation_t, e.g. 230932b31808SJens Wiklander * #PSA_AEAD_OPERATION_INIT. 231032b31808SJens Wiklander * -# Call psa_aead_decrypt_setup() to specify the algorithm and key. 231132b31808SJens Wiklander * -# If needed, call psa_aead_set_lengths() to specify the length of the 231232b31808SJens Wiklander * inputs to the subsequent calls to psa_aead_update_ad() and 231332b31808SJens Wiklander * psa_aead_update(). See the documentation of psa_aead_set_lengths() 231432b31808SJens Wiklander * for details. 231532b31808SJens Wiklander * -# Call psa_aead_set_nonce() with the nonce for the decryption. 231632b31808SJens Wiklander * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment 231732b31808SJens Wiklander * of the non-encrypted additional authenticated data each time. 231832b31808SJens Wiklander * -# Call psa_aead_update() zero, one or more times, passing a fragment 231932b31808SJens Wiklander * of the ciphertext to decrypt each time. 232032b31808SJens Wiklander * -# Call psa_aead_verify(). 232132b31808SJens Wiklander * 232232b31808SJens Wiklander * If an error occurs at any step after a call to psa_aead_decrypt_setup(), 232332b31808SJens Wiklander * the operation will need to be reset by a call to psa_aead_abort(). The 232432b31808SJens Wiklander * application may call psa_aead_abort() at any time after the operation 232532b31808SJens Wiklander * has been initialized. 232632b31808SJens Wiklander * 232732b31808SJens Wiklander * After a successful call to psa_aead_decrypt_setup(), the application must 232832b31808SJens Wiklander * eventually terminate the operation. The following events terminate an 232932b31808SJens Wiklander * operation: 233032b31808SJens Wiklander * - A successful call to psa_aead_verify(). 233132b31808SJens Wiklander * - A call to psa_aead_abort(). 233232b31808SJens Wiklander * 233332b31808SJens Wiklander * \param[in,out] operation The operation object to set up. It must have 233432b31808SJens Wiklander * been initialized as per the documentation for 233532b31808SJens Wiklander * #psa_aead_operation_t and not yet in use. 233632b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 233732b31808SJens Wiklander * It must remain valid until the operation 233832b31808SJens Wiklander * terminates. It must allow the usage 233932b31808SJens Wiklander * #PSA_KEY_USAGE_DECRYPT. 234032b31808SJens Wiklander * \param alg The AEAD algorithm to compute 234132b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 234232b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 234332b31808SJens Wiklander * 234432b31808SJens Wiklander * \retval #PSA_SUCCESS 234532b31808SJens Wiklander * Success. 234632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 234732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 234832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 234932b31808SJens Wiklander * \p key is not compatible with \p alg. 235032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 235132b31808SJens Wiklander * \p alg is not supported or is not an AEAD algorithm. 235232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 235332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 235432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 235532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 235632b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 235732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 235832b31808SJens Wiklander * The operation state is not valid (it must be inactive), or the 235932b31808SJens Wiklander * library has not been previously initialized by psa_crypto_init(). 236032b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 236132b31808SJens Wiklander * results in this error code. 236232b31808SJens Wiklander */ 236332b31808SJens Wiklander psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, 236432b31808SJens Wiklander mbedtls_svc_key_id_t key, 236532b31808SJens Wiklander psa_algorithm_t alg); 236632b31808SJens Wiklander 236732b31808SJens Wiklander /** Generate a random nonce for an authenticated encryption operation. 236832b31808SJens Wiklander * 236932b31808SJens Wiklander * This function generates a random nonce for the authenticated encryption 237032b31808SJens Wiklander * operation with an appropriate size for the chosen algorithm, key type 237132b31808SJens Wiklander * and key size. 237232b31808SJens Wiklander * 237332b31808SJens Wiklander * The application must call psa_aead_encrypt_setup() before 237432b31808SJens Wiklander * calling this function. 237532b31808SJens Wiklander * 237632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 237732b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 237832b31808SJens Wiklander * 237932b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 238032b31808SJens Wiklander * \param[out] nonce Buffer where the generated nonce is to be 238132b31808SJens Wiklander * written. 238232b31808SJens Wiklander * \param nonce_size Size of the \p nonce buffer in bytes. 238332b31808SJens Wiklander * \param[out] nonce_length On success, the number of bytes of the 238432b31808SJens Wiklander * generated nonce. 238532b31808SJens Wiklander * 238632b31808SJens Wiklander * \retval #PSA_SUCCESS 238732b31808SJens Wiklander * Success. 238832b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 238932b31808SJens Wiklander * The size of the \p nonce buffer is too small. 239032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 239132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 239232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 239332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 239432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 239532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 239632b31808SJens Wiklander * The operation state is not valid (it must be an active aead encrypt 239732b31808SJens Wiklander * operation, with no nonce set), or the library has not been 239832b31808SJens Wiklander * previously initialized by psa_crypto_init(). 239932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 240032b31808SJens Wiklander * results in this error code. 240132b31808SJens Wiklander */ 240232b31808SJens Wiklander psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, 240332b31808SJens Wiklander uint8_t *nonce, 240432b31808SJens Wiklander size_t nonce_size, 240532b31808SJens Wiklander size_t *nonce_length); 240632b31808SJens Wiklander 240732b31808SJens Wiklander /** Set the nonce for an authenticated encryption or decryption operation. 240832b31808SJens Wiklander * 240932b31808SJens Wiklander * This function sets the nonce for the authenticated 241032b31808SJens Wiklander * encryption or decryption operation. 241132b31808SJens Wiklander * 241232b31808SJens Wiklander * The application must call psa_aead_encrypt_setup() or 241332b31808SJens Wiklander * psa_aead_decrypt_setup() before calling this function. 241432b31808SJens Wiklander * 241532b31808SJens Wiklander * If this function returns an error status, the operation enters an error 241632b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 241732b31808SJens Wiklander * 241832b31808SJens Wiklander * \note When encrypting, applications should use psa_aead_generate_nonce() 241932b31808SJens Wiklander * instead of this function, unless implementing a protocol that requires 242032b31808SJens Wiklander * a non-random IV. 242132b31808SJens Wiklander * 242232b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 242332b31808SJens Wiklander * \param[in] nonce Buffer containing the nonce to use. 242432b31808SJens Wiklander * \param nonce_length Size of the nonce in bytes. 242532b31808SJens Wiklander * 242632b31808SJens Wiklander * \retval #PSA_SUCCESS 242732b31808SJens Wiklander * Success. 242832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 242932b31808SJens Wiklander * The size of \p nonce is not acceptable for the chosen algorithm. 243032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 243132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 243232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 243332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 243432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 243532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 243632b31808SJens Wiklander * The operation state is not valid (it must be active, with no nonce 243732b31808SJens Wiklander * set), or the library has not been previously initialized 243832b31808SJens Wiklander * by psa_crypto_init(). 243932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 244032b31808SJens Wiklander * results in this error code. 244132b31808SJens Wiklander */ 244232b31808SJens Wiklander psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, 244332b31808SJens Wiklander const uint8_t *nonce, 244432b31808SJens Wiklander size_t nonce_length); 244532b31808SJens Wiklander 244632b31808SJens Wiklander /** Declare the lengths of the message and additional data for AEAD. 244732b31808SJens Wiklander * 244832b31808SJens Wiklander * The application must call this function before calling 244932b31808SJens Wiklander * psa_aead_update_ad() or psa_aead_update() if the algorithm for 245032b31808SJens Wiklander * the operation requires it. If the algorithm does not require it, 245132b31808SJens Wiklander * calling this function is optional, but if this function is called 245232b31808SJens Wiklander * then the implementation must enforce the lengths. 245332b31808SJens Wiklander * 245432b31808SJens Wiklander * You may call this function before or after setting the nonce with 245532b31808SJens Wiklander * psa_aead_set_nonce() or psa_aead_generate_nonce(). 245632b31808SJens Wiklander * 245732b31808SJens Wiklander * - For #PSA_ALG_CCM, calling this function is required. 245832b31808SJens Wiklander * - For the other AEAD algorithms defined in this specification, calling 245932b31808SJens Wiklander * this function is not required. 246032b31808SJens Wiklander * - For vendor-defined algorithm, refer to the vendor documentation. 246132b31808SJens Wiklander * 246232b31808SJens Wiklander * If this function returns an error status, the operation enters an error 246332b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 246432b31808SJens Wiklander * 246532b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 246632b31808SJens Wiklander * \param ad_length Size of the non-encrypted additional 246732b31808SJens Wiklander * authenticated data in bytes. 246832b31808SJens Wiklander * \param plaintext_length Size of the plaintext to encrypt in bytes. 246932b31808SJens Wiklander * 247032b31808SJens Wiklander * \retval #PSA_SUCCESS 247132b31808SJens Wiklander * Success. 247232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 247332b31808SJens Wiklander * At least one of the lengths is not acceptable for the chosen 247432b31808SJens Wiklander * algorithm. 247532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 247632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 247732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 247832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 247932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 248032b31808SJens Wiklander * The operation state is not valid (it must be active, and 248132b31808SJens Wiklander * psa_aead_update_ad() and psa_aead_update() must not have been 248232b31808SJens Wiklander * called yet), or the library has not been previously initialized 248332b31808SJens Wiklander * by psa_crypto_init(). 248432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 248532b31808SJens Wiklander * results in this error code. 248632b31808SJens Wiklander */ 248732b31808SJens Wiklander psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, 248832b31808SJens Wiklander size_t ad_length, 248932b31808SJens Wiklander size_t plaintext_length); 249032b31808SJens Wiklander 249132b31808SJens Wiklander /** Pass additional data to an active AEAD operation. 249232b31808SJens Wiklander * 249332b31808SJens Wiklander * Additional data is authenticated, but not encrypted. 249432b31808SJens Wiklander * 249532b31808SJens Wiklander * You may call this function multiple times to pass successive fragments 249632b31808SJens Wiklander * of the additional data. You may not call this function after passing 249732b31808SJens Wiklander * data to encrypt or decrypt with psa_aead_update(). 249832b31808SJens Wiklander * 249932b31808SJens Wiklander * Before calling this function, you must: 250032b31808SJens Wiklander * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup(). 250132b31808SJens Wiklander * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce(). 250232b31808SJens Wiklander * 250332b31808SJens Wiklander * If this function returns an error status, the operation enters an error 250432b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 250532b31808SJens Wiklander * 250632b31808SJens Wiklander * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS, 250732b31808SJens Wiklander * there is no guarantee that the input is valid. Therefore, until 250832b31808SJens Wiklander * you have called psa_aead_verify() and it has returned #PSA_SUCCESS, 250932b31808SJens Wiklander * treat the input as untrusted and prepare to undo any action that 251032b31808SJens Wiklander * depends on the input if psa_aead_verify() returns an error status. 251132b31808SJens Wiklander * 251232b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 251332b31808SJens Wiklander * \param[in] input Buffer containing the fragment of 251432b31808SJens Wiklander * additional data. 251532b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 251632b31808SJens Wiklander * 251732b31808SJens Wiklander * \retval #PSA_SUCCESS 251832b31808SJens Wiklander * Success. 251932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 252032b31808SJens Wiklander * The total input length overflows the additional data length that 252132b31808SJens Wiklander * was previously specified with psa_aead_set_lengths(). 252232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 252332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 252432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 252532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 252632b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 252732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 252832b31808SJens Wiklander * The operation state is not valid (it must be active, have a nonce 252932b31808SJens Wiklander * set, have lengths set if required by the algorithm, and 253032b31808SJens Wiklander * psa_aead_update() must not have been called yet), or the library 253132b31808SJens Wiklander * has not been previously initialized by psa_crypto_init(). 253232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 253332b31808SJens Wiklander * results in this error code. 253432b31808SJens Wiklander */ 253532b31808SJens Wiklander psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, 253632b31808SJens Wiklander const uint8_t *input, 253732b31808SJens Wiklander size_t input_length); 253832b31808SJens Wiklander 253932b31808SJens Wiklander /** Encrypt or decrypt a message fragment in an active AEAD operation. 254032b31808SJens Wiklander * 254132b31808SJens Wiklander * Before calling this function, you must: 254232b31808SJens Wiklander * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup(). 254332b31808SJens Wiklander * The choice of setup function determines whether this function 254432b31808SJens Wiklander * encrypts or decrypts its input. 254532b31808SJens Wiklander * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce(). 254632b31808SJens Wiklander * 3. Call psa_aead_update_ad() to pass all the additional data. 254732b31808SJens Wiklander * 254832b31808SJens Wiklander * If this function returns an error status, the operation enters an error 254932b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 255032b31808SJens Wiklander * 255132b31808SJens Wiklander * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS, 255232b31808SJens Wiklander * there is no guarantee that the input is valid. Therefore, until 255332b31808SJens Wiklander * you have called psa_aead_verify() and it has returned #PSA_SUCCESS: 255432b31808SJens Wiklander * - Do not use the output in any way other than storing it in a 255532b31808SJens Wiklander * confidential location. If you take any action that depends 255632b31808SJens Wiklander * on the tentative decrypted data, this action will need to be 255732b31808SJens Wiklander * undone if the input turns out not to be valid. Furthermore, 255832b31808SJens Wiklander * if an adversary can observe that this action took place 255932b31808SJens Wiklander * (for example through timing), they may be able to use this 256032b31808SJens Wiklander * fact as an oracle to decrypt any message encrypted with the 256132b31808SJens Wiklander * same key. 256232b31808SJens Wiklander * - In particular, do not copy the output anywhere but to a 256332b31808SJens Wiklander * memory or storage space that you have exclusive access to. 256432b31808SJens Wiklander * 256532b31808SJens Wiklander * This function does not require the input to be aligned to any 256632b31808SJens Wiklander * particular block boundary. If the implementation can only process 256732b31808SJens Wiklander * a whole block at a time, it must consume all the input provided, but 256832b31808SJens Wiklander * it may delay the end of the corresponding output until a subsequent 256932b31808SJens Wiklander * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify() 257032b31808SJens Wiklander * provides sufficient input. The amount of data that can be delayed 257132b31808SJens Wiklander * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE. 257232b31808SJens Wiklander * 257332b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 257432b31808SJens Wiklander * \param[in] input Buffer containing the message fragment to 257532b31808SJens Wiklander * encrypt or decrypt. 257632b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 257732b31808SJens Wiklander * \param[out] output Buffer where the output is to be written. 257832b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 257932b31808SJens Wiklander * This must be appropriate for the selected 258032b31808SJens Wiklander * algorithm and key: 258132b31808SJens Wiklander * - A sufficient output size is 258232b31808SJens Wiklander * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, 258332b31808SJens Wiklander * \c alg, \p input_length) where 258432b31808SJens Wiklander * \c key_type is the type of key and \c alg is 258532b31808SJens Wiklander * the algorithm that were used to set up the 258632b31808SJens Wiklander * operation. 258732b31808SJens Wiklander * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p 258832b31808SJens Wiklander * input_length) evaluates to the maximum 258932b31808SJens Wiklander * output size of any supported AEAD 259032b31808SJens Wiklander * algorithm. 259132b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 259232b31808SJens Wiklander * that make up the returned output. 259332b31808SJens Wiklander * 259432b31808SJens Wiklander * \retval #PSA_SUCCESS 259532b31808SJens Wiklander * Success. 259632b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 259732b31808SJens Wiklander * The size of the \p output buffer is too small. 259832b31808SJens Wiklander * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or 259932b31808SJens Wiklander * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to 260032b31808SJens Wiklander * determine the required buffer size. 260132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 260232b31808SJens Wiklander * The total length of input to psa_aead_update_ad() so far is 260332b31808SJens Wiklander * less than the additional data length that was previously 260432b31808SJens Wiklander * specified with psa_aead_set_lengths(), or 260532b31808SJens Wiklander * the total input length overflows the plaintext length that 260632b31808SJens Wiklander * was previously specified with psa_aead_set_lengths(). 260732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 260832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 260932b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 261032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 261132b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 261232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 261332b31808SJens Wiklander * The operation state is not valid (it must be active, have a nonce 261432b31808SJens Wiklander * set, and have lengths set if required by the algorithm), or the 261532b31808SJens Wiklander * library has not been previously initialized by psa_crypto_init(). 261632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 261732b31808SJens Wiklander * results in this error code. 261832b31808SJens Wiklander */ 261932b31808SJens Wiklander psa_status_t psa_aead_update(psa_aead_operation_t *operation, 262032b31808SJens Wiklander const uint8_t *input, 262132b31808SJens Wiklander size_t input_length, 262232b31808SJens Wiklander uint8_t *output, 262332b31808SJens Wiklander size_t output_size, 262432b31808SJens Wiklander size_t *output_length); 262532b31808SJens Wiklander 262632b31808SJens Wiklander /** Finish encrypting a message in an AEAD operation. 262732b31808SJens Wiklander * 262832b31808SJens Wiklander * The operation must have been set up with psa_aead_encrypt_setup(). 262932b31808SJens Wiklander * 263032b31808SJens Wiklander * This function finishes the authentication of the additional data 263132b31808SJens Wiklander * formed by concatenating the inputs passed to preceding calls to 263232b31808SJens Wiklander * psa_aead_update_ad() with the plaintext formed by concatenating the 263332b31808SJens Wiklander * inputs passed to preceding calls to psa_aead_update(). 263432b31808SJens Wiklander * 263532b31808SJens Wiklander * This function has two output buffers: 263632b31808SJens Wiklander * - \p ciphertext contains trailing ciphertext that was buffered from 263732b31808SJens Wiklander * preceding calls to psa_aead_update(). 263832b31808SJens Wiklander * - \p tag contains the authentication tag. 263932b31808SJens Wiklander * 264032b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 264132b31808SJens Wiklander * If this function returns an error status, the operation enters an error 264232b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 264332b31808SJens Wiklander * 264432b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 264532b31808SJens Wiklander * \param[out] ciphertext Buffer where the last part of the ciphertext 264632b31808SJens Wiklander * is to be written. 264732b31808SJens Wiklander * \param ciphertext_size Size of the \p ciphertext buffer in bytes. 264832b31808SJens Wiklander * This must be appropriate for the selected 264932b31808SJens Wiklander * algorithm and key: 265032b31808SJens Wiklander * - A sufficient output size is 265132b31808SJens Wiklander * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, 265232b31808SJens Wiklander * \c alg) where \c key_type is the type of key 265332b31808SJens Wiklander * and \c alg is the algorithm that were used to 265432b31808SJens Wiklander * set up the operation. 265532b31808SJens Wiklander * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to 265632b31808SJens Wiklander * the maximum output size of any supported AEAD 265732b31808SJens Wiklander * algorithm. 265832b31808SJens Wiklander * \param[out] ciphertext_length On success, the number of bytes of 265932b31808SJens Wiklander * returned ciphertext. 266032b31808SJens Wiklander * \param[out] tag Buffer where the authentication tag is 266132b31808SJens Wiklander * to be written. 266232b31808SJens Wiklander * \param tag_size Size of the \p tag buffer in bytes. 266332b31808SJens Wiklander * This must be appropriate for the selected 266432b31808SJens Wiklander * algorithm and key: 266532b31808SJens Wiklander * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c 266632b31808SJens Wiklander * key_type, \c key_bits, \c alg) where 266732b31808SJens Wiklander * \c key_type and \c key_bits are the type and 266832b31808SJens Wiklander * bit-size of the key, and \c alg is the 266932b31808SJens Wiklander * algorithm that were used in the call to 267032b31808SJens Wiklander * psa_aead_encrypt_setup(). 267132b31808SJens Wiklander * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the 267232b31808SJens Wiklander * maximum tag size of any supported AEAD 267332b31808SJens Wiklander * algorithm. 267432b31808SJens Wiklander * \param[out] tag_length On success, the number of bytes 267532b31808SJens Wiklander * that make up the returned tag. 267632b31808SJens Wiklander * 267732b31808SJens Wiklander * \retval #PSA_SUCCESS 267832b31808SJens Wiklander * Success. 267932b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 268032b31808SJens Wiklander * The size of the \p ciphertext or \p tag buffer is too small. 268132b31808SJens Wiklander * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or 268232b31808SJens Wiklander * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the 268332b31808SJens Wiklander * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type, 268432b31808SJens Wiklander * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to 268532b31808SJens Wiklander * determine the required \p tag buffer size. 268632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 268732b31808SJens Wiklander * The total length of input to psa_aead_update_ad() so far is 268832b31808SJens Wiklander * less than the additional data length that was previously 268932b31808SJens Wiklander * specified with psa_aead_set_lengths(), or 269032b31808SJens Wiklander * the total length of input to psa_aead_update() so far is 269132b31808SJens Wiklander * less than the plaintext length that was previously 269232b31808SJens Wiklander * specified with psa_aead_set_lengths(). 269332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 269432b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 269532b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 269632b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 269732b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 269832b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 269932b31808SJens Wiklander * The operation state is not valid (it must be an active encryption 270032b31808SJens Wiklander * operation with a nonce set), or the library has not been previously 270132b31808SJens Wiklander * initialized by psa_crypto_init(). 270232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 270332b31808SJens Wiklander * results in this error code. 270432b31808SJens Wiklander */ 270532b31808SJens Wiklander psa_status_t psa_aead_finish(psa_aead_operation_t *operation, 270632b31808SJens Wiklander uint8_t *ciphertext, 270732b31808SJens Wiklander size_t ciphertext_size, 270832b31808SJens Wiklander size_t *ciphertext_length, 270932b31808SJens Wiklander uint8_t *tag, 271032b31808SJens Wiklander size_t tag_size, 271132b31808SJens Wiklander size_t *tag_length); 271232b31808SJens Wiklander 271332b31808SJens Wiklander /** Finish authenticating and decrypting a message in an AEAD operation. 271432b31808SJens Wiklander * 271532b31808SJens Wiklander * The operation must have been set up with psa_aead_decrypt_setup(). 271632b31808SJens Wiklander * 271732b31808SJens Wiklander * This function finishes the authenticated decryption of the message 271832b31808SJens Wiklander * components: 271932b31808SJens Wiklander * 272032b31808SJens Wiklander * - The additional data consisting of the concatenation of the inputs 272132b31808SJens Wiklander * passed to preceding calls to psa_aead_update_ad(). 272232b31808SJens Wiklander * - The ciphertext consisting of the concatenation of the inputs passed to 272332b31808SJens Wiklander * preceding calls to psa_aead_update(). 272432b31808SJens Wiklander * - The tag passed to this function call. 272532b31808SJens Wiklander * 272632b31808SJens Wiklander * If the authentication tag is correct, this function outputs any remaining 272732b31808SJens Wiklander * plaintext and reports success. If the authentication tag is not correct, 272832b31808SJens Wiklander * this function returns #PSA_ERROR_INVALID_SIGNATURE. 272932b31808SJens Wiklander * 273032b31808SJens Wiklander * When this function returns successfully, the operation becomes inactive. 273132b31808SJens Wiklander * If this function returns an error status, the operation enters an error 273232b31808SJens Wiklander * state and must be aborted by calling psa_aead_abort(). 273332b31808SJens Wiklander * 273432b31808SJens Wiklander * \note Implementations shall make the best effort to ensure that the 273532b31808SJens Wiklander * comparison between the actual tag and the expected tag is performed 273632b31808SJens Wiklander * in constant time. 273732b31808SJens Wiklander * 273832b31808SJens Wiklander * \param[in,out] operation Active AEAD operation. 273932b31808SJens Wiklander * \param[out] plaintext Buffer where the last part of the plaintext 274032b31808SJens Wiklander * is to be written. This is the remaining data 274132b31808SJens Wiklander * from previous calls to psa_aead_update() 274232b31808SJens Wiklander * that could not be processed until the end 274332b31808SJens Wiklander * of the input. 274432b31808SJens Wiklander * \param plaintext_size Size of the \p plaintext buffer in bytes. 274532b31808SJens Wiklander * This must be appropriate for the selected algorithm and key: 274632b31808SJens Wiklander * - A sufficient output size is 274732b31808SJens Wiklander * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, 274832b31808SJens Wiklander * \c alg) where \c key_type is the type of key 274932b31808SJens Wiklander * and \c alg is the algorithm that were used to 275032b31808SJens Wiklander * set up the operation. 275132b31808SJens Wiklander * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to 275232b31808SJens Wiklander * the maximum output size of any supported AEAD 275332b31808SJens Wiklander * algorithm. 275432b31808SJens Wiklander * \param[out] plaintext_length On success, the number of bytes of 275532b31808SJens Wiklander * returned plaintext. 275632b31808SJens Wiklander * \param[in] tag Buffer containing the authentication tag. 275732b31808SJens Wiklander * \param tag_length Size of the \p tag buffer in bytes. 275832b31808SJens Wiklander * 275932b31808SJens Wiklander * \retval #PSA_SUCCESS 276032b31808SJens Wiklander * Success. 276132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 276232b31808SJens Wiklander * The calculations were successful, but the authentication tag is 276332b31808SJens Wiklander * not correct. 276432b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 276532b31808SJens Wiklander * The size of the \p plaintext buffer is too small. 276632b31808SJens Wiklander * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or 276732b31808SJens Wiklander * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the 276832b31808SJens Wiklander * required buffer size. 276932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 277032b31808SJens Wiklander * The total length of input to psa_aead_update_ad() so far is 277132b31808SJens Wiklander * less than the additional data length that was previously 277232b31808SJens Wiklander * specified with psa_aead_set_lengths(), or 277332b31808SJens Wiklander * the total length of input to psa_aead_update() so far is 277432b31808SJens Wiklander * less than the plaintext length that was previously 277532b31808SJens Wiklander * specified with psa_aead_set_lengths(). 277632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 277732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 277832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 277932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 278032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 278132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 278232b31808SJens Wiklander * The operation state is not valid (it must be an active decryption 278332b31808SJens Wiklander * operation with a nonce set), or the library has not been previously 278432b31808SJens Wiklander * initialized by psa_crypto_init(). 278532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 278632b31808SJens Wiklander * results in this error code. 278732b31808SJens Wiklander */ 278832b31808SJens Wiklander psa_status_t psa_aead_verify(psa_aead_operation_t *operation, 278932b31808SJens Wiklander uint8_t *plaintext, 279032b31808SJens Wiklander size_t plaintext_size, 279132b31808SJens Wiklander size_t *plaintext_length, 279232b31808SJens Wiklander const uint8_t *tag, 279332b31808SJens Wiklander size_t tag_length); 279432b31808SJens Wiklander 279532b31808SJens Wiklander /** Abort an AEAD operation. 279632b31808SJens Wiklander * 279732b31808SJens Wiklander * Aborting an operation frees all associated resources except for the 279832b31808SJens Wiklander * \p operation structure itself. Once aborted, the operation object 279932b31808SJens Wiklander * can be reused for another operation by calling 280032b31808SJens Wiklander * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again. 280132b31808SJens Wiklander * 280232b31808SJens Wiklander * You may call this function any time after the operation object has 280332b31808SJens Wiklander * been initialized as described in #psa_aead_operation_t. 280432b31808SJens Wiklander * 280532b31808SJens Wiklander * In particular, calling psa_aead_abort() after the operation has been 280632b31808SJens Wiklander * terminated by a call to psa_aead_abort(), psa_aead_finish() or 280732b31808SJens Wiklander * psa_aead_verify() is safe and has no effect. 280832b31808SJens Wiklander * 280932b31808SJens Wiklander * \param[in,out] operation Initialized AEAD operation. 281032b31808SJens Wiklander * 281132b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 281232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 281332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 281432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 281532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 281632b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 281732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 281832b31808SJens Wiklander * results in this error code. 281932b31808SJens Wiklander */ 282032b31808SJens Wiklander psa_status_t psa_aead_abort(psa_aead_operation_t *operation); 282132b31808SJens Wiklander 282232b31808SJens Wiklander /**@}*/ 282332b31808SJens Wiklander 282432b31808SJens Wiklander /** \defgroup asymmetric Asymmetric cryptography 282532b31808SJens Wiklander * @{ 282632b31808SJens Wiklander */ 282732b31808SJens Wiklander 282832b31808SJens Wiklander /** 282932b31808SJens Wiklander * \brief Sign a message with a private key. For hash-and-sign algorithms, 283032b31808SJens Wiklander * this includes the hashing step. 283132b31808SJens Wiklander * 283232b31808SJens Wiklander * \note To perform a multi-part hash-and-sign signature algorithm, first use 283332b31808SJens Wiklander * a multi-part hash operation and then pass the resulting hash to 283432b31808SJens Wiklander * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the 283532b31808SJens Wiklander * hash algorithm to use. 283632b31808SJens Wiklander * 283732b31808SJens Wiklander * \param[in] key Identifier of the key to use for the operation. 283832b31808SJens Wiklander * It must be an asymmetric key pair. The key must 283932b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE. 284032b31808SJens Wiklander * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX 284132b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg) 284232b31808SJens Wiklander * is true), that is compatible with the type of 284332b31808SJens Wiklander * \p key. 284432b31808SJens Wiklander * \param[in] input The input message to sign. 284532b31808SJens Wiklander * \param[in] input_length Size of the \p input buffer in bytes. 284632b31808SJens Wiklander * \param[out] signature Buffer where the signature is to be written. 284732b31808SJens Wiklander * \param[in] signature_size Size of the \p signature buffer in bytes. This 284832b31808SJens Wiklander * must be appropriate for the selected 284932b31808SJens Wiklander * algorithm and key: 285032b31808SJens Wiklander * - The required signature size is 285132b31808SJens Wiklander * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 285232b31808SJens Wiklander * where \c key_type and \c key_bits are the type and 285332b31808SJens Wiklander * bit-size respectively of key. 285432b31808SJens Wiklander * - #PSA_SIGNATURE_MAX_SIZE evaluates to the 285532b31808SJens Wiklander * maximum signature size of any supported 285632b31808SJens Wiklander * signature algorithm. 285732b31808SJens Wiklander * \param[out] signature_length On success, the number of bytes that make up 285832b31808SJens Wiklander * the returned signature value. 285932b31808SJens Wiklander * 286032b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 286132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 286232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 286332b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag, 286432b31808SJens Wiklander * or it does not permit the requested algorithm. 286532b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 286632b31808SJens Wiklander * The size of the \p signature buffer is too small. You can 286732b31808SJens Wiklander * determine a sufficient buffer size by calling 286832b31808SJens Wiklander * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 286932b31808SJens Wiklander * where \c key_type and \c key_bits are the type and bit-size 287032b31808SJens Wiklander * respectively of \p key. 287132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 287232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 287332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 287432b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 287532b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 287632b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 287732b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 287832b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 287932b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 288032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 288132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 288232b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 288332b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 288432b31808SJens Wiklander * results in this error code. 288532b31808SJens Wiklander */ 288632b31808SJens Wiklander psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, 288732b31808SJens Wiklander psa_algorithm_t alg, 288832b31808SJens Wiklander const uint8_t *input, 288932b31808SJens Wiklander size_t input_length, 289032b31808SJens Wiklander uint8_t *signature, 289132b31808SJens Wiklander size_t signature_size, 289232b31808SJens Wiklander size_t *signature_length); 289332b31808SJens Wiklander 289432b31808SJens Wiklander /** \brief Verify the signature of a message with a public key, using 289532b31808SJens Wiklander * a hash-and-sign verification algorithm. 289632b31808SJens Wiklander * 289732b31808SJens Wiklander * \note To perform a multi-part hash-and-sign signature verification 289832b31808SJens Wiklander * algorithm, first use a multi-part hash operation to hash the message 289932b31808SJens Wiklander * and then pass the resulting hash to psa_verify_hash(). 290032b31808SJens Wiklander * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm 290132b31808SJens Wiklander * to use. 290232b31808SJens Wiklander * 290332b31808SJens Wiklander * \param[in] key Identifier of the key to use for the operation. 290432b31808SJens Wiklander * It must be a public key or an asymmetric key 290532b31808SJens Wiklander * pair. The key must allow the usage 290632b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_MESSAGE. 290732b31808SJens Wiklander * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX 290832b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg) 290932b31808SJens Wiklander * is true), that is compatible with the type of 291032b31808SJens Wiklander * \p key. 291132b31808SJens Wiklander * \param[in] input The message whose signature is to be verified. 291232b31808SJens Wiklander * \param[in] input_length Size of the \p input buffer in bytes. 291332b31808SJens Wiklander * \param[out] signature Buffer containing the signature to verify. 291432b31808SJens Wiklander * \param[in] signature_length Size of the \p signature buffer in bytes. 291532b31808SJens Wiklander * 291632b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 291732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 291832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 291932b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag, 292032b31808SJens Wiklander * or it does not permit the requested algorithm. 292132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 292232b31808SJens Wiklander * The calculation was performed successfully, but the passed signature 292332b31808SJens Wiklander * is not a valid signature. 292432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 292532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 292632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 292732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 292832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 292932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 293032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 293132b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 293232b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 293332b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 293432b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 293532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 293632b31808SJens Wiklander * results in this error code. 293732b31808SJens Wiklander */ 293832b31808SJens Wiklander psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, 293932b31808SJens Wiklander psa_algorithm_t alg, 294032b31808SJens Wiklander const uint8_t *input, 294132b31808SJens Wiklander size_t input_length, 294232b31808SJens Wiklander const uint8_t *signature, 294332b31808SJens Wiklander size_t signature_length); 294432b31808SJens Wiklander 294532b31808SJens Wiklander /** 294632b31808SJens Wiklander * \brief Sign a hash or short message with a private key. 294732b31808SJens Wiklander * 294832b31808SJens Wiklander * Note that to perform a hash-and-sign signature algorithm, you must 294932b31808SJens Wiklander * first calculate the hash by calling psa_hash_setup(), psa_hash_update() 295032b31808SJens Wiklander * and psa_hash_finish(), or alternatively by calling psa_hash_compute(). 295132b31808SJens Wiklander * Then pass the resulting hash as the \p hash 295232b31808SJens Wiklander * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) 295332b31808SJens Wiklander * to determine the hash algorithm to use. 295432b31808SJens Wiklander * 295532b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 295632b31808SJens Wiklander * It must be an asymmetric key pair. The key must 295732b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_SIGN_HASH. 295832b31808SJens Wiklander * \param alg A signature algorithm (PSA_ALG_XXX 295932b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_HASH(\p alg) 296032b31808SJens Wiklander * is true), that is compatible with 296132b31808SJens Wiklander * the type of \p key. 296232b31808SJens Wiklander * \param[in] hash The hash or message to sign. 296332b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 296432b31808SJens Wiklander * \param[out] signature Buffer where the signature is to be written. 296532b31808SJens Wiklander * \param signature_size Size of the \p signature buffer in bytes. 296632b31808SJens Wiklander * \param[out] signature_length On success, the number of bytes 296732b31808SJens Wiklander * that make up the returned signature value. 296832b31808SJens Wiklander * 296932b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 297032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 297132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 297232b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 297332b31808SJens Wiklander * The size of the \p signature buffer is too small. You can 297432b31808SJens Wiklander * determine a sufficient buffer size by calling 297532b31808SJens Wiklander * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 297632b31808SJens Wiklander * where \c key_type and \c key_bits are the type and bit-size 297732b31808SJens Wiklander * respectively of \p key. 297832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 297932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 298032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 298132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 298232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 298332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 298432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 298532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 298632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 298732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 298832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 298932b31808SJens Wiklander * results in this error code. 299032b31808SJens Wiklander */ 299132b31808SJens Wiklander psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, 299232b31808SJens Wiklander psa_algorithm_t alg, 299332b31808SJens Wiklander const uint8_t *hash, 299432b31808SJens Wiklander size_t hash_length, 299532b31808SJens Wiklander uint8_t *signature, 299632b31808SJens Wiklander size_t signature_size, 299732b31808SJens Wiklander size_t *signature_length); 299832b31808SJens Wiklander 299932b31808SJens Wiklander /** 300032b31808SJens Wiklander * \brief Verify the signature of a hash or short message using a public key. 300132b31808SJens Wiklander * 300232b31808SJens Wiklander * Note that to perform a hash-and-sign signature algorithm, you must 300332b31808SJens Wiklander * first calculate the hash by calling psa_hash_setup(), psa_hash_update() 300432b31808SJens Wiklander * and psa_hash_finish(), or alternatively by calling psa_hash_compute(). 300532b31808SJens Wiklander * Then pass the resulting hash as the \p hash 300632b31808SJens Wiklander * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) 300732b31808SJens Wiklander * to determine the hash algorithm to use. 300832b31808SJens Wiklander * 300932b31808SJens Wiklander * \param key Identifier of the key to use for the operation. It 301032b31808SJens Wiklander * must be a public key or an asymmetric key pair. The 301132b31808SJens Wiklander * key must allow the usage 301232b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_HASH. 301332b31808SJens Wiklander * \param alg A signature algorithm (PSA_ALG_XXX 301432b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_HASH(\p alg) 301532b31808SJens Wiklander * is true), that is compatible with 301632b31808SJens Wiklander * the type of \p key. 301732b31808SJens Wiklander * \param[in] hash The hash or message whose signature is to be 301832b31808SJens Wiklander * verified. 301932b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 302032b31808SJens Wiklander * \param[in] signature Buffer containing the signature to verify. 302132b31808SJens Wiklander * \param signature_length Size of the \p signature buffer in bytes. 302232b31808SJens Wiklander * 302332b31808SJens Wiklander * \retval #PSA_SUCCESS 302432b31808SJens Wiklander * The signature is valid. 302532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 302632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 302732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 302832b31808SJens Wiklander * The calculation was performed successfully, but the passed 302932b31808SJens Wiklander * signature is not a valid signature. 303032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 303132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 303232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 303332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 303432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 303532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 303632b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 303732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 303832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 303932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 304032b31808SJens Wiklander * results in this error code. 304132b31808SJens Wiklander */ 304232b31808SJens Wiklander psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, 304332b31808SJens Wiklander psa_algorithm_t alg, 304432b31808SJens Wiklander const uint8_t *hash, 304532b31808SJens Wiklander size_t hash_length, 304632b31808SJens Wiklander const uint8_t *signature, 304732b31808SJens Wiklander size_t signature_length); 304832b31808SJens Wiklander 304932b31808SJens Wiklander /** 305032b31808SJens Wiklander * \brief Encrypt a short message with a public key. 305132b31808SJens Wiklander * 305232b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 305332b31808SJens Wiklander * It must be a public key or an asymmetric key 305432b31808SJens Wiklander * pair. It must allow the usage 305532b31808SJens Wiklander * #PSA_KEY_USAGE_ENCRYPT. 305632b31808SJens Wiklander * \param alg An asymmetric encryption algorithm that is 305732b31808SJens Wiklander * compatible with the type of \p key. 305832b31808SJens Wiklander * \param[in] input The message to encrypt. 305932b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 306032b31808SJens Wiklander * \param[in] salt A salt or label, if supported by the 306132b31808SJens Wiklander * encryption algorithm. 306232b31808SJens Wiklander * If the algorithm does not support a 306332b31808SJens Wiklander * salt, pass \c NULL. 306432b31808SJens Wiklander * If the algorithm supports an optional 306532b31808SJens Wiklander * salt and you do not want to pass a salt, 306632b31808SJens Wiklander * pass \c NULL. 306732b31808SJens Wiklander * 306832b31808SJens Wiklander * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 306932b31808SJens Wiklander * supported. 307032b31808SJens Wiklander * \param salt_length Size of the \p salt buffer in bytes. 307132b31808SJens Wiklander * If \p salt is \c NULL, pass 0. 307232b31808SJens Wiklander * \param[out] output Buffer where the encrypted message is to 307332b31808SJens Wiklander * be written. 307432b31808SJens Wiklander * \param output_size Size of the \p output buffer in bytes. 307532b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 307632b31808SJens Wiklander * that make up the returned output. 307732b31808SJens Wiklander * 307832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 307932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 308032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 308132b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 308232b31808SJens Wiklander * The size of the \p output buffer is too small. You can 308332b31808SJens Wiklander * determine a sufficient buffer size by calling 308432b31808SJens Wiklander * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 308532b31808SJens Wiklander * where \c key_type and \c key_bits are the type and bit-size 308632b31808SJens Wiklander * respectively of \p key. 308732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 308832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 308932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 309032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 309132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 309232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 309332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 309432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 309532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 309632b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 309732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 309832b31808SJens Wiklander * results in this error code. 309932b31808SJens Wiklander */ 310032b31808SJens Wiklander psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, 310132b31808SJens Wiklander psa_algorithm_t alg, 310232b31808SJens Wiklander const uint8_t *input, 310332b31808SJens Wiklander size_t input_length, 310432b31808SJens Wiklander const uint8_t *salt, 310532b31808SJens Wiklander size_t salt_length, 310632b31808SJens Wiklander uint8_t *output, 310732b31808SJens Wiklander size_t output_size, 310832b31808SJens Wiklander size_t *output_length); 310932b31808SJens Wiklander 311032b31808SJens Wiklander /** 311132b31808SJens Wiklander * \brief Decrypt a short message with a private key. 311232b31808SJens Wiklander * 311332b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 311432b31808SJens Wiklander * It must be an asymmetric key pair. It must 311532b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_DECRYPT. 311632b31808SJens Wiklander * \param alg An asymmetric encryption algorithm that is 311732b31808SJens Wiklander * compatible with the type of \p key. 311832b31808SJens Wiklander * \param[in] input The message to decrypt. 311932b31808SJens Wiklander * \param input_length Size of the \p input buffer in bytes. 312032b31808SJens Wiklander * \param[in] salt A salt or label, if supported by the 312132b31808SJens Wiklander * encryption algorithm. 312232b31808SJens Wiklander * If the algorithm does not support a 312332b31808SJens Wiklander * salt, pass \c NULL. 312432b31808SJens Wiklander * If the algorithm supports an optional 312532b31808SJens Wiklander * salt and you do not want to pass a salt, 312632b31808SJens Wiklander * pass \c NULL. 312732b31808SJens Wiklander * 312832b31808SJens Wiklander * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 312932b31808SJens Wiklander * supported. 313032b31808SJens Wiklander * \param salt_length Size of the \p salt buffer in bytes. 313132b31808SJens Wiklander * If \p salt is \c NULL, pass 0. 313232b31808SJens Wiklander * \param[out] output Buffer where the decrypted message is to 313332b31808SJens Wiklander * be written. 313432b31808SJens Wiklander * \param output_size Size of the \c output buffer in bytes. 313532b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 313632b31808SJens Wiklander * that make up the returned output. 313732b31808SJens Wiklander * 313832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 313932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 314032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 314132b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 314232b31808SJens Wiklander * The size of the \p output buffer is too small. You can 314332b31808SJens Wiklander * determine a sufficient buffer size by calling 314432b31808SJens Wiklander * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 314532b31808SJens Wiklander * where \c key_type and \c key_bits are the type and bit-size 314632b31808SJens Wiklander * respectively of \p key. 314732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 314832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 314932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 315032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 315132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 315232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 315332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 315432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 315532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_PADDING \emptydescription 315632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 315732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 315832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 315932b31808SJens Wiklander * results in this error code. 316032b31808SJens Wiklander */ 316132b31808SJens Wiklander psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, 316232b31808SJens Wiklander psa_algorithm_t alg, 316332b31808SJens Wiklander const uint8_t *input, 316432b31808SJens Wiklander size_t input_length, 316532b31808SJens Wiklander const uint8_t *salt, 316632b31808SJens Wiklander size_t salt_length, 316732b31808SJens Wiklander uint8_t *output, 316832b31808SJens Wiklander size_t output_size, 316932b31808SJens Wiklander size_t *output_length); 317032b31808SJens Wiklander 317132b31808SJens Wiklander /**@}*/ 317232b31808SJens Wiklander 317332b31808SJens Wiklander /** \defgroup key_derivation Key derivation and pseudorandom generation 317432b31808SJens Wiklander * @{ 317532b31808SJens Wiklander */ 317632b31808SJens Wiklander 317732b31808SJens Wiklander /** The type of the state data structure for key derivation operations. 317832b31808SJens Wiklander * 317932b31808SJens Wiklander * Before calling any function on a key derivation operation object, the 318032b31808SJens Wiklander * application must initialize it by any of the following means: 318132b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 318232b31808SJens Wiklander * \code 318332b31808SJens Wiklander * psa_key_derivation_operation_t operation; 318432b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 318532b31808SJens Wiklander * \endcode 318632b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 318732b31808SJens Wiklander * \code 318832b31808SJens Wiklander * psa_key_derivation_operation_t operation = {0}; 318932b31808SJens Wiklander * \endcode 319032b31808SJens Wiklander * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT, 319132b31808SJens Wiklander * for example: 319232b31808SJens Wiklander * \code 319332b31808SJens Wiklander * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; 319432b31808SJens Wiklander * \endcode 319532b31808SJens Wiklander * - Assign the result of the function psa_key_derivation_operation_init() 319632b31808SJens Wiklander * to the structure, for example: 319732b31808SJens Wiklander * \code 319832b31808SJens Wiklander * psa_key_derivation_operation_t operation; 319932b31808SJens Wiklander * operation = psa_key_derivation_operation_init(); 320032b31808SJens Wiklander * \endcode 320132b31808SJens Wiklander * 320232b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 320332b31808SJens Wiklander * make any assumptions about the content of this structure. 320432b31808SJens Wiklander * Implementation details can change in future versions without notice. 320532b31808SJens Wiklander */ 320632b31808SJens Wiklander typedef struct psa_key_derivation_s psa_key_derivation_operation_t; 320732b31808SJens Wiklander 320832b31808SJens Wiklander /** \def PSA_KEY_DERIVATION_OPERATION_INIT 320932b31808SJens Wiklander * 321032b31808SJens Wiklander * This macro returns a suitable initializer for a key derivation operation 321132b31808SJens Wiklander * object of type #psa_key_derivation_operation_t. 321232b31808SJens Wiklander */ 321332b31808SJens Wiklander 321432b31808SJens Wiklander /** Return an initial value for a key derivation operation object. 321532b31808SJens Wiklander */ 321632b31808SJens Wiklander static psa_key_derivation_operation_t psa_key_derivation_operation_init(void); 321732b31808SJens Wiklander 321832b31808SJens Wiklander /** Set up a key derivation operation. 321932b31808SJens Wiklander * 322032b31808SJens Wiklander * A key derivation algorithm takes some inputs and uses them to generate 322132b31808SJens Wiklander * a byte stream in a deterministic way. 322232b31808SJens Wiklander * This byte stream can be used to produce keys and other 322332b31808SJens Wiklander * cryptographic material. 322432b31808SJens Wiklander * 322532b31808SJens Wiklander * To derive a key: 322632b31808SJens Wiklander * -# Start with an initialized object of type #psa_key_derivation_operation_t. 322732b31808SJens Wiklander * -# Call psa_key_derivation_setup() to select the algorithm. 322832b31808SJens Wiklander * -# Provide the inputs for the key derivation by calling 322932b31808SJens Wiklander * psa_key_derivation_input_bytes() or psa_key_derivation_input_key() 323032b31808SJens Wiklander * as appropriate. Which inputs are needed, in what order, and whether 323132b31808SJens Wiklander * they may be keys and if so of what type depends on the algorithm. 323232b31808SJens Wiklander * -# Optionally set the operation's maximum capacity with 323332b31808SJens Wiklander * psa_key_derivation_set_capacity(). You may do this before, in the middle 323432b31808SJens Wiklander * of or after providing inputs. For some algorithms, this step is mandatory 323532b31808SJens Wiklander * because the output depends on the maximum capacity. 3236*b0563631STom Van Eyck * -# To derive a key, call psa_key_derivation_output_key() or 3237*b0563631STom Van Eyck * psa_key_derivation_output_key_ext(). 323832b31808SJens Wiklander * To derive a byte string for a different purpose, call 323932b31808SJens Wiklander * psa_key_derivation_output_bytes(). 324032b31808SJens Wiklander * Successive calls to these functions use successive output bytes 324132b31808SJens Wiklander * calculated by the key derivation algorithm. 324232b31808SJens Wiklander * -# Clean up the key derivation operation object with 324332b31808SJens Wiklander * psa_key_derivation_abort(). 324432b31808SJens Wiklander * 324532b31808SJens Wiklander * If this function returns an error, the key derivation operation object is 324632b31808SJens Wiklander * not changed. 324732b31808SJens Wiklander * 324832b31808SJens Wiklander * If an error occurs at any step after a call to psa_key_derivation_setup(), 324932b31808SJens Wiklander * the operation will need to be reset by a call to psa_key_derivation_abort(). 325032b31808SJens Wiklander * 325132b31808SJens Wiklander * Implementations must reject an attempt to derive a key of size 0. 325232b31808SJens Wiklander * 325332b31808SJens Wiklander * \param[in,out] operation The key derivation operation object 325432b31808SJens Wiklander * to set up. It must 325532b31808SJens Wiklander * have been initialized but not set up yet. 325632b31808SJens Wiklander * \param alg The key derivation algorithm to compute 325732b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 325832b31808SJens Wiklander * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). 325932b31808SJens Wiklander * 326032b31808SJens Wiklander * \retval #PSA_SUCCESS 326132b31808SJens Wiklander * Success. 326232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 326332b31808SJens Wiklander * \c alg is not a key derivation algorithm. 326432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 326532b31808SJens Wiklander * \c alg is not supported or is not a key derivation algorithm. 326632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 326732b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 326832b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 326932b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 327032b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 327132b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 327232b31808SJens Wiklander * The operation state is not valid (it must be inactive), or 327332b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 327432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 327532b31808SJens Wiklander * results in this error code. 327632b31808SJens Wiklander */ 327732b31808SJens Wiklander psa_status_t psa_key_derivation_setup( 327832b31808SJens Wiklander psa_key_derivation_operation_t *operation, 327932b31808SJens Wiklander psa_algorithm_t alg); 328032b31808SJens Wiklander 328132b31808SJens Wiklander /** Retrieve the current capacity of a key derivation operation. 328232b31808SJens Wiklander * 328332b31808SJens Wiklander * The capacity of a key derivation is the maximum number of bytes that it can 328432b31808SJens Wiklander * return. When you get *N* bytes of output from a key derivation operation, 328532b31808SJens Wiklander * this reduces its capacity by *N*. 328632b31808SJens Wiklander * 328732b31808SJens Wiklander * \param[in] operation The operation to query. 328832b31808SJens Wiklander * \param[out] capacity On success, the capacity of the operation. 328932b31808SJens Wiklander * 329032b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 329132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 329232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 329332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 329432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 329532b31808SJens Wiklander * The operation state is not valid (it must be active), or 329632b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 329732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 329832b31808SJens Wiklander * results in this error code. 329932b31808SJens Wiklander */ 330032b31808SJens Wiklander psa_status_t psa_key_derivation_get_capacity( 330132b31808SJens Wiklander const psa_key_derivation_operation_t *operation, 330232b31808SJens Wiklander size_t *capacity); 330332b31808SJens Wiklander 330432b31808SJens Wiklander /** Set the maximum capacity of a key derivation operation. 330532b31808SJens Wiklander * 330632b31808SJens Wiklander * The capacity of a key derivation operation is the maximum number of bytes 330732b31808SJens Wiklander * that the key derivation operation can return from this point onwards. 330832b31808SJens Wiklander * 330932b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to modify. 331032b31808SJens Wiklander * \param capacity The new capacity of the operation. 331132b31808SJens Wiklander * It must be less or equal to the operation's 331232b31808SJens Wiklander * current capacity. 331332b31808SJens Wiklander * 331432b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 331532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 331632b31808SJens Wiklander * \p capacity is larger than the operation's current capacity. 331732b31808SJens Wiklander * In this case, the operation object remains valid and its capacity 331832b31808SJens Wiklander * remains unchanged. 331932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 332032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 332132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 332232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 332332b31808SJens Wiklander * The operation state is not valid (it must be active), or the 332432b31808SJens Wiklander * library has not been previously initialized by psa_crypto_init(). 332532b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 332632b31808SJens Wiklander * results in this error code. 332732b31808SJens Wiklander */ 332832b31808SJens Wiklander psa_status_t psa_key_derivation_set_capacity( 332932b31808SJens Wiklander psa_key_derivation_operation_t *operation, 333032b31808SJens Wiklander size_t capacity); 333132b31808SJens Wiklander 333232b31808SJens Wiklander /** Use the maximum possible capacity for a key derivation operation. 333332b31808SJens Wiklander * 333432b31808SJens Wiklander * Use this value as the capacity argument when setting up a key derivation 333532b31808SJens Wiklander * to indicate that the operation should have the maximum possible capacity. 333632b31808SJens Wiklander * The value of the maximum possible capacity depends on the key derivation 333732b31808SJens Wiklander * algorithm. 333832b31808SJens Wiklander */ 333932b31808SJens Wiklander #define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1)) 334032b31808SJens Wiklander 334132b31808SJens Wiklander /** Provide an input for key derivation or key agreement. 334232b31808SJens Wiklander * 334332b31808SJens Wiklander * Which inputs are required and in what order depends on the algorithm. 334432b31808SJens Wiklander * Refer to the documentation of each key derivation or key agreement 334532b31808SJens Wiklander * algorithm for information. 334632b31808SJens Wiklander * 334732b31808SJens Wiklander * This function passes direct inputs, which is usually correct for 334832b31808SJens Wiklander * non-secret inputs. To pass a secret input, which should be in a key 334932b31808SJens Wiklander * object, call psa_key_derivation_input_key() instead of this function. 335032b31808SJens Wiklander * Refer to the documentation of individual step types 335132b31808SJens Wiklander * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t) 335232b31808SJens Wiklander * for more information. 335332b31808SJens Wiklander * 335432b31808SJens Wiklander * If this function returns an error status, the operation enters an error 335532b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 335632b31808SJens Wiklander * 335732b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to use. 335832b31808SJens Wiklander * It must have been set up with 335932b31808SJens Wiklander * psa_key_derivation_setup() and must not 336032b31808SJens Wiklander * have produced any output yet. 336132b31808SJens Wiklander * \param step Which step the input data is for. 336232b31808SJens Wiklander * \param[in] data Input data to use. 336332b31808SJens Wiklander * \param data_length Size of the \p data buffer in bytes. 336432b31808SJens Wiklander * 336532b31808SJens Wiklander * \retval #PSA_SUCCESS 336632b31808SJens Wiklander * Success. 336732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 336832b31808SJens Wiklander * \c step is not compatible with the operation's algorithm, or 336932b31808SJens Wiklander * \c step does not allow direct inputs. 337032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 337132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 337232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 337332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 337432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 337532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 337632b31808SJens Wiklander * The operation state is not valid for this input \p step, or 337732b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 337832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 337932b31808SJens Wiklander * results in this error code. 338032b31808SJens Wiklander */ 338132b31808SJens Wiklander psa_status_t psa_key_derivation_input_bytes( 338232b31808SJens Wiklander psa_key_derivation_operation_t *operation, 338332b31808SJens Wiklander psa_key_derivation_step_t step, 338432b31808SJens Wiklander const uint8_t *data, 338532b31808SJens Wiklander size_t data_length); 338632b31808SJens Wiklander 338732b31808SJens Wiklander /** Provide a numeric input for key derivation or key agreement. 338832b31808SJens Wiklander * 338932b31808SJens Wiklander * Which inputs are required and in what order depends on the algorithm. 339032b31808SJens Wiklander * However, when an algorithm requires a particular order, numeric inputs 339132b31808SJens Wiklander * usually come first as they tend to be configuration parameters. 339232b31808SJens Wiklander * Refer to the documentation of each key derivation or key agreement 339332b31808SJens Wiklander * algorithm for information. 339432b31808SJens Wiklander * 339532b31808SJens Wiklander * This function is used for inputs which are fixed-size non-negative 339632b31808SJens Wiklander * integers. 339732b31808SJens Wiklander * 339832b31808SJens Wiklander * If this function returns an error status, the operation enters an error 339932b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 340032b31808SJens Wiklander * 340132b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to use. 340232b31808SJens Wiklander * It must have been set up with 340332b31808SJens Wiklander * psa_key_derivation_setup() and must not 340432b31808SJens Wiklander * have produced any output yet. 340532b31808SJens Wiklander * \param step Which step the input data is for. 340632b31808SJens Wiklander * \param[in] value The value of the numeric input. 340732b31808SJens Wiklander * 340832b31808SJens Wiklander * \retval #PSA_SUCCESS 340932b31808SJens Wiklander * Success. 341032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 341132b31808SJens Wiklander * \c step is not compatible with the operation's algorithm, or 341232b31808SJens Wiklander * \c step does not allow numeric inputs. 341332b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 341432b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 341532b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 341632b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 341732b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 341832b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 341932b31808SJens Wiklander * The operation state is not valid for this input \p step, or 342032b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 342132b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 342232b31808SJens Wiklander * results in this error code. 342332b31808SJens Wiklander */ 342432b31808SJens Wiklander psa_status_t psa_key_derivation_input_integer( 342532b31808SJens Wiklander psa_key_derivation_operation_t *operation, 342632b31808SJens Wiklander psa_key_derivation_step_t step, 342732b31808SJens Wiklander uint64_t value); 342832b31808SJens Wiklander 342932b31808SJens Wiklander /** Provide an input for key derivation in the form of a key. 343032b31808SJens Wiklander * 343132b31808SJens Wiklander * Which inputs are required and in what order depends on the algorithm. 343232b31808SJens Wiklander * Refer to the documentation of each key derivation or key agreement 343332b31808SJens Wiklander * algorithm for information. 343432b31808SJens Wiklander * 343532b31808SJens Wiklander * This function obtains input from a key object, which is usually correct for 343632b31808SJens Wiklander * secret inputs or for non-secret personalization strings kept in the key 343732b31808SJens Wiklander * store. To pass a non-secret parameter which is not in the key store, 343832b31808SJens Wiklander * call psa_key_derivation_input_bytes() instead of this function. 343932b31808SJens Wiklander * Refer to the documentation of individual step types 344032b31808SJens Wiklander * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t) 344132b31808SJens Wiklander * for more information. 344232b31808SJens Wiklander * 344332b31808SJens Wiklander * If this function returns an error status, the operation enters an error 344432b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 344532b31808SJens Wiklander * 344632b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to use. 344732b31808SJens Wiklander * It must have been set up with 344832b31808SJens Wiklander * psa_key_derivation_setup() and must not 344932b31808SJens Wiklander * have produced any output yet. 345032b31808SJens Wiklander * \param step Which step the input data is for. 345132b31808SJens Wiklander * \param key Identifier of the key. It must have an 345232b31808SJens Wiklander * appropriate type for step and must allow the 345332b31808SJens Wiklander * usage #PSA_KEY_USAGE_DERIVE or 345432b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note) 345532b31808SJens Wiklander * and the algorithm used by the operation. 345632b31808SJens Wiklander * 345732b31808SJens Wiklander * \note Once all inputs steps are completed, the operations will allow: 345832b31808SJens Wiklander * - psa_key_derivation_output_bytes() if each input was either a direct input 345932b31808SJens Wiklander * or a key with #PSA_KEY_USAGE_DERIVE set; 3460*b0563631STom Van Eyck * - psa_key_derivation_output_key() or psa_key_derivation_output_key_ext() 3461*b0563631STom Van Eyck * if the input for step 346232b31808SJens Wiklander * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD 346332b31808SJens Wiklander * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was 346432b31808SJens Wiklander * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set; 346532b31808SJens Wiklander * - psa_key_derivation_verify_bytes() if each input was either a direct input 346632b31808SJens Wiklander * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set; 346732b31808SJens Wiklander * - psa_key_derivation_verify_key() under the same conditions as 346832b31808SJens Wiklander * psa_key_derivation_verify_bytes(). 346932b31808SJens Wiklander * 347032b31808SJens Wiklander * \retval #PSA_SUCCESS 347132b31808SJens Wiklander * Success. 347232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 347332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 347432b31808SJens Wiklander * The key allows neither #PSA_KEY_USAGE_DERIVE nor 347532b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this 347632b31808SJens Wiklander * algorithm. 347732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 347832b31808SJens Wiklander * \c step is not compatible with the operation's algorithm, or 347932b31808SJens Wiklander * \c step does not allow key inputs of the given type 348032b31808SJens Wiklander * or does not allow key inputs at all. 348132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 348232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 348332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 348432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 348532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 348632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 348732b31808SJens Wiklander * The operation state is not valid for this input \p step, or 348832b31808SJens Wiklander * the library has not been previously initialized by psa_crypto_init(). 348932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 349032b31808SJens Wiklander * results in this error code. 349132b31808SJens Wiklander */ 349232b31808SJens Wiklander psa_status_t psa_key_derivation_input_key( 349332b31808SJens Wiklander psa_key_derivation_operation_t *operation, 349432b31808SJens Wiklander psa_key_derivation_step_t step, 349532b31808SJens Wiklander mbedtls_svc_key_id_t key); 349632b31808SJens Wiklander 349732b31808SJens Wiklander /** Perform a key agreement and use the shared secret as input to a key 349832b31808SJens Wiklander * derivation. 349932b31808SJens Wiklander * 350032b31808SJens Wiklander * A key agreement algorithm takes two inputs: a private key \p private_key 350132b31808SJens Wiklander * a public key \p peer_key. 350232b31808SJens Wiklander * The result of this function is passed as input to a key derivation. 350332b31808SJens Wiklander * The output of this key derivation can be extracted by reading from the 350432b31808SJens Wiklander * resulting operation to produce keys and other cryptographic material. 350532b31808SJens Wiklander * 350632b31808SJens Wiklander * If this function returns an error status, the operation enters an error 350732b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 350832b31808SJens Wiklander * 350932b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to use. 351032b31808SJens Wiklander * It must have been set up with 351132b31808SJens Wiklander * psa_key_derivation_setup() with a 351232b31808SJens Wiklander * key agreement and derivation algorithm 351332b31808SJens Wiklander * \c alg (\c PSA_ALG_XXX value such that 351432b31808SJens Wiklander * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true 351532b31808SJens Wiklander * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg) 351632b31808SJens Wiklander * is false). 351732b31808SJens Wiklander * The operation must be ready for an 351832b31808SJens Wiklander * input of the type given by \p step. 351932b31808SJens Wiklander * \param step Which step the input data is for. 352032b31808SJens Wiklander * \param private_key Identifier of the private key to use. It must 352132b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_DERIVE. 352232b31808SJens Wiklander * \param[in] peer_key Public key of the peer. The peer key must be in the 352332b31808SJens Wiklander * same format that psa_import_key() accepts for the 352432b31808SJens Wiklander * public key type corresponding to the type of 352532b31808SJens Wiklander * private_key. That is, this function performs the 352632b31808SJens Wiklander * equivalent of 352732b31808SJens Wiklander * #psa_import_key(..., 352832b31808SJens Wiklander * `peer_key`, `peer_key_length`) where 352932b31808SJens Wiklander * with key attributes indicating the public key 353032b31808SJens Wiklander * type corresponding to the type of `private_key`. 353132b31808SJens Wiklander * For example, for EC keys, this means that peer_key 353232b31808SJens Wiklander * is interpreted as a point on the curve that the 353332b31808SJens Wiklander * private key is on. The standard formats for public 353432b31808SJens Wiklander * keys are documented in the documentation of 353532b31808SJens Wiklander * psa_export_public_key(). 353632b31808SJens Wiklander * \param peer_key_length Size of \p peer_key in bytes. 353732b31808SJens Wiklander * 353832b31808SJens Wiklander * \retval #PSA_SUCCESS 353932b31808SJens Wiklander * Success. 354032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 354132b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 354232b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 354332b31808SJens Wiklander * \c private_key is not compatible with \c alg, 354432b31808SJens Wiklander * or \p peer_key is not valid for \c alg or not compatible with 354532b31808SJens Wiklander * \c private_key, or \c step does not allow an input resulting 354632b31808SJens Wiklander * from a key agreement. 354732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 354832b31808SJens Wiklander * \c alg is not supported or is not a key derivation algorithm. 354932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 355032b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 355132b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 355232b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 355332b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 355432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 355532b31808SJens Wiklander * The operation state is not valid for this key agreement \p step, 355632b31808SJens Wiklander * or the library has not been previously initialized by psa_crypto_init(). 355732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 355832b31808SJens Wiklander * results in this error code. 355932b31808SJens Wiklander */ 356032b31808SJens Wiklander psa_status_t psa_key_derivation_key_agreement( 356132b31808SJens Wiklander psa_key_derivation_operation_t *operation, 356232b31808SJens Wiklander psa_key_derivation_step_t step, 356332b31808SJens Wiklander mbedtls_svc_key_id_t private_key, 356432b31808SJens Wiklander const uint8_t *peer_key, 356532b31808SJens Wiklander size_t peer_key_length); 356632b31808SJens Wiklander 356732b31808SJens Wiklander /** Read some data from a key derivation operation. 356832b31808SJens Wiklander * 356932b31808SJens Wiklander * This function calculates output bytes from a key derivation algorithm and 357032b31808SJens Wiklander * return those bytes. 357132b31808SJens Wiklander * If you view the key derivation's output as a stream of bytes, this 357232b31808SJens Wiklander * function destructively reads the requested number of bytes from the 357332b31808SJens Wiklander * stream. 357432b31808SJens Wiklander * The operation's capacity decreases by the number of bytes read. 357532b31808SJens Wiklander * 357632b31808SJens Wiklander * If this function returns an error status other than 357732b31808SJens Wiklander * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error 357832b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 357932b31808SJens Wiklander * 358032b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to read from. 358132b31808SJens Wiklander * \param[out] output Buffer where the output will be written. 358232b31808SJens Wiklander * \param output_length Number of bytes to output. 358332b31808SJens Wiklander * 358432b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 358532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 358632b31808SJens Wiklander * One of the inputs was a key whose policy didn't allow 358732b31808SJens Wiklander * #PSA_KEY_USAGE_DERIVE. 358832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_DATA 358932b31808SJens Wiklander * The operation's capacity was less than 359032b31808SJens Wiklander * \p output_length bytes. Note that in this case, 359132b31808SJens Wiklander * no output is written to the output buffer. 359232b31808SJens Wiklander * The operation's capacity is set to 0, thus 359332b31808SJens Wiklander * subsequent calls to this function will not 359432b31808SJens Wiklander * succeed, even with a smaller output buffer. 359532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 359632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 359732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 359832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 359932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 360032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 360132b31808SJens Wiklander * The operation state is not valid (it must be active and completed 360232b31808SJens Wiklander * all required input steps), or the library has not been previously 360332b31808SJens Wiklander * initialized by psa_crypto_init(). 360432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 360532b31808SJens Wiklander * results in this error code. 360632b31808SJens Wiklander */ 360732b31808SJens Wiklander psa_status_t psa_key_derivation_output_bytes( 360832b31808SJens Wiklander psa_key_derivation_operation_t *operation, 360932b31808SJens Wiklander uint8_t *output, 361032b31808SJens Wiklander size_t output_length); 361132b31808SJens Wiklander 361232b31808SJens Wiklander /** Derive a key from an ongoing key derivation operation. 361332b31808SJens Wiklander * 361432b31808SJens Wiklander * This function calculates output bytes from a key derivation algorithm 361532b31808SJens Wiklander * and uses those bytes to generate a key deterministically. 361632b31808SJens Wiklander * The key's location, usage policy, type and size are taken from 361732b31808SJens Wiklander * \p attributes. 361832b31808SJens Wiklander * 361932b31808SJens Wiklander * If you view the key derivation's output as a stream of bytes, this 362032b31808SJens Wiklander * function destructively reads as many bytes as required from the 362132b31808SJens Wiklander * stream. 362232b31808SJens Wiklander * The operation's capacity decreases by the number of bytes read. 362332b31808SJens Wiklander * 362432b31808SJens Wiklander * If this function returns an error status other than 362532b31808SJens Wiklander * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error 362632b31808SJens Wiklander * state and must be aborted by calling psa_key_derivation_abort(). 362732b31808SJens Wiklander * 362832b31808SJens Wiklander * How much output is produced and consumed from the operation, and how 362932b31808SJens Wiklander * the key is derived, depends on the key type and on the key size 363032b31808SJens Wiklander * (denoted \c bits below): 363132b31808SJens Wiklander * 363232b31808SJens Wiklander * - For key types for which the key is an arbitrary sequence of bytes 363332b31808SJens Wiklander * of a given size, this function is functionally equivalent to 363432b31808SJens Wiklander * calling #psa_key_derivation_output_bytes 363532b31808SJens Wiklander * and passing the resulting output to #psa_import_key. 363632b31808SJens Wiklander * However, this function has a security benefit: 363732b31808SJens Wiklander * if the implementation provides an isolation boundary then 363832b31808SJens Wiklander * the key material is not exposed outside the isolation boundary. 363932b31808SJens Wiklander * As a consequence, for these key types, this function always consumes 364032b31808SJens Wiklander * exactly (\c bits / 8) bytes from the operation. 364132b31808SJens Wiklander * The following key types defined in this specification follow this scheme: 364232b31808SJens Wiklander * 364332b31808SJens Wiklander * - #PSA_KEY_TYPE_AES; 364432b31808SJens Wiklander * - #PSA_KEY_TYPE_ARIA; 364532b31808SJens Wiklander * - #PSA_KEY_TYPE_CAMELLIA; 364632b31808SJens Wiklander * - #PSA_KEY_TYPE_DERIVE; 364732b31808SJens Wiklander * - #PSA_KEY_TYPE_HMAC; 364832b31808SJens Wiklander * - #PSA_KEY_TYPE_PASSWORD_HASH. 364932b31808SJens Wiklander * 365032b31808SJens Wiklander * - For ECC keys on a Montgomery elliptic curve 365132b31808SJens Wiklander * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a 365232b31808SJens Wiklander * Montgomery curve), this function always draws a byte string whose 365332b31808SJens Wiklander * length is determined by the curve, and sets the mandatory bits 365432b31808SJens Wiklander * accordingly. That is: 365532b31808SJens Wiklander * 365632b31808SJens Wiklander * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte 365732b31808SJens Wiklander * string and process it as specified in RFC 7748 §5. 365832b31808SJens Wiklander * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte 365932b31808SJens Wiklander * string and process it as specified in RFC 7748 §5. 366032b31808SJens Wiklander * 366132b31808SJens Wiklander * - For key types for which the key is represented by a single sequence of 366232b31808SJens Wiklander * \c bits bits with constraints as to which bit sequences are acceptable, 366332b31808SJens Wiklander * this function draws a byte string of length (\c bits / 8) bytes rounded 366432b31808SJens Wiklander * up to the nearest whole number of bytes. If the resulting byte string 366532b31808SJens Wiklander * is acceptable, it becomes the key, otherwise the drawn bytes are discarded. 366632b31808SJens Wiklander * This process is repeated until an acceptable byte string is drawn. 366732b31808SJens Wiklander * The byte string drawn from the operation is interpreted as specified 366832b31808SJens Wiklander * for the output produced by psa_export_key(). 366932b31808SJens Wiklander * The following key types defined in this specification follow this scheme: 367032b31808SJens Wiklander * 367132b31808SJens Wiklander * - #PSA_KEY_TYPE_DES. 367232b31808SJens Wiklander * Force-set the parity bits, but discard forbidden weak keys. 367332b31808SJens Wiklander * For 2-key and 3-key triple-DES, the three keys are generated 367432b31808SJens Wiklander * successively (for example, for 3-key triple-DES, 367532b31808SJens Wiklander * if the first 8 bytes specify a weak key and the next 8 bytes do not, 367632b31808SJens Wiklander * discard the first 8 bytes, use the next 8 bytes as the first key, 367732b31808SJens Wiklander * and continue reading output from the operation to derive the other 367832b31808SJens Wiklander * two keys). 367932b31808SJens Wiklander * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group) 368032b31808SJens Wiklander * where \c group designates any Diffie-Hellman group) and 368132b31808SJens Wiklander * ECC keys on a Weierstrass elliptic curve 368232b31808SJens Wiklander * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a 368332b31808SJens Wiklander * Weierstrass curve). 368432b31808SJens Wiklander * For these key types, interpret the byte string as integer 368532b31808SJens Wiklander * in big-endian order. Discard it if it is not in the range 368632b31808SJens Wiklander * [0, *N* - 2] where *N* is the boundary of the private key domain 368732b31808SJens Wiklander * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA, 368832b31808SJens Wiklander * or the order of the curve's base point for ECC). 368932b31808SJens Wiklander * Add 1 to the resulting integer and use this as the private key *x*. 369032b31808SJens Wiklander * This method allows compliance to NIST standards, specifically 369132b31808SJens Wiklander * the methods titled "key-pair generation by testing candidates" 369232b31808SJens Wiklander * in NIST SP 800-56A §5.6.1.1.4 for Diffie-Hellman, 369332b31808SJens Wiklander * in FIPS 186-4 §B.1.2 for DSA, and 369432b31808SJens Wiklander * in NIST SP 800-56A §5.6.1.2.2 or 369532b31808SJens Wiklander * FIPS 186-4 §B.4.2 for elliptic curve keys. 369632b31808SJens Wiklander * 369732b31808SJens Wiklander * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR, 369832b31808SJens Wiklander * the way in which the operation output is consumed is 369932b31808SJens Wiklander * implementation-defined. 370032b31808SJens Wiklander * 370132b31808SJens Wiklander * In all cases, the data that is read is discarded from the operation. 370232b31808SJens Wiklander * The operation's capacity is decreased by the number of bytes read. 370332b31808SJens Wiklander * 370432b31808SJens Wiklander * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET, 370532b31808SJens Wiklander * the input to that step must be provided with psa_key_derivation_input_key(). 370632b31808SJens Wiklander * Future versions of this specification may include additional restrictions 370732b31808SJens Wiklander * on the derived key based on the attributes and strength of the secret key. 370832b31808SJens Wiklander * 3709*b0563631STom Van Eyck * \note This function is equivalent to calling 3710*b0563631STom Van Eyck * psa_key_derivation_output_key_ext() 3711*b0563631STom Van Eyck * with the production parameters #PSA_KEY_PRODUCTION_PARAMETERS_INIT 3712*b0563631STom Van Eyck * and `params_data_length == 0` (i.e. `params->data` is empty). 3713*b0563631STom Van Eyck * 371432b31808SJens Wiklander * \param[in] attributes The attributes for the new key. 371532b31808SJens Wiklander * If the key type to be created is 371632b31808SJens Wiklander * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in 371732b31808SJens Wiklander * the policy must be the same as in the current 371832b31808SJens Wiklander * operation. 371932b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to read from. 372032b31808SJens Wiklander * \param[out] key On success, an identifier for the newly created 372132b31808SJens Wiklander * key. For persistent keys, this is the key 372232b31808SJens Wiklander * identifier defined in \p attributes. 372332b31808SJens Wiklander * \c 0 on failure. 372432b31808SJens Wiklander * 372532b31808SJens Wiklander * \retval #PSA_SUCCESS 372632b31808SJens Wiklander * Success. 372732b31808SJens Wiklander * If the key is persistent, the key material and the key's metadata 372832b31808SJens Wiklander * have been saved to persistent storage. 372932b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS 373032b31808SJens Wiklander * This is an attempt to create a persistent key, and there is 373132b31808SJens Wiklander * already a persistent key with the given identifier. 373232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_DATA 373332b31808SJens Wiklander * There was not enough data to create the desired key. 373432b31808SJens Wiklander * Note that in this case, no output is written to the output buffer. 373532b31808SJens Wiklander * The operation's capacity is set to 0, thus subsequent calls to 373632b31808SJens Wiklander * this function will not succeed, even with a smaller output buffer. 373732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 373832b31808SJens Wiklander * The key type or key size is not supported, either by the 373932b31808SJens Wiklander * implementation in general or in this particular location. 374032b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 374132b31808SJens Wiklander * The provided key attributes are not valid for the operation. 374232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 374332b31808SJens Wiklander * The #PSA_KEY_DERIVATION_INPUT_SECRET or 374432b31808SJens Wiklander * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a 374532b31808SJens Wiklander * key; or one of the inputs was a key whose policy didn't allow 374632b31808SJens Wiklander * #PSA_KEY_USAGE_DERIVE. 374732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 374832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 374932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 375032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 375132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 375232b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 375332b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 375432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 375532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 375632b31808SJens Wiklander * The operation state is not valid (it must be active and completed 375732b31808SJens Wiklander * all required input steps), or the library has not been previously 375832b31808SJens Wiklander * initialized by psa_crypto_init(). 375932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 376032b31808SJens Wiklander * results in this error code. 376132b31808SJens Wiklander */ 376232b31808SJens Wiklander psa_status_t psa_key_derivation_output_key( 376332b31808SJens Wiklander const psa_key_attributes_t *attributes, 376432b31808SJens Wiklander psa_key_derivation_operation_t *operation, 376532b31808SJens Wiklander mbedtls_svc_key_id_t *key); 376632b31808SJens Wiklander 3767*b0563631STom Van Eyck /** Derive a key from an ongoing key derivation operation with custom 3768*b0563631STom Van Eyck * production parameters. 3769*b0563631STom Van Eyck * 3770*b0563631STom Van Eyck * See the description of psa_key_derivation_out_key() for the operation of 3771*b0563631STom Van Eyck * this function with the default production parameters. 3772*b0563631STom Van Eyck * Mbed TLS currently does not currently support any non-default production 3773*b0563631STom Van Eyck * parameters. 3774*b0563631STom Van Eyck * 3775*b0563631STom Van Eyck * \note This function is experimental and may change in future minor 3776*b0563631STom Van Eyck * versions of Mbed TLS. 3777*b0563631STom Van Eyck * 3778*b0563631STom Van Eyck * \param[in] attributes The attributes for the new key. 3779*b0563631STom Van Eyck * If the key type to be created is 3780*b0563631STom Van Eyck * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in 3781*b0563631STom Van Eyck * the policy must be the same as in the current 3782*b0563631STom Van Eyck * operation. 3783*b0563631STom Van Eyck * \param[in,out] operation The key derivation operation object to read from. 3784*b0563631STom Van Eyck * \param[in] params Customization parameters for the key derivation. 3785*b0563631STom Van Eyck * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT 3786*b0563631STom Van Eyck * with \p params_data_length = 0, 3787*b0563631STom Van Eyck * this function is equivalent to 3788*b0563631STom Van Eyck * psa_key_derivation_output_key(). 3789*b0563631STom Van Eyck * Mbed TLS currently only supports the default 3790*b0563631STom Van Eyck * production parameters, i.e. 3791*b0563631STom Van Eyck * #PSA_KEY_PRODUCTION_PARAMETERS_INIT, 3792*b0563631STom Van Eyck * for all key types. 3793*b0563631STom Van Eyck * \param params_data_length 3794*b0563631STom Van Eyck * Length of `params->data` in bytes. 3795*b0563631STom Van Eyck * \param[out] key On success, an identifier for the newly created 3796*b0563631STom Van Eyck * key. For persistent keys, this is the key 3797*b0563631STom Van Eyck * identifier defined in \p attributes. 3798*b0563631STom Van Eyck * \c 0 on failure. 3799*b0563631STom Van Eyck * 3800*b0563631STom Van Eyck * \retval #PSA_SUCCESS 3801*b0563631STom Van Eyck * Success. 3802*b0563631STom Van Eyck * If the key is persistent, the key material and the key's metadata 3803*b0563631STom Van Eyck * have been saved to persistent storage. 3804*b0563631STom Van Eyck * \retval #PSA_ERROR_ALREADY_EXISTS 3805*b0563631STom Van Eyck * This is an attempt to create a persistent key, and there is 3806*b0563631STom Van Eyck * already a persistent key with the given identifier. 3807*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_DATA 3808*b0563631STom Van Eyck * There was not enough data to create the desired key. 3809*b0563631STom Van Eyck * Note that in this case, no output is written to the output buffer. 3810*b0563631STom Van Eyck * The operation's capacity is set to 0, thus subsequent calls to 3811*b0563631STom Van Eyck * this function will not succeed, even with a smaller output buffer. 3812*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED 3813*b0563631STom Van Eyck * The key type or key size is not supported, either by the 3814*b0563631STom Van Eyck * implementation in general or in this particular location. 3815*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 3816*b0563631STom Van Eyck * The provided key attributes are not valid for the operation. 3817*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_PERMITTED 3818*b0563631STom Van Eyck * The #PSA_KEY_DERIVATION_INPUT_SECRET or 3819*b0563631STom Van Eyck * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a 3820*b0563631STom Van Eyck * key; or one of the inputs was a key whose policy didn't allow 3821*b0563631STom Van Eyck * #PSA_KEY_USAGE_DERIVE. 3822*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 3823*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 3824*b0563631STom Van Eyck * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 3825*b0563631STom Van Eyck * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 3826*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 3827*b0563631STom Van Eyck * \retval #PSA_ERROR_DATA_INVALID \emptydescription 3828*b0563631STom Van Eyck * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 3829*b0563631STom Van Eyck * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 3830*b0563631STom Van Eyck * \retval #PSA_ERROR_BAD_STATE 3831*b0563631STom Van Eyck * The operation state is not valid (it must be active and completed 3832*b0563631STom Van Eyck * all required input steps), or the library has not been previously 3833*b0563631STom Van Eyck * initialized by psa_crypto_init(). 3834*b0563631STom Van Eyck * It is implementation-dependent whether a failure to initialize 3835*b0563631STom Van Eyck * results in this error code. 3836*b0563631STom Van Eyck */ 3837*b0563631STom Van Eyck psa_status_t psa_key_derivation_output_key_ext( 3838*b0563631STom Van Eyck const psa_key_attributes_t *attributes, 3839*b0563631STom Van Eyck psa_key_derivation_operation_t *operation, 3840*b0563631STom Van Eyck const psa_key_production_parameters_t *params, 3841*b0563631STom Van Eyck size_t params_data_length, 3842*b0563631STom Van Eyck mbedtls_svc_key_id_t *key); 3843*b0563631STom Van Eyck 384432b31808SJens Wiklander /** Compare output data from a key derivation operation to an expected value. 384532b31808SJens Wiklander * 384632b31808SJens Wiklander * This function calculates output bytes from a key derivation algorithm and 384732b31808SJens Wiklander * compares those bytes to an expected value in constant time. 384832b31808SJens Wiklander * If you view the key derivation's output as a stream of bytes, this 384932b31808SJens Wiklander * function destructively reads the expected number of bytes from the 385032b31808SJens Wiklander * stream before comparing them. 385132b31808SJens Wiklander * The operation's capacity decreases by the number of bytes read. 385232b31808SJens Wiklander * 385332b31808SJens Wiklander * This is functionally equivalent to the following code: 385432b31808SJens Wiklander * \code 385532b31808SJens Wiklander * psa_key_derivation_output_bytes(operation, tmp, output_length); 385632b31808SJens Wiklander * if (memcmp(output, tmp, output_length) != 0) 385732b31808SJens Wiklander * return PSA_ERROR_INVALID_SIGNATURE; 385832b31808SJens Wiklander * \endcode 385932b31808SJens Wiklander * except (1) it works even if the key's policy does not allow outputting the 386032b31808SJens Wiklander * bytes, and (2) the comparison will be done in constant time. 386132b31808SJens Wiklander * 386232b31808SJens Wiklander * If this function returns an error status other than 386332b31808SJens Wiklander * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE, 386432b31808SJens Wiklander * the operation enters an error state and must be aborted by calling 386532b31808SJens Wiklander * psa_key_derivation_abort(). 386632b31808SJens Wiklander * 386732b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to read from. 386832b31808SJens Wiklander * \param[in] expected_output Buffer containing the expected derivation output. 386932b31808SJens Wiklander * \param output_length Length of the expected output; this is also the 387032b31808SJens Wiklander * number of bytes that will be read. 387132b31808SJens Wiklander * 387232b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 387332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 387432b31808SJens Wiklander * The output was read successfully, but it differs from the expected 387532b31808SJens Wiklander * output. 387632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 387732b31808SJens Wiklander * One of the inputs was a key whose policy didn't allow 387832b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_DERIVATION. 387932b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_DATA 388032b31808SJens Wiklander * The operation's capacity was less than 388132b31808SJens Wiklander * \p output_length bytes. Note that in this case, 388232b31808SJens Wiklander * the operation's capacity is set to 0, thus 388332b31808SJens Wiklander * subsequent calls to this function will not 388432b31808SJens Wiklander * succeed, even with a smaller expected output. 388532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 388632b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 388732b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 388832b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 388932b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 389032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 389132b31808SJens Wiklander * The operation state is not valid (it must be active and completed 389232b31808SJens Wiklander * all required input steps), or the library has not been previously 389332b31808SJens Wiklander * initialized by psa_crypto_init(). 389432b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 389532b31808SJens Wiklander * results in this error code. 389632b31808SJens Wiklander */ 389732b31808SJens Wiklander psa_status_t psa_key_derivation_verify_bytes( 389832b31808SJens Wiklander psa_key_derivation_operation_t *operation, 389932b31808SJens Wiklander const uint8_t *expected_output, 390032b31808SJens Wiklander size_t output_length); 390132b31808SJens Wiklander 390232b31808SJens Wiklander /** Compare output data from a key derivation operation to an expected value 390332b31808SJens Wiklander * stored in a key object. 390432b31808SJens Wiklander * 390532b31808SJens Wiklander * This function calculates output bytes from a key derivation algorithm and 390632b31808SJens Wiklander * compares those bytes to an expected value, provided as key of type 390732b31808SJens Wiklander * #PSA_KEY_TYPE_PASSWORD_HASH. 390832b31808SJens Wiklander * If you view the key derivation's output as a stream of bytes, this 390932b31808SJens Wiklander * function destructively reads the number of bytes corresponding to the 391032b31808SJens Wiklander * length of the expected value from the stream before comparing them. 391132b31808SJens Wiklander * The operation's capacity decreases by the number of bytes read. 391232b31808SJens Wiklander * 391332b31808SJens Wiklander * This is functionally equivalent to exporting the key and calling 391432b31808SJens Wiklander * psa_key_derivation_verify_bytes() on the result, except that it 391532b31808SJens Wiklander * works even if the key cannot be exported. 391632b31808SJens Wiklander * 391732b31808SJens Wiklander * If this function returns an error status other than 391832b31808SJens Wiklander * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE, 391932b31808SJens Wiklander * the operation enters an error state and must be aborted by calling 392032b31808SJens Wiklander * psa_key_derivation_abort(). 392132b31808SJens Wiklander * 392232b31808SJens Wiklander * \param[in,out] operation The key derivation operation object to read from. 392332b31808SJens Wiklander * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH 392432b31808SJens Wiklander * containing the expected output. Its policy must 392532b31808SJens Wiklander * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag 392632b31808SJens Wiklander * and the permitted algorithm must match the 392732b31808SJens Wiklander * operation. The value of this key was likely 392832b31808SJens Wiklander * computed by a previous call to 3929*b0563631STom Van Eyck * psa_key_derivation_output_key() or 3930*b0563631STom Van Eyck * psa_key_derivation_output_key_ext(). 393132b31808SJens Wiklander * 393232b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 393332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 393432b31808SJens Wiklander * The output was read successfully, but if differs from the expected 393532b31808SJens Wiklander * output. 393632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE 393732b31808SJens Wiklander * The key passed as the expected value does not exist. 393832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 393932b31808SJens Wiklander * The key passed as the expected value has an invalid type. 394032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 394132b31808SJens Wiklander * The key passed as the expected value does not allow this usage or 394232b31808SJens Wiklander * this algorithm; or one of the inputs was a key whose policy didn't 394332b31808SJens Wiklander * allow #PSA_KEY_USAGE_VERIFY_DERIVATION. 394432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_DATA 394532b31808SJens Wiklander * The operation's capacity was less than 394632b31808SJens Wiklander * the length of the expected value. In this case, 394732b31808SJens Wiklander * the operation's capacity is set to 0, thus 394832b31808SJens Wiklander * subsequent calls to this function will not 394932b31808SJens Wiklander * succeed, even with a smaller expected output. 395032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 395132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 395232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 395332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 395432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 395532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 395632b31808SJens Wiklander * The operation state is not valid (it must be active and completed 395732b31808SJens Wiklander * all required input steps), or the library has not been previously 395832b31808SJens Wiklander * initialized by psa_crypto_init(). 395932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 396032b31808SJens Wiklander * results in this error code. 396132b31808SJens Wiklander */ 396232b31808SJens Wiklander psa_status_t psa_key_derivation_verify_key( 396332b31808SJens Wiklander psa_key_derivation_operation_t *operation, 396432b31808SJens Wiklander psa_key_id_t expected); 396532b31808SJens Wiklander 396632b31808SJens Wiklander /** Abort a key derivation operation. 396732b31808SJens Wiklander * 396832b31808SJens Wiklander * Aborting an operation frees all associated resources except for the \c 396932b31808SJens Wiklander * operation structure itself. Once aborted, the operation object can be reused 397032b31808SJens Wiklander * for another operation by calling psa_key_derivation_setup() again. 397132b31808SJens Wiklander * 397232b31808SJens Wiklander * This function may be called at any time after the operation 397332b31808SJens Wiklander * object has been initialized as described in #psa_key_derivation_operation_t. 397432b31808SJens Wiklander * 397532b31808SJens Wiklander * In particular, it is valid to call psa_key_derivation_abort() twice, or to 397632b31808SJens Wiklander * call psa_key_derivation_abort() on an operation that has not been set up. 397732b31808SJens Wiklander * 397832b31808SJens Wiklander * \param[in,out] operation The operation to abort. 397932b31808SJens Wiklander * 398032b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 398132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 398232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 398332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 398432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 398532b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 398632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 398732b31808SJens Wiklander * results in this error code. 398832b31808SJens Wiklander */ 398932b31808SJens Wiklander psa_status_t psa_key_derivation_abort( 399032b31808SJens Wiklander psa_key_derivation_operation_t *operation); 399132b31808SJens Wiklander 399232b31808SJens Wiklander /** Perform a key agreement and return the raw shared secret. 399332b31808SJens Wiklander * 399432b31808SJens Wiklander * \warning The raw result of a key agreement algorithm such as finite-field 399532b31808SJens Wiklander * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should 399632b31808SJens Wiklander * not be used directly as key material. It should instead be passed as 399732b31808SJens Wiklander * input to a key derivation algorithm. To chain a key agreement with 399832b31808SJens Wiklander * a key derivation, use psa_key_derivation_key_agreement() and other 399932b31808SJens Wiklander * functions from the key derivation interface. 400032b31808SJens Wiklander * 400132b31808SJens Wiklander * \param alg The key agreement algorithm to compute 400232b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 400332b31808SJens Wiklander * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg) 400432b31808SJens Wiklander * is true). 400532b31808SJens Wiklander * \param private_key Identifier of the private key to use. It must 400632b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_DERIVE. 400732b31808SJens Wiklander * \param[in] peer_key Public key of the peer. It must be 400832b31808SJens Wiklander * in the same format that psa_import_key() 400932b31808SJens Wiklander * accepts. The standard formats for public 401032b31808SJens Wiklander * keys are documented in the documentation 401132b31808SJens Wiklander * of psa_export_public_key(). 401232b31808SJens Wiklander * \param peer_key_length Size of \p peer_key in bytes. 401332b31808SJens Wiklander * \param[out] output Buffer where the decrypted message is to 401432b31808SJens Wiklander * be written. 401532b31808SJens Wiklander * \param output_size Size of the \c output buffer in bytes. 401632b31808SJens Wiklander * \param[out] output_length On success, the number of bytes 401732b31808SJens Wiklander * that make up the returned output. 401832b31808SJens Wiklander * 401932b31808SJens Wiklander * \retval #PSA_SUCCESS 402032b31808SJens Wiklander * Success. 402132b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 402232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 402332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 402432b31808SJens Wiklander * \p alg is not a key agreement algorithm, or 402532b31808SJens Wiklander * \p private_key is not compatible with \p alg, 402632b31808SJens Wiklander * or \p peer_key is not valid for \p alg or not compatible with 402732b31808SJens Wiklander * \p private_key. 402832b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 402932b31808SJens Wiklander * \p output_size is too small 403032b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED 403132b31808SJens Wiklander * \p alg is not a supported key agreement algorithm. 403232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 403332b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 403432b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 403532b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 403632b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 403732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 403832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 403932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 404032b31808SJens Wiklander * results in this error code. 404132b31808SJens Wiklander */ 404232b31808SJens Wiklander psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, 404332b31808SJens Wiklander mbedtls_svc_key_id_t private_key, 404432b31808SJens Wiklander const uint8_t *peer_key, 404532b31808SJens Wiklander size_t peer_key_length, 404632b31808SJens Wiklander uint8_t *output, 404732b31808SJens Wiklander size_t output_size, 404832b31808SJens Wiklander size_t *output_length); 404932b31808SJens Wiklander 405032b31808SJens Wiklander /**@}*/ 405132b31808SJens Wiklander 405232b31808SJens Wiklander /** \defgroup random Random generation 405332b31808SJens Wiklander * @{ 405432b31808SJens Wiklander */ 405532b31808SJens Wiklander 405632b31808SJens Wiklander /** 405732b31808SJens Wiklander * \brief Generate random bytes. 405832b31808SJens Wiklander * 405932b31808SJens Wiklander * \warning This function **can** fail! Callers MUST check the return status 406032b31808SJens Wiklander * and MUST NOT use the content of the output buffer if the return 406132b31808SJens Wiklander * status is not #PSA_SUCCESS. 406232b31808SJens Wiklander * 406332b31808SJens Wiklander * \note To generate a key, use psa_generate_key() instead. 406432b31808SJens Wiklander * 406532b31808SJens Wiklander * \param[out] output Output buffer for the generated data. 406632b31808SJens Wiklander * \param output_size Number of bytes to generate and output. 406732b31808SJens Wiklander * 406832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 406932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 407032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 407132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 407232b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 407332b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 407432b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 407532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 407632b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 407732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 407832b31808SJens Wiklander * results in this error code. 407932b31808SJens Wiklander */ 408032b31808SJens Wiklander psa_status_t psa_generate_random(uint8_t *output, 408132b31808SJens Wiklander size_t output_size); 408232b31808SJens Wiklander 408332b31808SJens Wiklander /** 408432b31808SJens Wiklander * \brief Generate a key or key pair. 408532b31808SJens Wiklander * 408632b31808SJens Wiklander * The key is generated randomly. 408732b31808SJens Wiklander * Its location, usage policy, type and size are taken from \p attributes. 408832b31808SJens Wiklander * 408932b31808SJens Wiklander * Implementations must reject an attempt to generate a key of size 0. 409032b31808SJens Wiklander * 409132b31808SJens Wiklander * The following type-specific considerations apply: 409232b31808SJens Wiklander * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR), 409332b31808SJens Wiklander * the public exponent is 65537. 409432b31808SJens Wiklander * The modulus is a product of two probabilistic primes 409532b31808SJens Wiklander * between 2^{n-1} and 2^n where n is the bit size specified in the 409632b31808SJens Wiklander * attributes. 409732b31808SJens Wiklander * 4098*b0563631STom Van Eyck * \note This function is equivalent to calling psa_generate_key_ext() 4099*b0563631STom Van Eyck * with the production parameters #PSA_KEY_PRODUCTION_PARAMETERS_INIT 4100*b0563631STom Van Eyck * and `params_data_length == 0` (i.e. `params->data` is empty). 4101*b0563631STom Van Eyck * 410232b31808SJens Wiklander * \param[in] attributes The attributes for the new key. 410332b31808SJens Wiklander * \param[out] key On success, an identifier for the newly created 410432b31808SJens Wiklander * key. For persistent keys, this is the key 410532b31808SJens Wiklander * identifier defined in \p attributes. 410632b31808SJens Wiklander * \c 0 on failure. 410732b31808SJens Wiklander * 410832b31808SJens Wiklander * \retval #PSA_SUCCESS 410932b31808SJens Wiklander * Success. 411032b31808SJens Wiklander * If the key is persistent, the key material and the key's metadata 411132b31808SJens Wiklander * have been saved to persistent storage. 411232b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS 411332b31808SJens Wiklander * This is an attempt to create a persistent key, and there is 411432b31808SJens Wiklander * already a persistent key with the given identifier. 411532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 411632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 411732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 411832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 411932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 412032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 412132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 412232b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 412332b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 412432b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 412532b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 412632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 412732b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 412832b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 412932b31808SJens Wiklander * results in this error code. 413032b31808SJens Wiklander */ 413132b31808SJens Wiklander psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, 413232b31808SJens Wiklander mbedtls_svc_key_id_t *key); 413332b31808SJens Wiklander 4134*b0563631STom Van Eyck /** 4135*b0563631STom Van Eyck * \brief Generate a key or key pair using custom production parameters. 4136*b0563631STom Van Eyck * 4137*b0563631STom Van Eyck * See the description of psa_generate_key() for the operation of this 4138*b0563631STom Van Eyck * function with the default production parameters. In addition, this function 4139*b0563631STom Van Eyck * supports the following production customizations, described in more detail 4140*b0563631STom Van Eyck * in the documentation of ::psa_key_production_parameters_t: 4141*b0563631STom Van Eyck * 4142*b0563631STom Van Eyck * - RSA keys: generation with a custom public exponent. 4143*b0563631STom Van Eyck * 4144*b0563631STom Van Eyck * \note This function is experimental and may change in future minor 4145*b0563631STom Van Eyck * versions of Mbed TLS. 4146*b0563631STom Van Eyck * 4147*b0563631STom Van Eyck * \param[in] attributes The attributes for the new key. 4148*b0563631STom Van Eyck * \param[in] params Customization parameters for the key generation. 4149*b0563631STom Van Eyck * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT 4150*b0563631STom Van Eyck * with \p params_data_length = 0, 4151*b0563631STom Van Eyck * this function is equivalent to 4152*b0563631STom Van Eyck * psa_generate_key(). 4153*b0563631STom Van Eyck * \param params_data_length 4154*b0563631STom Van Eyck * Length of `params->data` in bytes. 4155*b0563631STom Van Eyck * \param[out] key On success, an identifier for the newly created 4156*b0563631STom Van Eyck * key. For persistent keys, this is the key 4157*b0563631STom Van Eyck * identifier defined in \p attributes. 4158*b0563631STom Van Eyck * \c 0 on failure. 4159*b0563631STom Van Eyck * 4160*b0563631STom Van Eyck * \retval #PSA_SUCCESS 4161*b0563631STom Van Eyck * Success. 4162*b0563631STom Van Eyck * If the key is persistent, the key material and the key's metadata 4163*b0563631STom Van Eyck * have been saved to persistent storage. 4164*b0563631STom Van Eyck * \retval #PSA_ERROR_ALREADY_EXISTS 4165*b0563631STom Van Eyck * This is an attempt to create a persistent key, and there is 4166*b0563631STom Van Eyck * already a persistent key with the given identifier. 4167*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 4168*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 4169*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 4170*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 4171*b0563631STom Van Eyck * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 4172*b0563631STom Van Eyck * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 4173*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 4174*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 4175*b0563631STom Van Eyck * \retval #PSA_ERROR_DATA_INVALID \emptydescription 4176*b0563631STom Van Eyck * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 4177*b0563631STom Van Eyck * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 4178*b0563631STom Van Eyck * \retval #PSA_ERROR_BAD_STATE 4179*b0563631STom Van Eyck * The library has not been previously initialized by psa_crypto_init(). 4180*b0563631STom Van Eyck * It is implementation-dependent whether a failure to initialize 4181*b0563631STom Van Eyck * results in this error code. 4182*b0563631STom Van Eyck */ 4183*b0563631STom Van Eyck psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes, 4184*b0563631STom Van Eyck const psa_key_production_parameters_t *params, 4185*b0563631STom Van Eyck size_t params_data_length, 4186*b0563631STom Van Eyck mbedtls_svc_key_id_t *key); 4187*b0563631STom Van Eyck 418832b31808SJens Wiklander /**@}*/ 418932b31808SJens Wiklander 419032b31808SJens Wiklander /** \defgroup interruptible_hash Interruptible sign/verify hash 419132b31808SJens Wiklander * @{ 419232b31808SJens Wiklander */ 419332b31808SJens Wiklander 419432b31808SJens Wiklander /** The type of the state data structure for interruptible hash 419532b31808SJens Wiklander * signing operations. 419632b31808SJens Wiklander * 419732b31808SJens Wiklander * Before calling any function on a sign hash operation object, the 419832b31808SJens Wiklander * application must initialize it by any of the following means: 419932b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 420032b31808SJens Wiklander * \code 420132b31808SJens Wiklander * psa_sign_hash_interruptible_operation_t operation; 420232b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 420332b31808SJens Wiklander * \endcode 420432b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 420532b31808SJens Wiklander * \code 420632b31808SJens Wiklander * psa_sign_hash_interruptible_operation_t operation = {0}; 420732b31808SJens Wiklander * \endcode 420832b31808SJens Wiklander * - Initialize the structure to the initializer 420932b31808SJens Wiklander * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example: 421032b31808SJens Wiklander * \code 421132b31808SJens Wiklander * psa_sign_hash_interruptible_operation_t operation = 421232b31808SJens Wiklander * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT; 421332b31808SJens Wiklander * \endcode 421432b31808SJens Wiklander * - Assign the result of the function 421532b31808SJens Wiklander * psa_sign_hash_interruptible_operation_init() to the structure, for 421632b31808SJens Wiklander * example: 421732b31808SJens Wiklander * \code 421832b31808SJens Wiklander * psa_sign_hash_interruptible_operation_t operation; 421932b31808SJens Wiklander * operation = psa_sign_hash_interruptible_operation_init(); 422032b31808SJens Wiklander * \endcode 422132b31808SJens Wiklander * 422232b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 422332b31808SJens Wiklander * make any assumptions about the content of this structure. 422432b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 422532b31808SJens Wiklander typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t; 422632b31808SJens Wiklander 422732b31808SJens Wiklander /** The type of the state data structure for interruptible hash 422832b31808SJens Wiklander * verification operations. 422932b31808SJens Wiklander * 423032b31808SJens Wiklander * Before calling any function on a sign hash operation object, the 423132b31808SJens Wiklander * application must initialize it by any of the following means: 423232b31808SJens Wiklander * - Set the structure to all-bits-zero, for example: 423332b31808SJens Wiklander * \code 423432b31808SJens Wiklander * psa_verify_hash_interruptible_operation_t operation; 423532b31808SJens Wiklander * memset(&operation, 0, sizeof(operation)); 423632b31808SJens Wiklander * \endcode 423732b31808SJens Wiklander * - Initialize the structure to logical zero values, for example: 423832b31808SJens Wiklander * \code 423932b31808SJens Wiklander * psa_verify_hash_interruptible_operation_t operation = {0}; 424032b31808SJens Wiklander * \endcode 424132b31808SJens Wiklander * - Initialize the structure to the initializer 424232b31808SJens Wiklander * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example: 424332b31808SJens Wiklander * \code 424432b31808SJens Wiklander * psa_verify_hash_interruptible_operation_t operation = 424532b31808SJens Wiklander * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT; 424632b31808SJens Wiklander * \endcode 424732b31808SJens Wiklander * - Assign the result of the function 424832b31808SJens Wiklander * psa_verify_hash_interruptible_operation_init() to the structure, for 424932b31808SJens Wiklander * example: 425032b31808SJens Wiklander * \code 425132b31808SJens Wiklander * psa_verify_hash_interruptible_operation_t operation; 425232b31808SJens Wiklander * operation = psa_verify_hash_interruptible_operation_init(); 425332b31808SJens Wiklander * \endcode 425432b31808SJens Wiklander * 425532b31808SJens Wiklander * This is an implementation-defined \c struct. Applications should not 425632b31808SJens Wiklander * make any assumptions about the content of this structure. 425732b31808SJens Wiklander * Implementation details can change in future versions without notice. */ 425832b31808SJens Wiklander typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t; 425932b31808SJens Wiklander 426032b31808SJens Wiklander /** 426132b31808SJens Wiklander * \brief Set the maximum number of ops allowed to be 426232b31808SJens Wiklander * executed by an interruptible function in a 426332b31808SJens Wiklander * single call. 426432b31808SJens Wiklander * 426532b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 426632b31808SJens Wiklander * at any point. It is not bound by the usual 426732b31808SJens Wiklander * interface stability promises. 426832b31808SJens Wiklander * 426932b31808SJens Wiklander * \note The time taken to execute a single op is 427032b31808SJens Wiklander * implementation specific and depends on 427132b31808SJens Wiklander * software, hardware, the algorithm, key type and 427232b31808SJens Wiklander * curve chosen. Even within a single operation, 427332b31808SJens Wiklander * successive ops can take differing amounts of 427432b31808SJens Wiklander * time. The only guarantee is that lower values 427532b31808SJens Wiklander * for \p max_ops means functions will block for a 427632b31808SJens Wiklander * lesser maximum amount of time. The functions 427732b31808SJens Wiklander * \c psa_sign_interruptible_get_num_ops() and 427832b31808SJens Wiklander * \c psa_verify_interruptible_get_num_ops() are 427932b31808SJens Wiklander * provided to help with tuning this value. 428032b31808SJens Wiklander * 428132b31808SJens Wiklander * \note This value defaults to 428232b31808SJens Wiklander * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which 428332b31808SJens Wiklander * means the whole operation will be done in one 428432b31808SJens Wiklander * go, regardless of the number of ops required. 428532b31808SJens Wiklander * 428632b31808SJens Wiklander * \note If more ops are needed to complete a 428732b31808SJens Wiklander * computation, #PSA_OPERATION_INCOMPLETE will be 428832b31808SJens Wiklander * returned by the function performing the 428932b31808SJens Wiklander * computation. It is then the caller's 429032b31808SJens Wiklander * responsibility to either call again with the 429132b31808SJens Wiklander * same operation context until it returns 0 or an 429232b31808SJens Wiklander * error code; or to call the relevant abort 429332b31808SJens Wiklander * function if the answer is no longer required. 429432b31808SJens Wiklander * 429532b31808SJens Wiklander * \note The interpretation of \p max_ops is also 429632b31808SJens Wiklander * implementation defined. On a hard real time 429732b31808SJens Wiklander * system, this can indicate a hard deadline, as a 429832b31808SJens Wiklander * real-time system needs a guarantee of not 429932b31808SJens Wiklander * spending more than X time, however care must be 430032b31808SJens Wiklander * taken in such an implementation to avoid the 430132b31808SJens Wiklander * situation whereby calls just return, not being 430232b31808SJens Wiklander * able to do any actual work within the allotted 430332b31808SJens Wiklander * time. On a non-real-time system, the 430432b31808SJens Wiklander * implementation can be more relaxed, but again 430532b31808SJens Wiklander * whether this number should be interpreted as as 430632b31808SJens Wiklander * hard or soft limit or even whether a less than 430732b31808SJens Wiklander * or equals as regards to ops executed in a 430832b31808SJens Wiklander * single call is implementation defined. 430932b31808SJens Wiklander * 431032b31808SJens Wiklander * \note For keys in local storage when no accelerator 431132b31808SJens Wiklander * driver applies, please see also the 431232b31808SJens Wiklander * documentation for \c mbedtls_ecp_set_max_ops(), 431332b31808SJens Wiklander * which is the internal implementation in these 431432b31808SJens Wiklander * cases. 431532b31808SJens Wiklander * 431632b31808SJens Wiklander * \warning With implementations that interpret this number 431732b31808SJens Wiklander * as a hard limit, setting this number too small 431832b31808SJens Wiklander * may result in an infinite loop, whereby each 431932b31808SJens Wiklander * call results in immediate return with no ops 432032b31808SJens Wiklander * done (as there is not enough time to execute 432132b31808SJens Wiklander * any), and thus no result will ever be achieved. 432232b31808SJens Wiklander * 432332b31808SJens Wiklander * \note This only applies to functions whose 432432b31808SJens Wiklander * documentation mentions they may return 432532b31808SJens Wiklander * #PSA_OPERATION_INCOMPLETE. 432632b31808SJens Wiklander * 432732b31808SJens Wiklander * \param max_ops The maximum number of ops to be executed in a 432832b31808SJens Wiklander * single call. This can be a number from 0 to 432932b31808SJens Wiklander * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0 433032b31808SJens Wiklander * is the least amount of work done per call. 433132b31808SJens Wiklander */ 433232b31808SJens Wiklander void psa_interruptible_set_max_ops(uint32_t max_ops); 433332b31808SJens Wiklander 433432b31808SJens Wiklander /** 433532b31808SJens Wiklander * \brief Get the maximum number of ops allowed to be 433632b31808SJens Wiklander * executed by an interruptible function in a 433732b31808SJens Wiklander * single call. This will return the last 433832b31808SJens Wiklander * value set by 433932b31808SJens Wiklander * \c psa_interruptible_set_max_ops() or 434032b31808SJens Wiklander * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if 434132b31808SJens Wiklander * that function has never been called. 434232b31808SJens Wiklander * 434332b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 434432b31808SJens Wiklander * at any point. It is not bound by the usual 434532b31808SJens Wiklander * interface stability promises. 434632b31808SJens Wiklander * 434732b31808SJens Wiklander * \return Maximum number of ops allowed to be 434832b31808SJens Wiklander * executed by an interruptible function in a 434932b31808SJens Wiklander * single call. 435032b31808SJens Wiklander */ 435132b31808SJens Wiklander uint32_t psa_interruptible_get_max_ops(void); 435232b31808SJens Wiklander 435332b31808SJens Wiklander /** 435432b31808SJens Wiklander * \brief Get the number of ops that a hash signing 435532b31808SJens Wiklander * operation has taken so far. If the operation 435632b31808SJens Wiklander * has completed, then this will represent the 435732b31808SJens Wiklander * number of ops required for the entire 435832b31808SJens Wiklander * operation. After initialization or calling 435932b31808SJens Wiklander * \c psa_sign_hash_interruptible_abort() on 436032b31808SJens Wiklander * the operation, a value of 0 will be returned. 436132b31808SJens Wiklander * 436232b31808SJens Wiklander * \note This interface is guaranteed re-entrant and 436332b31808SJens Wiklander * thus may be called from driver code. 436432b31808SJens Wiklander * 436532b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 436632b31808SJens Wiklander * at any point. It is not bound by the usual 436732b31808SJens Wiklander * interface stability promises. 436832b31808SJens Wiklander * 436932b31808SJens Wiklander * This is a helper provided to help you tune the 437032b31808SJens Wiklander * value passed to \c 437132b31808SJens Wiklander * psa_interruptible_set_max_ops(). 437232b31808SJens Wiklander * 437332b31808SJens Wiklander * \param operation The \c psa_sign_hash_interruptible_operation_t 437432b31808SJens Wiklander * to use. This must be initialized first. 437532b31808SJens Wiklander * 437632b31808SJens Wiklander * \return Number of ops that the operation has taken so 437732b31808SJens Wiklander * far. 437832b31808SJens Wiklander */ 437932b31808SJens Wiklander uint32_t psa_sign_hash_get_num_ops( 438032b31808SJens Wiklander const psa_sign_hash_interruptible_operation_t *operation); 438132b31808SJens Wiklander 438232b31808SJens Wiklander /** 438332b31808SJens Wiklander * \brief Get the number of ops that a hash verification 438432b31808SJens Wiklander * operation has taken so far. If the operation 438532b31808SJens Wiklander * has completed, then this will represent the 438632b31808SJens Wiklander * number of ops required for the entire 438732b31808SJens Wiklander * operation. After initialization or calling \c 438832b31808SJens Wiklander * psa_verify_hash_interruptible_abort() on the 438932b31808SJens Wiklander * operation, a value of 0 will be returned. 439032b31808SJens Wiklander * 439132b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 439232b31808SJens Wiklander * at any point. It is not bound by the usual 439332b31808SJens Wiklander * interface stability promises. 439432b31808SJens Wiklander * 439532b31808SJens Wiklander * This is a helper provided to help you tune the 439632b31808SJens Wiklander * value passed to \c 439732b31808SJens Wiklander * psa_interruptible_set_max_ops(). 439832b31808SJens Wiklander * 439932b31808SJens Wiklander * \param operation The \c 440032b31808SJens Wiklander * psa_verify_hash_interruptible_operation_t to 440132b31808SJens Wiklander * use. This must be initialized first. 440232b31808SJens Wiklander * 440332b31808SJens Wiklander * \return Number of ops that the operation has taken so 440432b31808SJens Wiklander * far. 440532b31808SJens Wiklander */ 440632b31808SJens Wiklander uint32_t psa_verify_hash_get_num_ops( 440732b31808SJens Wiklander const psa_verify_hash_interruptible_operation_t *operation); 440832b31808SJens Wiklander 440932b31808SJens Wiklander /** 441032b31808SJens Wiklander * \brief Start signing a hash or short message with a 441132b31808SJens Wiklander * private key, in an interruptible manner. 441232b31808SJens Wiklander * 441332b31808SJens Wiklander * \see \c psa_sign_hash_complete() 441432b31808SJens Wiklander * 441532b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 441632b31808SJens Wiklander * at any point. It is not bound by the usual 441732b31808SJens Wiklander * interface stability promises. 441832b31808SJens Wiklander * 441932b31808SJens Wiklander * \note This function combined with \c 442032b31808SJens Wiklander * psa_sign_hash_complete() is equivalent to 442132b31808SJens Wiklander * \c psa_sign_hash() but 442232b31808SJens Wiklander * \c psa_sign_hash_complete() can return early and 442332b31808SJens Wiklander * resume according to the limit set with \c 442432b31808SJens Wiklander * psa_interruptible_set_max_ops() to reduce the 442532b31808SJens Wiklander * maximum time spent in a function call. 442632b31808SJens Wiklander * 442732b31808SJens Wiklander * \note Users should call \c psa_sign_hash_complete() 442832b31808SJens Wiklander * repeatedly on the same context after a 442932b31808SJens Wiklander * successful call to this function until \c 443032b31808SJens Wiklander * psa_sign_hash_complete() either returns 0 or an 443132b31808SJens Wiklander * error. \c psa_sign_hash_complete() will return 443232b31808SJens Wiklander * #PSA_OPERATION_INCOMPLETE if there is more work 443332b31808SJens Wiklander * to do. Alternatively users can call 443432b31808SJens Wiklander * \c psa_sign_hash_abort() at any point if they no 443532b31808SJens Wiklander * longer want the result. 443632b31808SJens Wiklander * 443732b31808SJens Wiklander * \note If this function returns an error status, the 443832b31808SJens Wiklander * operation enters an error state and must be 443932b31808SJens Wiklander * aborted by calling \c psa_sign_hash_abort(). 444032b31808SJens Wiklander * 444132b31808SJens Wiklander * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t 444232b31808SJens Wiklander * to use. This must be initialized first. 444332b31808SJens Wiklander * 444432b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 444532b31808SJens Wiklander * It must be an asymmetric key pair. The key must 444632b31808SJens Wiklander * allow the usage #PSA_KEY_USAGE_SIGN_HASH. 444732b31808SJens Wiklander * \param alg A signature algorithm (\c PSA_ALG_XXX 444832b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_HASH(\p alg) 444932b31808SJens Wiklander * is true), that is compatible with 445032b31808SJens Wiklander * the type of \p key. 445132b31808SJens Wiklander * \param[in] hash The hash or message to sign. 445232b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 445332b31808SJens Wiklander * 445432b31808SJens Wiklander * \retval #PSA_SUCCESS 445532b31808SJens Wiklander * The operation started successfully - call \c psa_sign_hash_complete() 445632b31808SJens Wiklander * with the same context to complete the operation 445732b31808SJens Wiklander * 445832b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 445932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 446032b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does 446132b31808SJens Wiklander * not permit the requested algorithm. 446232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 446332b31808SJens Wiklander * An operation has previously been started on this context, and is 446432b31808SJens Wiklander * still in progress. 446532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 446632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 446732b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 446832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 446932b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 447032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 447132b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 447232b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 447332b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 447432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 447532b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 447632b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 447732b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 447832b31808SJens Wiklander * results in this error code. 447932b31808SJens Wiklander */ 448032b31808SJens Wiklander psa_status_t psa_sign_hash_start( 448132b31808SJens Wiklander psa_sign_hash_interruptible_operation_t *operation, 448232b31808SJens Wiklander mbedtls_svc_key_id_t key, psa_algorithm_t alg, 448332b31808SJens Wiklander const uint8_t *hash, size_t hash_length); 448432b31808SJens Wiklander 448532b31808SJens Wiklander /** 448632b31808SJens Wiklander * \brief Continue and eventually complete the action of 448732b31808SJens Wiklander * signing a hash or short message with a private 448832b31808SJens Wiklander * key, in an interruptible manner. 448932b31808SJens Wiklander * 449032b31808SJens Wiklander * \see \c psa_sign_hash_start() 449132b31808SJens Wiklander * 449232b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 449332b31808SJens Wiklander * at any point. It is not bound by the usual 449432b31808SJens Wiklander * interface stability promises. 449532b31808SJens Wiklander * 449632b31808SJens Wiklander * \note This function combined with \c 449732b31808SJens Wiklander * psa_sign_hash_start() is equivalent to 449832b31808SJens Wiklander * \c psa_sign_hash() but this function can return 449932b31808SJens Wiklander * early and resume according to the limit set with 450032b31808SJens Wiklander * \c psa_interruptible_set_max_ops() to reduce the 450132b31808SJens Wiklander * maximum time spent in a function call. 450232b31808SJens Wiklander * 450332b31808SJens Wiklander * \note Users should call this function on the same 450432b31808SJens Wiklander * operation object repeatedly until it either 450532b31808SJens Wiklander * returns 0 or an error. This function will return 450632b31808SJens Wiklander * #PSA_OPERATION_INCOMPLETE if there is more work 450732b31808SJens Wiklander * to do. Alternatively users can call 450832b31808SJens Wiklander * \c psa_sign_hash_abort() at any point if they no 450932b31808SJens Wiklander * longer want the result. 451032b31808SJens Wiklander * 451132b31808SJens Wiklander * \note When this function returns successfully, the 451232b31808SJens Wiklander * operation becomes inactive. If this function 451332b31808SJens Wiklander * returns an error status, the operation enters an 451432b31808SJens Wiklander * error state and must be aborted by calling 451532b31808SJens Wiklander * \c psa_sign_hash_abort(). 451632b31808SJens Wiklander * 451732b31808SJens Wiklander * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t 451832b31808SJens Wiklander * to use. This must be initialized first, and have 451932b31808SJens Wiklander * had \c psa_sign_hash_start() called with it 452032b31808SJens Wiklander * first. 452132b31808SJens Wiklander * 452232b31808SJens Wiklander * \param[out] signature Buffer where the signature is to be written. 452332b31808SJens Wiklander * \param signature_size Size of the \p signature buffer in bytes. This 452432b31808SJens Wiklander * must be appropriate for the selected 452532b31808SJens Wiklander * algorithm and key: 452632b31808SJens Wiklander * - The required signature size is 452732b31808SJens Wiklander * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c 452832b31808SJens Wiklander * key_bits, \c alg) where \c key_type and \c 452932b31808SJens Wiklander * key_bits are the type and bit-size 453032b31808SJens Wiklander * respectively of key. 453132b31808SJens Wiklander * - #PSA_SIGNATURE_MAX_SIZE evaluates to the 453232b31808SJens Wiklander * maximum signature size of any supported 453332b31808SJens Wiklander * signature algorithm. 453432b31808SJens Wiklander * \param[out] signature_length On success, the number of bytes that make up 453532b31808SJens Wiklander * the returned signature value. 453632b31808SJens Wiklander * 453732b31808SJens Wiklander * \retval #PSA_SUCCESS 453832b31808SJens Wiklander * Operation completed successfully 453932b31808SJens Wiklander * 454032b31808SJens Wiklander * \retval #PSA_OPERATION_INCOMPLETE 454132b31808SJens Wiklander * Operation was interrupted due to the setting of \c 454232b31808SJens Wiklander * psa_interruptible_set_max_ops(). There is still work to be done. 454332b31808SJens Wiklander * Call this function again with the same operation object. 454432b31808SJens Wiklander * 454532b31808SJens Wiklander * \retval #PSA_ERROR_BUFFER_TOO_SMALL 454632b31808SJens Wiklander * The size of the \p signature buffer is too small. You can 454732b31808SJens Wiklander * determine a sufficient buffer size by calling 4548*b0563631STom Van Eyck * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg) 454932b31808SJens Wiklander * where \c key_type and \c key_bits are the type and bit-size 4550*b0563631STom Van Eyck * respectively of \c key. 455132b31808SJens Wiklander * 455232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 455332b31808SJens Wiklander * An operation was not previously started on this context via 455432b31808SJens Wiklander * \c psa_sign_hash_start(). 455532b31808SJens Wiklander * 455632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 455732b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 455832b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 455932b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 456032b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 456132b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 456232b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 456332b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 456432b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 456532b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 456632b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 456732b31808SJens Wiklander * The library has either not been previously initialized by 456832b31808SJens Wiklander * psa_crypto_init() or you did not previously call 456932b31808SJens Wiklander * psa_sign_hash_start() with this operation object. It is 457032b31808SJens Wiklander * implementation-dependent whether a failure to initialize results in 457132b31808SJens Wiklander * this error code. 457232b31808SJens Wiklander */ 457332b31808SJens Wiklander psa_status_t psa_sign_hash_complete( 457432b31808SJens Wiklander psa_sign_hash_interruptible_operation_t *operation, 457532b31808SJens Wiklander uint8_t *signature, size_t signature_size, 457632b31808SJens Wiklander size_t *signature_length); 457732b31808SJens Wiklander 457832b31808SJens Wiklander /** 457932b31808SJens Wiklander * \brief Abort a sign hash operation. 458032b31808SJens Wiklander * 458132b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 458232b31808SJens Wiklander * at any point. It is not bound by the usual 458332b31808SJens Wiklander * interface stability promises. 458432b31808SJens Wiklander * 458532b31808SJens Wiklander * \note This function is the only function that clears 458632b31808SJens Wiklander * the number of ops completed as part of the 458732b31808SJens Wiklander * operation. Please ensure you copy this value via 458832b31808SJens Wiklander * \c psa_sign_hash_get_num_ops() if required 458932b31808SJens Wiklander * before calling. 459032b31808SJens Wiklander * 459132b31808SJens Wiklander * \note Aborting an operation frees all associated 459232b31808SJens Wiklander * resources except for the \p operation structure 459332b31808SJens Wiklander * itself. Once aborted, the operation object can 459432b31808SJens Wiklander * be reused for another operation by calling \c 459532b31808SJens Wiklander * psa_sign_hash_start() again. 459632b31808SJens Wiklander * 459732b31808SJens Wiklander * \note You may call this function any time after the 459832b31808SJens Wiklander * operation object has been initialized. In 459932b31808SJens Wiklander * particular, calling \c psa_sign_hash_abort() 460032b31808SJens Wiklander * after the operation has already been terminated 460132b31808SJens Wiklander * by a call to \c psa_sign_hash_abort() or 460232b31808SJens Wiklander * psa_sign_hash_complete() is safe. 460332b31808SJens Wiklander * 460432b31808SJens Wiklander * \param[in,out] operation Initialized sign hash operation. 460532b31808SJens Wiklander * 460632b31808SJens Wiklander * \retval #PSA_SUCCESS 460732b31808SJens Wiklander * The operation was aborted successfully. 460832b31808SJens Wiklander * 460932b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 461032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 461132b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 461232b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 461332b31808SJens Wiklander * results in this error code. 461432b31808SJens Wiklander */ 461532b31808SJens Wiklander psa_status_t psa_sign_hash_abort( 461632b31808SJens Wiklander psa_sign_hash_interruptible_operation_t *operation); 461732b31808SJens Wiklander 461832b31808SJens Wiklander /** 461932b31808SJens Wiklander * \brief Start reading and verifying a hash or short 462032b31808SJens Wiklander * message, in an interruptible manner. 462132b31808SJens Wiklander * 462232b31808SJens Wiklander * \see \c psa_verify_hash_complete() 462332b31808SJens Wiklander * 462432b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 462532b31808SJens Wiklander * at any point. It is not bound by the usual 462632b31808SJens Wiklander * interface stability promises. 462732b31808SJens Wiklander * 462832b31808SJens Wiklander * \note This function combined with \c 462932b31808SJens Wiklander * psa_verify_hash_complete() is equivalent to 463032b31808SJens Wiklander * \c psa_verify_hash() but \c 463132b31808SJens Wiklander * psa_verify_hash_complete() can return early and 463232b31808SJens Wiklander * resume according to the limit set with \c 463332b31808SJens Wiklander * psa_interruptible_set_max_ops() to reduce the 463432b31808SJens Wiklander * maximum time spent in a function. 463532b31808SJens Wiklander * 463632b31808SJens Wiklander * \note Users should call \c psa_verify_hash_complete() 463732b31808SJens Wiklander * repeatedly on the same operation object after a 463832b31808SJens Wiklander * successful call to this function until \c 463932b31808SJens Wiklander * psa_verify_hash_complete() either returns 0 or 464032b31808SJens Wiklander * an error. \c psa_verify_hash_complete() will 464132b31808SJens Wiklander * return #PSA_OPERATION_INCOMPLETE if there is 464232b31808SJens Wiklander * more work to do. Alternatively users can call 464332b31808SJens Wiklander * \c psa_verify_hash_abort() at any point if they 464432b31808SJens Wiklander * no longer want the result. 464532b31808SJens Wiklander * 464632b31808SJens Wiklander * \note If this function returns an error status, the 464732b31808SJens Wiklander * operation enters an error state and must be 464832b31808SJens Wiklander * aborted by calling \c psa_verify_hash_abort(). 464932b31808SJens Wiklander * 465032b31808SJens Wiklander * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t 465132b31808SJens Wiklander * to use. This must be initialized first. 465232b31808SJens Wiklander * 465332b31808SJens Wiklander * \param key Identifier of the key to use for the operation. 465432b31808SJens Wiklander * The key must allow the usage 465532b31808SJens Wiklander * #PSA_KEY_USAGE_VERIFY_HASH. 465632b31808SJens Wiklander * \param alg A signature algorithm (\c PSA_ALG_XXX 465732b31808SJens Wiklander * value such that #PSA_ALG_IS_SIGN_HASH(\p alg) 465832b31808SJens Wiklander * is true), that is compatible with 465932b31808SJens Wiklander * the type of \p key. 466032b31808SJens Wiklander * \param[in] hash The hash whose signature is to be verified. 466132b31808SJens Wiklander * \param hash_length Size of the \p hash buffer in bytes. 466232b31808SJens Wiklander * \param[in] signature Buffer containing the signature to verify. 466332b31808SJens Wiklander * \param signature_length Size of the \p signature buffer in bytes. 466432b31808SJens Wiklander * 466532b31808SJens Wiklander * \retval #PSA_SUCCESS 466632b31808SJens Wiklander * The operation started successfully - please call \c 466732b31808SJens Wiklander * psa_verify_hash_complete() with the same context to complete the 466832b31808SJens Wiklander * operation. 466932b31808SJens Wiklander * 467032b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 467132b31808SJens Wiklander * Another operation has already been started on this context, and is 467232b31808SJens Wiklander * still in progress. 467332b31808SJens Wiklander * 467432b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED 467532b31808SJens Wiklander * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does 467632b31808SJens Wiklander * not permit the requested algorithm. 467732b31808SJens Wiklander * 467832b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 467932b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 468032b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 468132b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 468232b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 468332b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 468432b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 468532b31808SJens Wiklander * \retval PSA_ERROR_DATA_CORRUPT \emptydescription 468632b31808SJens Wiklander * \retval PSA_ERROR_DATA_INVALID \emptydescription 468732b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 468832b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 468932b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 469032b31808SJens Wiklander * results in this error code. 469132b31808SJens Wiklander */ 469232b31808SJens Wiklander psa_status_t psa_verify_hash_start( 469332b31808SJens Wiklander psa_verify_hash_interruptible_operation_t *operation, 469432b31808SJens Wiklander mbedtls_svc_key_id_t key, psa_algorithm_t alg, 469532b31808SJens Wiklander const uint8_t *hash, size_t hash_length, 469632b31808SJens Wiklander const uint8_t *signature, size_t signature_length); 469732b31808SJens Wiklander 469832b31808SJens Wiklander /** 469932b31808SJens Wiklander * \brief Continue and eventually complete the action of 470032b31808SJens Wiklander * reading and verifying a hash or short message 470132b31808SJens Wiklander * signed with a private key, in an interruptible 470232b31808SJens Wiklander * manner. 470332b31808SJens Wiklander * 470432b31808SJens Wiklander * \see \c psa_verify_hash_start() 470532b31808SJens Wiklander * 470632b31808SJens Wiklander * \warning This is a beta API, and thus subject to change 470732b31808SJens Wiklander * at any point. It is not bound by the usual 470832b31808SJens Wiklander * interface stability promises. 470932b31808SJens Wiklander * 471032b31808SJens Wiklander * \note This function combined with \c 471132b31808SJens Wiklander * psa_verify_hash_start() is equivalent to 471232b31808SJens Wiklander * \c psa_verify_hash() but this function can 471332b31808SJens Wiklander * return early and resume according to the limit 471432b31808SJens Wiklander * set with \c psa_interruptible_set_max_ops() to 471532b31808SJens Wiklander * reduce the maximum time spent in a function 471632b31808SJens Wiklander * call. 471732b31808SJens Wiklander * 471832b31808SJens Wiklander * \note Users should call this function on the same 471932b31808SJens Wiklander * operation object repeatedly until it either 472032b31808SJens Wiklander * returns 0 or an error. This function will return 472132b31808SJens Wiklander * #PSA_OPERATION_INCOMPLETE if there is more work 472232b31808SJens Wiklander * to do. Alternatively users can call 472332b31808SJens Wiklander * \c psa_verify_hash_abort() at any point if they 472432b31808SJens Wiklander * no longer want the result. 472532b31808SJens Wiklander * 472632b31808SJens Wiklander * \note When this function returns successfully, the 472732b31808SJens Wiklander * operation becomes inactive. If this function 472832b31808SJens Wiklander * returns an error status, the operation enters an 472932b31808SJens Wiklander * error state and must be aborted by calling 473032b31808SJens Wiklander * \c psa_verify_hash_abort(). 473132b31808SJens Wiklander * 473232b31808SJens Wiklander * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t 473332b31808SJens Wiklander * to use. This must be initialized first, and have 473432b31808SJens Wiklander * had \c psa_verify_hash_start() called with it 473532b31808SJens Wiklander * first. 473632b31808SJens Wiklander * 473732b31808SJens Wiklander * \retval #PSA_SUCCESS 473832b31808SJens Wiklander * Operation completed successfully, and the passed signature is valid. 473932b31808SJens Wiklander * 474032b31808SJens Wiklander * \retval #PSA_OPERATION_INCOMPLETE 474132b31808SJens Wiklander * Operation was interrupted due to the setting of \c 474232b31808SJens Wiklander * psa_interruptible_set_max_ops(). There is still work to be done. 474332b31808SJens Wiklander * Call this function again with the same operation object. 474432b31808SJens Wiklander * 474532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 474632b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 474732b31808SJens Wiklander * The calculation was performed successfully, but the passed 474832b31808SJens Wiklander * signature is not a valid signature. 474932b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 475032b31808SJens Wiklander * An operation was not previously started on this context via 475132b31808SJens Wiklander * \c psa_verify_hash_start(). 475232b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 475332b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 475432b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 475532b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 475632b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 475732b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 475832b31808SJens Wiklander * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 475932b31808SJens Wiklander * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription 476032b31808SJens Wiklander * \retval #PSA_ERROR_DATA_INVALID \emptydescription 476132b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 476232b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 476332b31808SJens Wiklander * The library has either not been previously initialized by 476432b31808SJens Wiklander * psa_crypto_init() or you did not previously call 476532b31808SJens Wiklander * psa_verify_hash_start() on this object. It is 476632b31808SJens Wiklander * implementation-dependent whether a failure to initialize results in 476732b31808SJens Wiklander * this error code. 476832b31808SJens Wiklander */ 476932b31808SJens Wiklander psa_status_t psa_verify_hash_complete( 477032b31808SJens Wiklander psa_verify_hash_interruptible_operation_t *operation); 477132b31808SJens Wiklander 477232b31808SJens Wiklander /** 477332b31808SJens Wiklander * \brief Abort a verify hash operation. 477432b31808SJens Wiklander * 477532b31808SJens Wiklander * \warning This is a beta API, and thus subject to change at 477632b31808SJens Wiklander * any point. It is not bound by the usual interface 477732b31808SJens Wiklander * stability promises. 477832b31808SJens Wiklander * 477932b31808SJens Wiklander * \note This function is the only function that clears the 478032b31808SJens Wiklander * number of ops completed as part of the operation. 478132b31808SJens Wiklander * Please ensure you copy this value via 478232b31808SJens Wiklander * \c psa_verify_hash_get_num_ops() if required 478332b31808SJens Wiklander * before calling. 478432b31808SJens Wiklander * 478532b31808SJens Wiklander * \note Aborting an operation frees all associated 478632b31808SJens Wiklander * resources except for the operation structure 478732b31808SJens Wiklander * itself. Once aborted, the operation object can be 478832b31808SJens Wiklander * reused for another operation by calling \c 478932b31808SJens Wiklander * psa_verify_hash_start() again. 479032b31808SJens Wiklander * 479132b31808SJens Wiklander * \note You may call this function any time after the 479232b31808SJens Wiklander * operation object has been initialized. 479332b31808SJens Wiklander * In particular, calling \c psa_verify_hash_abort() 479432b31808SJens Wiklander * after the operation has already been terminated by 479532b31808SJens Wiklander * a call to \c psa_verify_hash_abort() or 479632b31808SJens Wiklander * psa_verify_hash_complete() is safe. 479732b31808SJens Wiklander * 479832b31808SJens Wiklander * \param[in,out] operation Initialized verify hash operation. 479932b31808SJens Wiklander * 480032b31808SJens Wiklander * \retval #PSA_SUCCESS 480132b31808SJens Wiklander * The operation was aborted successfully. 480232b31808SJens Wiklander * 480332b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 480432b31808SJens Wiklander * \retval #PSA_ERROR_BAD_STATE 480532b31808SJens Wiklander * The library has not been previously initialized by psa_crypto_init(). 480632b31808SJens Wiklander * It is implementation-dependent whether a failure to initialize 480732b31808SJens Wiklander * results in this error code. 480832b31808SJens Wiklander */ 480932b31808SJens Wiklander psa_status_t psa_verify_hash_abort( 481032b31808SJens Wiklander psa_verify_hash_interruptible_operation_t *operation); 481132b31808SJens Wiklander 481232b31808SJens Wiklander 481332b31808SJens Wiklander /**@}*/ 481432b31808SJens Wiklander 481532b31808SJens Wiklander #ifdef __cplusplus 481632b31808SJens Wiklander } 481732b31808SJens Wiklander #endif 481832b31808SJens Wiklander 481932b31808SJens Wiklander /* The file "crypto_sizes.h" contains definitions for size calculation 482032b31808SJens Wiklander * macros whose definitions are implementation-specific. */ 482132b31808SJens Wiklander #include "crypto_sizes.h" 482232b31808SJens Wiklander 482332b31808SJens Wiklander /* The file "crypto_struct.h" contains definitions for 482432b31808SJens Wiklander * implementation-specific structs that are declared above. */ 482532b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE) 482632b31808SJens Wiklander #include MBEDTLS_PSA_CRYPTO_STRUCT_FILE 482732b31808SJens Wiklander #else 482832b31808SJens Wiklander #include "crypto_struct.h" 482932b31808SJens Wiklander #endif 483032b31808SJens Wiklander 483132b31808SJens Wiklander /* The file "crypto_extra.h" contains vendor-specific definitions. This 483232b31808SJens Wiklander * can include vendor-defined algorithms, extra functions, etc. */ 483332b31808SJens Wiklander #include "crypto_extra.h" 483432b31808SJens Wiklander 483532b31808SJens Wiklander #endif /* PSA_CRYPTO_H */ 4836