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