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