xref: /aosp_15_r20/external/mbedtls/include/psa/crypto.h (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf Ebrahimi /**
2*62c56f98SSadaf Ebrahimi  * \file psa/crypto.h
3*62c56f98SSadaf Ebrahimi  * \brief Platform Security Architecture cryptography module
4*62c56f98SSadaf Ebrahimi  */
5*62c56f98SSadaf Ebrahimi /*
6*62c56f98SSadaf Ebrahimi  *  Copyright The Mbed TLS Contributors
7*62c56f98SSadaf Ebrahimi  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8*62c56f98SSadaf Ebrahimi  */
9*62c56f98SSadaf Ebrahimi 
10*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_H
11*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_H
12*62c56f98SSadaf Ebrahimi 
13*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
14*62c56f98SSadaf Ebrahimi #include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
15*62c56f98SSadaf Ebrahimi #else
16*62c56f98SSadaf Ebrahimi #include "crypto_platform.h"
17*62c56f98SSadaf Ebrahimi #endif
18*62c56f98SSadaf Ebrahimi 
19*62c56f98SSadaf Ebrahimi #include <stddef.h>
20*62c56f98SSadaf Ebrahimi 
21*62c56f98SSadaf Ebrahimi #ifdef __DOXYGEN_ONLY__
22*62c56f98SSadaf Ebrahimi /* This __DOXYGEN_ONLY__ block contains mock definitions for things that
23*62c56f98SSadaf Ebrahimi  * must be defined in the crypto_platform.h header. These mock definitions
24*62c56f98SSadaf Ebrahimi  * are present in this file as a convenience to generate pretty-printed
25*62c56f98SSadaf Ebrahimi  * documentation that includes those definitions. */
26*62c56f98SSadaf Ebrahimi 
27*62c56f98SSadaf Ebrahimi /** \defgroup platform Implementation-specific definitions
28*62c56f98SSadaf Ebrahimi  * @{
29*62c56f98SSadaf Ebrahimi  */
30*62c56f98SSadaf Ebrahimi 
31*62c56f98SSadaf Ebrahimi /**@}*/
32*62c56f98SSadaf Ebrahimi #endif /* __DOXYGEN_ONLY__ */
33*62c56f98SSadaf Ebrahimi 
34*62c56f98SSadaf Ebrahimi #ifdef __cplusplus
35*62c56f98SSadaf Ebrahimi extern "C" {
36*62c56f98SSadaf Ebrahimi #endif
37*62c56f98SSadaf Ebrahimi 
38*62c56f98SSadaf Ebrahimi /* The file "crypto_types.h" declares types that encode errors,
39*62c56f98SSadaf Ebrahimi  * algorithms, key types, policies, etc. */
40*62c56f98SSadaf Ebrahimi #include "crypto_types.h"
41*62c56f98SSadaf Ebrahimi 
42*62c56f98SSadaf Ebrahimi /** \defgroup version API version
43*62c56f98SSadaf Ebrahimi  * @{
44*62c56f98SSadaf Ebrahimi  */
45*62c56f98SSadaf Ebrahimi 
46*62c56f98SSadaf Ebrahimi /**
47*62c56f98SSadaf Ebrahimi  * The major version of this implementation of the PSA Crypto API
48*62c56f98SSadaf Ebrahimi  */
49*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_API_VERSION_MAJOR 1
50*62c56f98SSadaf Ebrahimi 
51*62c56f98SSadaf Ebrahimi /**
52*62c56f98SSadaf Ebrahimi  * The minor version of this implementation of the PSA Crypto API
53*62c56f98SSadaf Ebrahimi  */
54*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_API_VERSION_MINOR 0
55*62c56f98SSadaf Ebrahimi 
56*62c56f98SSadaf Ebrahimi /**@}*/
57*62c56f98SSadaf Ebrahimi 
58*62c56f98SSadaf Ebrahimi /* The file "crypto_values.h" declares macros to build and analyze values
59*62c56f98SSadaf Ebrahimi  * of integral types defined in "crypto_types.h". */
60*62c56f98SSadaf Ebrahimi #include "crypto_values.h"
61*62c56f98SSadaf Ebrahimi 
62*62c56f98SSadaf Ebrahimi /** \defgroup initialization Library initialization
63*62c56f98SSadaf Ebrahimi  * @{
64*62c56f98SSadaf Ebrahimi  */
65*62c56f98SSadaf Ebrahimi 
66*62c56f98SSadaf Ebrahimi /**
67*62c56f98SSadaf Ebrahimi  * \brief Library initialization.
68*62c56f98SSadaf Ebrahimi  *
69*62c56f98SSadaf Ebrahimi  * Applications must call this function before calling any other
70*62c56f98SSadaf Ebrahimi  * function in this module.
71*62c56f98SSadaf Ebrahimi  *
72*62c56f98SSadaf Ebrahimi  * Applications may call this function more than once. Once a call
73*62c56f98SSadaf Ebrahimi  * succeeds, subsequent calls are guaranteed to succeed.
74*62c56f98SSadaf Ebrahimi  *
75*62c56f98SSadaf Ebrahimi  * If the application calls other functions before calling psa_crypto_init(),
76*62c56f98SSadaf Ebrahimi  * the behavior is undefined. Implementations are encouraged to either perform
77*62c56f98SSadaf Ebrahimi  * the operation as if the library had been initialized or to return
78*62c56f98SSadaf Ebrahimi  * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
79*62c56f98SSadaf Ebrahimi  * implementations should not return a success status if the lack of
80*62c56f98SSadaf Ebrahimi  * initialization may have security implications, for example due to improper
81*62c56f98SSadaf Ebrahimi  * seeding of the random number generator.
82*62c56f98SSadaf Ebrahimi  *
83*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
84*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
85*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
86*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
87*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
88*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
89*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
90*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
91*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
92*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
93*62c56f98SSadaf Ebrahimi  */
94*62c56f98SSadaf Ebrahimi psa_status_t psa_crypto_init(void);
95*62c56f98SSadaf Ebrahimi 
96*62c56f98SSadaf Ebrahimi /**@}*/
97*62c56f98SSadaf Ebrahimi 
98*62c56f98SSadaf Ebrahimi /** \addtogroup attributes
99*62c56f98SSadaf Ebrahimi  * @{
100*62c56f98SSadaf Ebrahimi  */
101*62c56f98SSadaf Ebrahimi 
102*62c56f98SSadaf Ebrahimi /** \def PSA_KEY_ATTRIBUTES_INIT
103*62c56f98SSadaf Ebrahimi  *
104*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for a key attribute structure
105*62c56f98SSadaf Ebrahimi  * of type #psa_key_attributes_t.
106*62c56f98SSadaf Ebrahimi  */
107*62c56f98SSadaf Ebrahimi 
108*62c56f98SSadaf Ebrahimi /** Return an initial value for a key attributes structure.
109*62c56f98SSadaf Ebrahimi  */
110*62c56f98SSadaf Ebrahimi static psa_key_attributes_t psa_key_attributes_init(void);
111*62c56f98SSadaf Ebrahimi 
112*62c56f98SSadaf Ebrahimi /** Declare a key as persistent and set its key identifier.
113*62c56f98SSadaf Ebrahimi  *
114*62c56f98SSadaf Ebrahimi  * If the attribute structure currently declares the key as volatile (which
115*62c56f98SSadaf Ebrahimi  * is the default content of an attribute structure), this function sets
116*62c56f98SSadaf Ebrahimi  * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
117*62c56f98SSadaf Ebrahimi  *
118*62c56f98SSadaf Ebrahimi  * This function does not access storage, it merely stores the given
119*62c56f98SSadaf Ebrahimi  * value in the structure.
120*62c56f98SSadaf Ebrahimi  * The persistent key will be written to storage when the attribute
121*62c56f98SSadaf Ebrahimi  * structure is passed to a key creation function such as
122*62c56f98SSadaf Ebrahimi  * psa_import_key(), psa_generate_key(),
123*62c56f98SSadaf Ebrahimi  * psa_key_derivation_output_key() or psa_copy_key().
124*62c56f98SSadaf Ebrahimi  *
125*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
126*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
127*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
128*62c56f98SSadaf Ebrahimi  *
129*62c56f98SSadaf Ebrahimi  * \param[out] attributes  The attribute structure to write to.
130*62c56f98SSadaf Ebrahimi  * \param key              The persistent identifier for the key.
131*62c56f98SSadaf Ebrahimi  */
132*62c56f98SSadaf Ebrahimi static void psa_set_key_id(psa_key_attributes_t *attributes,
133*62c56f98SSadaf Ebrahimi                            mbedtls_svc_key_id_t key);
134*62c56f98SSadaf Ebrahimi 
135*62c56f98SSadaf Ebrahimi #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
136*62c56f98SSadaf Ebrahimi /** Set the owner identifier of a key.
137*62c56f98SSadaf Ebrahimi  *
138*62c56f98SSadaf Ebrahimi  * When key identifiers encode key owner identifiers, psa_set_key_id() does
139*62c56f98SSadaf Ebrahimi  * not allow to define in key attributes the owner of volatile keys as
140*62c56f98SSadaf Ebrahimi  * psa_set_key_id() enforces the key to be persistent.
141*62c56f98SSadaf Ebrahimi  *
142*62c56f98SSadaf Ebrahimi  * This function allows to set in key attributes the owner identifier of a
143*62c56f98SSadaf Ebrahimi  * key. It is intended to be used for volatile keys. For persistent keys,
144*62c56f98SSadaf Ebrahimi  * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
145*62c56f98SSadaf Ebrahimi  * the owner of a key.
146*62c56f98SSadaf Ebrahimi  *
147*62c56f98SSadaf Ebrahimi  * \param[out] attributes  The attribute structure to write to.
148*62c56f98SSadaf Ebrahimi  * \param owner            The key owner identifier.
149*62c56f98SSadaf Ebrahimi  */
150*62c56f98SSadaf Ebrahimi static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
151*62c56f98SSadaf Ebrahimi                                      mbedtls_key_owner_id_t owner);
152*62c56f98SSadaf Ebrahimi #endif
153*62c56f98SSadaf Ebrahimi 
154*62c56f98SSadaf Ebrahimi /** Set the location of a persistent key.
155*62c56f98SSadaf Ebrahimi  *
156*62c56f98SSadaf Ebrahimi  * To make a key persistent, you must give it a persistent key identifier
157*62c56f98SSadaf Ebrahimi  * with psa_set_key_id(). By default, a key that has a persistent identifier
158*62c56f98SSadaf Ebrahimi  * is stored in the default storage area identifier by
159*62c56f98SSadaf Ebrahimi  * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
160*62c56f98SSadaf Ebrahimi  * area, or to explicitly declare the key as volatile.
161*62c56f98SSadaf Ebrahimi  *
162*62c56f98SSadaf Ebrahimi  * This function does not access storage, it merely stores the given
163*62c56f98SSadaf Ebrahimi  * value in the structure.
164*62c56f98SSadaf Ebrahimi  * The persistent key will be written to storage when the attribute
165*62c56f98SSadaf Ebrahimi  * structure is passed to a key creation function such as
166*62c56f98SSadaf Ebrahimi  * psa_import_key(), psa_generate_key(),
167*62c56f98SSadaf Ebrahimi  * psa_key_derivation_output_key() or psa_copy_key().
168*62c56f98SSadaf Ebrahimi  *
169*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
170*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
171*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
172*62c56f98SSadaf Ebrahimi  *
173*62c56f98SSadaf Ebrahimi  * \param[out] attributes       The attribute structure to write to.
174*62c56f98SSadaf Ebrahimi  * \param lifetime              The lifetime for the key.
175*62c56f98SSadaf Ebrahimi  *                              If this is #PSA_KEY_LIFETIME_VOLATILE, the
176*62c56f98SSadaf Ebrahimi  *                              key will be volatile, and the key identifier
177*62c56f98SSadaf Ebrahimi  *                              attribute is reset to 0.
178*62c56f98SSadaf Ebrahimi  */
179*62c56f98SSadaf Ebrahimi static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
180*62c56f98SSadaf Ebrahimi                                  psa_key_lifetime_t lifetime);
181*62c56f98SSadaf Ebrahimi 
182*62c56f98SSadaf Ebrahimi /** Retrieve the key identifier from key attributes.
183*62c56f98SSadaf Ebrahimi  *
184*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
185*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
186*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
187*62c56f98SSadaf Ebrahimi  *
188*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
189*62c56f98SSadaf Ebrahimi  *
190*62c56f98SSadaf Ebrahimi  * \return The persistent identifier stored in the attribute structure.
191*62c56f98SSadaf Ebrahimi  *         This value is unspecified if the attribute structure declares
192*62c56f98SSadaf Ebrahimi  *         the key as volatile.
193*62c56f98SSadaf Ebrahimi  */
194*62c56f98SSadaf Ebrahimi static mbedtls_svc_key_id_t psa_get_key_id(
195*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes);
196*62c56f98SSadaf Ebrahimi 
197*62c56f98SSadaf Ebrahimi /** Retrieve the lifetime from key attributes.
198*62c56f98SSadaf Ebrahimi  *
199*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
200*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
201*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
202*62c56f98SSadaf Ebrahimi  *
203*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
204*62c56f98SSadaf Ebrahimi  *
205*62c56f98SSadaf Ebrahimi  * \return The lifetime value stored in the attribute structure.
206*62c56f98SSadaf Ebrahimi  */
207*62c56f98SSadaf Ebrahimi static psa_key_lifetime_t psa_get_key_lifetime(
208*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes);
209*62c56f98SSadaf Ebrahimi 
210*62c56f98SSadaf Ebrahimi /** Declare usage flags for a key.
211*62c56f98SSadaf Ebrahimi  *
212*62c56f98SSadaf Ebrahimi  * Usage flags are part of a key's usage policy. They encode what
213*62c56f98SSadaf Ebrahimi  * kind of operations are permitted on the key. For more details,
214*62c56f98SSadaf Ebrahimi  * refer to the documentation of the type #psa_key_usage_t.
215*62c56f98SSadaf Ebrahimi  *
216*62c56f98SSadaf Ebrahimi  * This function overwrites any usage flags
217*62c56f98SSadaf Ebrahimi  * previously set in \p attributes.
218*62c56f98SSadaf Ebrahimi  *
219*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
220*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
221*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
222*62c56f98SSadaf Ebrahimi  *
223*62c56f98SSadaf Ebrahimi  * \param[out] attributes       The attribute structure to write to.
224*62c56f98SSadaf Ebrahimi  * \param usage_flags           The usage flags to write.
225*62c56f98SSadaf Ebrahimi  */
226*62c56f98SSadaf Ebrahimi static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
227*62c56f98SSadaf Ebrahimi                                     psa_key_usage_t usage_flags);
228*62c56f98SSadaf Ebrahimi 
229*62c56f98SSadaf Ebrahimi /** Retrieve the usage flags from key attributes.
230*62c56f98SSadaf Ebrahimi  *
231*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
232*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
233*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
234*62c56f98SSadaf Ebrahimi  *
235*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
236*62c56f98SSadaf Ebrahimi  *
237*62c56f98SSadaf Ebrahimi  * \return The usage flags stored in the attribute structure.
238*62c56f98SSadaf Ebrahimi  */
239*62c56f98SSadaf Ebrahimi static psa_key_usage_t psa_get_key_usage_flags(
240*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes);
241*62c56f98SSadaf Ebrahimi 
242*62c56f98SSadaf Ebrahimi /** Declare the permitted algorithm policy for a key.
243*62c56f98SSadaf Ebrahimi  *
244*62c56f98SSadaf Ebrahimi  * The permitted algorithm policy of a key encodes which algorithm or
245*62c56f98SSadaf Ebrahimi  * algorithms are permitted to be used with this key. The following
246*62c56f98SSadaf Ebrahimi  * algorithm policies are supported:
247*62c56f98SSadaf Ebrahimi  * - 0 does not allow any cryptographic operation with the key. The key
248*62c56f98SSadaf Ebrahimi  *   may be used for non-cryptographic actions such as exporting (if
249*62c56f98SSadaf Ebrahimi  *   permitted by the usage flags).
250*62c56f98SSadaf Ebrahimi  * - An algorithm value permits this particular algorithm.
251*62c56f98SSadaf Ebrahimi  * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
252*62c56f98SSadaf Ebrahimi  *   signature scheme with any hash algorithm.
253*62c56f98SSadaf Ebrahimi  * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
254*62c56f98SSadaf Ebrahimi  *   any MAC algorithm from the same base class (e.g. CMAC) which
255*62c56f98SSadaf Ebrahimi  *   generates/verifies a MAC length greater than or equal to the length
256*62c56f98SSadaf Ebrahimi  *   encoded in the wildcard algorithm.
257*62c56f98SSadaf Ebrahimi  * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
258*62c56f98SSadaf Ebrahimi  *   allows any AEAD algorithm from the same base class (e.g. CCM) which
259*62c56f98SSadaf Ebrahimi  *   generates/verifies a tag length greater than or equal to the length
260*62c56f98SSadaf Ebrahimi  *   encoded in the wildcard algorithm.
261*62c56f98SSadaf Ebrahimi  *
262*62c56f98SSadaf Ebrahimi  * This function overwrites any algorithm policy
263*62c56f98SSadaf Ebrahimi  * previously set in \p attributes.
264*62c56f98SSadaf Ebrahimi  *
265*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
266*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
267*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
268*62c56f98SSadaf Ebrahimi  *
269*62c56f98SSadaf Ebrahimi  * \param[out] attributes       The attribute structure to write to.
270*62c56f98SSadaf Ebrahimi  * \param alg                   The permitted algorithm policy to write.
271*62c56f98SSadaf Ebrahimi  */
272*62c56f98SSadaf Ebrahimi static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
273*62c56f98SSadaf Ebrahimi                                   psa_algorithm_t alg);
274*62c56f98SSadaf Ebrahimi 
275*62c56f98SSadaf Ebrahimi 
276*62c56f98SSadaf Ebrahimi /** Retrieve the algorithm policy from key attributes.
277*62c56f98SSadaf Ebrahimi  *
278*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
279*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
280*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
281*62c56f98SSadaf Ebrahimi  *
282*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
283*62c56f98SSadaf Ebrahimi  *
284*62c56f98SSadaf Ebrahimi  * \return The algorithm stored in the attribute structure.
285*62c56f98SSadaf Ebrahimi  */
286*62c56f98SSadaf Ebrahimi static psa_algorithm_t psa_get_key_algorithm(
287*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes);
288*62c56f98SSadaf Ebrahimi 
289*62c56f98SSadaf Ebrahimi /** Declare the type of a key.
290*62c56f98SSadaf Ebrahimi  *
291*62c56f98SSadaf Ebrahimi  * This function overwrites any key type
292*62c56f98SSadaf Ebrahimi  * previously set in \p attributes.
293*62c56f98SSadaf Ebrahimi  *
294*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
295*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
296*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
297*62c56f98SSadaf Ebrahimi  *
298*62c56f98SSadaf Ebrahimi  * \param[out] attributes       The attribute structure to write to.
299*62c56f98SSadaf Ebrahimi  * \param type                  The key type to write.
300*62c56f98SSadaf Ebrahimi  *                              If this is 0, the key type in \p attributes
301*62c56f98SSadaf Ebrahimi  *                              becomes unspecified.
302*62c56f98SSadaf Ebrahimi  */
303*62c56f98SSadaf Ebrahimi static void psa_set_key_type(psa_key_attributes_t *attributes,
304*62c56f98SSadaf Ebrahimi                              psa_key_type_t type);
305*62c56f98SSadaf Ebrahimi 
306*62c56f98SSadaf Ebrahimi 
307*62c56f98SSadaf Ebrahimi /** Declare the size of a key.
308*62c56f98SSadaf Ebrahimi  *
309*62c56f98SSadaf Ebrahimi  * This function overwrites any key size previously set in \p attributes.
310*62c56f98SSadaf Ebrahimi  *
311*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
312*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
313*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate each of its arguments exactly once.
314*62c56f98SSadaf Ebrahimi  *
315*62c56f98SSadaf Ebrahimi  * \param[out] attributes       The attribute structure to write to.
316*62c56f98SSadaf Ebrahimi  * \param bits                  The key size in bits.
317*62c56f98SSadaf Ebrahimi  *                              If this is 0, the key size in \p attributes
318*62c56f98SSadaf Ebrahimi  *                              becomes unspecified. Keys of size 0 are
319*62c56f98SSadaf Ebrahimi  *                              not supported.
320*62c56f98SSadaf Ebrahimi  */
321*62c56f98SSadaf Ebrahimi static void psa_set_key_bits(psa_key_attributes_t *attributes,
322*62c56f98SSadaf Ebrahimi                              size_t bits);
323*62c56f98SSadaf Ebrahimi 
324*62c56f98SSadaf Ebrahimi /** Retrieve the key type from key attributes.
325*62c56f98SSadaf Ebrahimi  *
326*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
327*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
328*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
329*62c56f98SSadaf Ebrahimi  *
330*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
331*62c56f98SSadaf Ebrahimi  *
332*62c56f98SSadaf Ebrahimi  * \return The key type stored in the attribute structure.
333*62c56f98SSadaf Ebrahimi  */
334*62c56f98SSadaf Ebrahimi static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
335*62c56f98SSadaf Ebrahimi 
336*62c56f98SSadaf Ebrahimi /** Retrieve the key size from key attributes.
337*62c56f98SSadaf Ebrahimi  *
338*62c56f98SSadaf Ebrahimi  * This function may be declared as `static` (i.e. without external
339*62c56f98SSadaf Ebrahimi  * linkage). This function may be provided as a function-like macro,
340*62c56f98SSadaf Ebrahimi  * but in this case it must evaluate its argument exactly once.
341*62c56f98SSadaf Ebrahimi  *
342*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The key attribute structure to query.
343*62c56f98SSadaf Ebrahimi  *
344*62c56f98SSadaf Ebrahimi  * \return The key size stored in the attribute structure, in bits.
345*62c56f98SSadaf Ebrahimi  */
346*62c56f98SSadaf Ebrahimi static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
347*62c56f98SSadaf Ebrahimi 
348*62c56f98SSadaf Ebrahimi /** Retrieve the attributes of a key.
349*62c56f98SSadaf Ebrahimi  *
350*62c56f98SSadaf Ebrahimi  * This function first resets the attribute structure as with
351*62c56f98SSadaf Ebrahimi  * psa_reset_key_attributes(). It then copies the attributes of
352*62c56f98SSadaf Ebrahimi  * the given key into the given attribute structure.
353*62c56f98SSadaf Ebrahimi  *
354*62c56f98SSadaf Ebrahimi  * \note This function may allocate memory or other resources.
355*62c56f98SSadaf Ebrahimi  *       Once you have called this function on an attribute structure,
356*62c56f98SSadaf Ebrahimi  *       you must call psa_reset_key_attributes() to free these resources.
357*62c56f98SSadaf Ebrahimi  *
358*62c56f98SSadaf Ebrahimi  * \param[in] key               Identifier of the key to query.
359*62c56f98SSadaf Ebrahimi  * \param[in,out] attributes    On success, the attributes of the key.
360*62c56f98SSadaf Ebrahimi  *                              On failure, equivalent to a
361*62c56f98SSadaf Ebrahimi  *                              freshly-initialized structure.
362*62c56f98SSadaf Ebrahimi  *
363*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
364*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
365*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
366*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
367*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
368*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
369*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
370*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
371*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
372*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
373*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
374*62c56f98SSadaf Ebrahimi  *         results in this error code.
375*62c56f98SSadaf Ebrahimi  */
376*62c56f98SSadaf Ebrahimi psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
377*62c56f98SSadaf Ebrahimi                                     psa_key_attributes_t *attributes);
378*62c56f98SSadaf Ebrahimi 
379*62c56f98SSadaf Ebrahimi /** Reset a key attribute structure to a freshly initialized state.
380*62c56f98SSadaf Ebrahimi  *
381*62c56f98SSadaf Ebrahimi  * You must initialize the attribute structure as described in the
382*62c56f98SSadaf Ebrahimi  * documentation of the type #psa_key_attributes_t before calling this
383*62c56f98SSadaf Ebrahimi  * function. Once the structure has been initialized, you may call this
384*62c56f98SSadaf Ebrahimi  * function at any time.
385*62c56f98SSadaf Ebrahimi  *
386*62c56f98SSadaf Ebrahimi  * This function frees any auxiliary resources that the structure
387*62c56f98SSadaf Ebrahimi  * may contain.
388*62c56f98SSadaf Ebrahimi  *
389*62c56f98SSadaf Ebrahimi  * \param[in,out] attributes    The attribute structure to reset.
390*62c56f98SSadaf Ebrahimi  */
391*62c56f98SSadaf Ebrahimi void psa_reset_key_attributes(psa_key_attributes_t *attributes);
392*62c56f98SSadaf Ebrahimi 
393*62c56f98SSadaf Ebrahimi /**@}*/
394*62c56f98SSadaf Ebrahimi 
395*62c56f98SSadaf Ebrahimi /** \defgroup key_management Key management
396*62c56f98SSadaf Ebrahimi  * @{
397*62c56f98SSadaf Ebrahimi  */
398*62c56f98SSadaf Ebrahimi 
399*62c56f98SSadaf Ebrahimi /** Remove non-essential copies of key material from memory.
400*62c56f98SSadaf Ebrahimi  *
401*62c56f98SSadaf Ebrahimi  * If the key identifier designates a volatile key, this functions does not do
402*62c56f98SSadaf Ebrahimi  * anything and returns successfully.
403*62c56f98SSadaf Ebrahimi  *
404*62c56f98SSadaf Ebrahimi  * If the key identifier designates a persistent key, then this function will
405*62c56f98SSadaf Ebrahimi  * free all resources associated with the key in volatile memory. The key
406*62c56f98SSadaf Ebrahimi  * data in persistent storage is not affected and the key can still be used.
407*62c56f98SSadaf Ebrahimi  *
408*62c56f98SSadaf Ebrahimi  * \param key Identifier of the key to purge.
409*62c56f98SSadaf Ebrahimi  *
410*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
411*62c56f98SSadaf Ebrahimi  *         The key material will have been removed from memory if it is not
412*62c56f98SSadaf Ebrahimi  *         currently required.
413*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
414*62c56f98SSadaf Ebrahimi  *         \p key is not a valid key identifier.
415*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
416*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
417*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
418*62c56f98SSadaf Ebrahimi  *         results in this error code.
419*62c56f98SSadaf Ebrahimi  */
420*62c56f98SSadaf Ebrahimi psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
421*62c56f98SSadaf Ebrahimi 
422*62c56f98SSadaf Ebrahimi /** Make a copy of a key.
423*62c56f98SSadaf Ebrahimi  *
424*62c56f98SSadaf Ebrahimi  * Copy key material from one location to another.
425*62c56f98SSadaf Ebrahimi  *
426*62c56f98SSadaf Ebrahimi  * This function is primarily useful to copy a key from one location
427*62c56f98SSadaf Ebrahimi  * to another, since it populates a key using the material from
428*62c56f98SSadaf Ebrahimi  * another key which may have a different lifetime.
429*62c56f98SSadaf Ebrahimi  *
430*62c56f98SSadaf Ebrahimi  * This function may be used to share a key with a different party,
431*62c56f98SSadaf Ebrahimi  * subject to implementation-defined restrictions on key sharing.
432*62c56f98SSadaf Ebrahimi  *
433*62c56f98SSadaf Ebrahimi  * The policy on the source key must have the usage flag
434*62c56f98SSadaf Ebrahimi  * #PSA_KEY_USAGE_COPY set.
435*62c56f98SSadaf Ebrahimi  * This flag is sufficient to permit the copy if the key has the lifetime
436*62c56f98SSadaf Ebrahimi  * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
437*62c56f98SSadaf Ebrahimi  * Some secure elements do not provide a way to copy a key without
438*62c56f98SSadaf Ebrahimi  * making it extractable from the secure element. If a key is located
439*62c56f98SSadaf Ebrahimi  * in such a secure element, then the key must have both usage flags
440*62c56f98SSadaf Ebrahimi  * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
441*62c56f98SSadaf Ebrahimi  * a copy of the key outside the secure element.
442*62c56f98SSadaf Ebrahimi  *
443*62c56f98SSadaf Ebrahimi  * The resulting key may only be used in a way that conforms to
444*62c56f98SSadaf Ebrahimi  * both the policy of the original key and the policy specified in
445*62c56f98SSadaf Ebrahimi  * the \p attributes parameter:
446*62c56f98SSadaf Ebrahimi  * - The usage flags on the resulting key are the bitwise-and of the
447*62c56f98SSadaf Ebrahimi  *   usage flags on the source policy and the usage flags in \p attributes.
448*62c56f98SSadaf Ebrahimi  * - If both allow the same algorithm or wildcard-based
449*62c56f98SSadaf Ebrahimi  *   algorithm policy, the resulting key has the same algorithm policy.
450*62c56f98SSadaf Ebrahimi  * - If either of the policies allows an algorithm and the other policy
451*62c56f98SSadaf Ebrahimi  *   allows a wildcard-based algorithm policy that includes this algorithm,
452*62c56f98SSadaf Ebrahimi  *   the resulting key allows the same algorithm.
453*62c56f98SSadaf Ebrahimi  * - If the policies do not allow any algorithm in common, this function
454*62c56f98SSadaf Ebrahimi  *   fails with the status #PSA_ERROR_INVALID_ARGUMENT.
455*62c56f98SSadaf Ebrahimi  *
456*62c56f98SSadaf Ebrahimi  * The effect of this function on implementation-defined attributes is
457*62c56f98SSadaf Ebrahimi  * implementation-defined.
458*62c56f98SSadaf Ebrahimi  *
459*62c56f98SSadaf Ebrahimi  * \param source_key        The key to copy. It must allow the usage
460*62c56f98SSadaf Ebrahimi  *                          #PSA_KEY_USAGE_COPY. If a private or secret key is
461*62c56f98SSadaf Ebrahimi  *                          being copied outside of a secure element it must
462*62c56f98SSadaf Ebrahimi  *                          also allow #PSA_KEY_USAGE_EXPORT.
463*62c56f98SSadaf Ebrahimi  * \param[in] attributes    The attributes for the new key.
464*62c56f98SSadaf Ebrahimi  *                          They are used as follows:
465*62c56f98SSadaf Ebrahimi  *                          - The key type and size may be 0. If either is
466*62c56f98SSadaf Ebrahimi  *                            nonzero, it must match the corresponding
467*62c56f98SSadaf Ebrahimi  *                            attribute of the source key.
468*62c56f98SSadaf Ebrahimi  *                          - The key location (the lifetime and, for
469*62c56f98SSadaf Ebrahimi  *                            persistent keys, the key identifier) is
470*62c56f98SSadaf Ebrahimi  *                            used directly.
471*62c56f98SSadaf Ebrahimi  *                          - The policy constraints (usage flags and
472*62c56f98SSadaf Ebrahimi  *                            algorithm policy) are combined from
473*62c56f98SSadaf Ebrahimi  *                            the source key and \p attributes so that
474*62c56f98SSadaf Ebrahimi  *                            both sets of restrictions apply, as
475*62c56f98SSadaf Ebrahimi  *                            described in the documentation of this function.
476*62c56f98SSadaf Ebrahimi  * \param[out] target_key   On success, an identifier for the newly created
477*62c56f98SSadaf Ebrahimi  *                          key. For persistent keys, this is the key
478*62c56f98SSadaf Ebrahimi  *                          identifier defined in \p attributes.
479*62c56f98SSadaf Ebrahimi  *                          \c 0 on failure.
480*62c56f98SSadaf Ebrahimi  *
481*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
482*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE
483*62c56f98SSadaf Ebrahimi  *         \p source_key is invalid.
484*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_ALREADY_EXISTS
485*62c56f98SSadaf Ebrahimi  *         This is an attempt to create a persistent key, and there is
486*62c56f98SSadaf Ebrahimi  *         already a persistent key with the given identifier.
487*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
488*62c56f98SSadaf Ebrahimi  *         The lifetime or identifier in \p attributes are invalid, or
489*62c56f98SSadaf Ebrahimi  *         the policy constraints on the source and specified in
490*62c56f98SSadaf Ebrahimi  *         \p attributes are incompatible, or
491*62c56f98SSadaf Ebrahimi  *         \p attributes specifies a key type or key size
492*62c56f98SSadaf Ebrahimi  *         which does not match the attributes of the source key.
493*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
494*62c56f98SSadaf Ebrahimi  *         The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
495*62c56f98SSadaf Ebrahimi  *         the source key is not exportable and its lifetime does not
496*62c56f98SSadaf Ebrahimi  *         allow copying it to the target's lifetime.
497*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
498*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
499*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
500*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
501*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
502*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
503*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
504*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
505*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
506*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
507*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
508*62c56f98SSadaf Ebrahimi  *         results in this error code.
509*62c56f98SSadaf Ebrahimi  */
510*62c56f98SSadaf Ebrahimi psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
511*62c56f98SSadaf Ebrahimi                           const psa_key_attributes_t *attributes,
512*62c56f98SSadaf Ebrahimi                           mbedtls_svc_key_id_t *target_key);
513*62c56f98SSadaf Ebrahimi 
514*62c56f98SSadaf Ebrahimi 
515*62c56f98SSadaf Ebrahimi /**
516*62c56f98SSadaf Ebrahimi  * \brief Destroy a key.
517*62c56f98SSadaf Ebrahimi  *
518*62c56f98SSadaf Ebrahimi  * This function destroys a key from both volatile
519*62c56f98SSadaf Ebrahimi  * memory and, if applicable, non-volatile storage. Implementations shall
520*62c56f98SSadaf Ebrahimi  * make a best effort to ensure that the key material cannot be recovered.
521*62c56f98SSadaf Ebrahimi  *
522*62c56f98SSadaf Ebrahimi  * This function also erases any metadata such as policies and frees
523*62c56f98SSadaf Ebrahimi  * resources associated with the key.
524*62c56f98SSadaf Ebrahimi  *
525*62c56f98SSadaf Ebrahimi  * If a key is currently in use in a multipart operation, then destroying the
526*62c56f98SSadaf Ebrahimi  * key will cause the multipart operation to fail.
527*62c56f98SSadaf Ebrahimi  *
528*62c56f98SSadaf Ebrahimi  * \param key  Identifier of the key to erase. If this is \c 0, do nothing and
529*62c56f98SSadaf Ebrahimi  *             return #PSA_SUCCESS.
530*62c56f98SSadaf Ebrahimi  *
531*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
532*62c56f98SSadaf Ebrahimi  *         \p key was a valid identifier and the key material that it
533*62c56f98SSadaf Ebrahimi  *         referred to has been erased. Alternatively, \p key is \c 0.
534*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
535*62c56f98SSadaf Ebrahimi  *         The key cannot be erased because it is
536*62c56f98SSadaf Ebrahimi  *         read-only, either due to a policy or due to physical restrictions.
537*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE
538*62c56f98SSadaf Ebrahimi  *         \p key is not a valid identifier nor \c 0.
539*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE
540*62c56f98SSadaf Ebrahimi  *         There was a failure in communication with the cryptoprocessor.
541*62c56f98SSadaf Ebrahimi  *         The key material may still be present in the cryptoprocessor.
542*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID
543*62c56f98SSadaf Ebrahimi  *         This error is typically a result of either storage corruption on a
544*62c56f98SSadaf Ebrahimi  *         cleartext storage backend, or an attempt to read data that was
545*62c56f98SSadaf Ebrahimi  *         written by an incompatible version of the library.
546*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
547*62c56f98SSadaf Ebrahimi  *         The storage is corrupted. Implementations shall make a best effort
548*62c56f98SSadaf Ebrahimi  *         to erase key material even in this stage, however applications
549*62c56f98SSadaf Ebrahimi  *         should be aware that it may be impossible to guarantee that the
550*62c56f98SSadaf Ebrahimi  *         key material is not recoverable in such cases.
551*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED
552*62c56f98SSadaf Ebrahimi  *         An unexpected condition which is not a storage corruption or
553*62c56f98SSadaf Ebrahimi  *         a communication failure occurred. The cryptoprocessor may have
554*62c56f98SSadaf Ebrahimi  *         been compromised.
555*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
556*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
557*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
558*62c56f98SSadaf Ebrahimi  *         results in this error code.
559*62c56f98SSadaf Ebrahimi  */
560*62c56f98SSadaf Ebrahimi psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
561*62c56f98SSadaf Ebrahimi 
562*62c56f98SSadaf Ebrahimi /**@}*/
563*62c56f98SSadaf Ebrahimi 
564*62c56f98SSadaf Ebrahimi /** \defgroup import_export Key import and export
565*62c56f98SSadaf Ebrahimi  * @{
566*62c56f98SSadaf Ebrahimi  */
567*62c56f98SSadaf Ebrahimi 
568*62c56f98SSadaf Ebrahimi /**
569*62c56f98SSadaf Ebrahimi  * \brief Import a key in binary format.
570*62c56f98SSadaf Ebrahimi  *
571*62c56f98SSadaf Ebrahimi  * This function supports any output from psa_export_key(). Refer to the
572*62c56f98SSadaf Ebrahimi  * documentation of psa_export_public_key() for the format of public keys
573*62c56f98SSadaf Ebrahimi  * and to the documentation of psa_export_key() for the format for
574*62c56f98SSadaf Ebrahimi  * other key types.
575*62c56f98SSadaf Ebrahimi  *
576*62c56f98SSadaf Ebrahimi  * The key data determines the key size. The attributes may optionally
577*62c56f98SSadaf Ebrahimi  * specify a key size; in this case it must match the size determined
578*62c56f98SSadaf Ebrahimi  * from the key data. A key size of 0 in \p attributes indicates that
579*62c56f98SSadaf Ebrahimi  * the key size is solely determined by the key data.
580*62c56f98SSadaf Ebrahimi  *
581*62c56f98SSadaf Ebrahimi  * Implementations must reject an attempt to import a key of size 0.
582*62c56f98SSadaf Ebrahimi  *
583*62c56f98SSadaf Ebrahimi  * This specification supports a single format for each key type.
584*62c56f98SSadaf Ebrahimi  * Implementations may support other formats as long as the standard
585*62c56f98SSadaf Ebrahimi  * format is supported. Implementations that support other formats
586*62c56f98SSadaf Ebrahimi  * should ensure that the formats are clearly unambiguous so as to
587*62c56f98SSadaf Ebrahimi  * minimize the risk that an invalid input is accidentally interpreted
588*62c56f98SSadaf Ebrahimi  * according to a different format.
589*62c56f98SSadaf Ebrahimi  *
590*62c56f98SSadaf Ebrahimi  * \param[in] attributes    The attributes for the new key.
591*62c56f98SSadaf Ebrahimi  *                          The key size is always determined from the
592*62c56f98SSadaf Ebrahimi  *                          \p data buffer.
593*62c56f98SSadaf Ebrahimi  *                          If the key size in \p attributes is nonzero,
594*62c56f98SSadaf Ebrahimi  *                          it must be equal to the size from \p data.
595*62c56f98SSadaf Ebrahimi  * \param[out] key          On success, an identifier to the newly created key.
596*62c56f98SSadaf Ebrahimi  *                          For persistent keys, this is the key identifier
597*62c56f98SSadaf Ebrahimi  *                          defined in \p attributes.
598*62c56f98SSadaf Ebrahimi  *                          \c 0 on failure.
599*62c56f98SSadaf Ebrahimi  * \param[in] data    Buffer containing the key data. The content of this
600*62c56f98SSadaf Ebrahimi  *                    buffer is interpreted according to the type declared
601*62c56f98SSadaf Ebrahimi  *                    in \p attributes.
602*62c56f98SSadaf Ebrahimi  *                    All implementations must support at least the format
603*62c56f98SSadaf Ebrahimi  *                    described in the documentation
604*62c56f98SSadaf Ebrahimi  *                    of psa_export_key() or psa_export_public_key() for
605*62c56f98SSadaf Ebrahimi  *                    the chosen type. Implementations may allow other
606*62c56f98SSadaf Ebrahimi  *                    formats, but should be conservative: implementations
607*62c56f98SSadaf Ebrahimi  *                    should err on the side of rejecting content if it
608*62c56f98SSadaf Ebrahimi  *                    may be erroneous (e.g. wrong type or truncated data).
609*62c56f98SSadaf Ebrahimi  * \param data_length Size of the \p data buffer in bytes.
610*62c56f98SSadaf Ebrahimi  *
611*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
612*62c56f98SSadaf Ebrahimi  *         Success.
613*62c56f98SSadaf Ebrahimi  *         If the key is persistent, the key material and the key's metadata
614*62c56f98SSadaf Ebrahimi  *         have been saved to persistent storage.
615*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_ALREADY_EXISTS
616*62c56f98SSadaf Ebrahimi  *         This is an attempt to create a persistent key, and there is
617*62c56f98SSadaf Ebrahimi  *         already a persistent key with the given identifier.
618*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
619*62c56f98SSadaf Ebrahimi  *         The key type or key size is not supported, either by the
620*62c56f98SSadaf Ebrahimi  *         implementation in general or in this particular persistent location.
621*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
622*62c56f98SSadaf Ebrahimi  *         The key attributes, as a whole, are invalid, or
623*62c56f98SSadaf Ebrahimi  *         the key data is not correctly formatted, or
624*62c56f98SSadaf Ebrahimi  *         the size in \p attributes is nonzero and does not match the size
625*62c56f98SSadaf Ebrahimi  *         of the key data.
626*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
627*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
628*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
629*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
630*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
631*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
632*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
633*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
634*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
635*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
636*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
637*62c56f98SSadaf Ebrahimi  *         results in this error code.
638*62c56f98SSadaf Ebrahimi  */
639*62c56f98SSadaf Ebrahimi psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
640*62c56f98SSadaf Ebrahimi                             const uint8_t *data,
641*62c56f98SSadaf Ebrahimi                             size_t data_length,
642*62c56f98SSadaf Ebrahimi                             mbedtls_svc_key_id_t *key);
643*62c56f98SSadaf Ebrahimi 
644*62c56f98SSadaf Ebrahimi 
645*62c56f98SSadaf Ebrahimi 
646*62c56f98SSadaf Ebrahimi /**
647*62c56f98SSadaf Ebrahimi  * \brief Export a key in binary format.
648*62c56f98SSadaf Ebrahimi  *
649*62c56f98SSadaf Ebrahimi  * The output of this function can be passed to psa_import_key() to
650*62c56f98SSadaf Ebrahimi  * create an equivalent object.
651*62c56f98SSadaf Ebrahimi  *
652*62c56f98SSadaf Ebrahimi  * If the implementation of psa_import_key() supports other formats
653*62c56f98SSadaf Ebrahimi  * beyond the format specified here, the output from psa_export_key()
654*62c56f98SSadaf Ebrahimi  * must use the representation specified here, not the original
655*62c56f98SSadaf Ebrahimi  * representation.
656*62c56f98SSadaf Ebrahimi  *
657*62c56f98SSadaf Ebrahimi  * For standard key types, the output format is as follows:
658*62c56f98SSadaf Ebrahimi  *
659*62c56f98SSadaf Ebrahimi  * - For symmetric keys (including MAC keys), the format is the
660*62c56f98SSadaf Ebrahimi  *   raw bytes of the key.
661*62c56f98SSadaf Ebrahimi  * - For DES, the key data consists of 8 bytes. The parity bits must be
662*62c56f98SSadaf Ebrahimi  *   correct.
663*62c56f98SSadaf Ebrahimi  * - For Triple-DES, the format is the concatenation of the
664*62c56f98SSadaf Ebrahimi  *   two or three DES keys.
665*62c56f98SSadaf Ebrahimi  * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
666*62c56f98SSadaf Ebrahimi  *   is the non-encrypted DER encoding of the representation defined by
667*62c56f98SSadaf Ebrahimi  *   PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
668*62c56f98SSadaf Ebrahimi  *   ```
669*62c56f98SSadaf Ebrahimi  *   RSAPrivateKey ::= SEQUENCE {
670*62c56f98SSadaf Ebrahimi  *       version             INTEGER,  -- must be 0
671*62c56f98SSadaf Ebrahimi  *       modulus             INTEGER,  -- n
672*62c56f98SSadaf Ebrahimi  *       publicExponent      INTEGER,  -- e
673*62c56f98SSadaf Ebrahimi  *       privateExponent     INTEGER,  -- d
674*62c56f98SSadaf Ebrahimi  *       prime1              INTEGER,  -- p
675*62c56f98SSadaf Ebrahimi  *       prime2              INTEGER,  -- q
676*62c56f98SSadaf Ebrahimi  *       exponent1           INTEGER,  -- d mod (p-1)
677*62c56f98SSadaf Ebrahimi  *       exponent2           INTEGER,  -- d mod (q-1)
678*62c56f98SSadaf Ebrahimi  *       coefficient         INTEGER,  -- (inverse of q) mod p
679*62c56f98SSadaf Ebrahimi  *   }
680*62c56f98SSadaf Ebrahimi  *   ```
681*62c56f98SSadaf Ebrahimi  * - For elliptic curve key pairs (key types for which
682*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
683*62c56f98SSadaf Ebrahimi  *   a representation of the private value as a `ceiling(m/8)`-byte string
684*62c56f98SSadaf Ebrahimi  *   where `m` is the bit size associated with the curve, i.e. the bit size
685*62c56f98SSadaf Ebrahimi  *   of the order of the curve's coordinate field. This byte string is
686*62c56f98SSadaf Ebrahimi  *   in little-endian order for Montgomery curves (curve types
687*62c56f98SSadaf Ebrahimi  *   `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
688*62c56f98SSadaf Ebrahimi  *   curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
689*62c56f98SSadaf Ebrahimi  *   and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
690*62c56f98SSadaf Ebrahimi  *   For Weierstrass curves, this is the content of the `privateKey` field of
691*62c56f98SSadaf Ebrahimi  *   the `ECPrivateKey` format defined by RFC 5915.  For Montgomery curves,
692*62c56f98SSadaf Ebrahimi  *   the format is defined by RFC 7748, and output is masked according to §5.
693*62c56f98SSadaf Ebrahimi  *   For twisted Edwards curves, the private key is as defined by RFC 8032
694*62c56f98SSadaf Ebrahimi  *   (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
695*62c56f98SSadaf Ebrahimi  * - For Diffie-Hellman key exchange key pairs (key types for which
696*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
697*62c56f98SSadaf Ebrahimi  *   format is the representation of the private key `x` as a big-endian byte
698*62c56f98SSadaf Ebrahimi  *   string. The length of the byte string is the private key size in bytes
699*62c56f98SSadaf Ebrahimi  *   (leading zeroes are not stripped).
700*62c56f98SSadaf Ebrahimi  * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
701*62c56f98SSadaf Ebrahimi  *   true), the format is the same as for psa_export_public_key().
702*62c56f98SSadaf Ebrahimi  *
703*62c56f98SSadaf Ebrahimi  * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
704*62c56f98SSadaf Ebrahimi  *
705*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to export. It must allow the
706*62c56f98SSadaf Ebrahimi  *                          usage #PSA_KEY_USAGE_EXPORT, unless it is a public
707*62c56f98SSadaf Ebrahimi  *                          key.
708*62c56f98SSadaf Ebrahimi  * \param[out] data         Buffer where the key data is to be written.
709*62c56f98SSadaf Ebrahimi  * \param data_size         Size of the \p data buffer in bytes.
710*62c56f98SSadaf Ebrahimi  * \param[out] data_length  On success, the number of bytes
711*62c56f98SSadaf Ebrahimi  *                          that make up the key data.
712*62c56f98SSadaf Ebrahimi  *
713*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
714*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
715*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
716*62c56f98SSadaf Ebrahimi  *         The key does not have the #PSA_KEY_USAGE_EXPORT flag.
717*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
718*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
719*62c56f98SSadaf Ebrahimi  *         The size of the \p data buffer is too small. You can determine a
720*62c56f98SSadaf Ebrahimi  *         sufficient buffer size by calling
721*62c56f98SSadaf Ebrahimi  *         #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
722*62c56f98SSadaf Ebrahimi  *         where \c type is the key type
723*62c56f98SSadaf Ebrahimi  *         and \c bits is the key size in bits.
724*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
725*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
726*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
727*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
728*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
729*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
730*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
731*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
732*62c56f98SSadaf Ebrahimi  *         results in this error code.
733*62c56f98SSadaf Ebrahimi  */
734*62c56f98SSadaf Ebrahimi psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
735*62c56f98SSadaf Ebrahimi                             uint8_t *data,
736*62c56f98SSadaf Ebrahimi                             size_t data_size,
737*62c56f98SSadaf Ebrahimi                             size_t *data_length);
738*62c56f98SSadaf Ebrahimi 
739*62c56f98SSadaf Ebrahimi /**
740*62c56f98SSadaf Ebrahimi  * \brief Export a public key or the public part of a key pair in binary format.
741*62c56f98SSadaf Ebrahimi  *
742*62c56f98SSadaf Ebrahimi  * The output of this function can be passed to psa_import_key() to
743*62c56f98SSadaf Ebrahimi  * create an object that is equivalent to the public key.
744*62c56f98SSadaf Ebrahimi  *
745*62c56f98SSadaf Ebrahimi  * This specification supports a single format for each key type.
746*62c56f98SSadaf Ebrahimi  * Implementations may support other formats as long as the standard
747*62c56f98SSadaf Ebrahimi  * format is supported. Implementations that support other formats
748*62c56f98SSadaf Ebrahimi  * should ensure that the formats are clearly unambiguous so as to
749*62c56f98SSadaf Ebrahimi  * minimize the risk that an invalid input is accidentally interpreted
750*62c56f98SSadaf Ebrahimi  * according to a different format.
751*62c56f98SSadaf Ebrahimi  *
752*62c56f98SSadaf Ebrahimi  * For standard key types, the output format is as follows:
753*62c56f98SSadaf Ebrahimi  * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
754*62c56f98SSadaf Ebrahimi  *   the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
755*62c56f98SSadaf Ebrahimi  *   ```
756*62c56f98SSadaf Ebrahimi  *   RSAPublicKey ::= SEQUENCE {
757*62c56f98SSadaf Ebrahimi  *      modulus            INTEGER,    -- n
758*62c56f98SSadaf Ebrahimi  *      publicExponent     INTEGER  }  -- e
759*62c56f98SSadaf Ebrahimi  *   ```
760*62c56f98SSadaf Ebrahimi  * - For elliptic curve keys on a twisted Edwards curve (key types for which
761*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
762*62c56f98SSadaf Ebrahimi  *   returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
763*62c56f98SSadaf Ebrahimi  *   by RFC 8032
764*62c56f98SSadaf Ebrahimi  *   (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
765*62c56f98SSadaf Ebrahimi  * - For other elliptic curve public keys (key types for which
766*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
767*62c56f98SSadaf Ebrahimi  *   representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
768*62c56f98SSadaf Ebrahimi  *   Let `m` be the bit size associated with the curve, i.e. the bit size of
769*62c56f98SSadaf Ebrahimi  *   `q` for a curve over `F_q`. The representation consists of:
770*62c56f98SSadaf Ebrahimi  *      - The byte 0x04;
771*62c56f98SSadaf Ebrahimi  *      - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
772*62c56f98SSadaf Ebrahimi  *      - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
773*62c56f98SSadaf Ebrahimi  * - For Diffie-Hellman key exchange public keys (key types for which
774*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
775*62c56f98SSadaf Ebrahimi  *   the format is the representation of the public key `y = g^x mod p` as a
776*62c56f98SSadaf Ebrahimi  *   big-endian byte string. The length of the byte string is the length of the
777*62c56f98SSadaf Ebrahimi  *   base prime `p` in bytes.
778*62c56f98SSadaf Ebrahimi  *
779*62c56f98SSadaf Ebrahimi  * Exporting a public key object or the public part of a key pair is
780*62c56f98SSadaf Ebrahimi  * always permitted, regardless of the key's usage flags.
781*62c56f98SSadaf Ebrahimi  *
782*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to export.
783*62c56f98SSadaf Ebrahimi  * \param[out] data         Buffer where the key data is to be written.
784*62c56f98SSadaf Ebrahimi  * \param data_size         Size of the \p data buffer in bytes.
785*62c56f98SSadaf Ebrahimi  * \param[out] data_length  On success, the number of bytes
786*62c56f98SSadaf Ebrahimi  *                          that make up the key data.
787*62c56f98SSadaf Ebrahimi  *
788*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
789*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
790*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
791*62c56f98SSadaf Ebrahimi  *         The key is neither a public key nor a key pair.
792*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
793*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
794*62c56f98SSadaf Ebrahimi  *         The size of the \p data buffer is too small. You can determine a
795*62c56f98SSadaf Ebrahimi  *         sufficient buffer size by calling
796*62c56f98SSadaf Ebrahimi  *         #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
797*62c56f98SSadaf Ebrahimi  *         where \c type is the key type
798*62c56f98SSadaf Ebrahimi  *         and \c bits is the key size in bits.
799*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
800*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
801*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
802*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
803*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
804*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
805*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
806*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
807*62c56f98SSadaf Ebrahimi  *         results in this error code.
808*62c56f98SSadaf Ebrahimi  */
809*62c56f98SSadaf Ebrahimi psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
810*62c56f98SSadaf Ebrahimi                                    uint8_t *data,
811*62c56f98SSadaf Ebrahimi                                    size_t data_size,
812*62c56f98SSadaf Ebrahimi                                    size_t *data_length);
813*62c56f98SSadaf Ebrahimi 
814*62c56f98SSadaf Ebrahimi 
815*62c56f98SSadaf Ebrahimi 
816*62c56f98SSadaf Ebrahimi /**@}*/
817*62c56f98SSadaf Ebrahimi 
818*62c56f98SSadaf Ebrahimi /** \defgroup hash Message digests
819*62c56f98SSadaf Ebrahimi  * @{
820*62c56f98SSadaf Ebrahimi  */
821*62c56f98SSadaf Ebrahimi 
822*62c56f98SSadaf Ebrahimi /** Calculate the hash (digest) of a message.
823*62c56f98SSadaf Ebrahimi  *
824*62c56f98SSadaf Ebrahimi  * \note To verify the hash of a message against an
825*62c56f98SSadaf Ebrahimi  *       expected value, use psa_hash_compare() instead.
826*62c56f98SSadaf Ebrahimi  *
827*62c56f98SSadaf Ebrahimi  * \param alg               The hash algorithm to compute (\c PSA_ALG_XXX value
828*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_HASH(\p alg) is true).
829*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the message to hash.
830*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
831*62c56f98SSadaf Ebrahimi  * \param[out] hash         Buffer where the hash is to be written.
832*62c56f98SSadaf Ebrahimi  * \param hash_size         Size of the \p hash buffer in bytes.
833*62c56f98SSadaf Ebrahimi  * \param[out] hash_length  On success, the number of bytes
834*62c56f98SSadaf Ebrahimi  *                          that make up the hash value. This is always
835*62c56f98SSadaf Ebrahimi  *                          #PSA_HASH_LENGTH(\p alg).
836*62c56f98SSadaf Ebrahimi  *
837*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
838*62c56f98SSadaf Ebrahimi  *         Success.
839*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
840*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a hash algorithm.
841*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
842*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
843*62c56f98SSadaf Ebrahimi  *         \p hash_size is too small
844*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
845*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
846*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
847*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
848*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
849*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
850*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
851*62c56f98SSadaf Ebrahimi  *         results in this error code.
852*62c56f98SSadaf Ebrahimi  */
853*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_compute(psa_algorithm_t alg,
854*62c56f98SSadaf Ebrahimi                               const uint8_t *input,
855*62c56f98SSadaf Ebrahimi                               size_t input_length,
856*62c56f98SSadaf Ebrahimi                               uint8_t *hash,
857*62c56f98SSadaf Ebrahimi                               size_t hash_size,
858*62c56f98SSadaf Ebrahimi                               size_t *hash_length);
859*62c56f98SSadaf Ebrahimi 
860*62c56f98SSadaf Ebrahimi /** Calculate the hash (digest) of a message and compare it with a
861*62c56f98SSadaf Ebrahimi  * reference value.
862*62c56f98SSadaf Ebrahimi  *
863*62c56f98SSadaf Ebrahimi  * \param alg               The hash algorithm to compute (\c PSA_ALG_XXX value
864*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_HASH(\p alg) is true).
865*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the message to hash.
866*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
867*62c56f98SSadaf Ebrahimi  * \param[out] hash         Buffer containing the expected hash value.
868*62c56f98SSadaf Ebrahimi  * \param hash_length       Size of the \p hash buffer in bytes.
869*62c56f98SSadaf Ebrahimi  *
870*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
871*62c56f98SSadaf Ebrahimi  *         The expected hash is identical to the actual hash of the input.
872*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
873*62c56f98SSadaf Ebrahimi  *         The hash of the message was calculated successfully, but it
874*62c56f98SSadaf Ebrahimi  *         differs from the expected hash.
875*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
876*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a hash algorithm.
877*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
878*62c56f98SSadaf Ebrahimi  *         \p input_length or \p hash_length do not match the hash size for \p alg
879*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
880*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
881*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
882*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
883*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
884*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
885*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
886*62c56f98SSadaf Ebrahimi  *         results in this error code.
887*62c56f98SSadaf Ebrahimi  */
888*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_compare(psa_algorithm_t alg,
889*62c56f98SSadaf Ebrahimi                               const uint8_t *input,
890*62c56f98SSadaf Ebrahimi                               size_t input_length,
891*62c56f98SSadaf Ebrahimi                               const uint8_t *hash,
892*62c56f98SSadaf Ebrahimi                               size_t hash_length);
893*62c56f98SSadaf Ebrahimi 
894*62c56f98SSadaf Ebrahimi /** The type of the state data structure for multipart hash operations.
895*62c56f98SSadaf Ebrahimi  *
896*62c56f98SSadaf Ebrahimi  * Before calling any function on a hash operation object, the application must
897*62c56f98SSadaf Ebrahimi  * initialize it by any of the following means:
898*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
899*62c56f98SSadaf Ebrahimi  *   \code
900*62c56f98SSadaf Ebrahimi  *   psa_hash_operation_t operation;
901*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
902*62c56f98SSadaf Ebrahimi  *   \endcode
903*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
904*62c56f98SSadaf Ebrahimi  *   \code
905*62c56f98SSadaf Ebrahimi  *   psa_hash_operation_t operation = {0};
906*62c56f98SSadaf Ebrahimi  *   \endcode
907*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
908*62c56f98SSadaf Ebrahimi  *   for example:
909*62c56f98SSadaf Ebrahimi  *   \code
910*62c56f98SSadaf Ebrahimi  *   psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
911*62c56f98SSadaf Ebrahimi  *   \endcode
912*62c56f98SSadaf Ebrahimi  * - Assign the result of the function psa_hash_operation_init()
913*62c56f98SSadaf Ebrahimi  *   to the structure, for example:
914*62c56f98SSadaf Ebrahimi  *   \code
915*62c56f98SSadaf Ebrahimi  *   psa_hash_operation_t operation;
916*62c56f98SSadaf Ebrahimi  *   operation = psa_hash_operation_init();
917*62c56f98SSadaf Ebrahimi  *   \endcode
918*62c56f98SSadaf Ebrahimi  *
919*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
920*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
921*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
922*62c56f98SSadaf Ebrahimi typedef struct psa_hash_operation_s psa_hash_operation_t;
923*62c56f98SSadaf Ebrahimi 
924*62c56f98SSadaf Ebrahimi /** \def PSA_HASH_OPERATION_INIT
925*62c56f98SSadaf Ebrahimi  *
926*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for a hash operation object
927*62c56f98SSadaf Ebrahimi  * of type #psa_hash_operation_t.
928*62c56f98SSadaf Ebrahimi  */
929*62c56f98SSadaf Ebrahimi 
930*62c56f98SSadaf Ebrahimi /** Return an initial value for a hash operation object.
931*62c56f98SSadaf Ebrahimi  */
932*62c56f98SSadaf Ebrahimi static psa_hash_operation_t psa_hash_operation_init(void);
933*62c56f98SSadaf Ebrahimi 
934*62c56f98SSadaf Ebrahimi /** Set up a multipart hash operation.
935*62c56f98SSadaf Ebrahimi  *
936*62c56f98SSadaf Ebrahimi  * The sequence of operations to calculate a hash (message digest)
937*62c56f98SSadaf Ebrahimi  * is as follows:
938*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
939*62c56f98SSadaf Ebrahimi  *    listed here.
940*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
941*62c56f98SSadaf Ebrahimi  *    documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
942*62c56f98SSadaf Ebrahimi  * -# Call psa_hash_setup() to specify the algorithm.
943*62c56f98SSadaf Ebrahimi  * -# Call psa_hash_update() zero, one or more times, passing a fragment
944*62c56f98SSadaf Ebrahimi  *    of the message each time. The hash that is calculated is the hash
945*62c56f98SSadaf Ebrahimi  *    of the concatenation of these messages in order.
946*62c56f98SSadaf Ebrahimi  * -# To calculate the hash, call psa_hash_finish().
947*62c56f98SSadaf Ebrahimi  *    To compare the hash with an expected value, call psa_hash_verify().
948*62c56f98SSadaf Ebrahimi  *
949*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_hash_setup(), the
950*62c56f98SSadaf Ebrahimi  * operation will need to be reset by a call to psa_hash_abort(). The
951*62c56f98SSadaf Ebrahimi  * application may call psa_hash_abort() at any time after the operation
952*62c56f98SSadaf Ebrahimi  * has been initialized.
953*62c56f98SSadaf Ebrahimi  *
954*62c56f98SSadaf Ebrahimi  * After a successful call to psa_hash_setup(), the application must
955*62c56f98SSadaf Ebrahimi  * eventually terminate the operation. The following events terminate an
956*62c56f98SSadaf Ebrahimi  * operation:
957*62c56f98SSadaf Ebrahimi  * - A successful call to psa_hash_finish() or psa_hash_verify().
958*62c56f98SSadaf Ebrahimi  * - A call to psa_hash_abort().
959*62c56f98SSadaf Ebrahimi  *
960*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The operation object to set up. It must have
961*62c56f98SSadaf Ebrahimi  *                          been initialized as per the documentation for
962*62c56f98SSadaf Ebrahimi  *                          #psa_hash_operation_t and not yet in use.
963*62c56f98SSadaf Ebrahimi  * \param alg               The hash algorithm to compute (\c PSA_ALG_XXX value
964*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_HASH(\p alg) is true).
965*62c56f98SSadaf Ebrahimi  *
966*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
967*62c56f98SSadaf Ebrahimi  *         Success.
968*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
969*62c56f98SSadaf Ebrahimi  *         \p alg is not a supported hash algorithm.
970*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
971*62c56f98SSadaf Ebrahimi  *         \p alg is not a hash algorithm.
972*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
973*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
974*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
975*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
976*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
977*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
978*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
979*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
980*62c56f98SSadaf Ebrahimi  *         results in this error code.
981*62c56f98SSadaf Ebrahimi  */
982*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
983*62c56f98SSadaf Ebrahimi                             psa_algorithm_t alg);
984*62c56f98SSadaf Ebrahimi 
985*62c56f98SSadaf Ebrahimi /** Add a message fragment to a multipart hash operation.
986*62c56f98SSadaf Ebrahimi  *
987*62c56f98SSadaf Ebrahimi  * The application must call psa_hash_setup() before calling this function.
988*62c56f98SSadaf Ebrahimi  *
989*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
990*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_hash_abort().
991*62c56f98SSadaf Ebrahimi  *
992*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active hash operation.
993*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the message fragment to hash.
994*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
995*62c56f98SSadaf Ebrahimi  *
996*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
997*62c56f98SSadaf Ebrahimi  *         Success.
998*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
999*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1000*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1001*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1002*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1003*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or
1004*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1005*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1006*62c56f98SSadaf Ebrahimi  *         results in this error code.
1007*62c56f98SSadaf Ebrahimi  */
1008*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1009*62c56f98SSadaf Ebrahimi                              const uint8_t *input,
1010*62c56f98SSadaf Ebrahimi                              size_t input_length);
1011*62c56f98SSadaf Ebrahimi 
1012*62c56f98SSadaf Ebrahimi /** Finish the calculation of the hash of a message.
1013*62c56f98SSadaf Ebrahimi  *
1014*62c56f98SSadaf Ebrahimi  * The application must call psa_hash_setup() before calling this function.
1015*62c56f98SSadaf Ebrahimi  * This function calculates the hash of the message formed by concatenating
1016*62c56f98SSadaf Ebrahimi  * the inputs passed to preceding calls to psa_hash_update().
1017*62c56f98SSadaf Ebrahimi  *
1018*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
1019*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1020*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_hash_abort().
1021*62c56f98SSadaf Ebrahimi  *
1022*62c56f98SSadaf Ebrahimi  * \warning Applications should not call this function if they expect
1023*62c56f98SSadaf Ebrahimi  *          a specific value for the hash. Call psa_hash_verify() instead.
1024*62c56f98SSadaf Ebrahimi  *          Beware that comparing integrity or authenticity data such as
1025*62c56f98SSadaf Ebrahimi  *          hash values with a function such as \c memcmp is risky
1026*62c56f98SSadaf Ebrahimi  *          because the time taken by the comparison may leak information
1027*62c56f98SSadaf Ebrahimi  *          about the hashed data which could allow an attacker to guess
1028*62c56f98SSadaf Ebrahimi  *          a valid hash and thereby bypass security controls.
1029*62c56f98SSadaf Ebrahimi  *
1030*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active hash operation.
1031*62c56f98SSadaf Ebrahimi  * \param[out] hash             Buffer where the hash is to be written.
1032*62c56f98SSadaf Ebrahimi  * \param hash_size             Size of the \p hash buffer in bytes.
1033*62c56f98SSadaf Ebrahimi  * \param[out] hash_length      On success, the number of bytes
1034*62c56f98SSadaf Ebrahimi  *                              that make up the hash value. This is always
1035*62c56f98SSadaf Ebrahimi  *                              #PSA_HASH_LENGTH(\c alg) where \c alg is the
1036*62c56f98SSadaf Ebrahimi  *                              hash algorithm that is calculated.
1037*62c56f98SSadaf Ebrahimi  *
1038*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1039*62c56f98SSadaf Ebrahimi  *         Success.
1040*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1041*62c56f98SSadaf Ebrahimi  *         The size of the \p hash buffer is too small. You can determine a
1042*62c56f98SSadaf Ebrahimi  *         sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
1043*62c56f98SSadaf Ebrahimi  *         where \c alg is the hash algorithm that is calculated.
1044*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1045*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1046*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1047*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1048*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1049*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or
1050*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1051*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1052*62c56f98SSadaf Ebrahimi  *         results in this error code.
1053*62c56f98SSadaf Ebrahimi  */
1054*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1055*62c56f98SSadaf Ebrahimi                              uint8_t *hash,
1056*62c56f98SSadaf Ebrahimi                              size_t hash_size,
1057*62c56f98SSadaf Ebrahimi                              size_t *hash_length);
1058*62c56f98SSadaf Ebrahimi 
1059*62c56f98SSadaf Ebrahimi /** Finish the calculation of the hash of a message and compare it with
1060*62c56f98SSadaf Ebrahimi  * an expected value.
1061*62c56f98SSadaf Ebrahimi  *
1062*62c56f98SSadaf Ebrahimi  * The application must call psa_hash_setup() before calling this function.
1063*62c56f98SSadaf Ebrahimi  * This function calculates the hash of the message formed by concatenating
1064*62c56f98SSadaf Ebrahimi  * the inputs passed to preceding calls to psa_hash_update(). It then
1065*62c56f98SSadaf Ebrahimi  * compares the calculated hash with the expected hash passed as a
1066*62c56f98SSadaf Ebrahimi  * parameter to this function.
1067*62c56f98SSadaf Ebrahimi  *
1068*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
1069*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1070*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_hash_abort().
1071*62c56f98SSadaf Ebrahimi  *
1072*62c56f98SSadaf Ebrahimi  * \note Implementations shall make the best effort to ensure that the
1073*62c56f98SSadaf Ebrahimi  * comparison between the actual hash and the expected hash is performed
1074*62c56f98SSadaf Ebrahimi  * in constant time.
1075*62c56f98SSadaf Ebrahimi  *
1076*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active hash operation.
1077*62c56f98SSadaf Ebrahimi  * \param[in] hash              Buffer containing the expected hash value.
1078*62c56f98SSadaf Ebrahimi  * \param hash_length           Size of the \p hash buffer in bytes.
1079*62c56f98SSadaf Ebrahimi  *
1080*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1081*62c56f98SSadaf Ebrahimi  *         The expected hash is identical to the actual hash of the message.
1082*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
1083*62c56f98SSadaf Ebrahimi  *         The hash of the message was calculated successfully, but it
1084*62c56f98SSadaf Ebrahimi  *         differs from the expected hash.
1085*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1086*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1087*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1088*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1089*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1090*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or
1091*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1092*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1093*62c56f98SSadaf Ebrahimi  *         results in this error code.
1094*62c56f98SSadaf Ebrahimi  */
1095*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1096*62c56f98SSadaf Ebrahimi                              const uint8_t *hash,
1097*62c56f98SSadaf Ebrahimi                              size_t hash_length);
1098*62c56f98SSadaf Ebrahimi 
1099*62c56f98SSadaf Ebrahimi /** Abort a hash operation.
1100*62c56f98SSadaf Ebrahimi  *
1101*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the
1102*62c56f98SSadaf Ebrahimi  * \p operation structure itself. Once aborted, the operation object
1103*62c56f98SSadaf Ebrahimi  * can be reused for another operation by calling
1104*62c56f98SSadaf Ebrahimi  * psa_hash_setup() again.
1105*62c56f98SSadaf Ebrahimi  *
1106*62c56f98SSadaf Ebrahimi  * You may call this function any time after the operation object has
1107*62c56f98SSadaf Ebrahimi  * been initialized by one of the methods described in #psa_hash_operation_t.
1108*62c56f98SSadaf Ebrahimi  *
1109*62c56f98SSadaf Ebrahimi  * In particular, calling psa_hash_abort() after the operation has been
1110*62c56f98SSadaf Ebrahimi  * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1111*62c56f98SSadaf Ebrahimi  * psa_hash_verify() is safe and has no effect.
1112*62c56f98SSadaf Ebrahimi  *
1113*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Initialized hash operation.
1114*62c56f98SSadaf Ebrahimi  *
1115*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
1116*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1117*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1118*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1119*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1120*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1121*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1122*62c56f98SSadaf Ebrahimi  *         results in this error code.
1123*62c56f98SSadaf Ebrahimi  */
1124*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1125*62c56f98SSadaf Ebrahimi 
1126*62c56f98SSadaf Ebrahimi /** Clone a hash operation.
1127*62c56f98SSadaf Ebrahimi  *
1128*62c56f98SSadaf Ebrahimi  * This function copies the state of an ongoing hash operation to
1129*62c56f98SSadaf Ebrahimi  * a new operation object. In other words, this function is equivalent
1130*62c56f98SSadaf Ebrahimi  * to calling psa_hash_setup() on \p target_operation with the same
1131*62c56f98SSadaf Ebrahimi  * algorithm that \p source_operation was set up for, then
1132*62c56f98SSadaf Ebrahimi  * psa_hash_update() on \p target_operation with the same input that
1133*62c56f98SSadaf Ebrahimi  * that was passed to \p source_operation. After this function returns, the
1134*62c56f98SSadaf Ebrahimi  * two objects are independent, i.e. subsequent calls involving one of
1135*62c56f98SSadaf Ebrahimi  * the objects do not affect the other object.
1136*62c56f98SSadaf Ebrahimi  *
1137*62c56f98SSadaf Ebrahimi  * \param[in] source_operation      The active hash operation to clone.
1138*62c56f98SSadaf Ebrahimi  * \param[in,out] target_operation  The operation object to set up.
1139*62c56f98SSadaf Ebrahimi  *                                  It must be initialized but not active.
1140*62c56f98SSadaf Ebrahimi  *
1141*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
1142*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1143*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1144*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1145*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1146*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1147*62c56f98SSadaf Ebrahimi  *         The \p source_operation state is not valid (it must be active), or
1148*62c56f98SSadaf Ebrahimi  *         the \p target_operation state is not valid (it must be inactive), or
1149*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1150*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1151*62c56f98SSadaf Ebrahimi  *         results in this error code.
1152*62c56f98SSadaf Ebrahimi  */
1153*62c56f98SSadaf Ebrahimi psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1154*62c56f98SSadaf Ebrahimi                             psa_hash_operation_t *target_operation);
1155*62c56f98SSadaf Ebrahimi 
1156*62c56f98SSadaf Ebrahimi /**@}*/
1157*62c56f98SSadaf Ebrahimi 
1158*62c56f98SSadaf Ebrahimi /** \defgroup MAC Message authentication codes
1159*62c56f98SSadaf Ebrahimi  * @{
1160*62c56f98SSadaf Ebrahimi  */
1161*62c56f98SSadaf Ebrahimi 
1162*62c56f98SSadaf Ebrahimi /** Calculate the MAC (message authentication code) of a message.
1163*62c56f98SSadaf Ebrahimi  *
1164*62c56f98SSadaf Ebrahimi  * \note To verify the MAC of a message against an
1165*62c56f98SSadaf Ebrahimi  *       expected value, use psa_mac_verify() instead.
1166*62c56f98SSadaf Ebrahimi  *       Beware that comparing integrity or authenticity data such as
1167*62c56f98SSadaf Ebrahimi  *       MAC values with a function such as \c memcmp is risky
1168*62c56f98SSadaf Ebrahimi  *       because the time taken by the comparison may leak information
1169*62c56f98SSadaf Ebrahimi  *       about the MAC value which could allow an attacker to guess
1170*62c56f98SSadaf Ebrahimi  *       a valid MAC and thereby bypass security controls.
1171*62c56f98SSadaf Ebrahimi  *
1172*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to use for the operation. It
1173*62c56f98SSadaf Ebrahimi  *                          must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1174*62c56f98SSadaf Ebrahimi  * \param alg               The MAC algorithm to compute (\c PSA_ALG_XXX value
1175*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_MAC(\p alg) is true).
1176*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the input message.
1177*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
1178*62c56f98SSadaf Ebrahimi  * \param[out] mac          Buffer where the MAC value is to be written.
1179*62c56f98SSadaf Ebrahimi  * \param mac_size          Size of the \p mac buffer in bytes.
1180*62c56f98SSadaf Ebrahimi  * \param[out] mac_length   On success, the number of bytes
1181*62c56f98SSadaf Ebrahimi  *                          that make up the MAC value.
1182*62c56f98SSadaf Ebrahimi  *
1183*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1184*62c56f98SSadaf Ebrahimi  *         Success.
1185*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1186*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1187*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1188*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1189*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1190*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a MAC algorithm.
1191*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1192*62c56f98SSadaf Ebrahimi  *         \p mac_size is too small
1193*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1194*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1195*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1196*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1197*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
1198*62c56f98SSadaf Ebrahimi  *         The key could not be retrieved from storage.
1199*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1200*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1201*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1202*62c56f98SSadaf Ebrahimi  *         results in this error code.
1203*62c56f98SSadaf Ebrahimi  */
1204*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
1205*62c56f98SSadaf Ebrahimi                              psa_algorithm_t alg,
1206*62c56f98SSadaf Ebrahimi                              const uint8_t *input,
1207*62c56f98SSadaf Ebrahimi                              size_t input_length,
1208*62c56f98SSadaf Ebrahimi                              uint8_t *mac,
1209*62c56f98SSadaf Ebrahimi                              size_t mac_size,
1210*62c56f98SSadaf Ebrahimi                              size_t *mac_length);
1211*62c56f98SSadaf Ebrahimi 
1212*62c56f98SSadaf Ebrahimi /** Calculate the MAC of a message and compare it with a reference value.
1213*62c56f98SSadaf Ebrahimi  *
1214*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to use for the operation. It
1215*62c56f98SSadaf Ebrahimi  *                          must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
1216*62c56f98SSadaf Ebrahimi  * \param alg               The MAC algorithm to compute (\c PSA_ALG_XXX value
1217*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_MAC(\p alg) is true).
1218*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the input message.
1219*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
1220*62c56f98SSadaf Ebrahimi  * \param[out] mac          Buffer containing the expected MAC value.
1221*62c56f98SSadaf Ebrahimi  * \param mac_length        Size of the \p mac buffer in bytes.
1222*62c56f98SSadaf Ebrahimi  *
1223*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1224*62c56f98SSadaf Ebrahimi  *         The expected MAC is identical to the actual MAC of the input.
1225*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
1226*62c56f98SSadaf Ebrahimi  *         The MAC of the message was calculated successfully, but it
1227*62c56f98SSadaf Ebrahimi  *         differs from the expected value.
1228*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1229*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1230*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1231*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1232*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1233*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a MAC algorithm.
1234*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1235*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1236*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1237*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1238*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
1239*62c56f98SSadaf Ebrahimi  *         The key could not be retrieved from storage.
1240*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1241*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1242*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1243*62c56f98SSadaf Ebrahimi  *         results in this error code.
1244*62c56f98SSadaf Ebrahimi  */
1245*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
1246*62c56f98SSadaf Ebrahimi                             psa_algorithm_t alg,
1247*62c56f98SSadaf Ebrahimi                             const uint8_t *input,
1248*62c56f98SSadaf Ebrahimi                             size_t input_length,
1249*62c56f98SSadaf Ebrahimi                             const uint8_t *mac,
1250*62c56f98SSadaf Ebrahimi                             size_t mac_length);
1251*62c56f98SSadaf Ebrahimi 
1252*62c56f98SSadaf Ebrahimi /** The type of the state data structure for multipart MAC operations.
1253*62c56f98SSadaf Ebrahimi  *
1254*62c56f98SSadaf Ebrahimi  * Before calling any function on a MAC operation object, the application must
1255*62c56f98SSadaf Ebrahimi  * initialize it by any of the following means:
1256*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
1257*62c56f98SSadaf Ebrahimi  *   \code
1258*62c56f98SSadaf Ebrahimi  *   psa_mac_operation_t operation;
1259*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
1260*62c56f98SSadaf Ebrahimi  *   \endcode
1261*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
1262*62c56f98SSadaf Ebrahimi  *   \code
1263*62c56f98SSadaf Ebrahimi  *   psa_mac_operation_t operation = {0};
1264*62c56f98SSadaf Ebrahimi  *   \endcode
1265*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1266*62c56f98SSadaf Ebrahimi  *   for example:
1267*62c56f98SSadaf Ebrahimi  *   \code
1268*62c56f98SSadaf Ebrahimi  *   psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1269*62c56f98SSadaf Ebrahimi  *   \endcode
1270*62c56f98SSadaf Ebrahimi  * - Assign the result of the function psa_mac_operation_init()
1271*62c56f98SSadaf Ebrahimi  *   to the structure, for example:
1272*62c56f98SSadaf Ebrahimi  *   \code
1273*62c56f98SSadaf Ebrahimi  *   psa_mac_operation_t operation;
1274*62c56f98SSadaf Ebrahimi  *   operation = psa_mac_operation_init();
1275*62c56f98SSadaf Ebrahimi  *   \endcode
1276*62c56f98SSadaf Ebrahimi  *
1277*62c56f98SSadaf Ebrahimi  *
1278*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
1279*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
1280*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
1281*62c56f98SSadaf Ebrahimi typedef struct psa_mac_operation_s psa_mac_operation_t;
1282*62c56f98SSadaf Ebrahimi 
1283*62c56f98SSadaf Ebrahimi /** \def PSA_MAC_OPERATION_INIT
1284*62c56f98SSadaf Ebrahimi  *
1285*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for a MAC operation object of type
1286*62c56f98SSadaf Ebrahimi  * #psa_mac_operation_t.
1287*62c56f98SSadaf Ebrahimi  */
1288*62c56f98SSadaf Ebrahimi 
1289*62c56f98SSadaf Ebrahimi /** Return an initial value for a MAC operation object.
1290*62c56f98SSadaf Ebrahimi  */
1291*62c56f98SSadaf Ebrahimi static psa_mac_operation_t psa_mac_operation_init(void);
1292*62c56f98SSadaf Ebrahimi 
1293*62c56f98SSadaf Ebrahimi /** Set up a multipart MAC calculation operation.
1294*62c56f98SSadaf Ebrahimi  *
1295*62c56f98SSadaf Ebrahimi  * This function sets up the calculation of the MAC
1296*62c56f98SSadaf Ebrahimi  * (message authentication code) of a byte string.
1297*62c56f98SSadaf Ebrahimi  * To verify the MAC of a message against an
1298*62c56f98SSadaf Ebrahimi  * expected value, use psa_mac_verify_setup() instead.
1299*62c56f98SSadaf Ebrahimi  *
1300*62c56f98SSadaf Ebrahimi  * The sequence of operations to calculate a MAC is as follows:
1301*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
1302*62c56f98SSadaf Ebrahimi  *    listed here.
1303*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
1304*62c56f98SSadaf Ebrahimi  *    documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1305*62c56f98SSadaf Ebrahimi  * -# Call psa_mac_sign_setup() to specify the algorithm and key.
1306*62c56f98SSadaf Ebrahimi  * -# Call psa_mac_update() zero, one or more times, passing a fragment
1307*62c56f98SSadaf Ebrahimi  *    of the message each time. The MAC that is calculated is the MAC
1308*62c56f98SSadaf Ebrahimi  *    of the concatenation of these messages in order.
1309*62c56f98SSadaf Ebrahimi  * -# At the end of the message, call psa_mac_sign_finish() to finish
1310*62c56f98SSadaf Ebrahimi  *    calculating the MAC value and retrieve it.
1311*62c56f98SSadaf Ebrahimi  *
1312*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1313*62c56f98SSadaf Ebrahimi  * operation will need to be reset by a call to psa_mac_abort(). The
1314*62c56f98SSadaf Ebrahimi  * application may call psa_mac_abort() at any time after the operation
1315*62c56f98SSadaf Ebrahimi  * has been initialized.
1316*62c56f98SSadaf Ebrahimi  *
1317*62c56f98SSadaf Ebrahimi  * After a successful call to psa_mac_sign_setup(), the application must
1318*62c56f98SSadaf Ebrahimi  * eventually terminate the operation through one of the following methods:
1319*62c56f98SSadaf Ebrahimi  * - A successful call to psa_mac_sign_finish().
1320*62c56f98SSadaf Ebrahimi  * - A call to psa_mac_abort().
1321*62c56f98SSadaf Ebrahimi  *
1322*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The operation object to set up. It must have
1323*62c56f98SSadaf Ebrahimi  *                          been initialized as per the documentation for
1324*62c56f98SSadaf Ebrahimi  *                          #psa_mac_operation_t and not yet in use.
1325*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to use for the operation. It
1326*62c56f98SSadaf Ebrahimi  *                          must remain valid until the operation terminates.
1327*62c56f98SSadaf Ebrahimi  *                          It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1328*62c56f98SSadaf Ebrahimi  * \param alg               The MAC algorithm to compute (\c PSA_ALG_XXX value
1329*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_MAC(\p alg) is true).
1330*62c56f98SSadaf Ebrahimi  *
1331*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1332*62c56f98SSadaf Ebrahimi  *         Success.
1333*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1334*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1335*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1336*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1337*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1338*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a MAC algorithm.
1339*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1340*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1341*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1342*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1343*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
1344*62c56f98SSadaf Ebrahimi  *         The key could not be retrieved from storage.
1345*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1346*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
1347*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1348*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1349*62c56f98SSadaf Ebrahimi  *         results in this error code.
1350*62c56f98SSadaf Ebrahimi  */
1351*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1352*62c56f98SSadaf Ebrahimi                                 mbedtls_svc_key_id_t key,
1353*62c56f98SSadaf Ebrahimi                                 psa_algorithm_t alg);
1354*62c56f98SSadaf Ebrahimi 
1355*62c56f98SSadaf Ebrahimi /** Set up a multipart MAC verification operation.
1356*62c56f98SSadaf Ebrahimi  *
1357*62c56f98SSadaf Ebrahimi  * This function sets up the verification of the MAC
1358*62c56f98SSadaf Ebrahimi  * (message authentication code) of a byte string against an expected value.
1359*62c56f98SSadaf Ebrahimi  *
1360*62c56f98SSadaf Ebrahimi  * The sequence of operations to verify a MAC is as follows:
1361*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
1362*62c56f98SSadaf Ebrahimi  *    listed here.
1363*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
1364*62c56f98SSadaf Ebrahimi  *    documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1365*62c56f98SSadaf Ebrahimi  * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1366*62c56f98SSadaf Ebrahimi  * -# Call psa_mac_update() zero, one or more times, passing a fragment
1367*62c56f98SSadaf Ebrahimi  *    of the message each time. The MAC that is calculated is the MAC
1368*62c56f98SSadaf Ebrahimi  *    of the concatenation of these messages in order.
1369*62c56f98SSadaf Ebrahimi  * -# At the end of the message, call psa_mac_verify_finish() to finish
1370*62c56f98SSadaf Ebrahimi  *    calculating the actual MAC of the message and verify it against
1371*62c56f98SSadaf Ebrahimi  *    the expected value.
1372*62c56f98SSadaf Ebrahimi  *
1373*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1374*62c56f98SSadaf Ebrahimi  * operation will need to be reset by a call to psa_mac_abort(). The
1375*62c56f98SSadaf Ebrahimi  * application may call psa_mac_abort() at any time after the operation
1376*62c56f98SSadaf Ebrahimi  * has been initialized.
1377*62c56f98SSadaf Ebrahimi  *
1378*62c56f98SSadaf Ebrahimi  * After a successful call to psa_mac_verify_setup(), the application must
1379*62c56f98SSadaf Ebrahimi  * eventually terminate the operation through one of the following methods:
1380*62c56f98SSadaf Ebrahimi  * - A successful call to psa_mac_verify_finish().
1381*62c56f98SSadaf Ebrahimi  * - A call to psa_mac_abort().
1382*62c56f98SSadaf Ebrahimi  *
1383*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The operation object to set up. It must have
1384*62c56f98SSadaf Ebrahimi  *                          been initialized as per the documentation for
1385*62c56f98SSadaf Ebrahimi  *                          #psa_mac_operation_t and not yet in use.
1386*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to use for the operation. It
1387*62c56f98SSadaf Ebrahimi  *                          must remain valid until the operation terminates.
1388*62c56f98SSadaf Ebrahimi  *                          It must allow the usage
1389*62c56f98SSadaf Ebrahimi  *                          PSA_KEY_USAGE_VERIFY_MESSAGE.
1390*62c56f98SSadaf Ebrahimi  * \param alg               The MAC algorithm to compute (\c PSA_ALG_XXX value
1391*62c56f98SSadaf Ebrahimi  *                          such that #PSA_ALG_IS_MAC(\p alg) is true).
1392*62c56f98SSadaf Ebrahimi  *
1393*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1394*62c56f98SSadaf Ebrahimi  *         Success.
1395*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1396*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1397*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1398*62c56f98SSadaf Ebrahimi  *         \c key is not compatible with \c alg.
1399*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1400*62c56f98SSadaf Ebrahimi  *         \c alg is not supported or is not a MAC algorithm.
1401*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1402*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1403*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1404*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1405*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
1406*62c56f98SSadaf Ebrahimi  *         The key could not be retrieved from storage.
1407*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1408*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
1409*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1410*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1411*62c56f98SSadaf Ebrahimi  *         results in this error code.
1412*62c56f98SSadaf Ebrahimi  */
1413*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1414*62c56f98SSadaf Ebrahimi                                   mbedtls_svc_key_id_t key,
1415*62c56f98SSadaf Ebrahimi                                   psa_algorithm_t alg);
1416*62c56f98SSadaf Ebrahimi 
1417*62c56f98SSadaf Ebrahimi /** Add a message fragment to a multipart MAC operation.
1418*62c56f98SSadaf Ebrahimi  *
1419*62c56f98SSadaf Ebrahimi  * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1420*62c56f98SSadaf Ebrahimi  * before calling this function.
1421*62c56f98SSadaf Ebrahimi  *
1422*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1423*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_mac_abort().
1424*62c56f98SSadaf Ebrahimi  *
1425*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
1426*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the message fragment to add to
1427*62c56f98SSadaf Ebrahimi  *                          the MAC calculation.
1428*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
1429*62c56f98SSadaf Ebrahimi  *
1430*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1431*62c56f98SSadaf Ebrahimi  *         Success.
1432*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1433*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1434*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1435*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1436*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1437*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1438*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or
1439*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1440*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1441*62c56f98SSadaf Ebrahimi  *         results in this error code.
1442*62c56f98SSadaf Ebrahimi  */
1443*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1444*62c56f98SSadaf Ebrahimi                             const uint8_t *input,
1445*62c56f98SSadaf Ebrahimi                             size_t input_length);
1446*62c56f98SSadaf Ebrahimi 
1447*62c56f98SSadaf Ebrahimi /** Finish the calculation of the MAC of a message.
1448*62c56f98SSadaf Ebrahimi  *
1449*62c56f98SSadaf Ebrahimi  * The application must call psa_mac_sign_setup() before calling this function.
1450*62c56f98SSadaf Ebrahimi  * This function calculates the MAC of the message formed by concatenating
1451*62c56f98SSadaf Ebrahimi  * the inputs passed to preceding calls to psa_mac_update().
1452*62c56f98SSadaf Ebrahimi  *
1453*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
1454*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1455*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_mac_abort().
1456*62c56f98SSadaf Ebrahimi  *
1457*62c56f98SSadaf Ebrahimi  * \warning Applications should not call this function if they expect
1458*62c56f98SSadaf Ebrahimi  *          a specific value for the MAC. Call psa_mac_verify_finish() instead.
1459*62c56f98SSadaf Ebrahimi  *          Beware that comparing integrity or authenticity data such as
1460*62c56f98SSadaf Ebrahimi  *          MAC values with a function such as \c memcmp is risky
1461*62c56f98SSadaf Ebrahimi  *          because the time taken by the comparison may leak information
1462*62c56f98SSadaf Ebrahimi  *          about the MAC value which could allow an attacker to guess
1463*62c56f98SSadaf Ebrahimi  *          a valid MAC and thereby bypass security controls.
1464*62c56f98SSadaf Ebrahimi  *
1465*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
1466*62c56f98SSadaf Ebrahimi  * \param[out] mac          Buffer where the MAC value is to be written.
1467*62c56f98SSadaf Ebrahimi  * \param mac_size          Size of the \p mac buffer in bytes.
1468*62c56f98SSadaf Ebrahimi  * \param[out] mac_length   On success, the number of bytes
1469*62c56f98SSadaf Ebrahimi  *                          that make up the MAC value. This is always
1470*62c56f98SSadaf Ebrahimi  *                          #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
1471*62c56f98SSadaf Ebrahimi  *                          where \c key_type and \c key_bits are the type and
1472*62c56f98SSadaf Ebrahimi  *                          bit-size respectively of the key and \c alg is the
1473*62c56f98SSadaf Ebrahimi  *                          MAC algorithm that is calculated.
1474*62c56f98SSadaf Ebrahimi  *
1475*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1476*62c56f98SSadaf Ebrahimi  *         Success.
1477*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1478*62c56f98SSadaf Ebrahimi  *         The size of the \p mac buffer is too small. You can determine a
1479*62c56f98SSadaf Ebrahimi  *         sufficient buffer size by calling PSA_MAC_LENGTH().
1480*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1481*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1482*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1483*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1484*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1485*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1486*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active mac sign
1487*62c56f98SSadaf Ebrahimi  *         operation), or the library has not been previously initialized
1488*62c56f98SSadaf Ebrahimi  *         by psa_crypto_init().
1489*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1490*62c56f98SSadaf Ebrahimi  *         results in this error code.
1491*62c56f98SSadaf Ebrahimi  */
1492*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1493*62c56f98SSadaf Ebrahimi                                  uint8_t *mac,
1494*62c56f98SSadaf Ebrahimi                                  size_t mac_size,
1495*62c56f98SSadaf Ebrahimi                                  size_t *mac_length);
1496*62c56f98SSadaf Ebrahimi 
1497*62c56f98SSadaf Ebrahimi /** Finish the calculation of the MAC of a message and compare it with
1498*62c56f98SSadaf Ebrahimi  * an expected value.
1499*62c56f98SSadaf Ebrahimi  *
1500*62c56f98SSadaf Ebrahimi  * The application must call psa_mac_verify_setup() before calling this function.
1501*62c56f98SSadaf Ebrahimi  * This function calculates the MAC of the message formed by concatenating
1502*62c56f98SSadaf Ebrahimi  * the inputs passed to preceding calls to psa_mac_update(). It then
1503*62c56f98SSadaf Ebrahimi  * compares the calculated MAC with the expected MAC passed as a
1504*62c56f98SSadaf Ebrahimi  * parameter to this function.
1505*62c56f98SSadaf Ebrahimi  *
1506*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
1507*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1508*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_mac_abort().
1509*62c56f98SSadaf Ebrahimi  *
1510*62c56f98SSadaf Ebrahimi  * \note Implementations shall make the best effort to ensure that the
1511*62c56f98SSadaf Ebrahimi  * comparison between the actual MAC and the expected MAC is performed
1512*62c56f98SSadaf Ebrahimi  * in constant time.
1513*62c56f98SSadaf Ebrahimi  *
1514*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
1515*62c56f98SSadaf Ebrahimi  * \param[in] mac           Buffer containing the expected MAC value.
1516*62c56f98SSadaf Ebrahimi  * \param mac_length        Size of the \p mac buffer in bytes.
1517*62c56f98SSadaf Ebrahimi  *
1518*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1519*62c56f98SSadaf Ebrahimi  *         The expected MAC is identical to the actual MAC of the message.
1520*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
1521*62c56f98SSadaf Ebrahimi  *         The MAC of the message was calculated successfully, but it
1522*62c56f98SSadaf Ebrahimi  *         differs from the expected MAC.
1523*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1524*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1525*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1526*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1527*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1528*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1529*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active mac verify
1530*62c56f98SSadaf Ebrahimi  *         operation), or the library has not been previously initialized
1531*62c56f98SSadaf Ebrahimi  *         by psa_crypto_init().
1532*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1533*62c56f98SSadaf Ebrahimi  *         results in this error code.
1534*62c56f98SSadaf Ebrahimi  */
1535*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1536*62c56f98SSadaf Ebrahimi                                    const uint8_t *mac,
1537*62c56f98SSadaf Ebrahimi                                    size_t mac_length);
1538*62c56f98SSadaf Ebrahimi 
1539*62c56f98SSadaf Ebrahimi /** Abort a MAC operation.
1540*62c56f98SSadaf Ebrahimi  *
1541*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the
1542*62c56f98SSadaf Ebrahimi  * \p operation structure itself. Once aborted, the operation object
1543*62c56f98SSadaf Ebrahimi  * can be reused for another operation by calling
1544*62c56f98SSadaf Ebrahimi  * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1545*62c56f98SSadaf Ebrahimi  *
1546*62c56f98SSadaf Ebrahimi  * You may call this function any time after the operation object has
1547*62c56f98SSadaf Ebrahimi  * been initialized by one of the methods described in #psa_mac_operation_t.
1548*62c56f98SSadaf Ebrahimi  *
1549*62c56f98SSadaf Ebrahimi  * In particular, calling psa_mac_abort() after the operation has been
1550*62c56f98SSadaf Ebrahimi  * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1551*62c56f98SSadaf Ebrahimi  * psa_mac_verify_finish() is safe and has no effect.
1552*62c56f98SSadaf Ebrahimi  *
1553*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Initialized MAC operation.
1554*62c56f98SSadaf Ebrahimi  *
1555*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
1556*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1557*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1558*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1559*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1560*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1561*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1562*62c56f98SSadaf Ebrahimi  *         results in this error code.
1563*62c56f98SSadaf Ebrahimi  */
1564*62c56f98SSadaf Ebrahimi psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1565*62c56f98SSadaf Ebrahimi 
1566*62c56f98SSadaf Ebrahimi /**@}*/
1567*62c56f98SSadaf Ebrahimi 
1568*62c56f98SSadaf Ebrahimi /** \defgroup cipher Symmetric ciphers
1569*62c56f98SSadaf Ebrahimi  * @{
1570*62c56f98SSadaf Ebrahimi  */
1571*62c56f98SSadaf Ebrahimi 
1572*62c56f98SSadaf Ebrahimi /** Encrypt a message using a symmetric cipher.
1573*62c56f98SSadaf Ebrahimi  *
1574*62c56f98SSadaf Ebrahimi  * This function encrypts a message with a random IV (initialization
1575*62c56f98SSadaf Ebrahimi  * vector). Use the multipart operation interface with a
1576*62c56f98SSadaf Ebrahimi  * #psa_cipher_operation_t object to provide other forms of IV.
1577*62c56f98SSadaf Ebrahimi  *
1578*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
1579*62c56f98SSadaf Ebrahimi  *                              It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
1580*62c56f98SSadaf Ebrahimi  * \param alg                   The cipher algorithm to compute
1581*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
1582*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_CIPHER(\p alg) is true).
1583*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the message to encrypt.
1584*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
1585*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the output is to be written.
1586*62c56f98SSadaf Ebrahimi  *                              The output contains the IV followed by
1587*62c56f98SSadaf Ebrahimi  *                              the ciphertext proper.
1588*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
1589*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
1590*62c56f98SSadaf Ebrahimi  *                              that make up the output.
1591*62c56f98SSadaf Ebrahimi  *
1592*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1593*62c56f98SSadaf Ebrahimi  *         Success.
1594*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1595*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1596*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1597*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1598*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1599*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a cipher algorithm.
1600*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1601*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1602*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1603*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1604*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1605*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1606*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1607*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1608*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1609*62c56f98SSadaf Ebrahimi  *         results in this error code.
1610*62c56f98SSadaf Ebrahimi  */
1611*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
1612*62c56f98SSadaf Ebrahimi                                 psa_algorithm_t alg,
1613*62c56f98SSadaf Ebrahimi                                 const uint8_t *input,
1614*62c56f98SSadaf Ebrahimi                                 size_t input_length,
1615*62c56f98SSadaf Ebrahimi                                 uint8_t *output,
1616*62c56f98SSadaf Ebrahimi                                 size_t output_size,
1617*62c56f98SSadaf Ebrahimi                                 size_t *output_length);
1618*62c56f98SSadaf Ebrahimi 
1619*62c56f98SSadaf Ebrahimi /** Decrypt a message using a symmetric cipher.
1620*62c56f98SSadaf Ebrahimi  *
1621*62c56f98SSadaf Ebrahimi  * This function decrypts a message encrypted with a symmetric cipher.
1622*62c56f98SSadaf Ebrahimi  *
1623*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
1624*62c56f98SSadaf Ebrahimi  *                              It must remain valid until the operation
1625*62c56f98SSadaf Ebrahimi  *                              terminates. It must allow the usage
1626*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_DECRYPT.
1627*62c56f98SSadaf Ebrahimi  * \param alg                   The cipher algorithm to compute
1628*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
1629*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_CIPHER(\p alg) is true).
1630*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the message to decrypt.
1631*62c56f98SSadaf Ebrahimi  *                              This consists of the IV followed by the
1632*62c56f98SSadaf Ebrahimi  *                              ciphertext proper.
1633*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
1634*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the plaintext is to be written.
1635*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
1636*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
1637*62c56f98SSadaf Ebrahimi  *                              that make up the output.
1638*62c56f98SSadaf Ebrahimi  *
1639*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1640*62c56f98SSadaf Ebrahimi  *         Success.
1641*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1642*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1643*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1644*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1645*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1646*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a cipher algorithm.
1647*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1648*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1649*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1650*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1651*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1652*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1653*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1654*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
1655*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1656*62c56f98SSadaf Ebrahimi  *         results in this error code.
1657*62c56f98SSadaf Ebrahimi  */
1658*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
1659*62c56f98SSadaf Ebrahimi                                 psa_algorithm_t alg,
1660*62c56f98SSadaf Ebrahimi                                 const uint8_t *input,
1661*62c56f98SSadaf Ebrahimi                                 size_t input_length,
1662*62c56f98SSadaf Ebrahimi                                 uint8_t *output,
1663*62c56f98SSadaf Ebrahimi                                 size_t output_size,
1664*62c56f98SSadaf Ebrahimi                                 size_t *output_length);
1665*62c56f98SSadaf Ebrahimi 
1666*62c56f98SSadaf Ebrahimi /** The type of the state data structure for multipart cipher operations.
1667*62c56f98SSadaf Ebrahimi  *
1668*62c56f98SSadaf Ebrahimi  * Before calling any function on a cipher operation object, the application
1669*62c56f98SSadaf Ebrahimi  * must initialize it by any of the following means:
1670*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
1671*62c56f98SSadaf Ebrahimi  *   \code
1672*62c56f98SSadaf Ebrahimi  *   psa_cipher_operation_t operation;
1673*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
1674*62c56f98SSadaf Ebrahimi  *   \endcode
1675*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
1676*62c56f98SSadaf Ebrahimi  *   \code
1677*62c56f98SSadaf Ebrahimi  *   psa_cipher_operation_t operation = {0};
1678*62c56f98SSadaf Ebrahimi  *   \endcode
1679*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1680*62c56f98SSadaf Ebrahimi  *   for example:
1681*62c56f98SSadaf Ebrahimi  *   \code
1682*62c56f98SSadaf Ebrahimi  *   psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1683*62c56f98SSadaf Ebrahimi  *   \endcode
1684*62c56f98SSadaf Ebrahimi  * - Assign the result of the function psa_cipher_operation_init()
1685*62c56f98SSadaf Ebrahimi  *   to the structure, for example:
1686*62c56f98SSadaf Ebrahimi  *   \code
1687*62c56f98SSadaf Ebrahimi  *   psa_cipher_operation_t operation;
1688*62c56f98SSadaf Ebrahimi  *   operation = psa_cipher_operation_init();
1689*62c56f98SSadaf Ebrahimi  *   \endcode
1690*62c56f98SSadaf Ebrahimi  *
1691*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
1692*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
1693*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
1694*62c56f98SSadaf Ebrahimi typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1695*62c56f98SSadaf Ebrahimi 
1696*62c56f98SSadaf Ebrahimi /** \def PSA_CIPHER_OPERATION_INIT
1697*62c56f98SSadaf Ebrahimi  *
1698*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for a cipher operation object of
1699*62c56f98SSadaf Ebrahimi  * type #psa_cipher_operation_t.
1700*62c56f98SSadaf Ebrahimi  */
1701*62c56f98SSadaf Ebrahimi 
1702*62c56f98SSadaf Ebrahimi /** Return an initial value for a cipher operation object.
1703*62c56f98SSadaf Ebrahimi  */
1704*62c56f98SSadaf Ebrahimi static psa_cipher_operation_t psa_cipher_operation_init(void);
1705*62c56f98SSadaf Ebrahimi 
1706*62c56f98SSadaf Ebrahimi /** Set the key for a multipart symmetric encryption operation.
1707*62c56f98SSadaf Ebrahimi  *
1708*62c56f98SSadaf Ebrahimi  * The sequence of operations to encrypt a message with a symmetric cipher
1709*62c56f98SSadaf Ebrahimi  * is as follows:
1710*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
1711*62c56f98SSadaf Ebrahimi  *    listed here.
1712*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
1713*62c56f98SSadaf Ebrahimi  *    documentation for #psa_cipher_operation_t, e.g.
1714*62c56f98SSadaf Ebrahimi  *    #PSA_CIPHER_OPERATION_INIT.
1715*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
1716*62c56f98SSadaf Ebrahimi  * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
1717*62c56f98SSadaf Ebrahimi  *    generate or set the IV (initialization vector). You should use
1718*62c56f98SSadaf Ebrahimi  *    psa_cipher_generate_iv() unless the protocol you are implementing
1719*62c56f98SSadaf Ebrahimi  *    requires a specific IV value.
1720*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1721*62c56f98SSadaf Ebrahimi  *    of the message each time.
1722*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_finish().
1723*62c56f98SSadaf Ebrahimi  *
1724*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1725*62c56f98SSadaf Ebrahimi  * the operation will need to be reset by a call to psa_cipher_abort(). The
1726*62c56f98SSadaf Ebrahimi  * application may call psa_cipher_abort() at any time after the operation
1727*62c56f98SSadaf Ebrahimi  * has been initialized.
1728*62c56f98SSadaf Ebrahimi  *
1729*62c56f98SSadaf Ebrahimi  * After a successful call to psa_cipher_encrypt_setup(), the application must
1730*62c56f98SSadaf Ebrahimi  * eventually terminate the operation. The following events terminate an
1731*62c56f98SSadaf Ebrahimi  * operation:
1732*62c56f98SSadaf Ebrahimi  * - A successful call to psa_cipher_finish().
1733*62c56f98SSadaf Ebrahimi  * - A call to psa_cipher_abort().
1734*62c56f98SSadaf Ebrahimi  *
1735*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
1736*62c56f98SSadaf Ebrahimi  *                              been initialized as per the documentation for
1737*62c56f98SSadaf Ebrahimi  *                              #psa_cipher_operation_t and not yet in use.
1738*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
1739*62c56f98SSadaf Ebrahimi  *                              It must remain valid until the operation
1740*62c56f98SSadaf Ebrahimi  *                              terminates. It must allow the usage
1741*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_ENCRYPT.
1742*62c56f98SSadaf Ebrahimi  * \param alg                   The cipher algorithm to compute
1743*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
1744*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_CIPHER(\p alg) is true).
1745*62c56f98SSadaf Ebrahimi  *
1746*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1747*62c56f98SSadaf Ebrahimi  *         Success.
1748*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1749*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1750*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1751*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1752*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1753*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a cipher algorithm.
1754*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1755*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1756*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1757*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1758*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1759*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1760*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
1761*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1762*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1763*62c56f98SSadaf Ebrahimi  *         results in this error code.
1764*62c56f98SSadaf Ebrahimi  */
1765*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1766*62c56f98SSadaf Ebrahimi                                       mbedtls_svc_key_id_t key,
1767*62c56f98SSadaf Ebrahimi                                       psa_algorithm_t alg);
1768*62c56f98SSadaf Ebrahimi 
1769*62c56f98SSadaf Ebrahimi /** Set the key for a multipart symmetric decryption operation.
1770*62c56f98SSadaf Ebrahimi  *
1771*62c56f98SSadaf Ebrahimi  * The sequence of operations to decrypt a message with a symmetric cipher
1772*62c56f98SSadaf Ebrahimi  * is as follows:
1773*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
1774*62c56f98SSadaf Ebrahimi  *    listed here.
1775*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
1776*62c56f98SSadaf Ebrahimi  *    documentation for #psa_cipher_operation_t, e.g.
1777*62c56f98SSadaf Ebrahimi  *    #PSA_CIPHER_OPERATION_INIT.
1778*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
1779*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
1780*62c56f98SSadaf Ebrahimi  *    decryption. If the IV is prepended to the ciphertext, you can call
1781*62c56f98SSadaf Ebrahimi  *    psa_cipher_update() on a buffer containing the IV followed by the
1782*62c56f98SSadaf Ebrahimi  *    beginning of the message.
1783*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1784*62c56f98SSadaf Ebrahimi  *    of the message each time.
1785*62c56f98SSadaf Ebrahimi  * -# Call psa_cipher_finish().
1786*62c56f98SSadaf Ebrahimi  *
1787*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1788*62c56f98SSadaf Ebrahimi  * the operation will need to be reset by a call to psa_cipher_abort(). The
1789*62c56f98SSadaf Ebrahimi  * application may call psa_cipher_abort() at any time after the operation
1790*62c56f98SSadaf Ebrahimi  * has been initialized.
1791*62c56f98SSadaf Ebrahimi  *
1792*62c56f98SSadaf Ebrahimi  * After a successful call to psa_cipher_decrypt_setup(), the application must
1793*62c56f98SSadaf Ebrahimi  * eventually terminate the operation. The following events terminate an
1794*62c56f98SSadaf Ebrahimi  * operation:
1795*62c56f98SSadaf Ebrahimi  * - A successful call to psa_cipher_finish().
1796*62c56f98SSadaf Ebrahimi  * - A call to psa_cipher_abort().
1797*62c56f98SSadaf Ebrahimi  *
1798*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
1799*62c56f98SSadaf Ebrahimi  *                              been initialized as per the documentation for
1800*62c56f98SSadaf Ebrahimi  *                              #psa_cipher_operation_t and not yet in use.
1801*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
1802*62c56f98SSadaf Ebrahimi  *                              It must remain valid until the operation
1803*62c56f98SSadaf Ebrahimi  *                              terminates. It must allow the usage
1804*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_DECRYPT.
1805*62c56f98SSadaf Ebrahimi  * \param alg                   The cipher algorithm to compute
1806*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
1807*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_CIPHER(\p alg) is true).
1808*62c56f98SSadaf Ebrahimi  *
1809*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1810*62c56f98SSadaf Ebrahimi  *         Success.
1811*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1812*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1813*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1814*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
1815*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
1816*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not a cipher algorithm.
1817*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1818*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1819*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1820*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1821*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1822*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1823*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
1824*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
1825*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1826*62c56f98SSadaf Ebrahimi  *         results in this error code.
1827*62c56f98SSadaf Ebrahimi  */
1828*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1829*62c56f98SSadaf Ebrahimi                                       mbedtls_svc_key_id_t key,
1830*62c56f98SSadaf Ebrahimi                                       psa_algorithm_t alg);
1831*62c56f98SSadaf Ebrahimi 
1832*62c56f98SSadaf Ebrahimi /** Generate an IV for a symmetric encryption operation.
1833*62c56f98SSadaf Ebrahimi  *
1834*62c56f98SSadaf Ebrahimi  * This function generates a random IV (initialization vector), nonce
1835*62c56f98SSadaf Ebrahimi  * or initial counter value for the encryption operation as appropriate
1836*62c56f98SSadaf Ebrahimi  * for the chosen algorithm, key type and key size.
1837*62c56f98SSadaf Ebrahimi  *
1838*62c56f98SSadaf Ebrahimi  * The application must call psa_cipher_encrypt_setup() before
1839*62c56f98SSadaf Ebrahimi  * calling this function.
1840*62c56f98SSadaf Ebrahimi  *
1841*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1842*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_cipher_abort().
1843*62c56f98SSadaf Ebrahimi  *
1844*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active cipher operation.
1845*62c56f98SSadaf Ebrahimi  * \param[out] iv               Buffer where the generated IV is to be written.
1846*62c56f98SSadaf Ebrahimi  * \param iv_size               Size of the \p iv buffer in bytes.
1847*62c56f98SSadaf Ebrahimi  * \param[out] iv_length        On success, the number of bytes of the
1848*62c56f98SSadaf Ebrahimi  *                              generated IV.
1849*62c56f98SSadaf Ebrahimi  *
1850*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1851*62c56f98SSadaf Ebrahimi  *         Success.
1852*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1853*62c56f98SSadaf Ebrahimi  *         The size of the \p iv buffer is too small.
1854*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1855*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1856*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1857*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1858*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1859*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1860*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, with no IV set),
1861*62c56f98SSadaf Ebrahimi  *         or the library has not been previously initialized
1862*62c56f98SSadaf Ebrahimi  *         by psa_crypto_init().
1863*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1864*62c56f98SSadaf Ebrahimi  *         results in this error code.
1865*62c56f98SSadaf Ebrahimi  */
1866*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1867*62c56f98SSadaf Ebrahimi                                     uint8_t *iv,
1868*62c56f98SSadaf Ebrahimi                                     size_t iv_size,
1869*62c56f98SSadaf Ebrahimi                                     size_t *iv_length);
1870*62c56f98SSadaf Ebrahimi 
1871*62c56f98SSadaf Ebrahimi /** Set the IV for a symmetric encryption or decryption operation.
1872*62c56f98SSadaf Ebrahimi  *
1873*62c56f98SSadaf Ebrahimi  * This function sets the IV (initialization vector), nonce
1874*62c56f98SSadaf Ebrahimi  * or initial counter value for the encryption or decryption operation.
1875*62c56f98SSadaf Ebrahimi  *
1876*62c56f98SSadaf Ebrahimi  * The application must call psa_cipher_encrypt_setup() before
1877*62c56f98SSadaf Ebrahimi  * calling this function.
1878*62c56f98SSadaf Ebrahimi  *
1879*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1880*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_cipher_abort().
1881*62c56f98SSadaf Ebrahimi  *
1882*62c56f98SSadaf Ebrahimi  * \note When encrypting, applications should use psa_cipher_generate_iv()
1883*62c56f98SSadaf Ebrahimi  * instead of this function, unless implementing a protocol that requires
1884*62c56f98SSadaf Ebrahimi  * a non-random IV.
1885*62c56f98SSadaf Ebrahimi  *
1886*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active cipher operation.
1887*62c56f98SSadaf Ebrahimi  * \param[in] iv                Buffer containing the IV to use.
1888*62c56f98SSadaf Ebrahimi  * \param iv_length             Size of the IV in bytes.
1889*62c56f98SSadaf Ebrahimi  *
1890*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1891*62c56f98SSadaf Ebrahimi  *         Success.
1892*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1893*62c56f98SSadaf Ebrahimi  *         The size of \p iv is not acceptable for the chosen algorithm,
1894*62c56f98SSadaf Ebrahimi  *         or the chosen algorithm does not use an IV.
1895*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1896*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1897*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1898*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1899*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1900*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1901*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active cipher
1902*62c56f98SSadaf Ebrahimi  *         encrypt operation, with no IV set), or the library has not been
1903*62c56f98SSadaf Ebrahimi  *         previously initialized by psa_crypto_init().
1904*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1905*62c56f98SSadaf Ebrahimi  *         results in this error code.
1906*62c56f98SSadaf Ebrahimi  */
1907*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1908*62c56f98SSadaf Ebrahimi                                const uint8_t *iv,
1909*62c56f98SSadaf Ebrahimi                                size_t iv_length);
1910*62c56f98SSadaf Ebrahimi 
1911*62c56f98SSadaf Ebrahimi /** Encrypt or decrypt a message fragment in an active cipher operation.
1912*62c56f98SSadaf Ebrahimi  *
1913*62c56f98SSadaf Ebrahimi  * Before calling this function, you must:
1914*62c56f98SSadaf Ebrahimi  * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1915*62c56f98SSadaf Ebrahimi  *    The choice of setup function determines whether this function
1916*62c56f98SSadaf Ebrahimi  *    encrypts or decrypts its input.
1917*62c56f98SSadaf Ebrahimi  * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1918*62c56f98SSadaf Ebrahimi  *    (recommended when encrypting) or psa_cipher_set_iv().
1919*62c56f98SSadaf Ebrahimi  *
1920*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1921*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_cipher_abort().
1922*62c56f98SSadaf Ebrahimi  *
1923*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active cipher operation.
1924*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the message fragment to
1925*62c56f98SSadaf Ebrahimi  *                              encrypt or decrypt.
1926*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
1927*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the output is to be written.
1928*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
1929*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
1930*62c56f98SSadaf Ebrahimi  *                              that make up the returned output.
1931*62c56f98SSadaf Ebrahimi  *
1932*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1933*62c56f98SSadaf Ebrahimi  *         Success.
1934*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1935*62c56f98SSadaf Ebrahimi  *         The size of the \p output buffer is too small.
1936*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1937*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1938*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1939*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1940*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1941*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1942*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, with an IV set
1943*62c56f98SSadaf Ebrahimi  *         if required for the algorithm), or the library has not been
1944*62c56f98SSadaf Ebrahimi  *         previously initialized by psa_crypto_init().
1945*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1946*62c56f98SSadaf Ebrahimi  *         results in this error code.
1947*62c56f98SSadaf Ebrahimi  */
1948*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1949*62c56f98SSadaf Ebrahimi                                const uint8_t *input,
1950*62c56f98SSadaf Ebrahimi                                size_t input_length,
1951*62c56f98SSadaf Ebrahimi                                uint8_t *output,
1952*62c56f98SSadaf Ebrahimi                                size_t output_size,
1953*62c56f98SSadaf Ebrahimi                                size_t *output_length);
1954*62c56f98SSadaf Ebrahimi 
1955*62c56f98SSadaf Ebrahimi /** Finish encrypting or decrypting a message in a cipher operation.
1956*62c56f98SSadaf Ebrahimi  *
1957*62c56f98SSadaf Ebrahimi  * The application must call psa_cipher_encrypt_setup() or
1958*62c56f98SSadaf Ebrahimi  * psa_cipher_decrypt_setup() before calling this function. The choice
1959*62c56f98SSadaf Ebrahimi  * of setup function determines whether this function encrypts or
1960*62c56f98SSadaf Ebrahimi  * decrypts its input.
1961*62c56f98SSadaf Ebrahimi  *
1962*62c56f98SSadaf Ebrahimi  * This function finishes the encryption or decryption of the message
1963*62c56f98SSadaf Ebrahimi  * formed by concatenating the inputs passed to preceding calls to
1964*62c56f98SSadaf Ebrahimi  * psa_cipher_update().
1965*62c56f98SSadaf Ebrahimi  *
1966*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
1967*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
1968*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_cipher_abort().
1969*62c56f98SSadaf Ebrahimi  *
1970*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active cipher operation.
1971*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the output is to be written.
1972*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
1973*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
1974*62c56f98SSadaf Ebrahimi  *                              that make up the returned output.
1975*62c56f98SSadaf Ebrahimi  *
1976*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
1977*62c56f98SSadaf Ebrahimi  *         Success.
1978*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
1979*62c56f98SSadaf Ebrahimi  *         The total input size passed to this operation is not valid for
1980*62c56f98SSadaf Ebrahimi  *         this particular algorithm. For example, the algorithm is a based
1981*62c56f98SSadaf Ebrahimi  *         on block cipher and requires a whole number of blocks, but the
1982*62c56f98SSadaf Ebrahimi  *         total input size is not a multiple of the block size.
1983*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_PADDING
1984*62c56f98SSadaf Ebrahimi  *         This is a decryption operation for an algorithm that includes
1985*62c56f98SSadaf Ebrahimi  *         padding, and the ciphertext does not contain valid padding.
1986*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1987*62c56f98SSadaf Ebrahimi  *         The size of the \p output buffer is too small.
1988*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1989*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1990*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1991*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1992*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1993*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
1994*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, with an IV set
1995*62c56f98SSadaf Ebrahimi  *         if required for the algorithm), or the library has not been
1996*62c56f98SSadaf Ebrahimi  *         previously initialized by psa_crypto_init().
1997*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
1998*62c56f98SSadaf Ebrahimi  *         results in this error code.
1999*62c56f98SSadaf Ebrahimi  */
2000*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2001*62c56f98SSadaf Ebrahimi                                uint8_t *output,
2002*62c56f98SSadaf Ebrahimi                                size_t output_size,
2003*62c56f98SSadaf Ebrahimi                                size_t *output_length);
2004*62c56f98SSadaf Ebrahimi 
2005*62c56f98SSadaf Ebrahimi /** Abort a cipher operation.
2006*62c56f98SSadaf Ebrahimi  *
2007*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the
2008*62c56f98SSadaf Ebrahimi  * \p operation structure itself. Once aborted, the operation object
2009*62c56f98SSadaf Ebrahimi  * can be reused for another operation by calling
2010*62c56f98SSadaf Ebrahimi  * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2011*62c56f98SSadaf Ebrahimi  *
2012*62c56f98SSadaf Ebrahimi  * You may call this function any time after the operation object has
2013*62c56f98SSadaf Ebrahimi  * been initialized as described in #psa_cipher_operation_t.
2014*62c56f98SSadaf Ebrahimi  *
2015*62c56f98SSadaf Ebrahimi  * In particular, calling psa_cipher_abort() after the operation has been
2016*62c56f98SSadaf Ebrahimi  * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2017*62c56f98SSadaf Ebrahimi  * is safe and has no effect.
2018*62c56f98SSadaf Ebrahimi  *
2019*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Initialized cipher operation.
2020*62c56f98SSadaf Ebrahimi  *
2021*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
2022*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2023*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2024*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2025*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2026*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2027*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2028*62c56f98SSadaf Ebrahimi  *         results in this error code.
2029*62c56f98SSadaf Ebrahimi  */
2030*62c56f98SSadaf Ebrahimi psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2031*62c56f98SSadaf Ebrahimi 
2032*62c56f98SSadaf Ebrahimi /**@}*/
2033*62c56f98SSadaf Ebrahimi 
2034*62c56f98SSadaf Ebrahimi /** \defgroup aead Authenticated encryption with associated data (AEAD)
2035*62c56f98SSadaf Ebrahimi  * @{
2036*62c56f98SSadaf Ebrahimi  */
2037*62c56f98SSadaf Ebrahimi 
2038*62c56f98SSadaf Ebrahimi /** Process an authenticated encryption operation.
2039*62c56f98SSadaf Ebrahimi  *
2040*62c56f98SSadaf Ebrahimi  * \param key                     Identifier of the key to use for the
2041*62c56f98SSadaf Ebrahimi  *                                operation. It must allow the usage
2042*62c56f98SSadaf Ebrahimi  *                                #PSA_KEY_USAGE_ENCRYPT.
2043*62c56f98SSadaf Ebrahimi  * \param alg                     The AEAD algorithm to compute
2044*62c56f98SSadaf Ebrahimi  *                                (\c PSA_ALG_XXX value such that
2045*62c56f98SSadaf Ebrahimi  *                                #PSA_ALG_IS_AEAD(\p alg) is true).
2046*62c56f98SSadaf Ebrahimi  * \param[in] nonce               Nonce or IV to use.
2047*62c56f98SSadaf Ebrahimi  * \param nonce_length            Size of the \p nonce buffer in bytes.
2048*62c56f98SSadaf Ebrahimi  * \param[in] additional_data     Additional data that will be authenticated
2049*62c56f98SSadaf Ebrahimi  *                                but not encrypted.
2050*62c56f98SSadaf Ebrahimi  * \param additional_data_length  Size of \p additional_data in bytes.
2051*62c56f98SSadaf Ebrahimi  * \param[in] plaintext           Data that will be authenticated and
2052*62c56f98SSadaf Ebrahimi  *                                encrypted.
2053*62c56f98SSadaf Ebrahimi  * \param plaintext_length        Size of \p plaintext in bytes.
2054*62c56f98SSadaf Ebrahimi  * \param[out] ciphertext         Output buffer for the authenticated and
2055*62c56f98SSadaf Ebrahimi  *                                encrypted data. The additional data is not
2056*62c56f98SSadaf Ebrahimi  *                                part of this output. For algorithms where the
2057*62c56f98SSadaf Ebrahimi  *                                encrypted data and the authentication tag
2058*62c56f98SSadaf Ebrahimi  *                                are defined as separate outputs, the
2059*62c56f98SSadaf Ebrahimi  *                                authentication tag is appended to the
2060*62c56f98SSadaf Ebrahimi  *                                encrypted data.
2061*62c56f98SSadaf Ebrahimi  * \param ciphertext_size         Size of the \p ciphertext buffer in bytes.
2062*62c56f98SSadaf Ebrahimi  *                                This must be appropriate for the selected
2063*62c56f98SSadaf Ebrahimi  *                                algorithm and key:
2064*62c56f98SSadaf Ebrahimi  *                                - A sufficient output size is
2065*62c56f98SSadaf Ebrahimi  *                                  #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2066*62c56f98SSadaf Ebrahimi  *                                  \p alg, \p plaintext_length) where
2067*62c56f98SSadaf Ebrahimi  *                                  \c key_type is the type of \p key.
2068*62c56f98SSadaf Ebrahimi  *                                - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2069*62c56f98SSadaf Ebrahimi  *                                  plaintext_length) evaluates to the maximum
2070*62c56f98SSadaf Ebrahimi  *                                  ciphertext size of any supported AEAD
2071*62c56f98SSadaf Ebrahimi  *                                  encryption.
2072*62c56f98SSadaf Ebrahimi  * \param[out] ciphertext_length  On success, the size of the output
2073*62c56f98SSadaf Ebrahimi  *                                in the \p ciphertext buffer.
2074*62c56f98SSadaf Ebrahimi  *
2075*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2076*62c56f98SSadaf Ebrahimi  *         Success.
2077*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2078*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2079*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2080*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
2081*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
2082*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not an AEAD algorithm.
2083*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2084*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2085*62c56f98SSadaf Ebrahimi  *         \p ciphertext_size is too small.
2086*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2087*62c56f98SSadaf Ebrahimi  *         \p plaintext_length) or
2088*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2089*62c56f98SSadaf Ebrahimi  *         determine the required buffer size.
2090*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2091*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2092*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2093*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2094*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2095*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2096*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2097*62c56f98SSadaf Ebrahimi  *         results in this error code.
2098*62c56f98SSadaf Ebrahimi  */
2099*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
2100*62c56f98SSadaf Ebrahimi                               psa_algorithm_t alg,
2101*62c56f98SSadaf Ebrahimi                               const uint8_t *nonce,
2102*62c56f98SSadaf Ebrahimi                               size_t nonce_length,
2103*62c56f98SSadaf Ebrahimi                               const uint8_t *additional_data,
2104*62c56f98SSadaf Ebrahimi                               size_t additional_data_length,
2105*62c56f98SSadaf Ebrahimi                               const uint8_t *plaintext,
2106*62c56f98SSadaf Ebrahimi                               size_t plaintext_length,
2107*62c56f98SSadaf Ebrahimi                               uint8_t *ciphertext,
2108*62c56f98SSadaf Ebrahimi                               size_t ciphertext_size,
2109*62c56f98SSadaf Ebrahimi                               size_t *ciphertext_length);
2110*62c56f98SSadaf Ebrahimi 
2111*62c56f98SSadaf Ebrahimi /** Process an authenticated decryption operation.
2112*62c56f98SSadaf Ebrahimi  *
2113*62c56f98SSadaf Ebrahimi  * \param key                     Identifier of the key to use for the
2114*62c56f98SSadaf Ebrahimi  *                                operation. It must allow the usage
2115*62c56f98SSadaf Ebrahimi  *                                #PSA_KEY_USAGE_DECRYPT.
2116*62c56f98SSadaf Ebrahimi  * \param alg                     The AEAD algorithm to compute
2117*62c56f98SSadaf Ebrahimi  *                                (\c PSA_ALG_XXX value such that
2118*62c56f98SSadaf Ebrahimi  *                                #PSA_ALG_IS_AEAD(\p alg) is true).
2119*62c56f98SSadaf Ebrahimi  * \param[in] nonce               Nonce or IV to use.
2120*62c56f98SSadaf Ebrahimi  * \param nonce_length            Size of the \p nonce buffer in bytes.
2121*62c56f98SSadaf Ebrahimi  * \param[in] additional_data     Additional data that has been authenticated
2122*62c56f98SSadaf Ebrahimi  *                                but not encrypted.
2123*62c56f98SSadaf Ebrahimi  * \param additional_data_length  Size of \p additional_data in bytes.
2124*62c56f98SSadaf Ebrahimi  * \param[in] ciphertext          Data that has been authenticated and
2125*62c56f98SSadaf Ebrahimi  *                                encrypted. For algorithms where the
2126*62c56f98SSadaf Ebrahimi  *                                encrypted data and the authentication tag
2127*62c56f98SSadaf Ebrahimi  *                                are defined as separate inputs, the buffer
2128*62c56f98SSadaf Ebrahimi  *                                must contain the encrypted data followed
2129*62c56f98SSadaf Ebrahimi  *                                by the authentication tag.
2130*62c56f98SSadaf Ebrahimi  * \param ciphertext_length       Size of \p ciphertext in bytes.
2131*62c56f98SSadaf Ebrahimi  * \param[out] plaintext          Output buffer for the decrypted data.
2132*62c56f98SSadaf Ebrahimi  * \param plaintext_size          Size of the \p plaintext buffer in bytes.
2133*62c56f98SSadaf Ebrahimi  *                                This must be appropriate for the selected
2134*62c56f98SSadaf Ebrahimi  *                                algorithm and key:
2135*62c56f98SSadaf Ebrahimi  *                                - A sufficient output size is
2136*62c56f98SSadaf Ebrahimi  *                                  #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2137*62c56f98SSadaf Ebrahimi  *                                  \p alg, \p ciphertext_length) where
2138*62c56f98SSadaf Ebrahimi  *                                  \c key_type is the type of \p key.
2139*62c56f98SSadaf Ebrahimi  *                                - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2140*62c56f98SSadaf Ebrahimi  *                                  ciphertext_length) evaluates to the maximum
2141*62c56f98SSadaf Ebrahimi  *                                  plaintext size of any supported AEAD
2142*62c56f98SSadaf Ebrahimi  *                                  decryption.
2143*62c56f98SSadaf Ebrahimi  * \param[out] plaintext_length   On success, the size of the output
2144*62c56f98SSadaf Ebrahimi  *                                in the \p plaintext buffer.
2145*62c56f98SSadaf Ebrahimi  *
2146*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2147*62c56f98SSadaf Ebrahimi  *         Success.
2148*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2149*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
2150*62c56f98SSadaf Ebrahimi  *         The ciphertext is not authentic.
2151*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2152*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2153*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
2154*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
2155*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not an AEAD algorithm.
2156*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2157*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2158*62c56f98SSadaf Ebrahimi  *         \p plaintext_size is too small.
2159*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2160*62c56f98SSadaf Ebrahimi  *         \p ciphertext_length) or
2161*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2162*62c56f98SSadaf Ebrahimi  *         to determine the required buffer size.
2163*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2164*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2165*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2166*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2167*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2168*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2169*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2170*62c56f98SSadaf Ebrahimi  *         results in this error code.
2171*62c56f98SSadaf Ebrahimi  */
2172*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
2173*62c56f98SSadaf Ebrahimi                               psa_algorithm_t alg,
2174*62c56f98SSadaf Ebrahimi                               const uint8_t *nonce,
2175*62c56f98SSadaf Ebrahimi                               size_t nonce_length,
2176*62c56f98SSadaf Ebrahimi                               const uint8_t *additional_data,
2177*62c56f98SSadaf Ebrahimi                               size_t additional_data_length,
2178*62c56f98SSadaf Ebrahimi                               const uint8_t *ciphertext,
2179*62c56f98SSadaf Ebrahimi                               size_t ciphertext_length,
2180*62c56f98SSadaf Ebrahimi                               uint8_t *plaintext,
2181*62c56f98SSadaf Ebrahimi                               size_t plaintext_size,
2182*62c56f98SSadaf Ebrahimi                               size_t *plaintext_length);
2183*62c56f98SSadaf Ebrahimi 
2184*62c56f98SSadaf Ebrahimi /** The type of the state data structure for multipart AEAD operations.
2185*62c56f98SSadaf Ebrahimi  *
2186*62c56f98SSadaf Ebrahimi  * Before calling any function on an AEAD operation object, the application
2187*62c56f98SSadaf Ebrahimi  * must initialize it by any of the following means:
2188*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
2189*62c56f98SSadaf Ebrahimi  *   \code
2190*62c56f98SSadaf Ebrahimi  *   psa_aead_operation_t operation;
2191*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
2192*62c56f98SSadaf Ebrahimi  *   \endcode
2193*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
2194*62c56f98SSadaf Ebrahimi  *   \code
2195*62c56f98SSadaf Ebrahimi  *   psa_aead_operation_t operation = {0};
2196*62c56f98SSadaf Ebrahimi  *   \endcode
2197*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2198*62c56f98SSadaf Ebrahimi  *   for example:
2199*62c56f98SSadaf Ebrahimi  *   \code
2200*62c56f98SSadaf Ebrahimi  *   psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2201*62c56f98SSadaf Ebrahimi  *   \endcode
2202*62c56f98SSadaf Ebrahimi  * - Assign the result of the function psa_aead_operation_init()
2203*62c56f98SSadaf Ebrahimi  *   to the structure, for example:
2204*62c56f98SSadaf Ebrahimi  *   \code
2205*62c56f98SSadaf Ebrahimi  *   psa_aead_operation_t operation;
2206*62c56f98SSadaf Ebrahimi  *   operation = psa_aead_operation_init();
2207*62c56f98SSadaf Ebrahimi  *   \endcode
2208*62c56f98SSadaf Ebrahimi  *
2209*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
2210*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
2211*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
2212*62c56f98SSadaf Ebrahimi typedef struct psa_aead_operation_s psa_aead_operation_t;
2213*62c56f98SSadaf Ebrahimi 
2214*62c56f98SSadaf Ebrahimi /** \def PSA_AEAD_OPERATION_INIT
2215*62c56f98SSadaf Ebrahimi  *
2216*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for an AEAD operation object of
2217*62c56f98SSadaf Ebrahimi  * type #psa_aead_operation_t.
2218*62c56f98SSadaf Ebrahimi  */
2219*62c56f98SSadaf Ebrahimi 
2220*62c56f98SSadaf Ebrahimi /** Return an initial value for an AEAD operation object.
2221*62c56f98SSadaf Ebrahimi  */
2222*62c56f98SSadaf Ebrahimi static psa_aead_operation_t psa_aead_operation_init(void);
2223*62c56f98SSadaf Ebrahimi 
2224*62c56f98SSadaf Ebrahimi /** Set the key for a multipart authenticated encryption operation.
2225*62c56f98SSadaf Ebrahimi  *
2226*62c56f98SSadaf Ebrahimi  * The sequence of operations to encrypt a message with authentication
2227*62c56f98SSadaf Ebrahimi  * is as follows:
2228*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
2229*62c56f98SSadaf Ebrahimi  *    listed here.
2230*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
2231*62c56f98SSadaf Ebrahimi  *    documentation for #psa_aead_operation_t, e.g.
2232*62c56f98SSadaf Ebrahimi  *    #PSA_AEAD_OPERATION_INIT.
2233*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2234*62c56f98SSadaf Ebrahimi  * -# If needed, call psa_aead_set_lengths() to specify the length of the
2235*62c56f98SSadaf Ebrahimi  *    inputs to the subsequent calls to psa_aead_update_ad() and
2236*62c56f98SSadaf Ebrahimi  *    psa_aead_update(). See the documentation of psa_aead_set_lengths()
2237*62c56f98SSadaf Ebrahimi  *    for details.
2238*62c56f98SSadaf Ebrahimi  * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2239*62c56f98SSadaf Ebrahimi  *    generate or set the nonce. You should use
2240*62c56f98SSadaf Ebrahimi  *    psa_aead_generate_nonce() unless the protocol you are implementing
2241*62c56f98SSadaf Ebrahimi  *    requires a specific nonce value.
2242*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2243*62c56f98SSadaf Ebrahimi  *    of the non-encrypted additional authenticated data each time.
2244*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_update() zero, one or more times, passing a fragment
2245*62c56f98SSadaf Ebrahimi  *    of the message to encrypt each time.
2246*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_finish().
2247*62c56f98SSadaf Ebrahimi  *
2248*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2249*62c56f98SSadaf Ebrahimi  * the operation will need to be reset by a call to psa_aead_abort(). The
2250*62c56f98SSadaf Ebrahimi  * application may call psa_aead_abort() at any time after the operation
2251*62c56f98SSadaf Ebrahimi  * has been initialized.
2252*62c56f98SSadaf Ebrahimi  *
2253*62c56f98SSadaf Ebrahimi  * After a successful call to psa_aead_encrypt_setup(), the application must
2254*62c56f98SSadaf Ebrahimi  * eventually terminate the operation. The following events terminate an
2255*62c56f98SSadaf Ebrahimi  * operation:
2256*62c56f98SSadaf Ebrahimi  * - A successful call to psa_aead_finish().
2257*62c56f98SSadaf Ebrahimi  * - A call to psa_aead_abort().
2258*62c56f98SSadaf Ebrahimi  *
2259*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
2260*62c56f98SSadaf Ebrahimi  *                              been initialized as per the documentation for
2261*62c56f98SSadaf Ebrahimi  *                              #psa_aead_operation_t and not yet in use.
2262*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
2263*62c56f98SSadaf Ebrahimi  *                              It must remain valid until the operation
2264*62c56f98SSadaf Ebrahimi  *                              terminates. It must allow the usage
2265*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_ENCRYPT.
2266*62c56f98SSadaf Ebrahimi  * \param alg                   The AEAD algorithm to compute
2267*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
2268*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_AEAD(\p alg) is true).
2269*62c56f98SSadaf Ebrahimi  *
2270*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2271*62c56f98SSadaf Ebrahimi  *         Success.
2272*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2273*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
2274*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
2275*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2276*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2277*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2278*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
2279*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
2280*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not an AEAD algorithm.
2281*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2282*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2283*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2284*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2285*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE
2286*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2287*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2288*62c56f98SSadaf Ebrahimi  *         results in this error code.
2289*62c56f98SSadaf Ebrahimi  */
2290*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2291*62c56f98SSadaf Ebrahimi                                     mbedtls_svc_key_id_t key,
2292*62c56f98SSadaf Ebrahimi                                     psa_algorithm_t alg);
2293*62c56f98SSadaf Ebrahimi 
2294*62c56f98SSadaf Ebrahimi /** Set the key for a multipart authenticated decryption operation.
2295*62c56f98SSadaf Ebrahimi  *
2296*62c56f98SSadaf Ebrahimi  * The sequence of operations to decrypt a message with authentication
2297*62c56f98SSadaf Ebrahimi  * is as follows:
2298*62c56f98SSadaf Ebrahimi  * -# Allocate an operation object which will be passed to all the functions
2299*62c56f98SSadaf Ebrahimi  *    listed here.
2300*62c56f98SSadaf Ebrahimi  * -# Initialize the operation object with one of the methods described in the
2301*62c56f98SSadaf Ebrahimi  *    documentation for #psa_aead_operation_t, e.g.
2302*62c56f98SSadaf Ebrahimi  *    #PSA_AEAD_OPERATION_INIT.
2303*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2304*62c56f98SSadaf Ebrahimi  * -# If needed, call psa_aead_set_lengths() to specify the length of the
2305*62c56f98SSadaf Ebrahimi  *    inputs to the subsequent calls to psa_aead_update_ad() and
2306*62c56f98SSadaf Ebrahimi  *    psa_aead_update(). See the documentation of psa_aead_set_lengths()
2307*62c56f98SSadaf Ebrahimi  *    for details.
2308*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2309*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2310*62c56f98SSadaf Ebrahimi  *    of the non-encrypted additional authenticated data each time.
2311*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_update() zero, one or more times, passing a fragment
2312*62c56f98SSadaf Ebrahimi  *    of the ciphertext to decrypt each time.
2313*62c56f98SSadaf Ebrahimi  * -# Call psa_aead_verify().
2314*62c56f98SSadaf Ebrahimi  *
2315*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2316*62c56f98SSadaf Ebrahimi  * the operation will need to be reset by a call to psa_aead_abort(). The
2317*62c56f98SSadaf Ebrahimi  * application may call psa_aead_abort() at any time after the operation
2318*62c56f98SSadaf Ebrahimi  * has been initialized.
2319*62c56f98SSadaf Ebrahimi  *
2320*62c56f98SSadaf Ebrahimi  * After a successful call to psa_aead_decrypt_setup(), the application must
2321*62c56f98SSadaf Ebrahimi  * eventually terminate the operation. The following events terminate an
2322*62c56f98SSadaf Ebrahimi  * operation:
2323*62c56f98SSadaf Ebrahimi  * - A successful call to psa_aead_verify().
2324*62c56f98SSadaf Ebrahimi  * - A call to psa_aead_abort().
2325*62c56f98SSadaf Ebrahimi  *
2326*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
2327*62c56f98SSadaf Ebrahimi  *                              been initialized as per the documentation for
2328*62c56f98SSadaf Ebrahimi  *                              #psa_aead_operation_t and not yet in use.
2329*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
2330*62c56f98SSadaf Ebrahimi  *                              It must remain valid until the operation
2331*62c56f98SSadaf Ebrahimi  *                              terminates. It must allow the usage
2332*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_DECRYPT.
2333*62c56f98SSadaf Ebrahimi  * \param alg                   The AEAD algorithm to compute
2334*62c56f98SSadaf Ebrahimi  *                              (\c PSA_ALG_XXX value such that
2335*62c56f98SSadaf Ebrahimi  *                              #PSA_ALG_IS_AEAD(\p alg) is true).
2336*62c56f98SSadaf Ebrahimi  *
2337*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2338*62c56f98SSadaf Ebrahimi  *         Success.
2339*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2340*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2341*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2342*62c56f98SSadaf Ebrahimi  *         \p key is not compatible with \p alg.
2343*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
2344*62c56f98SSadaf Ebrahimi  *         \p alg is not supported or is not an AEAD algorithm.
2345*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2346*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2347*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2348*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2349*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2350*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2351*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or the
2352*62c56f98SSadaf Ebrahimi  *         library has not been previously initialized by psa_crypto_init().
2353*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2354*62c56f98SSadaf Ebrahimi  *         results in this error code.
2355*62c56f98SSadaf Ebrahimi  */
2356*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2357*62c56f98SSadaf Ebrahimi                                     mbedtls_svc_key_id_t key,
2358*62c56f98SSadaf Ebrahimi                                     psa_algorithm_t alg);
2359*62c56f98SSadaf Ebrahimi 
2360*62c56f98SSadaf Ebrahimi /** Generate a random nonce for an authenticated encryption operation.
2361*62c56f98SSadaf Ebrahimi  *
2362*62c56f98SSadaf Ebrahimi  * This function generates a random nonce for the authenticated encryption
2363*62c56f98SSadaf Ebrahimi  * operation with an appropriate size for the chosen algorithm, key type
2364*62c56f98SSadaf Ebrahimi  * and key size.
2365*62c56f98SSadaf Ebrahimi  *
2366*62c56f98SSadaf Ebrahimi  * The application must call psa_aead_encrypt_setup() before
2367*62c56f98SSadaf Ebrahimi  * calling this function.
2368*62c56f98SSadaf Ebrahimi  *
2369*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2370*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2371*62c56f98SSadaf Ebrahimi  *
2372*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2373*62c56f98SSadaf Ebrahimi  * \param[out] nonce            Buffer where the generated nonce is to be
2374*62c56f98SSadaf Ebrahimi  *                              written.
2375*62c56f98SSadaf Ebrahimi  * \param nonce_size            Size of the \p nonce buffer in bytes.
2376*62c56f98SSadaf Ebrahimi  * \param[out] nonce_length     On success, the number of bytes of the
2377*62c56f98SSadaf Ebrahimi  *                              generated nonce.
2378*62c56f98SSadaf Ebrahimi  *
2379*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2380*62c56f98SSadaf Ebrahimi  *         Success.
2381*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2382*62c56f98SSadaf Ebrahimi  *         The size of the \p nonce buffer is too small.
2383*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2384*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2385*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2386*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2387*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2388*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2389*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active aead encrypt
2390*62c56f98SSadaf Ebrahimi  *         operation, with no nonce set), or the library has not been
2391*62c56f98SSadaf Ebrahimi  *         previously initialized by psa_crypto_init().
2392*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2393*62c56f98SSadaf Ebrahimi  *         results in this error code.
2394*62c56f98SSadaf Ebrahimi  */
2395*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2396*62c56f98SSadaf Ebrahimi                                      uint8_t *nonce,
2397*62c56f98SSadaf Ebrahimi                                      size_t nonce_size,
2398*62c56f98SSadaf Ebrahimi                                      size_t *nonce_length);
2399*62c56f98SSadaf Ebrahimi 
2400*62c56f98SSadaf Ebrahimi /** Set the nonce for an authenticated encryption or decryption operation.
2401*62c56f98SSadaf Ebrahimi  *
2402*62c56f98SSadaf Ebrahimi  * This function sets the nonce for the authenticated
2403*62c56f98SSadaf Ebrahimi  * encryption or decryption operation.
2404*62c56f98SSadaf Ebrahimi  *
2405*62c56f98SSadaf Ebrahimi  * The application must call psa_aead_encrypt_setup() or
2406*62c56f98SSadaf Ebrahimi  * psa_aead_decrypt_setup() before calling this function.
2407*62c56f98SSadaf Ebrahimi  *
2408*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2409*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2410*62c56f98SSadaf Ebrahimi  *
2411*62c56f98SSadaf Ebrahimi  * \note When encrypting, applications should use psa_aead_generate_nonce()
2412*62c56f98SSadaf Ebrahimi  * instead of this function, unless implementing a protocol that requires
2413*62c56f98SSadaf Ebrahimi  * a non-random IV.
2414*62c56f98SSadaf Ebrahimi  *
2415*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2416*62c56f98SSadaf Ebrahimi  * \param[in] nonce             Buffer containing the nonce to use.
2417*62c56f98SSadaf Ebrahimi  * \param nonce_length          Size of the nonce in bytes.
2418*62c56f98SSadaf Ebrahimi  *
2419*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2420*62c56f98SSadaf Ebrahimi  *         Success.
2421*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2422*62c56f98SSadaf Ebrahimi  *         The size of \p nonce is not acceptable for the chosen algorithm.
2423*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2424*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2425*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2426*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2427*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2428*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2429*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, with no nonce
2430*62c56f98SSadaf Ebrahimi  *         set), or the library has not been previously initialized
2431*62c56f98SSadaf Ebrahimi  *         by psa_crypto_init().
2432*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2433*62c56f98SSadaf Ebrahimi  *         results in this error code.
2434*62c56f98SSadaf Ebrahimi  */
2435*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2436*62c56f98SSadaf Ebrahimi                                 const uint8_t *nonce,
2437*62c56f98SSadaf Ebrahimi                                 size_t nonce_length);
2438*62c56f98SSadaf Ebrahimi 
2439*62c56f98SSadaf Ebrahimi /** Declare the lengths of the message and additional data for AEAD.
2440*62c56f98SSadaf Ebrahimi  *
2441*62c56f98SSadaf Ebrahimi  * The application must call this function before calling
2442*62c56f98SSadaf Ebrahimi  * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2443*62c56f98SSadaf Ebrahimi  * the operation requires it. If the algorithm does not require it,
2444*62c56f98SSadaf Ebrahimi  * calling this function is optional, but if this function is called
2445*62c56f98SSadaf Ebrahimi  * then the implementation must enforce the lengths.
2446*62c56f98SSadaf Ebrahimi  *
2447*62c56f98SSadaf Ebrahimi  * You may call this function before or after setting the nonce with
2448*62c56f98SSadaf Ebrahimi  * psa_aead_set_nonce() or psa_aead_generate_nonce().
2449*62c56f98SSadaf Ebrahimi  *
2450*62c56f98SSadaf Ebrahimi  * - For #PSA_ALG_CCM, calling this function is required.
2451*62c56f98SSadaf Ebrahimi  * - For the other AEAD algorithms defined in this specification, calling
2452*62c56f98SSadaf Ebrahimi  *   this function is not required.
2453*62c56f98SSadaf Ebrahimi  * - For vendor-defined algorithm, refer to the vendor documentation.
2454*62c56f98SSadaf Ebrahimi  *
2455*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2456*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2457*62c56f98SSadaf Ebrahimi  *
2458*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2459*62c56f98SSadaf Ebrahimi  * \param ad_length             Size of the non-encrypted additional
2460*62c56f98SSadaf Ebrahimi  *                              authenticated data in bytes.
2461*62c56f98SSadaf Ebrahimi  * \param plaintext_length      Size of the plaintext to encrypt in bytes.
2462*62c56f98SSadaf Ebrahimi  *
2463*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2464*62c56f98SSadaf Ebrahimi  *         Success.
2465*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2466*62c56f98SSadaf Ebrahimi  *         At least one of the lengths is not acceptable for the chosen
2467*62c56f98SSadaf Ebrahimi  *         algorithm.
2468*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2469*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2470*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2471*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2472*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2473*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, and
2474*62c56f98SSadaf Ebrahimi  *         psa_aead_update_ad() and psa_aead_update() must not have been
2475*62c56f98SSadaf Ebrahimi  *         called yet), or the library has not been previously initialized
2476*62c56f98SSadaf Ebrahimi  *         by psa_crypto_init().
2477*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2478*62c56f98SSadaf Ebrahimi  *         results in this error code.
2479*62c56f98SSadaf Ebrahimi  */
2480*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2481*62c56f98SSadaf Ebrahimi                                   size_t ad_length,
2482*62c56f98SSadaf Ebrahimi                                   size_t plaintext_length);
2483*62c56f98SSadaf Ebrahimi 
2484*62c56f98SSadaf Ebrahimi /** Pass additional data to an active AEAD operation.
2485*62c56f98SSadaf Ebrahimi  *
2486*62c56f98SSadaf Ebrahimi  * Additional data is authenticated, but not encrypted.
2487*62c56f98SSadaf Ebrahimi  *
2488*62c56f98SSadaf Ebrahimi  * You may call this function multiple times to pass successive fragments
2489*62c56f98SSadaf Ebrahimi  * of the additional data. You may not call this function after passing
2490*62c56f98SSadaf Ebrahimi  * data to encrypt or decrypt with psa_aead_update().
2491*62c56f98SSadaf Ebrahimi  *
2492*62c56f98SSadaf Ebrahimi  * Before calling this function, you must:
2493*62c56f98SSadaf Ebrahimi  * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2494*62c56f98SSadaf Ebrahimi  * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2495*62c56f98SSadaf Ebrahimi  *
2496*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2497*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2498*62c56f98SSadaf Ebrahimi  *
2499*62c56f98SSadaf Ebrahimi  * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2500*62c56f98SSadaf Ebrahimi  *          there is no guarantee that the input is valid. Therefore, until
2501*62c56f98SSadaf Ebrahimi  *          you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2502*62c56f98SSadaf Ebrahimi  *          treat the input as untrusted and prepare to undo any action that
2503*62c56f98SSadaf Ebrahimi  *          depends on the input if psa_aead_verify() returns an error status.
2504*62c56f98SSadaf Ebrahimi  *
2505*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2506*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the fragment of
2507*62c56f98SSadaf Ebrahimi  *                              additional data.
2508*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
2509*62c56f98SSadaf Ebrahimi  *
2510*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2511*62c56f98SSadaf Ebrahimi  *         Success.
2512*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2513*62c56f98SSadaf Ebrahimi  *         The total input length overflows the additional data length that
2514*62c56f98SSadaf Ebrahimi  *         was previously specified with psa_aead_set_lengths().
2515*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2516*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2517*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2518*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2519*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2520*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2521*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, have a nonce
2522*62c56f98SSadaf Ebrahimi  *         set, have lengths set if required by the algorithm, and
2523*62c56f98SSadaf Ebrahimi  *         psa_aead_update() must not have been called yet), or the library
2524*62c56f98SSadaf Ebrahimi  *         has not been previously initialized by psa_crypto_init().
2525*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2526*62c56f98SSadaf Ebrahimi  *         results in this error code.
2527*62c56f98SSadaf Ebrahimi  */
2528*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2529*62c56f98SSadaf Ebrahimi                                 const uint8_t *input,
2530*62c56f98SSadaf Ebrahimi                                 size_t input_length);
2531*62c56f98SSadaf Ebrahimi 
2532*62c56f98SSadaf Ebrahimi /** Encrypt or decrypt a message fragment in an active AEAD operation.
2533*62c56f98SSadaf Ebrahimi  *
2534*62c56f98SSadaf Ebrahimi  * Before calling this function, you must:
2535*62c56f98SSadaf Ebrahimi  * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2536*62c56f98SSadaf Ebrahimi  *    The choice of setup function determines whether this function
2537*62c56f98SSadaf Ebrahimi  *    encrypts or decrypts its input.
2538*62c56f98SSadaf Ebrahimi  * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2539*62c56f98SSadaf Ebrahimi  * 3. Call psa_aead_update_ad() to pass all the additional data.
2540*62c56f98SSadaf Ebrahimi  *
2541*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2542*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2543*62c56f98SSadaf Ebrahimi  *
2544*62c56f98SSadaf Ebrahimi  * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2545*62c56f98SSadaf Ebrahimi  *          there is no guarantee that the input is valid. Therefore, until
2546*62c56f98SSadaf Ebrahimi  *          you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2547*62c56f98SSadaf Ebrahimi  *          - Do not use the output in any way other than storing it in a
2548*62c56f98SSadaf Ebrahimi  *            confidential location. If you take any action that depends
2549*62c56f98SSadaf Ebrahimi  *            on the tentative decrypted data, this action will need to be
2550*62c56f98SSadaf Ebrahimi  *            undone if the input turns out not to be valid. Furthermore,
2551*62c56f98SSadaf Ebrahimi  *            if an adversary can observe that this action took place
2552*62c56f98SSadaf Ebrahimi  *            (for example through timing), they may be able to use this
2553*62c56f98SSadaf Ebrahimi  *            fact as an oracle to decrypt any message encrypted with the
2554*62c56f98SSadaf Ebrahimi  *            same key.
2555*62c56f98SSadaf Ebrahimi  *          - In particular, do not copy the output anywhere but to a
2556*62c56f98SSadaf Ebrahimi  *            memory or storage space that you have exclusive access to.
2557*62c56f98SSadaf Ebrahimi  *
2558*62c56f98SSadaf Ebrahimi  * This function does not require the input to be aligned to any
2559*62c56f98SSadaf Ebrahimi  * particular block boundary. If the implementation can only process
2560*62c56f98SSadaf Ebrahimi  * a whole block at a time, it must consume all the input provided, but
2561*62c56f98SSadaf Ebrahimi  * it may delay the end of the corresponding output until a subsequent
2562*62c56f98SSadaf Ebrahimi  * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2563*62c56f98SSadaf Ebrahimi  * provides sufficient input. The amount of data that can be delayed
2564*62c56f98SSadaf Ebrahimi  * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2565*62c56f98SSadaf Ebrahimi  *
2566*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2567*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the message fragment to
2568*62c56f98SSadaf Ebrahimi  *                              encrypt or decrypt.
2569*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
2570*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the output is to be written.
2571*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
2572*62c56f98SSadaf Ebrahimi  *                              This must be appropriate for the selected
2573*62c56f98SSadaf Ebrahimi  *                                algorithm and key:
2574*62c56f98SSadaf Ebrahimi  *                                - A sufficient output size is
2575*62c56f98SSadaf Ebrahimi  *                                  #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2576*62c56f98SSadaf Ebrahimi  *                                  \c alg, \p input_length) where
2577*62c56f98SSadaf Ebrahimi  *                                  \c key_type is the type of key and \c alg is
2578*62c56f98SSadaf Ebrahimi  *                                  the algorithm that were used to set up the
2579*62c56f98SSadaf Ebrahimi  *                                  operation.
2580*62c56f98SSadaf Ebrahimi  *                                - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2581*62c56f98SSadaf Ebrahimi  *                                  input_length) evaluates to the maximum
2582*62c56f98SSadaf Ebrahimi  *                                  output size of any supported AEAD
2583*62c56f98SSadaf Ebrahimi  *                                  algorithm.
2584*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
2585*62c56f98SSadaf Ebrahimi  *                              that make up the returned output.
2586*62c56f98SSadaf Ebrahimi  *
2587*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2588*62c56f98SSadaf Ebrahimi  *         Success.
2589*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2590*62c56f98SSadaf Ebrahimi  *         The size of the \p output buffer is too small.
2591*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2592*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2593*62c56f98SSadaf Ebrahimi  *         determine the required buffer size.
2594*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2595*62c56f98SSadaf Ebrahimi  *         The total length of input to psa_aead_update_ad() so far is
2596*62c56f98SSadaf Ebrahimi  *         less than the additional data length that was previously
2597*62c56f98SSadaf Ebrahimi  *         specified with psa_aead_set_lengths(), or
2598*62c56f98SSadaf Ebrahimi  *         the total input length overflows the plaintext length that
2599*62c56f98SSadaf Ebrahimi  *         was previously specified with psa_aead_set_lengths().
2600*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2601*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2602*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2603*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2604*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2605*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2606*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active, have a nonce
2607*62c56f98SSadaf Ebrahimi  *         set, and have lengths set if required by the algorithm), or the
2608*62c56f98SSadaf Ebrahimi  *         library has not been previously initialized by psa_crypto_init().
2609*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2610*62c56f98SSadaf Ebrahimi  *         results in this error code.
2611*62c56f98SSadaf Ebrahimi  */
2612*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2613*62c56f98SSadaf Ebrahimi                              const uint8_t *input,
2614*62c56f98SSadaf Ebrahimi                              size_t input_length,
2615*62c56f98SSadaf Ebrahimi                              uint8_t *output,
2616*62c56f98SSadaf Ebrahimi                              size_t output_size,
2617*62c56f98SSadaf Ebrahimi                              size_t *output_length);
2618*62c56f98SSadaf Ebrahimi 
2619*62c56f98SSadaf Ebrahimi /** Finish encrypting a message in an AEAD operation.
2620*62c56f98SSadaf Ebrahimi  *
2621*62c56f98SSadaf Ebrahimi  * The operation must have been set up with psa_aead_encrypt_setup().
2622*62c56f98SSadaf Ebrahimi  *
2623*62c56f98SSadaf Ebrahimi  * This function finishes the authentication of the additional data
2624*62c56f98SSadaf Ebrahimi  * formed by concatenating the inputs passed to preceding calls to
2625*62c56f98SSadaf Ebrahimi  * psa_aead_update_ad() with the plaintext formed by concatenating the
2626*62c56f98SSadaf Ebrahimi  * inputs passed to preceding calls to psa_aead_update().
2627*62c56f98SSadaf Ebrahimi  *
2628*62c56f98SSadaf Ebrahimi  * This function has two output buffers:
2629*62c56f98SSadaf Ebrahimi  * - \p ciphertext contains trailing ciphertext that was buffered from
2630*62c56f98SSadaf Ebrahimi  *   preceding calls to psa_aead_update().
2631*62c56f98SSadaf Ebrahimi  * - \p tag contains the authentication tag.
2632*62c56f98SSadaf Ebrahimi  *
2633*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
2634*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2635*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2636*62c56f98SSadaf Ebrahimi  *
2637*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2638*62c56f98SSadaf Ebrahimi  * \param[out] ciphertext       Buffer where the last part of the ciphertext
2639*62c56f98SSadaf Ebrahimi  *                              is to be written.
2640*62c56f98SSadaf Ebrahimi  * \param ciphertext_size       Size of the \p ciphertext buffer in bytes.
2641*62c56f98SSadaf Ebrahimi  *                              This must be appropriate for the selected
2642*62c56f98SSadaf Ebrahimi  *                              algorithm and key:
2643*62c56f98SSadaf Ebrahimi  *                              - A sufficient output size is
2644*62c56f98SSadaf Ebrahimi  *                                #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2645*62c56f98SSadaf Ebrahimi  *                                \c alg) where \c key_type is the type of key
2646*62c56f98SSadaf Ebrahimi  *                                and \c alg is the algorithm that were used to
2647*62c56f98SSadaf Ebrahimi  *                                set up the operation.
2648*62c56f98SSadaf Ebrahimi  *                              - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2649*62c56f98SSadaf Ebrahimi  *                                the maximum output size of any supported AEAD
2650*62c56f98SSadaf Ebrahimi  *                                algorithm.
2651*62c56f98SSadaf Ebrahimi  * \param[out] ciphertext_length On success, the number of bytes of
2652*62c56f98SSadaf Ebrahimi  *                              returned ciphertext.
2653*62c56f98SSadaf Ebrahimi  * \param[out] tag              Buffer where the authentication tag is
2654*62c56f98SSadaf Ebrahimi  *                              to be written.
2655*62c56f98SSadaf Ebrahimi  * \param tag_size              Size of the \p tag buffer in bytes.
2656*62c56f98SSadaf Ebrahimi  *                              This must be appropriate for the selected
2657*62c56f98SSadaf Ebrahimi  *                              algorithm and key:
2658*62c56f98SSadaf Ebrahimi  *                              - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2659*62c56f98SSadaf Ebrahimi  *                                key_type, \c key_bits, \c alg) where
2660*62c56f98SSadaf Ebrahimi  *                                \c key_type and \c key_bits are the type and
2661*62c56f98SSadaf Ebrahimi  *                                bit-size of the key, and \c alg is the
2662*62c56f98SSadaf Ebrahimi  *                                algorithm that were used in the call to
2663*62c56f98SSadaf Ebrahimi  *                                psa_aead_encrypt_setup().
2664*62c56f98SSadaf Ebrahimi  *                              - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2665*62c56f98SSadaf Ebrahimi  *                                maximum tag size of any supported AEAD
2666*62c56f98SSadaf Ebrahimi  *                                algorithm.
2667*62c56f98SSadaf Ebrahimi  * \param[out] tag_length       On success, the number of bytes
2668*62c56f98SSadaf Ebrahimi  *                              that make up the returned tag.
2669*62c56f98SSadaf Ebrahimi  *
2670*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2671*62c56f98SSadaf Ebrahimi  *         Success.
2672*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2673*62c56f98SSadaf Ebrahimi  *         The size of the \p ciphertext or \p tag buffer is too small.
2674*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2675*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2676*62c56f98SSadaf Ebrahimi  *         required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2677*62c56f98SSadaf Ebrahimi  *         \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2678*62c56f98SSadaf Ebrahimi  *         determine the required \p tag buffer size.
2679*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2680*62c56f98SSadaf Ebrahimi  *         The total length of input to psa_aead_update_ad() so far is
2681*62c56f98SSadaf Ebrahimi  *         less than the additional data length that was previously
2682*62c56f98SSadaf Ebrahimi  *         specified with psa_aead_set_lengths(), or
2683*62c56f98SSadaf Ebrahimi  *         the total length of input to psa_aead_update() so far is
2684*62c56f98SSadaf Ebrahimi  *         less than the plaintext length that was previously
2685*62c56f98SSadaf Ebrahimi  *         specified with psa_aead_set_lengths().
2686*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2687*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2688*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2689*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2690*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2691*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2692*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active encryption
2693*62c56f98SSadaf Ebrahimi  *         operation with a nonce set), or the library has not been previously
2694*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
2695*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2696*62c56f98SSadaf Ebrahimi  *         results in this error code.
2697*62c56f98SSadaf Ebrahimi  */
2698*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2699*62c56f98SSadaf Ebrahimi                              uint8_t *ciphertext,
2700*62c56f98SSadaf Ebrahimi                              size_t ciphertext_size,
2701*62c56f98SSadaf Ebrahimi                              size_t *ciphertext_length,
2702*62c56f98SSadaf Ebrahimi                              uint8_t *tag,
2703*62c56f98SSadaf Ebrahimi                              size_t tag_size,
2704*62c56f98SSadaf Ebrahimi                              size_t *tag_length);
2705*62c56f98SSadaf Ebrahimi 
2706*62c56f98SSadaf Ebrahimi /** Finish authenticating and decrypting a message in an AEAD operation.
2707*62c56f98SSadaf Ebrahimi  *
2708*62c56f98SSadaf Ebrahimi  * The operation must have been set up with psa_aead_decrypt_setup().
2709*62c56f98SSadaf Ebrahimi  *
2710*62c56f98SSadaf Ebrahimi  * This function finishes the authenticated decryption of the message
2711*62c56f98SSadaf Ebrahimi  * components:
2712*62c56f98SSadaf Ebrahimi  *
2713*62c56f98SSadaf Ebrahimi  * -  The additional data consisting of the concatenation of the inputs
2714*62c56f98SSadaf Ebrahimi  *    passed to preceding calls to psa_aead_update_ad().
2715*62c56f98SSadaf Ebrahimi  * -  The ciphertext consisting of the concatenation of the inputs passed to
2716*62c56f98SSadaf Ebrahimi  *    preceding calls to psa_aead_update().
2717*62c56f98SSadaf Ebrahimi  * -  The tag passed to this function call.
2718*62c56f98SSadaf Ebrahimi  *
2719*62c56f98SSadaf Ebrahimi  * If the authentication tag is correct, this function outputs any remaining
2720*62c56f98SSadaf Ebrahimi  * plaintext and reports success. If the authentication tag is not correct,
2721*62c56f98SSadaf Ebrahimi  * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2722*62c56f98SSadaf Ebrahimi  *
2723*62c56f98SSadaf Ebrahimi  * When this function returns successfully, the operation becomes inactive.
2724*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
2725*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_aead_abort().
2726*62c56f98SSadaf Ebrahimi  *
2727*62c56f98SSadaf Ebrahimi  * \note Implementations shall make the best effort to ensure that the
2728*62c56f98SSadaf Ebrahimi  * comparison between the actual tag and the expected tag is performed
2729*62c56f98SSadaf Ebrahimi  * in constant time.
2730*62c56f98SSadaf Ebrahimi  *
2731*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Active AEAD operation.
2732*62c56f98SSadaf Ebrahimi  * \param[out] plaintext        Buffer where the last part of the plaintext
2733*62c56f98SSadaf Ebrahimi  *                              is to be written. This is the remaining data
2734*62c56f98SSadaf Ebrahimi  *                              from previous calls to psa_aead_update()
2735*62c56f98SSadaf Ebrahimi  *                              that could not be processed until the end
2736*62c56f98SSadaf Ebrahimi  *                              of the input.
2737*62c56f98SSadaf Ebrahimi  * \param plaintext_size        Size of the \p plaintext buffer in bytes.
2738*62c56f98SSadaf Ebrahimi  *                              This must be appropriate for the selected algorithm and key:
2739*62c56f98SSadaf Ebrahimi  *                              - A sufficient output size is
2740*62c56f98SSadaf Ebrahimi  *                                #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2741*62c56f98SSadaf Ebrahimi  *                                \c alg) where \c key_type is the type of key
2742*62c56f98SSadaf Ebrahimi  *                                and \c alg is the algorithm that were used to
2743*62c56f98SSadaf Ebrahimi  *                                set up the operation.
2744*62c56f98SSadaf Ebrahimi  *                              - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2745*62c56f98SSadaf Ebrahimi  *                                the maximum output size of any supported AEAD
2746*62c56f98SSadaf Ebrahimi  *                                algorithm.
2747*62c56f98SSadaf Ebrahimi  * \param[out] plaintext_length On success, the number of bytes of
2748*62c56f98SSadaf Ebrahimi  *                              returned plaintext.
2749*62c56f98SSadaf Ebrahimi  * \param[in] tag               Buffer containing the authentication tag.
2750*62c56f98SSadaf Ebrahimi  * \param tag_length            Size of the \p tag buffer in bytes.
2751*62c56f98SSadaf Ebrahimi  *
2752*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
2753*62c56f98SSadaf Ebrahimi  *         Success.
2754*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
2755*62c56f98SSadaf Ebrahimi  *         The calculations were successful, but the authentication tag is
2756*62c56f98SSadaf Ebrahimi  *         not correct.
2757*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2758*62c56f98SSadaf Ebrahimi  *         The size of the \p plaintext buffer is too small.
2759*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2760*62c56f98SSadaf Ebrahimi  *         #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2761*62c56f98SSadaf Ebrahimi  *         required buffer size.
2762*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
2763*62c56f98SSadaf Ebrahimi  *         The total length of input to psa_aead_update_ad() so far is
2764*62c56f98SSadaf Ebrahimi  *         less than the additional data length that was previously
2765*62c56f98SSadaf Ebrahimi  *         specified with psa_aead_set_lengths(), or
2766*62c56f98SSadaf Ebrahimi  *         the total length of input to psa_aead_update() so far is
2767*62c56f98SSadaf Ebrahimi  *         less than the plaintext length that was previously
2768*62c56f98SSadaf Ebrahimi  *         specified with psa_aead_set_lengths().
2769*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2770*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2771*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2772*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2773*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2774*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2775*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active decryption
2776*62c56f98SSadaf Ebrahimi  *         operation with a nonce set), or the library has not been previously
2777*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
2778*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2779*62c56f98SSadaf Ebrahimi  *         results in this error code.
2780*62c56f98SSadaf Ebrahimi  */
2781*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2782*62c56f98SSadaf Ebrahimi                              uint8_t *plaintext,
2783*62c56f98SSadaf Ebrahimi                              size_t plaintext_size,
2784*62c56f98SSadaf Ebrahimi                              size_t *plaintext_length,
2785*62c56f98SSadaf Ebrahimi                              const uint8_t *tag,
2786*62c56f98SSadaf Ebrahimi                              size_t tag_length);
2787*62c56f98SSadaf Ebrahimi 
2788*62c56f98SSadaf Ebrahimi /** Abort an AEAD operation.
2789*62c56f98SSadaf Ebrahimi  *
2790*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the
2791*62c56f98SSadaf Ebrahimi  * \p operation structure itself. Once aborted, the operation object
2792*62c56f98SSadaf Ebrahimi  * can be reused for another operation by calling
2793*62c56f98SSadaf Ebrahimi  * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2794*62c56f98SSadaf Ebrahimi  *
2795*62c56f98SSadaf Ebrahimi  * You may call this function any time after the operation object has
2796*62c56f98SSadaf Ebrahimi  * been initialized as described in #psa_aead_operation_t.
2797*62c56f98SSadaf Ebrahimi  *
2798*62c56f98SSadaf Ebrahimi  * In particular, calling psa_aead_abort() after the operation has been
2799*62c56f98SSadaf Ebrahimi  * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2800*62c56f98SSadaf Ebrahimi  * psa_aead_verify() is safe and has no effect.
2801*62c56f98SSadaf Ebrahimi  *
2802*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Initialized AEAD operation.
2803*62c56f98SSadaf Ebrahimi  *
2804*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
2805*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2806*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2807*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2808*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2809*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2810*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2811*62c56f98SSadaf Ebrahimi  *         results in this error code.
2812*62c56f98SSadaf Ebrahimi  */
2813*62c56f98SSadaf Ebrahimi psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2814*62c56f98SSadaf Ebrahimi 
2815*62c56f98SSadaf Ebrahimi /**@}*/
2816*62c56f98SSadaf Ebrahimi 
2817*62c56f98SSadaf Ebrahimi /** \defgroup asymmetric Asymmetric cryptography
2818*62c56f98SSadaf Ebrahimi  * @{
2819*62c56f98SSadaf Ebrahimi  */
2820*62c56f98SSadaf Ebrahimi 
2821*62c56f98SSadaf Ebrahimi /**
2822*62c56f98SSadaf Ebrahimi  * \brief Sign a message with a private key. For hash-and-sign algorithms,
2823*62c56f98SSadaf Ebrahimi  *        this includes the hashing step.
2824*62c56f98SSadaf Ebrahimi  *
2825*62c56f98SSadaf Ebrahimi  * \note To perform a multi-part hash-and-sign signature algorithm, first use
2826*62c56f98SSadaf Ebrahimi  *       a multi-part hash operation and then pass the resulting hash to
2827*62c56f98SSadaf Ebrahimi  *       psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2828*62c56f98SSadaf Ebrahimi  *       hash algorithm to use.
2829*62c56f98SSadaf Ebrahimi  *
2830*62c56f98SSadaf Ebrahimi  * \param[in]  key              Identifier of the key to use for the operation.
2831*62c56f98SSadaf Ebrahimi  *                              It must be an asymmetric key pair. The key must
2832*62c56f98SSadaf Ebrahimi  *                              allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
2833*62c56f98SSadaf Ebrahimi  * \param[in]  alg              An asymmetric signature algorithm (PSA_ALG_XXX
2834*62c56f98SSadaf Ebrahimi  *                              value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2835*62c56f98SSadaf Ebrahimi  *                              is true), that is compatible with the type of
2836*62c56f98SSadaf Ebrahimi  *                              \p key.
2837*62c56f98SSadaf Ebrahimi  * \param[in]  input            The input message to sign.
2838*62c56f98SSadaf Ebrahimi  * \param[in]  input_length     Size of the \p input buffer in bytes.
2839*62c56f98SSadaf Ebrahimi  * \param[out] signature        Buffer where the signature is to be written.
2840*62c56f98SSadaf Ebrahimi  * \param[in]  signature_size   Size of the \p signature buffer in bytes. This
2841*62c56f98SSadaf Ebrahimi  *                              must be appropriate for the selected
2842*62c56f98SSadaf Ebrahimi  *                              algorithm and key:
2843*62c56f98SSadaf Ebrahimi  *                              - The required signature size is
2844*62c56f98SSadaf Ebrahimi  *                                #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2845*62c56f98SSadaf Ebrahimi  *                                where \c key_type and \c key_bits are the type and
2846*62c56f98SSadaf Ebrahimi  *                                bit-size respectively of key.
2847*62c56f98SSadaf Ebrahimi  *                              - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2848*62c56f98SSadaf Ebrahimi  *                                maximum signature size of any supported
2849*62c56f98SSadaf Ebrahimi  *                                signature algorithm.
2850*62c56f98SSadaf Ebrahimi  * \param[out] signature_length On success, the number of bytes that make up
2851*62c56f98SSadaf Ebrahimi  *                              the returned signature value.
2852*62c56f98SSadaf Ebrahimi  *
2853*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
2854*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2855*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
2856*62c56f98SSadaf Ebrahimi  *         The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2857*62c56f98SSadaf Ebrahimi  *         or it does not permit the requested algorithm.
2858*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2859*62c56f98SSadaf Ebrahimi  *         The size of the \p signature buffer is too small. You can
2860*62c56f98SSadaf Ebrahimi  *         determine a sufficient buffer size by calling
2861*62c56f98SSadaf Ebrahimi  *         #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2862*62c56f98SSadaf Ebrahimi  *         where \c key_type and \c key_bits are the type and bit-size
2863*62c56f98SSadaf Ebrahimi  *         respectively of \p key.
2864*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2865*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2866*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2867*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2868*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2869*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2870*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2871*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2872*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2873*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
2874*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2875*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2876*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2877*62c56f98SSadaf Ebrahimi  *         results in this error code.
2878*62c56f98SSadaf Ebrahimi  */
2879*62c56f98SSadaf Ebrahimi psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2880*62c56f98SSadaf Ebrahimi                               psa_algorithm_t alg,
2881*62c56f98SSadaf Ebrahimi                               const uint8_t *input,
2882*62c56f98SSadaf Ebrahimi                               size_t input_length,
2883*62c56f98SSadaf Ebrahimi                               uint8_t *signature,
2884*62c56f98SSadaf Ebrahimi                               size_t signature_size,
2885*62c56f98SSadaf Ebrahimi                               size_t *signature_length);
2886*62c56f98SSadaf Ebrahimi 
2887*62c56f98SSadaf Ebrahimi /** \brief Verify the signature of a message with a public key, using
2888*62c56f98SSadaf Ebrahimi  *         a hash-and-sign verification algorithm.
2889*62c56f98SSadaf Ebrahimi  *
2890*62c56f98SSadaf Ebrahimi  * \note To perform a multi-part hash-and-sign signature verification
2891*62c56f98SSadaf Ebrahimi  *       algorithm, first use a multi-part hash operation to hash the message
2892*62c56f98SSadaf Ebrahimi  *       and then pass the resulting hash to psa_verify_hash().
2893*62c56f98SSadaf Ebrahimi  *       PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2894*62c56f98SSadaf Ebrahimi  *       to use.
2895*62c56f98SSadaf Ebrahimi  *
2896*62c56f98SSadaf Ebrahimi  * \param[in]  key              Identifier of the key to use for the operation.
2897*62c56f98SSadaf Ebrahimi  *                              It must be a public key or an asymmetric key
2898*62c56f98SSadaf Ebrahimi  *                              pair. The key must allow the usage
2899*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_VERIFY_MESSAGE.
2900*62c56f98SSadaf Ebrahimi  * \param[in]  alg              An asymmetric signature algorithm (PSA_ALG_XXX
2901*62c56f98SSadaf Ebrahimi  *                              value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2902*62c56f98SSadaf Ebrahimi  *                              is true), that is compatible with the type of
2903*62c56f98SSadaf Ebrahimi  *                              \p key.
2904*62c56f98SSadaf Ebrahimi  * \param[in]  input            The message whose signature is to be verified.
2905*62c56f98SSadaf Ebrahimi  * \param[in]  input_length     Size of the \p input buffer in bytes.
2906*62c56f98SSadaf Ebrahimi  * \param[out] signature        Buffer containing the signature to verify.
2907*62c56f98SSadaf Ebrahimi  * \param[in]  signature_length Size of the \p signature buffer in bytes.
2908*62c56f98SSadaf Ebrahimi  *
2909*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
2910*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2911*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
2912*62c56f98SSadaf Ebrahimi  *         The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2913*62c56f98SSadaf Ebrahimi  *         or it does not permit the requested algorithm.
2914*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
2915*62c56f98SSadaf Ebrahimi  *         The calculation was performed successfully, but the passed signature
2916*62c56f98SSadaf Ebrahimi  *         is not a valid signature.
2917*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2918*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2919*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2920*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2921*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2922*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2923*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2924*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2925*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2926*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2927*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2928*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2929*62c56f98SSadaf Ebrahimi  *         results in this error code.
2930*62c56f98SSadaf Ebrahimi  */
2931*62c56f98SSadaf Ebrahimi psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2932*62c56f98SSadaf Ebrahimi                                 psa_algorithm_t alg,
2933*62c56f98SSadaf Ebrahimi                                 const uint8_t *input,
2934*62c56f98SSadaf Ebrahimi                                 size_t input_length,
2935*62c56f98SSadaf Ebrahimi                                 const uint8_t *signature,
2936*62c56f98SSadaf Ebrahimi                                 size_t signature_length);
2937*62c56f98SSadaf Ebrahimi 
2938*62c56f98SSadaf Ebrahimi /**
2939*62c56f98SSadaf Ebrahimi  * \brief Sign a hash or short message with a private key.
2940*62c56f98SSadaf Ebrahimi  *
2941*62c56f98SSadaf Ebrahimi  * Note that to perform a hash-and-sign signature algorithm, you must
2942*62c56f98SSadaf Ebrahimi  * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2943*62c56f98SSadaf Ebrahimi  * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2944*62c56f98SSadaf Ebrahimi  * Then pass the resulting hash as the \p hash
2945*62c56f98SSadaf Ebrahimi  * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2946*62c56f98SSadaf Ebrahimi  * to determine the hash algorithm to use.
2947*62c56f98SSadaf Ebrahimi  *
2948*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
2949*62c56f98SSadaf Ebrahimi  *                              It must be an asymmetric key pair. The key must
2950*62c56f98SSadaf Ebrahimi  *                              allow the usage #PSA_KEY_USAGE_SIGN_HASH.
2951*62c56f98SSadaf Ebrahimi  * \param alg                   A signature algorithm (PSA_ALG_XXX
2952*62c56f98SSadaf Ebrahimi  *                              value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2953*62c56f98SSadaf Ebrahimi  *                              is true), that is compatible with
2954*62c56f98SSadaf Ebrahimi  *                              the type of \p key.
2955*62c56f98SSadaf Ebrahimi  * \param[in] hash              The hash or message to sign.
2956*62c56f98SSadaf Ebrahimi  * \param hash_length           Size of the \p hash buffer in bytes.
2957*62c56f98SSadaf Ebrahimi  * \param[out] signature        Buffer where the signature is to be written.
2958*62c56f98SSadaf Ebrahimi  * \param signature_size        Size of the \p signature buffer in bytes.
2959*62c56f98SSadaf Ebrahimi  * \param[out] signature_length On success, the number of bytes
2960*62c56f98SSadaf Ebrahimi  *                              that make up the returned signature value.
2961*62c56f98SSadaf Ebrahimi  *
2962*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
2963*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2964*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2965*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2966*62c56f98SSadaf Ebrahimi  *         The size of the \p signature buffer is too small. You can
2967*62c56f98SSadaf Ebrahimi  *         determine a sufficient buffer size by calling
2968*62c56f98SSadaf Ebrahimi  *         #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2969*62c56f98SSadaf Ebrahimi  *         where \c key_type and \c key_bits are the type and bit-size
2970*62c56f98SSadaf Ebrahimi  *         respectively of \p key.
2971*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2972*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2973*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2974*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2975*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2976*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2977*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2978*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
2979*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
2980*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
2981*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
2982*62c56f98SSadaf Ebrahimi  *         results in this error code.
2983*62c56f98SSadaf Ebrahimi  */
2984*62c56f98SSadaf Ebrahimi psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2985*62c56f98SSadaf Ebrahimi                            psa_algorithm_t alg,
2986*62c56f98SSadaf Ebrahimi                            const uint8_t *hash,
2987*62c56f98SSadaf Ebrahimi                            size_t hash_length,
2988*62c56f98SSadaf Ebrahimi                            uint8_t *signature,
2989*62c56f98SSadaf Ebrahimi                            size_t signature_size,
2990*62c56f98SSadaf Ebrahimi                            size_t *signature_length);
2991*62c56f98SSadaf Ebrahimi 
2992*62c56f98SSadaf Ebrahimi /**
2993*62c56f98SSadaf Ebrahimi  * \brief Verify the signature of a hash or short message using a public key.
2994*62c56f98SSadaf Ebrahimi  *
2995*62c56f98SSadaf Ebrahimi  * Note that to perform a hash-and-sign signature algorithm, you must
2996*62c56f98SSadaf Ebrahimi  * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2997*62c56f98SSadaf Ebrahimi  * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2998*62c56f98SSadaf Ebrahimi  * Then pass the resulting hash as the \p hash
2999*62c56f98SSadaf Ebrahimi  * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3000*62c56f98SSadaf Ebrahimi  * to determine the hash algorithm to use.
3001*62c56f98SSadaf Ebrahimi  *
3002*62c56f98SSadaf Ebrahimi  * \param key               Identifier of the key to use for the operation. It
3003*62c56f98SSadaf Ebrahimi  *                          must be a public key or an asymmetric key pair. The
3004*62c56f98SSadaf Ebrahimi  *                          key must allow the usage
3005*62c56f98SSadaf Ebrahimi  *                          #PSA_KEY_USAGE_VERIFY_HASH.
3006*62c56f98SSadaf Ebrahimi  * \param alg               A signature algorithm (PSA_ALG_XXX
3007*62c56f98SSadaf Ebrahimi  *                          value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3008*62c56f98SSadaf Ebrahimi  *                          is true), that is compatible with
3009*62c56f98SSadaf Ebrahimi  *                          the type of \p key.
3010*62c56f98SSadaf Ebrahimi  * \param[in] hash          The hash or message whose signature is to be
3011*62c56f98SSadaf Ebrahimi  *                          verified.
3012*62c56f98SSadaf Ebrahimi  * \param hash_length       Size of the \p hash buffer in bytes.
3013*62c56f98SSadaf Ebrahimi  * \param[in] signature     Buffer containing the signature to verify.
3014*62c56f98SSadaf Ebrahimi  * \param signature_length  Size of the \p signature buffer in bytes.
3015*62c56f98SSadaf Ebrahimi  *
3016*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3017*62c56f98SSadaf Ebrahimi  *         The signature is valid.
3018*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3019*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3020*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
3021*62c56f98SSadaf Ebrahimi  *         The calculation was performed successfully, but the passed
3022*62c56f98SSadaf Ebrahimi  *         signature is not a valid signature.
3023*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3024*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3025*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3026*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3027*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3028*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3029*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3030*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3031*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3032*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3033*62c56f98SSadaf Ebrahimi  *         results in this error code.
3034*62c56f98SSadaf Ebrahimi  */
3035*62c56f98SSadaf Ebrahimi psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3036*62c56f98SSadaf Ebrahimi                              psa_algorithm_t alg,
3037*62c56f98SSadaf Ebrahimi                              const uint8_t *hash,
3038*62c56f98SSadaf Ebrahimi                              size_t hash_length,
3039*62c56f98SSadaf Ebrahimi                              const uint8_t *signature,
3040*62c56f98SSadaf Ebrahimi                              size_t signature_length);
3041*62c56f98SSadaf Ebrahimi 
3042*62c56f98SSadaf Ebrahimi /**
3043*62c56f98SSadaf Ebrahimi  * \brief Encrypt a short message with a public key.
3044*62c56f98SSadaf Ebrahimi  *
3045*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
3046*62c56f98SSadaf Ebrahimi  *                              It must be a public key or an asymmetric key
3047*62c56f98SSadaf Ebrahimi  *                              pair. It must allow the usage
3048*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_ENCRYPT.
3049*62c56f98SSadaf Ebrahimi  * \param alg                   An asymmetric encryption algorithm that is
3050*62c56f98SSadaf Ebrahimi  *                              compatible with the type of \p key.
3051*62c56f98SSadaf Ebrahimi  * \param[in] input             The message to encrypt.
3052*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
3053*62c56f98SSadaf Ebrahimi  * \param[in] salt              A salt or label, if supported by the
3054*62c56f98SSadaf Ebrahimi  *                              encryption algorithm.
3055*62c56f98SSadaf Ebrahimi  *                              If the algorithm does not support a
3056*62c56f98SSadaf Ebrahimi  *                              salt, pass \c NULL.
3057*62c56f98SSadaf Ebrahimi  *                              If the algorithm supports an optional
3058*62c56f98SSadaf Ebrahimi  *                              salt and you do not want to pass a salt,
3059*62c56f98SSadaf Ebrahimi  *                              pass \c NULL.
3060*62c56f98SSadaf Ebrahimi  *
3061*62c56f98SSadaf Ebrahimi  *                              - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3062*62c56f98SSadaf Ebrahimi  *                                supported.
3063*62c56f98SSadaf Ebrahimi  * \param salt_length           Size of the \p salt buffer in bytes.
3064*62c56f98SSadaf Ebrahimi  *                              If \p salt is \c NULL, pass 0.
3065*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the encrypted message is to
3066*62c56f98SSadaf Ebrahimi  *                              be written.
3067*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \p output buffer in bytes.
3068*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
3069*62c56f98SSadaf Ebrahimi  *                              that make up the returned output.
3070*62c56f98SSadaf Ebrahimi  *
3071*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3072*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3073*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3074*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3075*62c56f98SSadaf Ebrahimi  *         The size of the \p output buffer is too small. You can
3076*62c56f98SSadaf Ebrahimi  *         determine a sufficient buffer size by calling
3077*62c56f98SSadaf Ebrahimi  *         #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3078*62c56f98SSadaf Ebrahimi  *         where \c key_type and \c key_bits are the type and bit-size
3079*62c56f98SSadaf Ebrahimi  *         respectively of \p key.
3080*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3081*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3082*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3083*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3084*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3085*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3086*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3087*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3088*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3089*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3090*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3091*62c56f98SSadaf Ebrahimi  *         results in this error code.
3092*62c56f98SSadaf Ebrahimi  */
3093*62c56f98SSadaf Ebrahimi psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3094*62c56f98SSadaf Ebrahimi                                     psa_algorithm_t alg,
3095*62c56f98SSadaf Ebrahimi                                     const uint8_t *input,
3096*62c56f98SSadaf Ebrahimi                                     size_t input_length,
3097*62c56f98SSadaf Ebrahimi                                     const uint8_t *salt,
3098*62c56f98SSadaf Ebrahimi                                     size_t salt_length,
3099*62c56f98SSadaf Ebrahimi                                     uint8_t *output,
3100*62c56f98SSadaf Ebrahimi                                     size_t output_size,
3101*62c56f98SSadaf Ebrahimi                                     size_t *output_length);
3102*62c56f98SSadaf Ebrahimi 
3103*62c56f98SSadaf Ebrahimi /**
3104*62c56f98SSadaf Ebrahimi  * \brief Decrypt a short message with a private key.
3105*62c56f98SSadaf Ebrahimi  *
3106*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
3107*62c56f98SSadaf Ebrahimi  *                              It must be an asymmetric key pair. It must
3108*62c56f98SSadaf Ebrahimi  *                              allow the usage #PSA_KEY_USAGE_DECRYPT.
3109*62c56f98SSadaf Ebrahimi  * \param alg                   An asymmetric encryption algorithm that is
3110*62c56f98SSadaf Ebrahimi  *                              compatible with the type of \p key.
3111*62c56f98SSadaf Ebrahimi  * \param[in] input             The message to decrypt.
3112*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
3113*62c56f98SSadaf Ebrahimi  * \param[in] salt              A salt or label, if supported by the
3114*62c56f98SSadaf Ebrahimi  *                              encryption algorithm.
3115*62c56f98SSadaf Ebrahimi  *                              If the algorithm does not support a
3116*62c56f98SSadaf Ebrahimi  *                              salt, pass \c NULL.
3117*62c56f98SSadaf Ebrahimi  *                              If the algorithm supports an optional
3118*62c56f98SSadaf Ebrahimi  *                              salt and you do not want to pass a salt,
3119*62c56f98SSadaf Ebrahimi  *                              pass \c NULL.
3120*62c56f98SSadaf Ebrahimi  *
3121*62c56f98SSadaf Ebrahimi  *                              - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3122*62c56f98SSadaf Ebrahimi  *                                supported.
3123*62c56f98SSadaf Ebrahimi  * \param salt_length           Size of the \p salt buffer in bytes.
3124*62c56f98SSadaf Ebrahimi  *                              If \p salt is \c NULL, pass 0.
3125*62c56f98SSadaf Ebrahimi  * \param[out] output           Buffer where the decrypted message is to
3126*62c56f98SSadaf Ebrahimi  *                              be written.
3127*62c56f98SSadaf Ebrahimi  * \param output_size           Size of the \c output buffer in bytes.
3128*62c56f98SSadaf Ebrahimi  * \param[out] output_length    On success, the number of bytes
3129*62c56f98SSadaf Ebrahimi  *                              that make up the returned output.
3130*62c56f98SSadaf Ebrahimi  *
3131*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3132*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3133*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3134*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3135*62c56f98SSadaf Ebrahimi  *         The size of the \p output buffer is too small. You can
3136*62c56f98SSadaf Ebrahimi  *         determine a sufficient buffer size by calling
3137*62c56f98SSadaf Ebrahimi  *         #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3138*62c56f98SSadaf Ebrahimi  *         where \c key_type and \c key_bits are the type and bit-size
3139*62c56f98SSadaf Ebrahimi  *         respectively of \p key.
3140*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3141*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3142*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3143*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3144*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3145*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3146*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3147*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3148*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
3149*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3150*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3151*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3152*62c56f98SSadaf Ebrahimi  *         results in this error code.
3153*62c56f98SSadaf Ebrahimi  */
3154*62c56f98SSadaf Ebrahimi psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3155*62c56f98SSadaf Ebrahimi                                     psa_algorithm_t alg,
3156*62c56f98SSadaf Ebrahimi                                     const uint8_t *input,
3157*62c56f98SSadaf Ebrahimi                                     size_t input_length,
3158*62c56f98SSadaf Ebrahimi                                     const uint8_t *salt,
3159*62c56f98SSadaf Ebrahimi                                     size_t salt_length,
3160*62c56f98SSadaf Ebrahimi                                     uint8_t *output,
3161*62c56f98SSadaf Ebrahimi                                     size_t output_size,
3162*62c56f98SSadaf Ebrahimi                                     size_t *output_length);
3163*62c56f98SSadaf Ebrahimi 
3164*62c56f98SSadaf Ebrahimi /**@}*/
3165*62c56f98SSadaf Ebrahimi 
3166*62c56f98SSadaf Ebrahimi /** \defgroup key_derivation Key derivation and pseudorandom generation
3167*62c56f98SSadaf Ebrahimi  * @{
3168*62c56f98SSadaf Ebrahimi  */
3169*62c56f98SSadaf Ebrahimi 
3170*62c56f98SSadaf Ebrahimi /** The type of the state data structure for key derivation operations.
3171*62c56f98SSadaf Ebrahimi  *
3172*62c56f98SSadaf Ebrahimi  * Before calling any function on a key derivation operation object, the
3173*62c56f98SSadaf Ebrahimi  * application must initialize it by any of the following means:
3174*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
3175*62c56f98SSadaf Ebrahimi  *   \code
3176*62c56f98SSadaf Ebrahimi  *   psa_key_derivation_operation_t operation;
3177*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
3178*62c56f98SSadaf Ebrahimi  *   \endcode
3179*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
3180*62c56f98SSadaf Ebrahimi  *   \code
3181*62c56f98SSadaf Ebrahimi  *   psa_key_derivation_operation_t operation = {0};
3182*62c56f98SSadaf Ebrahimi  *   \endcode
3183*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
3184*62c56f98SSadaf Ebrahimi  *   for example:
3185*62c56f98SSadaf Ebrahimi  *   \code
3186*62c56f98SSadaf Ebrahimi  *   psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3187*62c56f98SSadaf Ebrahimi  *   \endcode
3188*62c56f98SSadaf Ebrahimi  * - Assign the result of the function psa_key_derivation_operation_init()
3189*62c56f98SSadaf Ebrahimi  *   to the structure, for example:
3190*62c56f98SSadaf Ebrahimi  *   \code
3191*62c56f98SSadaf Ebrahimi  *   psa_key_derivation_operation_t operation;
3192*62c56f98SSadaf Ebrahimi  *   operation = psa_key_derivation_operation_init();
3193*62c56f98SSadaf Ebrahimi  *   \endcode
3194*62c56f98SSadaf Ebrahimi  *
3195*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
3196*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
3197*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice.
3198*62c56f98SSadaf Ebrahimi  */
3199*62c56f98SSadaf Ebrahimi typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
3200*62c56f98SSadaf Ebrahimi 
3201*62c56f98SSadaf Ebrahimi /** \def PSA_KEY_DERIVATION_OPERATION_INIT
3202*62c56f98SSadaf Ebrahimi  *
3203*62c56f98SSadaf Ebrahimi  * This macro returns a suitable initializer for a key derivation operation
3204*62c56f98SSadaf Ebrahimi  * object of type #psa_key_derivation_operation_t.
3205*62c56f98SSadaf Ebrahimi  */
3206*62c56f98SSadaf Ebrahimi 
3207*62c56f98SSadaf Ebrahimi /** Return an initial value for a key derivation operation object.
3208*62c56f98SSadaf Ebrahimi  */
3209*62c56f98SSadaf Ebrahimi static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
3210*62c56f98SSadaf Ebrahimi 
3211*62c56f98SSadaf Ebrahimi /** Set up a key derivation operation.
3212*62c56f98SSadaf Ebrahimi  *
3213*62c56f98SSadaf Ebrahimi  * A key derivation algorithm takes some inputs and uses them to generate
3214*62c56f98SSadaf Ebrahimi  * a byte stream in a deterministic way.
3215*62c56f98SSadaf Ebrahimi  * This byte stream can be used to produce keys and other
3216*62c56f98SSadaf Ebrahimi  * cryptographic material.
3217*62c56f98SSadaf Ebrahimi  *
3218*62c56f98SSadaf Ebrahimi  * To derive a key:
3219*62c56f98SSadaf Ebrahimi  * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3220*62c56f98SSadaf Ebrahimi  * -# Call psa_key_derivation_setup() to select the algorithm.
3221*62c56f98SSadaf Ebrahimi  * -# Provide the inputs for the key derivation by calling
3222*62c56f98SSadaf Ebrahimi  *    psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3223*62c56f98SSadaf Ebrahimi  *    as appropriate. Which inputs are needed, in what order, and whether
3224*62c56f98SSadaf Ebrahimi  *    they may be keys and if so of what type depends on the algorithm.
3225*62c56f98SSadaf Ebrahimi  * -# Optionally set the operation's maximum capacity with
3226*62c56f98SSadaf Ebrahimi  *    psa_key_derivation_set_capacity(). You may do this before, in the middle
3227*62c56f98SSadaf Ebrahimi  *    of or after providing inputs. For some algorithms, this step is mandatory
3228*62c56f98SSadaf Ebrahimi  *    because the output depends on the maximum capacity.
3229*62c56f98SSadaf Ebrahimi  * -# To derive a key, call psa_key_derivation_output_key().
3230*62c56f98SSadaf Ebrahimi  *    To derive a byte string for a different purpose, call
3231*62c56f98SSadaf Ebrahimi  *    psa_key_derivation_output_bytes().
3232*62c56f98SSadaf Ebrahimi  *    Successive calls to these functions use successive output bytes
3233*62c56f98SSadaf Ebrahimi  *    calculated by the key derivation algorithm.
3234*62c56f98SSadaf Ebrahimi  * -# Clean up the key derivation operation object with
3235*62c56f98SSadaf Ebrahimi  *    psa_key_derivation_abort().
3236*62c56f98SSadaf Ebrahimi  *
3237*62c56f98SSadaf Ebrahimi  * If this function returns an error, the key derivation operation object is
3238*62c56f98SSadaf Ebrahimi  * not changed.
3239*62c56f98SSadaf Ebrahimi  *
3240*62c56f98SSadaf Ebrahimi  * If an error occurs at any step after a call to psa_key_derivation_setup(),
3241*62c56f98SSadaf Ebrahimi  * the operation will need to be reset by a call to psa_key_derivation_abort().
3242*62c56f98SSadaf Ebrahimi  *
3243*62c56f98SSadaf Ebrahimi  * Implementations must reject an attempt to derive a key of size 0.
3244*62c56f98SSadaf Ebrahimi  *
3245*62c56f98SSadaf Ebrahimi  * \param[in,out] operation       The key derivation operation object
3246*62c56f98SSadaf Ebrahimi  *                                to set up. It must
3247*62c56f98SSadaf Ebrahimi  *                                have been initialized but not set up yet.
3248*62c56f98SSadaf Ebrahimi  * \param alg                     The key derivation algorithm to compute
3249*62c56f98SSadaf Ebrahimi  *                                (\c PSA_ALG_XXX value such that
3250*62c56f98SSadaf Ebrahimi  *                                #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3251*62c56f98SSadaf Ebrahimi  *
3252*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3253*62c56f98SSadaf Ebrahimi  *         Success.
3254*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3255*62c56f98SSadaf Ebrahimi  *         \c alg is not a key derivation algorithm.
3256*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
3257*62c56f98SSadaf Ebrahimi  *         \c alg is not supported or is not a key derivation algorithm.
3258*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3259*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3260*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3261*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3262*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3263*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3264*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive), or
3265*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
3266*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3267*62c56f98SSadaf Ebrahimi  *         results in this error code.
3268*62c56f98SSadaf Ebrahimi  */
3269*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_setup(
3270*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3271*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg);
3272*62c56f98SSadaf Ebrahimi 
3273*62c56f98SSadaf Ebrahimi /** Retrieve the current capacity of a key derivation operation.
3274*62c56f98SSadaf Ebrahimi  *
3275*62c56f98SSadaf Ebrahimi  * The capacity of a key derivation is the maximum number of bytes that it can
3276*62c56f98SSadaf Ebrahimi  * return. When you get *N* bytes of output from a key derivation operation,
3277*62c56f98SSadaf Ebrahimi  * this reduces its capacity by *N*.
3278*62c56f98SSadaf Ebrahimi  *
3279*62c56f98SSadaf Ebrahimi  * \param[in] operation     The operation to query.
3280*62c56f98SSadaf Ebrahimi  * \param[out] capacity     On success, the capacity of the operation.
3281*62c56f98SSadaf Ebrahimi  *
3282*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3283*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3284*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3285*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3286*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3287*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or
3288*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
3289*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3290*62c56f98SSadaf Ebrahimi  *         results in this error code.
3291*62c56f98SSadaf Ebrahimi  */
3292*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_get_capacity(
3293*62c56f98SSadaf Ebrahimi     const psa_key_derivation_operation_t *operation,
3294*62c56f98SSadaf Ebrahimi     size_t *capacity);
3295*62c56f98SSadaf Ebrahimi 
3296*62c56f98SSadaf Ebrahimi /** Set the maximum capacity of a key derivation operation.
3297*62c56f98SSadaf Ebrahimi  *
3298*62c56f98SSadaf Ebrahimi  * The capacity of a key derivation operation is the maximum number of bytes
3299*62c56f98SSadaf Ebrahimi  * that the key derivation operation can return from this point onwards.
3300*62c56f98SSadaf Ebrahimi  *
3301*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The key derivation operation object to modify.
3302*62c56f98SSadaf Ebrahimi  * \param capacity          The new capacity of the operation.
3303*62c56f98SSadaf Ebrahimi  *                          It must be less or equal to the operation's
3304*62c56f98SSadaf Ebrahimi  *                          current capacity.
3305*62c56f98SSadaf Ebrahimi  *
3306*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3307*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3308*62c56f98SSadaf Ebrahimi  *         \p capacity is larger than the operation's current capacity.
3309*62c56f98SSadaf Ebrahimi  *         In this case, the operation object remains valid and its capacity
3310*62c56f98SSadaf Ebrahimi  *         remains unchanged.
3311*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3312*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3313*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3314*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3315*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active), or the
3316*62c56f98SSadaf Ebrahimi  *         library has not been previously initialized by psa_crypto_init().
3317*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3318*62c56f98SSadaf Ebrahimi  *         results in this error code.
3319*62c56f98SSadaf Ebrahimi  */
3320*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_set_capacity(
3321*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3322*62c56f98SSadaf Ebrahimi     size_t capacity);
3323*62c56f98SSadaf Ebrahimi 
3324*62c56f98SSadaf Ebrahimi /** Use the maximum possible capacity for a key derivation operation.
3325*62c56f98SSadaf Ebrahimi  *
3326*62c56f98SSadaf Ebrahimi  * Use this value as the capacity argument when setting up a key derivation
3327*62c56f98SSadaf Ebrahimi  * to indicate that the operation should have the maximum possible capacity.
3328*62c56f98SSadaf Ebrahimi  * The value of the maximum possible capacity depends on the key derivation
3329*62c56f98SSadaf Ebrahimi  * algorithm.
3330*62c56f98SSadaf Ebrahimi  */
3331*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
3332*62c56f98SSadaf Ebrahimi 
3333*62c56f98SSadaf Ebrahimi /** Provide an input for key derivation or key agreement.
3334*62c56f98SSadaf Ebrahimi  *
3335*62c56f98SSadaf Ebrahimi  * Which inputs are required and in what order depends on the algorithm.
3336*62c56f98SSadaf Ebrahimi  * Refer to the documentation of each key derivation or key agreement
3337*62c56f98SSadaf Ebrahimi  * algorithm for information.
3338*62c56f98SSadaf Ebrahimi  *
3339*62c56f98SSadaf Ebrahimi  * This function passes direct inputs, which is usually correct for
3340*62c56f98SSadaf Ebrahimi  * non-secret inputs. To pass a secret input, which should be in a key
3341*62c56f98SSadaf Ebrahimi  * object, call psa_key_derivation_input_key() instead of this function.
3342*62c56f98SSadaf Ebrahimi  * Refer to the documentation of individual step types
3343*62c56f98SSadaf Ebrahimi  * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3344*62c56f98SSadaf Ebrahimi  * for more information.
3345*62c56f98SSadaf Ebrahimi  *
3346*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
3347*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3348*62c56f98SSadaf Ebrahimi  *
3349*62c56f98SSadaf Ebrahimi  * \param[in,out] operation       The key derivation operation object to use.
3350*62c56f98SSadaf Ebrahimi  *                                It must have been set up with
3351*62c56f98SSadaf Ebrahimi  *                                psa_key_derivation_setup() and must not
3352*62c56f98SSadaf Ebrahimi  *                                have produced any output yet.
3353*62c56f98SSadaf Ebrahimi  * \param step                    Which step the input data is for.
3354*62c56f98SSadaf Ebrahimi  * \param[in] data                Input data to use.
3355*62c56f98SSadaf Ebrahimi  * \param data_length             Size of the \p data buffer in bytes.
3356*62c56f98SSadaf Ebrahimi  *
3357*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3358*62c56f98SSadaf Ebrahimi  *         Success.
3359*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3360*62c56f98SSadaf Ebrahimi  *         \c step is not compatible with the operation's algorithm, or
3361*62c56f98SSadaf Ebrahimi  *         \c step does not allow direct inputs.
3362*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3363*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3364*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3365*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3366*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3367*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3368*62c56f98SSadaf Ebrahimi  *         The operation state is not valid for this input \p step, or
3369*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
3370*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3371*62c56f98SSadaf Ebrahimi  *         results in this error code.
3372*62c56f98SSadaf Ebrahimi  */
3373*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_input_bytes(
3374*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3375*62c56f98SSadaf Ebrahimi     psa_key_derivation_step_t step,
3376*62c56f98SSadaf Ebrahimi     const uint8_t *data,
3377*62c56f98SSadaf Ebrahimi     size_t data_length);
3378*62c56f98SSadaf Ebrahimi 
3379*62c56f98SSadaf Ebrahimi /** Provide a numeric input for key derivation or key agreement.
3380*62c56f98SSadaf Ebrahimi  *
3381*62c56f98SSadaf Ebrahimi  * Which inputs are required and in what order depends on the algorithm.
3382*62c56f98SSadaf Ebrahimi  * However, when an algorithm requires a particular order, numeric inputs
3383*62c56f98SSadaf Ebrahimi  * usually come first as they tend to be configuration parameters.
3384*62c56f98SSadaf Ebrahimi  * Refer to the documentation of each key derivation or key agreement
3385*62c56f98SSadaf Ebrahimi  * algorithm for information.
3386*62c56f98SSadaf Ebrahimi  *
3387*62c56f98SSadaf Ebrahimi  * This function is used for inputs which are fixed-size non-negative
3388*62c56f98SSadaf Ebrahimi  * integers.
3389*62c56f98SSadaf Ebrahimi  *
3390*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
3391*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3392*62c56f98SSadaf Ebrahimi  *
3393*62c56f98SSadaf Ebrahimi  * \param[in,out] operation       The key derivation operation object to use.
3394*62c56f98SSadaf Ebrahimi  *                                It must have been set up with
3395*62c56f98SSadaf Ebrahimi  *                                psa_key_derivation_setup() and must not
3396*62c56f98SSadaf Ebrahimi  *                                have produced any output yet.
3397*62c56f98SSadaf Ebrahimi  * \param step                    Which step the input data is for.
3398*62c56f98SSadaf Ebrahimi  * \param[in] value               The value of the numeric input.
3399*62c56f98SSadaf Ebrahimi  *
3400*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3401*62c56f98SSadaf Ebrahimi  *         Success.
3402*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3403*62c56f98SSadaf Ebrahimi  *         \c step is not compatible with the operation's algorithm, or
3404*62c56f98SSadaf Ebrahimi  *         \c step does not allow numeric inputs.
3405*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3406*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3407*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3408*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3409*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3410*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3411*62c56f98SSadaf Ebrahimi  *         The operation state is not valid for this input \p step, or
3412*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
3413*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3414*62c56f98SSadaf Ebrahimi  *         results in this error code.
3415*62c56f98SSadaf Ebrahimi  */
3416*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_input_integer(
3417*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3418*62c56f98SSadaf Ebrahimi     psa_key_derivation_step_t step,
3419*62c56f98SSadaf Ebrahimi     uint64_t value);
3420*62c56f98SSadaf Ebrahimi 
3421*62c56f98SSadaf Ebrahimi /** Provide an input for key derivation in the form of a key.
3422*62c56f98SSadaf Ebrahimi  *
3423*62c56f98SSadaf Ebrahimi  * Which inputs are required and in what order depends on the algorithm.
3424*62c56f98SSadaf Ebrahimi  * Refer to the documentation of each key derivation or key agreement
3425*62c56f98SSadaf Ebrahimi  * algorithm for information.
3426*62c56f98SSadaf Ebrahimi  *
3427*62c56f98SSadaf Ebrahimi  * This function obtains input from a key object, which is usually correct for
3428*62c56f98SSadaf Ebrahimi  * secret inputs or for non-secret personalization strings kept in the key
3429*62c56f98SSadaf Ebrahimi  * store. To pass a non-secret parameter which is not in the key store,
3430*62c56f98SSadaf Ebrahimi  * call psa_key_derivation_input_bytes() instead of this function.
3431*62c56f98SSadaf Ebrahimi  * Refer to the documentation of individual step types
3432*62c56f98SSadaf Ebrahimi  * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3433*62c56f98SSadaf Ebrahimi  * for more information.
3434*62c56f98SSadaf Ebrahimi  *
3435*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
3436*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3437*62c56f98SSadaf Ebrahimi  *
3438*62c56f98SSadaf Ebrahimi  * \param[in,out] operation       The key derivation operation object to use.
3439*62c56f98SSadaf Ebrahimi  *                                It must have been set up with
3440*62c56f98SSadaf Ebrahimi  *                                psa_key_derivation_setup() and must not
3441*62c56f98SSadaf Ebrahimi  *                                have produced any output yet.
3442*62c56f98SSadaf Ebrahimi  * \param step                    Which step the input data is for.
3443*62c56f98SSadaf Ebrahimi  * \param key                     Identifier of the key. It must have an
3444*62c56f98SSadaf Ebrahimi  *                                appropriate type for step and must allow the
3445*62c56f98SSadaf Ebrahimi  *                                usage #PSA_KEY_USAGE_DERIVE or
3446*62c56f98SSadaf Ebrahimi  *                                #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3447*62c56f98SSadaf Ebrahimi  *                                and the algorithm used by the operation.
3448*62c56f98SSadaf Ebrahimi  *
3449*62c56f98SSadaf Ebrahimi  * \note Once all inputs steps are completed, the operations will allow:
3450*62c56f98SSadaf Ebrahimi  * - psa_key_derivation_output_bytes() if each input was either a direct input
3451*62c56f98SSadaf Ebrahimi  *   or  a key with #PSA_KEY_USAGE_DERIVE set;
3452*62c56f98SSadaf Ebrahimi  * - psa_key_derivation_output_key() if the input for step
3453*62c56f98SSadaf Ebrahimi  *   #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
3454*62c56f98SSadaf Ebrahimi  *   was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3455*62c56f98SSadaf Ebrahimi  *   either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
3456*62c56f98SSadaf Ebrahimi  * - psa_key_derivation_verify_bytes() if each input was either a direct input
3457*62c56f98SSadaf Ebrahimi  *   or  a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
3458*62c56f98SSadaf Ebrahimi  * - psa_key_derivation_verify_key() under the same conditions as
3459*62c56f98SSadaf Ebrahimi  *   psa_key_derivation_verify_bytes().
3460*62c56f98SSadaf Ebrahimi  *
3461*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3462*62c56f98SSadaf Ebrahimi  *         Success.
3463*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3464*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
3465*62c56f98SSadaf Ebrahimi  *         The key allows neither #PSA_KEY_USAGE_DERIVE nor
3466*62c56f98SSadaf Ebrahimi  *         #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3467*62c56f98SSadaf Ebrahimi  *         algorithm.
3468*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3469*62c56f98SSadaf Ebrahimi  *         \c step is not compatible with the operation's algorithm, or
3470*62c56f98SSadaf Ebrahimi  *         \c step does not allow key inputs of the given type
3471*62c56f98SSadaf Ebrahimi  *         or does not allow key inputs at all.
3472*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3473*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3474*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3475*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3476*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3477*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3478*62c56f98SSadaf Ebrahimi  *         The operation state is not valid for this input \p step, or
3479*62c56f98SSadaf Ebrahimi  *         the library has not been previously initialized by psa_crypto_init().
3480*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3481*62c56f98SSadaf Ebrahimi  *         results in this error code.
3482*62c56f98SSadaf Ebrahimi  */
3483*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_input_key(
3484*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3485*62c56f98SSadaf Ebrahimi     psa_key_derivation_step_t step,
3486*62c56f98SSadaf Ebrahimi     mbedtls_svc_key_id_t key);
3487*62c56f98SSadaf Ebrahimi 
3488*62c56f98SSadaf Ebrahimi /** Perform a key agreement and use the shared secret as input to a key
3489*62c56f98SSadaf Ebrahimi  * derivation.
3490*62c56f98SSadaf Ebrahimi  *
3491*62c56f98SSadaf Ebrahimi  * A key agreement algorithm takes two inputs: a private key \p private_key
3492*62c56f98SSadaf Ebrahimi  * a public key \p peer_key.
3493*62c56f98SSadaf Ebrahimi  * The result of this function is passed as input to a key derivation.
3494*62c56f98SSadaf Ebrahimi  * The output of this key derivation can be extracted by reading from the
3495*62c56f98SSadaf Ebrahimi  * resulting operation to produce keys and other cryptographic material.
3496*62c56f98SSadaf Ebrahimi  *
3497*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the operation enters an error
3498*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3499*62c56f98SSadaf Ebrahimi  *
3500*62c56f98SSadaf Ebrahimi  * \param[in,out] operation       The key derivation operation object to use.
3501*62c56f98SSadaf Ebrahimi  *                                It must have been set up with
3502*62c56f98SSadaf Ebrahimi  *                                psa_key_derivation_setup() with a
3503*62c56f98SSadaf Ebrahimi  *                                key agreement and derivation algorithm
3504*62c56f98SSadaf Ebrahimi  *                                \c alg (\c PSA_ALG_XXX value such that
3505*62c56f98SSadaf Ebrahimi  *                                #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3506*62c56f98SSadaf Ebrahimi  *                                and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3507*62c56f98SSadaf Ebrahimi  *                                is false).
3508*62c56f98SSadaf Ebrahimi  *                                The operation must be ready for an
3509*62c56f98SSadaf Ebrahimi  *                                input of the type given by \p step.
3510*62c56f98SSadaf Ebrahimi  * \param step                    Which step the input data is for.
3511*62c56f98SSadaf Ebrahimi  * \param private_key             Identifier of the private key to use. It must
3512*62c56f98SSadaf Ebrahimi  *                                allow the usage #PSA_KEY_USAGE_DERIVE.
3513*62c56f98SSadaf Ebrahimi  * \param[in] peer_key      Public key of the peer. The peer key must be in the
3514*62c56f98SSadaf Ebrahimi  *                          same format that psa_import_key() accepts for the
3515*62c56f98SSadaf Ebrahimi  *                          public key type corresponding to the type of
3516*62c56f98SSadaf Ebrahimi  *                          private_key. That is, this function performs the
3517*62c56f98SSadaf Ebrahimi  *                          equivalent of
3518*62c56f98SSadaf Ebrahimi  *                          #psa_import_key(...,
3519*62c56f98SSadaf Ebrahimi  *                          `peer_key`, `peer_key_length`) where
3520*62c56f98SSadaf Ebrahimi  *                          with key attributes indicating the public key
3521*62c56f98SSadaf Ebrahimi  *                          type corresponding to the type of `private_key`.
3522*62c56f98SSadaf Ebrahimi  *                          For example, for EC keys, this means that peer_key
3523*62c56f98SSadaf Ebrahimi  *                          is interpreted as a point on the curve that the
3524*62c56f98SSadaf Ebrahimi  *                          private key is on. The standard formats for public
3525*62c56f98SSadaf Ebrahimi  *                          keys are documented in the documentation of
3526*62c56f98SSadaf Ebrahimi  *                          psa_export_public_key().
3527*62c56f98SSadaf Ebrahimi  * \param peer_key_length         Size of \p peer_key in bytes.
3528*62c56f98SSadaf Ebrahimi  *
3529*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3530*62c56f98SSadaf Ebrahimi  *         Success.
3531*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3532*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3533*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3534*62c56f98SSadaf Ebrahimi  *         \c private_key is not compatible with \c alg,
3535*62c56f98SSadaf Ebrahimi  *         or \p peer_key is not valid for \c alg or not compatible with
3536*62c56f98SSadaf Ebrahimi  *         \c private_key, or \c step does not allow an input resulting
3537*62c56f98SSadaf Ebrahimi  *         from a key agreement.
3538*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
3539*62c56f98SSadaf Ebrahimi  *         \c alg is not supported or is not a key derivation algorithm.
3540*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3541*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3542*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3543*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3544*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3545*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3546*62c56f98SSadaf Ebrahimi  *         The operation state is not valid for this key agreement \p step,
3547*62c56f98SSadaf Ebrahimi  *         or the library has not been previously initialized by psa_crypto_init().
3548*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3549*62c56f98SSadaf Ebrahimi  *         results in this error code.
3550*62c56f98SSadaf Ebrahimi  */
3551*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_key_agreement(
3552*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3553*62c56f98SSadaf Ebrahimi     psa_key_derivation_step_t step,
3554*62c56f98SSadaf Ebrahimi     mbedtls_svc_key_id_t private_key,
3555*62c56f98SSadaf Ebrahimi     const uint8_t *peer_key,
3556*62c56f98SSadaf Ebrahimi     size_t peer_key_length);
3557*62c56f98SSadaf Ebrahimi 
3558*62c56f98SSadaf Ebrahimi /** Read some data from a key derivation operation.
3559*62c56f98SSadaf Ebrahimi  *
3560*62c56f98SSadaf Ebrahimi  * This function calculates output bytes from a key derivation algorithm and
3561*62c56f98SSadaf Ebrahimi  * return those bytes.
3562*62c56f98SSadaf Ebrahimi  * If you view the key derivation's output as a stream of bytes, this
3563*62c56f98SSadaf Ebrahimi  * function destructively reads the requested number of bytes from the
3564*62c56f98SSadaf Ebrahimi  * stream.
3565*62c56f98SSadaf Ebrahimi  * The operation's capacity decreases by the number of bytes read.
3566*62c56f98SSadaf Ebrahimi  *
3567*62c56f98SSadaf Ebrahimi  * If this function returns an error status other than
3568*62c56f98SSadaf Ebrahimi  * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3569*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3570*62c56f98SSadaf Ebrahimi  *
3571*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The key derivation operation object to read from.
3572*62c56f98SSadaf Ebrahimi  * \param[out] output       Buffer where the output will be written.
3573*62c56f98SSadaf Ebrahimi  * \param output_length     Number of bytes to output.
3574*62c56f98SSadaf Ebrahimi  *
3575*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3576*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
3577*62c56f98SSadaf Ebrahimi  *         One of the inputs was a key whose policy didn't allow
3578*62c56f98SSadaf Ebrahimi  *         #PSA_KEY_USAGE_DERIVE.
3579*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_DATA
3580*62c56f98SSadaf Ebrahimi  *                          The operation's capacity was less than
3581*62c56f98SSadaf Ebrahimi  *                          \p output_length bytes. Note that in this case,
3582*62c56f98SSadaf Ebrahimi  *                          no output is written to the output buffer.
3583*62c56f98SSadaf Ebrahimi  *                          The operation's capacity is set to 0, thus
3584*62c56f98SSadaf Ebrahimi  *                          subsequent calls to this function will not
3585*62c56f98SSadaf Ebrahimi  *                          succeed, even with a smaller output buffer.
3586*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3587*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3588*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3589*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3590*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3591*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3592*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active and completed
3593*62c56f98SSadaf Ebrahimi  *         all required input steps), or the library has not been previously
3594*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
3595*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3596*62c56f98SSadaf Ebrahimi  *         results in this error code.
3597*62c56f98SSadaf Ebrahimi  */
3598*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_output_bytes(
3599*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3600*62c56f98SSadaf Ebrahimi     uint8_t *output,
3601*62c56f98SSadaf Ebrahimi     size_t output_length);
3602*62c56f98SSadaf Ebrahimi 
3603*62c56f98SSadaf Ebrahimi /** Derive a key from an ongoing key derivation operation.
3604*62c56f98SSadaf Ebrahimi  *
3605*62c56f98SSadaf Ebrahimi  * This function calculates output bytes from a key derivation algorithm
3606*62c56f98SSadaf Ebrahimi  * and uses those bytes to generate a key deterministically.
3607*62c56f98SSadaf Ebrahimi  * The key's location, usage policy, type and size are taken from
3608*62c56f98SSadaf Ebrahimi  * \p attributes.
3609*62c56f98SSadaf Ebrahimi  *
3610*62c56f98SSadaf Ebrahimi  * If you view the key derivation's output as a stream of bytes, this
3611*62c56f98SSadaf Ebrahimi  * function destructively reads as many bytes as required from the
3612*62c56f98SSadaf Ebrahimi  * stream.
3613*62c56f98SSadaf Ebrahimi  * The operation's capacity decreases by the number of bytes read.
3614*62c56f98SSadaf Ebrahimi  *
3615*62c56f98SSadaf Ebrahimi  * If this function returns an error status other than
3616*62c56f98SSadaf Ebrahimi  * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3617*62c56f98SSadaf Ebrahimi  * state and must be aborted by calling psa_key_derivation_abort().
3618*62c56f98SSadaf Ebrahimi  *
3619*62c56f98SSadaf Ebrahimi  * How much output is produced and consumed from the operation, and how
3620*62c56f98SSadaf Ebrahimi  * the key is derived, depends on the key type and on the key size
3621*62c56f98SSadaf Ebrahimi  * (denoted \c bits below):
3622*62c56f98SSadaf Ebrahimi  *
3623*62c56f98SSadaf Ebrahimi  * - For key types for which the key is an arbitrary sequence of bytes
3624*62c56f98SSadaf Ebrahimi  *   of a given size, this function is functionally equivalent to
3625*62c56f98SSadaf Ebrahimi  *   calling #psa_key_derivation_output_bytes
3626*62c56f98SSadaf Ebrahimi  *   and passing the resulting output to #psa_import_key.
3627*62c56f98SSadaf Ebrahimi  *   However, this function has a security benefit:
3628*62c56f98SSadaf Ebrahimi  *   if the implementation provides an isolation boundary then
3629*62c56f98SSadaf Ebrahimi  *   the key material is not exposed outside the isolation boundary.
3630*62c56f98SSadaf Ebrahimi  *   As a consequence, for these key types, this function always consumes
3631*62c56f98SSadaf Ebrahimi  *   exactly (\c bits / 8) bytes from the operation.
3632*62c56f98SSadaf Ebrahimi  *   The following key types defined in this specification follow this scheme:
3633*62c56f98SSadaf Ebrahimi  *
3634*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_AES;
3635*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_ARIA;
3636*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_CAMELLIA;
3637*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_DERIVE;
3638*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_HMAC;
3639*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_PASSWORD_HASH.
3640*62c56f98SSadaf Ebrahimi  *
3641*62c56f98SSadaf Ebrahimi  * - For ECC keys on a Montgomery elliptic curve
3642*62c56f98SSadaf Ebrahimi  *   (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3643*62c56f98SSadaf Ebrahimi  *   Montgomery curve), this function always draws a byte string whose
3644*62c56f98SSadaf Ebrahimi  *   length is determined by the curve, and sets the mandatory bits
3645*62c56f98SSadaf Ebrahimi  *   accordingly. That is:
3646*62c56f98SSadaf Ebrahimi  *
3647*62c56f98SSadaf Ebrahimi  *     - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
3648*62c56f98SSadaf Ebrahimi  *       string and process it as specified in RFC 7748 &sect;5.
3649*62c56f98SSadaf Ebrahimi  *     - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
3650*62c56f98SSadaf Ebrahimi  *       string and process it as specified in RFC 7748 &sect;5.
3651*62c56f98SSadaf Ebrahimi  *
3652*62c56f98SSadaf Ebrahimi  * - For key types for which the key is represented by a single sequence of
3653*62c56f98SSadaf Ebrahimi  *   \c bits bits with constraints as to which bit sequences are acceptable,
3654*62c56f98SSadaf Ebrahimi  *   this function draws a byte string of length (\c bits / 8) bytes rounded
3655*62c56f98SSadaf Ebrahimi  *   up to the nearest whole number of bytes. If the resulting byte string
3656*62c56f98SSadaf Ebrahimi  *   is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3657*62c56f98SSadaf Ebrahimi  *   This process is repeated until an acceptable byte string is drawn.
3658*62c56f98SSadaf Ebrahimi  *   The byte string drawn from the operation is interpreted as specified
3659*62c56f98SSadaf Ebrahimi  *   for the output produced by psa_export_key().
3660*62c56f98SSadaf Ebrahimi  *   The following key types defined in this specification follow this scheme:
3661*62c56f98SSadaf Ebrahimi  *
3662*62c56f98SSadaf Ebrahimi  *     - #PSA_KEY_TYPE_DES.
3663*62c56f98SSadaf Ebrahimi  *       Force-set the parity bits, but discard forbidden weak keys.
3664*62c56f98SSadaf Ebrahimi  *       For 2-key and 3-key triple-DES, the three keys are generated
3665*62c56f98SSadaf Ebrahimi  *       successively (for example, for 3-key triple-DES,
3666*62c56f98SSadaf Ebrahimi  *       if the first 8 bytes specify a weak key and the next 8 bytes do not,
3667*62c56f98SSadaf Ebrahimi  *       discard the first 8 bytes, use the next 8 bytes as the first key,
3668*62c56f98SSadaf Ebrahimi  *       and continue reading output from the operation to derive the other
3669*62c56f98SSadaf Ebrahimi  *       two keys).
3670*62c56f98SSadaf Ebrahimi  *     - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3671*62c56f98SSadaf Ebrahimi  *       where \c group designates any Diffie-Hellman group) and
3672*62c56f98SSadaf Ebrahimi  *       ECC keys on a Weierstrass elliptic curve
3673*62c56f98SSadaf Ebrahimi  *       (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3674*62c56f98SSadaf Ebrahimi  *       Weierstrass curve).
3675*62c56f98SSadaf Ebrahimi  *       For these key types, interpret the byte string as integer
3676*62c56f98SSadaf Ebrahimi  *       in big-endian order. Discard it if it is not in the range
3677*62c56f98SSadaf Ebrahimi  *       [0, *N* - 2] where *N* is the boundary of the private key domain
3678*62c56f98SSadaf Ebrahimi  *       (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3679*62c56f98SSadaf Ebrahimi  *       or the order of the curve's base point for ECC).
3680*62c56f98SSadaf Ebrahimi  *       Add 1 to the resulting integer and use this as the private key *x*.
3681*62c56f98SSadaf Ebrahimi  *       This method allows compliance to NIST standards, specifically
3682*62c56f98SSadaf Ebrahimi  *       the methods titled "key-pair generation by testing candidates"
3683*62c56f98SSadaf Ebrahimi  *       in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3684*62c56f98SSadaf Ebrahimi  *       in FIPS 186-4 &sect;B.1.2 for DSA, and
3685*62c56f98SSadaf Ebrahimi  *       in NIST SP 800-56A &sect;5.6.1.2.2 or
3686*62c56f98SSadaf Ebrahimi  *       FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3687*62c56f98SSadaf Ebrahimi  *
3688*62c56f98SSadaf Ebrahimi  * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3689*62c56f98SSadaf Ebrahimi  *   the way in which the operation output is consumed is
3690*62c56f98SSadaf Ebrahimi  *   implementation-defined.
3691*62c56f98SSadaf Ebrahimi  *
3692*62c56f98SSadaf Ebrahimi  * In all cases, the data that is read is discarded from the operation.
3693*62c56f98SSadaf Ebrahimi  * The operation's capacity is decreased by the number of bytes read.
3694*62c56f98SSadaf Ebrahimi  *
3695*62c56f98SSadaf Ebrahimi  * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3696*62c56f98SSadaf Ebrahimi  * the input to that step must be provided with psa_key_derivation_input_key().
3697*62c56f98SSadaf Ebrahimi  * Future versions of this specification may include additional restrictions
3698*62c56f98SSadaf Ebrahimi  * on the derived key based on the attributes and strength of the secret key.
3699*62c56f98SSadaf Ebrahimi  *
3700*62c56f98SSadaf Ebrahimi  * \param[in] attributes    The attributes for the new key.
3701*62c56f98SSadaf Ebrahimi  *                          If the key type to be created is
3702*62c56f98SSadaf Ebrahimi  *                          #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3703*62c56f98SSadaf Ebrahimi  *                          the policy must be the same as in the current
3704*62c56f98SSadaf Ebrahimi  *                          operation.
3705*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The key derivation operation object to read from.
3706*62c56f98SSadaf Ebrahimi  * \param[out] key          On success, an identifier for the newly created
3707*62c56f98SSadaf Ebrahimi  *                          key. For persistent keys, this is the key
3708*62c56f98SSadaf Ebrahimi  *                          identifier defined in \p attributes.
3709*62c56f98SSadaf Ebrahimi  *                          \c 0 on failure.
3710*62c56f98SSadaf Ebrahimi  *
3711*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3712*62c56f98SSadaf Ebrahimi  *         Success.
3713*62c56f98SSadaf Ebrahimi  *         If the key is persistent, the key material and the key's metadata
3714*62c56f98SSadaf Ebrahimi  *         have been saved to persistent storage.
3715*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_ALREADY_EXISTS
3716*62c56f98SSadaf Ebrahimi  *         This is an attempt to create a persistent key, and there is
3717*62c56f98SSadaf Ebrahimi  *         already a persistent key with the given identifier.
3718*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_DATA
3719*62c56f98SSadaf Ebrahimi  *         There was not enough data to create the desired key.
3720*62c56f98SSadaf Ebrahimi  *         Note that in this case, no output is written to the output buffer.
3721*62c56f98SSadaf Ebrahimi  *         The operation's capacity is set to 0, thus subsequent calls to
3722*62c56f98SSadaf Ebrahimi  *         this function will not succeed, even with a smaller output buffer.
3723*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
3724*62c56f98SSadaf Ebrahimi  *         The key type or key size is not supported, either by the
3725*62c56f98SSadaf Ebrahimi  *         implementation in general or in this particular location.
3726*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3727*62c56f98SSadaf Ebrahimi  *         The provided key attributes are not valid for the operation.
3728*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
3729*62c56f98SSadaf Ebrahimi  *         The #PSA_KEY_DERIVATION_INPUT_SECRET or
3730*62c56f98SSadaf Ebrahimi  *         #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3731*62c56f98SSadaf Ebrahimi  *         key; or one of the inputs was a key whose policy didn't allow
3732*62c56f98SSadaf Ebrahimi  *         #PSA_KEY_USAGE_DERIVE.
3733*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3734*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3735*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3736*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3737*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3738*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3739*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3740*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3741*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3742*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active and completed
3743*62c56f98SSadaf Ebrahimi  *         all required input steps), or the library has not been previously
3744*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
3745*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3746*62c56f98SSadaf Ebrahimi  *         results in this error code.
3747*62c56f98SSadaf Ebrahimi  */
3748*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_output_key(
3749*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
3750*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3751*62c56f98SSadaf Ebrahimi     mbedtls_svc_key_id_t *key);
3752*62c56f98SSadaf Ebrahimi 
3753*62c56f98SSadaf Ebrahimi /** Compare output data from a key derivation operation to an expected value.
3754*62c56f98SSadaf Ebrahimi  *
3755*62c56f98SSadaf Ebrahimi  * This function calculates output bytes from a key derivation algorithm and
3756*62c56f98SSadaf Ebrahimi  * compares those bytes to an expected value in constant time.
3757*62c56f98SSadaf Ebrahimi  * If you view the key derivation's output as a stream of bytes, this
3758*62c56f98SSadaf Ebrahimi  * function destructively reads the expected number of bytes from the
3759*62c56f98SSadaf Ebrahimi  * stream before comparing them.
3760*62c56f98SSadaf Ebrahimi  * The operation's capacity decreases by the number of bytes read.
3761*62c56f98SSadaf Ebrahimi  *
3762*62c56f98SSadaf Ebrahimi  * This is functionally equivalent to the following code:
3763*62c56f98SSadaf Ebrahimi  * \code
3764*62c56f98SSadaf Ebrahimi  * psa_key_derivation_output_bytes(operation, tmp, output_length);
3765*62c56f98SSadaf Ebrahimi  * if (memcmp(output, tmp, output_length) != 0)
3766*62c56f98SSadaf Ebrahimi  *     return PSA_ERROR_INVALID_SIGNATURE;
3767*62c56f98SSadaf Ebrahimi  * \endcode
3768*62c56f98SSadaf Ebrahimi  * except (1) it works even if the key's policy does not allow outputting the
3769*62c56f98SSadaf Ebrahimi  * bytes, and (2) the comparison will be done in constant time.
3770*62c56f98SSadaf Ebrahimi  *
3771*62c56f98SSadaf Ebrahimi  * If this function returns an error status other than
3772*62c56f98SSadaf Ebrahimi  * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3773*62c56f98SSadaf Ebrahimi  * the operation enters an error state and must be aborted by calling
3774*62c56f98SSadaf Ebrahimi  * psa_key_derivation_abort().
3775*62c56f98SSadaf Ebrahimi  *
3776*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The key derivation operation object to read from.
3777*62c56f98SSadaf Ebrahimi  * \param[in] expected_output Buffer containing the expected derivation output.
3778*62c56f98SSadaf Ebrahimi  * \param output_length     Length of the expected output; this is also the
3779*62c56f98SSadaf Ebrahimi  *                          number of bytes that will be read.
3780*62c56f98SSadaf Ebrahimi  *
3781*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3782*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
3783*62c56f98SSadaf Ebrahimi  *         The output was read successfully, but it differs from the expected
3784*62c56f98SSadaf Ebrahimi  *         output.
3785*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
3786*62c56f98SSadaf Ebrahimi  *         One of the inputs was a key whose policy didn't allow
3787*62c56f98SSadaf Ebrahimi  *         #PSA_KEY_USAGE_VERIFY_DERIVATION.
3788*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_DATA
3789*62c56f98SSadaf Ebrahimi  *                          The operation's capacity was less than
3790*62c56f98SSadaf Ebrahimi  *                          \p output_length bytes. Note that in this case,
3791*62c56f98SSadaf Ebrahimi  *                          the operation's capacity is set to 0, thus
3792*62c56f98SSadaf Ebrahimi  *                          subsequent calls to this function will not
3793*62c56f98SSadaf Ebrahimi  *                          succeed, even with a smaller expected output.
3794*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3795*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3796*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3797*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3798*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3799*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3800*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active and completed
3801*62c56f98SSadaf Ebrahimi  *         all required input steps), or the library has not been previously
3802*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
3803*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3804*62c56f98SSadaf Ebrahimi  *         results in this error code.
3805*62c56f98SSadaf Ebrahimi  */
3806*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_verify_bytes(
3807*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3808*62c56f98SSadaf Ebrahimi     const uint8_t *expected_output,
3809*62c56f98SSadaf Ebrahimi     size_t output_length);
3810*62c56f98SSadaf Ebrahimi 
3811*62c56f98SSadaf Ebrahimi /** Compare output data from a key derivation operation to an expected value
3812*62c56f98SSadaf Ebrahimi  * stored in a key object.
3813*62c56f98SSadaf Ebrahimi  *
3814*62c56f98SSadaf Ebrahimi  * This function calculates output bytes from a key derivation algorithm and
3815*62c56f98SSadaf Ebrahimi  * compares those bytes to an expected value, provided as key of type
3816*62c56f98SSadaf Ebrahimi  * #PSA_KEY_TYPE_PASSWORD_HASH.
3817*62c56f98SSadaf Ebrahimi  * If you view the key derivation's output as a stream of bytes, this
3818*62c56f98SSadaf Ebrahimi  * function destructively reads the number of bytes corresponding to the
3819*62c56f98SSadaf Ebrahimi  * length of the expected value from the stream before comparing them.
3820*62c56f98SSadaf Ebrahimi  * The operation's capacity decreases by the number of bytes read.
3821*62c56f98SSadaf Ebrahimi  *
3822*62c56f98SSadaf Ebrahimi  * This is functionally equivalent to exporting the key and calling
3823*62c56f98SSadaf Ebrahimi  * psa_key_derivation_verify_bytes() on the result, except that it
3824*62c56f98SSadaf Ebrahimi  * works even if the key cannot be exported.
3825*62c56f98SSadaf Ebrahimi  *
3826*62c56f98SSadaf Ebrahimi  * If this function returns an error status other than
3827*62c56f98SSadaf Ebrahimi  * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3828*62c56f98SSadaf Ebrahimi  * the operation enters an error state and must be aborted by calling
3829*62c56f98SSadaf Ebrahimi  * psa_key_derivation_abort().
3830*62c56f98SSadaf Ebrahimi  *
3831*62c56f98SSadaf Ebrahimi  * \param[in,out] operation The key derivation operation object to read from.
3832*62c56f98SSadaf Ebrahimi  * \param[in] expected      A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3833*62c56f98SSadaf Ebrahimi  *                          containing the expected output. Its policy must
3834*62c56f98SSadaf Ebrahimi  *                          include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3835*62c56f98SSadaf Ebrahimi  *                          and the permitted algorithm must match the
3836*62c56f98SSadaf Ebrahimi  *                          operation. The value of this key was likely
3837*62c56f98SSadaf Ebrahimi  *                          computed by a previous call to
3838*62c56f98SSadaf Ebrahimi  *                          psa_key_derivation_output_key().
3839*62c56f98SSadaf Ebrahimi  *
3840*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3841*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
3842*62c56f98SSadaf Ebrahimi  *         The output was read successfully, but if differs from the expected
3843*62c56f98SSadaf Ebrahimi  *         output.
3844*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE
3845*62c56f98SSadaf Ebrahimi  *         The key passed as the expected value does not exist.
3846*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3847*62c56f98SSadaf Ebrahimi  *         The key passed as the expected value has an invalid type.
3848*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
3849*62c56f98SSadaf Ebrahimi  *         The key passed as the expected value does not allow this usage or
3850*62c56f98SSadaf Ebrahimi  *         this algorithm; or one of the inputs was a key whose policy didn't
3851*62c56f98SSadaf Ebrahimi  *         allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
3852*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_DATA
3853*62c56f98SSadaf Ebrahimi  *                          The operation's capacity was less than
3854*62c56f98SSadaf Ebrahimi  *                          the length of the expected value. In this case,
3855*62c56f98SSadaf Ebrahimi  *                          the operation's capacity is set to 0, thus
3856*62c56f98SSadaf Ebrahimi  *                          subsequent calls to this function will not
3857*62c56f98SSadaf Ebrahimi  *                          succeed, even with a smaller expected output.
3858*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3859*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3860*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3861*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3862*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3863*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3864*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active and completed
3865*62c56f98SSadaf Ebrahimi  *         all required input steps), or the library has not been previously
3866*62c56f98SSadaf Ebrahimi  *         initialized by psa_crypto_init().
3867*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3868*62c56f98SSadaf Ebrahimi  *         results in this error code.
3869*62c56f98SSadaf Ebrahimi  */
3870*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_verify_key(
3871*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation,
3872*62c56f98SSadaf Ebrahimi     psa_key_id_t expected);
3873*62c56f98SSadaf Ebrahimi 
3874*62c56f98SSadaf Ebrahimi /** Abort a key derivation operation.
3875*62c56f98SSadaf Ebrahimi  *
3876*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the \c
3877*62c56f98SSadaf Ebrahimi  * operation structure itself. Once aborted, the operation object can be reused
3878*62c56f98SSadaf Ebrahimi  * for another operation by calling psa_key_derivation_setup() again.
3879*62c56f98SSadaf Ebrahimi  *
3880*62c56f98SSadaf Ebrahimi  * This function may be called at any time after the operation
3881*62c56f98SSadaf Ebrahimi  * object has been initialized as described in #psa_key_derivation_operation_t.
3882*62c56f98SSadaf Ebrahimi  *
3883*62c56f98SSadaf Ebrahimi  * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3884*62c56f98SSadaf Ebrahimi  * call psa_key_derivation_abort() on an operation that has not been set up.
3885*62c56f98SSadaf Ebrahimi  *
3886*62c56f98SSadaf Ebrahimi  * \param[in,out] operation    The operation to abort.
3887*62c56f98SSadaf Ebrahimi  *
3888*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3889*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3890*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3891*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3892*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3893*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3894*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3895*62c56f98SSadaf Ebrahimi  *         results in this error code.
3896*62c56f98SSadaf Ebrahimi  */
3897*62c56f98SSadaf Ebrahimi psa_status_t psa_key_derivation_abort(
3898*62c56f98SSadaf Ebrahimi     psa_key_derivation_operation_t *operation);
3899*62c56f98SSadaf Ebrahimi 
3900*62c56f98SSadaf Ebrahimi /** Perform a key agreement and return the raw shared secret.
3901*62c56f98SSadaf Ebrahimi  *
3902*62c56f98SSadaf Ebrahimi  * \warning The raw result of a key agreement algorithm such as finite-field
3903*62c56f98SSadaf Ebrahimi  * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3904*62c56f98SSadaf Ebrahimi  * not be used directly as key material. It should instead be passed as
3905*62c56f98SSadaf Ebrahimi  * input to a key derivation algorithm. To chain a key agreement with
3906*62c56f98SSadaf Ebrahimi  * a key derivation, use psa_key_derivation_key_agreement() and other
3907*62c56f98SSadaf Ebrahimi  * functions from the key derivation interface.
3908*62c56f98SSadaf Ebrahimi  *
3909*62c56f98SSadaf Ebrahimi  * \param alg                     The key agreement algorithm to compute
3910*62c56f98SSadaf Ebrahimi  *                                (\c PSA_ALG_XXX value such that
3911*62c56f98SSadaf Ebrahimi  *                                #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3912*62c56f98SSadaf Ebrahimi  *                                is true).
3913*62c56f98SSadaf Ebrahimi  * \param private_key             Identifier of the private key to use. It must
3914*62c56f98SSadaf Ebrahimi  *                                allow the usage #PSA_KEY_USAGE_DERIVE.
3915*62c56f98SSadaf Ebrahimi  * \param[in] peer_key            Public key of the peer. It must be
3916*62c56f98SSadaf Ebrahimi  *                                in the same format that psa_import_key()
3917*62c56f98SSadaf Ebrahimi  *                                accepts. The standard formats for public
3918*62c56f98SSadaf Ebrahimi  *                                keys are documented in the documentation
3919*62c56f98SSadaf Ebrahimi  *                                of psa_export_public_key().
3920*62c56f98SSadaf Ebrahimi  * \param peer_key_length         Size of \p peer_key in bytes.
3921*62c56f98SSadaf Ebrahimi  * \param[out] output             Buffer where the decrypted message is to
3922*62c56f98SSadaf Ebrahimi  *                                be written.
3923*62c56f98SSadaf Ebrahimi  * \param output_size             Size of the \c output buffer in bytes.
3924*62c56f98SSadaf Ebrahimi  * \param[out] output_length      On success, the number of bytes
3925*62c56f98SSadaf Ebrahimi  *                                that make up the returned output.
3926*62c56f98SSadaf Ebrahimi  *
3927*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
3928*62c56f98SSadaf Ebrahimi  *         Success.
3929*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3930*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3931*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
3932*62c56f98SSadaf Ebrahimi  *         \p alg is not a key agreement algorithm, or
3933*62c56f98SSadaf Ebrahimi  *         \p private_key is not compatible with \p alg,
3934*62c56f98SSadaf Ebrahimi  *         or \p peer_key is not valid for \p alg or not compatible with
3935*62c56f98SSadaf Ebrahimi  *         \p private_key.
3936*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3937*62c56f98SSadaf Ebrahimi  *         \p output_size is too small
3938*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
3939*62c56f98SSadaf Ebrahimi  *         \p alg is not a supported key agreement algorithm.
3940*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3941*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3942*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3943*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3944*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3945*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3946*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3947*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3948*62c56f98SSadaf Ebrahimi  *         results in this error code.
3949*62c56f98SSadaf Ebrahimi  */
3950*62c56f98SSadaf Ebrahimi psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3951*62c56f98SSadaf Ebrahimi                                    mbedtls_svc_key_id_t private_key,
3952*62c56f98SSadaf Ebrahimi                                    const uint8_t *peer_key,
3953*62c56f98SSadaf Ebrahimi                                    size_t peer_key_length,
3954*62c56f98SSadaf Ebrahimi                                    uint8_t *output,
3955*62c56f98SSadaf Ebrahimi                                    size_t output_size,
3956*62c56f98SSadaf Ebrahimi                                    size_t *output_length);
3957*62c56f98SSadaf Ebrahimi 
3958*62c56f98SSadaf Ebrahimi /**@}*/
3959*62c56f98SSadaf Ebrahimi 
3960*62c56f98SSadaf Ebrahimi /** \defgroup random Random generation
3961*62c56f98SSadaf Ebrahimi  * @{
3962*62c56f98SSadaf Ebrahimi  */
3963*62c56f98SSadaf Ebrahimi 
3964*62c56f98SSadaf Ebrahimi /**
3965*62c56f98SSadaf Ebrahimi  * \brief Generate random bytes.
3966*62c56f98SSadaf Ebrahimi  *
3967*62c56f98SSadaf Ebrahimi  * \warning This function **can** fail! Callers MUST check the return status
3968*62c56f98SSadaf Ebrahimi  *          and MUST NOT use the content of the output buffer if the return
3969*62c56f98SSadaf Ebrahimi  *          status is not #PSA_SUCCESS.
3970*62c56f98SSadaf Ebrahimi  *
3971*62c56f98SSadaf Ebrahimi  * \note    To generate a key, use psa_generate_key() instead.
3972*62c56f98SSadaf Ebrahimi  *
3973*62c56f98SSadaf Ebrahimi  * \param[out] output       Output buffer for the generated data.
3974*62c56f98SSadaf Ebrahimi  * \param output_size       Number of bytes to generate and output.
3975*62c56f98SSadaf Ebrahimi  *
3976*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
3977*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3978*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3979*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3980*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3981*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3982*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3983*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
3984*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
3985*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
3986*62c56f98SSadaf Ebrahimi  *         results in this error code.
3987*62c56f98SSadaf Ebrahimi  */
3988*62c56f98SSadaf Ebrahimi psa_status_t psa_generate_random(uint8_t *output,
3989*62c56f98SSadaf Ebrahimi                                  size_t output_size);
3990*62c56f98SSadaf Ebrahimi 
3991*62c56f98SSadaf Ebrahimi /**
3992*62c56f98SSadaf Ebrahimi  * \brief Generate a key or key pair.
3993*62c56f98SSadaf Ebrahimi  *
3994*62c56f98SSadaf Ebrahimi  * The key is generated randomly.
3995*62c56f98SSadaf Ebrahimi  * Its location, usage policy, type and size are taken from \p attributes.
3996*62c56f98SSadaf Ebrahimi  *
3997*62c56f98SSadaf Ebrahimi  * Implementations must reject an attempt to generate a key of size 0.
3998*62c56f98SSadaf Ebrahimi  *
3999*62c56f98SSadaf Ebrahimi  * The following type-specific considerations apply:
4000*62c56f98SSadaf Ebrahimi  * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
4001*62c56f98SSadaf Ebrahimi  *   the public exponent is 65537.
4002*62c56f98SSadaf Ebrahimi  *   The modulus is a product of two probabilistic primes
4003*62c56f98SSadaf Ebrahimi  *   between 2^{n-1} and 2^n where n is the bit size specified in the
4004*62c56f98SSadaf Ebrahimi  *   attributes.
4005*62c56f98SSadaf Ebrahimi  *
4006*62c56f98SSadaf Ebrahimi  * \param[in] attributes    The attributes for the new key.
4007*62c56f98SSadaf Ebrahimi  * \param[out] key          On success, an identifier for the newly created
4008*62c56f98SSadaf Ebrahimi  *                          key. For persistent keys, this is the key
4009*62c56f98SSadaf Ebrahimi  *                          identifier defined in \p attributes.
4010*62c56f98SSadaf Ebrahimi  *                          \c 0 on failure.
4011*62c56f98SSadaf Ebrahimi  *
4012*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4013*62c56f98SSadaf Ebrahimi  *         Success.
4014*62c56f98SSadaf Ebrahimi  *         If the key is persistent, the key material and the key's metadata
4015*62c56f98SSadaf Ebrahimi  *         have been saved to persistent storage.
4016*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_ALREADY_EXISTS
4017*62c56f98SSadaf Ebrahimi  *         This is an attempt to create a persistent key, and there is
4018*62c56f98SSadaf Ebrahimi  *         already a persistent key with the given identifier.
4019*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4020*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4021*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4022*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4023*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4024*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4025*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4026*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4027*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4028*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4029*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4030*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4031*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
4032*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
4033*62c56f98SSadaf Ebrahimi  *         results in this error code.
4034*62c56f98SSadaf Ebrahimi  */
4035*62c56f98SSadaf Ebrahimi psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
4036*62c56f98SSadaf Ebrahimi                               mbedtls_svc_key_id_t *key);
4037*62c56f98SSadaf Ebrahimi 
4038*62c56f98SSadaf Ebrahimi /**@}*/
4039*62c56f98SSadaf Ebrahimi 
4040*62c56f98SSadaf Ebrahimi /** \defgroup interruptible_hash Interruptible sign/verify hash
4041*62c56f98SSadaf Ebrahimi  * @{
4042*62c56f98SSadaf Ebrahimi  */
4043*62c56f98SSadaf Ebrahimi 
4044*62c56f98SSadaf Ebrahimi /** The type of the state data structure for interruptible hash
4045*62c56f98SSadaf Ebrahimi  *  signing operations.
4046*62c56f98SSadaf Ebrahimi  *
4047*62c56f98SSadaf Ebrahimi  * Before calling any function on a sign hash operation object, the
4048*62c56f98SSadaf Ebrahimi  * application must initialize it by any of the following means:
4049*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
4050*62c56f98SSadaf Ebrahimi  *   \code
4051*62c56f98SSadaf Ebrahimi  *   psa_sign_hash_interruptible_operation_t operation;
4052*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
4053*62c56f98SSadaf Ebrahimi  *   \endcode
4054*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
4055*62c56f98SSadaf Ebrahimi  *   \code
4056*62c56f98SSadaf Ebrahimi  *   psa_sign_hash_interruptible_operation_t operation = {0};
4057*62c56f98SSadaf Ebrahimi  *   \endcode
4058*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer
4059*62c56f98SSadaf Ebrahimi  *   #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4060*62c56f98SSadaf Ebrahimi  *   \code
4061*62c56f98SSadaf Ebrahimi  *   psa_sign_hash_interruptible_operation_t operation =
4062*62c56f98SSadaf Ebrahimi  *   PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4063*62c56f98SSadaf Ebrahimi  *   \endcode
4064*62c56f98SSadaf Ebrahimi  * - Assign the result of the function
4065*62c56f98SSadaf Ebrahimi  *   psa_sign_hash_interruptible_operation_init() to the structure, for
4066*62c56f98SSadaf Ebrahimi  *   example:
4067*62c56f98SSadaf Ebrahimi  *   \code
4068*62c56f98SSadaf Ebrahimi  *   psa_sign_hash_interruptible_operation_t operation;
4069*62c56f98SSadaf Ebrahimi  *   operation = psa_sign_hash_interruptible_operation_init();
4070*62c56f98SSadaf Ebrahimi  *   \endcode
4071*62c56f98SSadaf Ebrahimi  *
4072*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
4073*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
4074*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
4075*62c56f98SSadaf Ebrahimi typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4076*62c56f98SSadaf Ebrahimi 
4077*62c56f98SSadaf Ebrahimi /** The type of the state data structure for interruptible hash
4078*62c56f98SSadaf Ebrahimi  *  verification operations.
4079*62c56f98SSadaf Ebrahimi  *
4080*62c56f98SSadaf Ebrahimi  * Before calling any function on a sign hash operation object, the
4081*62c56f98SSadaf Ebrahimi  * application must initialize it by any of the following means:
4082*62c56f98SSadaf Ebrahimi  * - Set the structure to all-bits-zero, for example:
4083*62c56f98SSadaf Ebrahimi  *   \code
4084*62c56f98SSadaf Ebrahimi  *   psa_verify_hash_interruptible_operation_t operation;
4085*62c56f98SSadaf Ebrahimi  *   memset(&operation, 0, sizeof(operation));
4086*62c56f98SSadaf Ebrahimi  *   \endcode
4087*62c56f98SSadaf Ebrahimi  * - Initialize the structure to logical zero values, for example:
4088*62c56f98SSadaf Ebrahimi  *   \code
4089*62c56f98SSadaf Ebrahimi  *   psa_verify_hash_interruptible_operation_t operation = {0};
4090*62c56f98SSadaf Ebrahimi  *   \endcode
4091*62c56f98SSadaf Ebrahimi  * - Initialize the structure to the initializer
4092*62c56f98SSadaf Ebrahimi  *   #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4093*62c56f98SSadaf Ebrahimi  *   \code
4094*62c56f98SSadaf Ebrahimi  *   psa_verify_hash_interruptible_operation_t operation =
4095*62c56f98SSadaf Ebrahimi  *   PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4096*62c56f98SSadaf Ebrahimi  *   \endcode
4097*62c56f98SSadaf Ebrahimi  * - Assign the result of the function
4098*62c56f98SSadaf Ebrahimi  *   psa_verify_hash_interruptible_operation_init() to the structure, for
4099*62c56f98SSadaf Ebrahimi  *   example:
4100*62c56f98SSadaf Ebrahimi  *   \code
4101*62c56f98SSadaf Ebrahimi  *   psa_verify_hash_interruptible_operation_t operation;
4102*62c56f98SSadaf Ebrahimi  *   operation = psa_verify_hash_interruptible_operation_init();
4103*62c56f98SSadaf Ebrahimi  *   \endcode
4104*62c56f98SSadaf Ebrahimi  *
4105*62c56f98SSadaf Ebrahimi  * This is an implementation-defined \c struct. Applications should not
4106*62c56f98SSadaf Ebrahimi  * make any assumptions about the content of this structure.
4107*62c56f98SSadaf Ebrahimi  * Implementation details can change in future versions without notice. */
4108*62c56f98SSadaf Ebrahimi typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4109*62c56f98SSadaf Ebrahimi 
4110*62c56f98SSadaf Ebrahimi /**
4111*62c56f98SSadaf Ebrahimi  * \brief                       Set the maximum number of ops allowed to be
4112*62c56f98SSadaf Ebrahimi  *                              executed by an interruptible function in a
4113*62c56f98SSadaf Ebrahimi  *                              single call.
4114*62c56f98SSadaf Ebrahimi  *
4115*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4116*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4117*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4118*62c56f98SSadaf Ebrahimi  *
4119*62c56f98SSadaf Ebrahimi  * \note                        The time taken to execute a single op is
4120*62c56f98SSadaf Ebrahimi  *                              implementation specific and depends on
4121*62c56f98SSadaf Ebrahimi  *                              software, hardware, the algorithm, key type and
4122*62c56f98SSadaf Ebrahimi  *                              curve chosen. Even within a single operation,
4123*62c56f98SSadaf Ebrahimi  *                              successive ops can take differing amounts of
4124*62c56f98SSadaf Ebrahimi  *                              time. The only guarantee is that lower values
4125*62c56f98SSadaf Ebrahimi  *                              for \p max_ops means functions will block for a
4126*62c56f98SSadaf Ebrahimi  *                              lesser maximum amount of time. The functions
4127*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_interruptible_get_num_ops() and
4128*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_interruptible_get_num_ops() are
4129*62c56f98SSadaf Ebrahimi  *                              provided to help with tuning this value.
4130*62c56f98SSadaf Ebrahimi  *
4131*62c56f98SSadaf Ebrahimi  * \note                        This value defaults to
4132*62c56f98SSadaf Ebrahimi  *                              #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4133*62c56f98SSadaf Ebrahimi  *                              means the whole operation will be done in one
4134*62c56f98SSadaf Ebrahimi  *                              go, regardless of the number of ops required.
4135*62c56f98SSadaf Ebrahimi  *
4136*62c56f98SSadaf Ebrahimi  * \note                        If more ops are needed to complete a
4137*62c56f98SSadaf Ebrahimi  *                              computation, #PSA_OPERATION_INCOMPLETE will be
4138*62c56f98SSadaf Ebrahimi  *                              returned by the function performing the
4139*62c56f98SSadaf Ebrahimi  *                              computation. It is then the caller's
4140*62c56f98SSadaf Ebrahimi  *                              responsibility to either call again with the
4141*62c56f98SSadaf Ebrahimi  *                              same operation context until it returns 0 or an
4142*62c56f98SSadaf Ebrahimi  *                              error code; or to call the relevant abort
4143*62c56f98SSadaf Ebrahimi  *                              function if the answer is no longer required.
4144*62c56f98SSadaf Ebrahimi  *
4145*62c56f98SSadaf Ebrahimi  * \note                        The interpretation of \p max_ops is also
4146*62c56f98SSadaf Ebrahimi  *                              implementation defined. On a hard real time
4147*62c56f98SSadaf Ebrahimi  *                              system, this can indicate a hard deadline, as a
4148*62c56f98SSadaf Ebrahimi  *                              real-time system needs a guarantee of not
4149*62c56f98SSadaf Ebrahimi  *                              spending more than X time, however care must be
4150*62c56f98SSadaf Ebrahimi  *                              taken in such an implementation to avoid the
4151*62c56f98SSadaf Ebrahimi  *                              situation whereby calls just return, not being
4152*62c56f98SSadaf Ebrahimi  *                              able to do any actual work within the allotted
4153*62c56f98SSadaf Ebrahimi  *                              time.  On a non-real-time system, the
4154*62c56f98SSadaf Ebrahimi  *                              implementation can be more relaxed, but again
4155*62c56f98SSadaf Ebrahimi  *                              whether this number should be interpreted as as
4156*62c56f98SSadaf Ebrahimi  *                              hard or soft limit or even whether a less than
4157*62c56f98SSadaf Ebrahimi  *                              or equals as regards to ops executed in a
4158*62c56f98SSadaf Ebrahimi  *                              single call is implementation defined.
4159*62c56f98SSadaf Ebrahimi  *
4160*62c56f98SSadaf Ebrahimi  * \note                        For keys in local storage when no accelerator
4161*62c56f98SSadaf Ebrahimi  *                              driver applies, please see also the
4162*62c56f98SSadaf Ebrahimi  *                              documentation for \c mbedtls_ecp_set_max_ops(),
4163*62c56f98SSadaf Ebrahimi  *                              which is the internal implementation in these
4164*62c56f98SSadaf Ebrahimi  *                              cases.
4165*62c56f98SSadaf Ebrahimi  *
4166*62c56f98SSadaf Ebrahimi  * \warning                     With implementations that interpret this number
4167*62c56f98SSadaf Ebrahimi  *                              as a hard limit, setting this number too small
4168*62c56f98SSadaf Ebrahimi  *                              may result in an infinite loop, whereby each
4169*62c56f98SSadaf Ebrahimi  *                              call results in immediate return with no ops
4170*62c56f98SSadaf Ebrahimi  *                              done (as there is not enough time to execute
4171*62c56f98SSadaf Ebrahimi  *                              any), and thus no result will ever be achieved.
4172*62c56f98SSadaf Ebrahimi  *
4173*62c56f98SSadaf Ebrahimi  * \note                        This only applies to functions whose
4174*62c56f98SSadaf Ebrahimi  *                              documentation mentions they may return
4175*62c56f98SSadaf Ebrahimi  *                              #PSA_OPERATION_INCOMPLETE.
4176*62c56f98SSadaf Ebrahimi  *
4177*62c56f98SSadaf Ebrahimi  * \param max_ops               The maximum number of ops to be executed in a
4178*62c56f98SSadaf Ebrahimi  *                              single call. This can be a number from 0 to
4179*62c56f98SSadaf Ebrahimi  *                              #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4180*62c56f98SSadaf Ebrahimi  *                              is the least amount of work done per call.
4181*62c56f98SSadaf Ebrahimi  */
4182*62c56f98SSadaf Ebrahimi void psa_interruptible_set_max_ops(uint32_t max_ops);
4183*62c56f98SSadaf Ebrahimi 
4184*62c56f98SSadaf Ebrahimi /**
4185*62c56f98SSadaf Ebrahimi  * \brief                       Get the maximum number of ops allowed to be
4186*62c56f98SSadaf Ebrahimi  *                              executed by an interruptible function in a
4187*62c56f98SSadaf Ebrahimi  *                              single call. This will return the last
4188*62c56f98SSadaf Ebrahimi  *                              value set by
4189*62c56f98SSadaf Ebrahimi  *                              \c psa_interruptible_set_max_ops() or
4190*62c56f98SSadaf Ebrahimi  *                              #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4191*62c56f98SSadaf Ebrahimi  *                              that function has never been called.
4192*62c56f98SSadaf Ebrahimi  *
4193*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4194*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4195*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4196*62c56f98SSadaf Ebrahimi  *
4197*62c56f98SSadaf Ebrahimi  * \return                      Maximum number of ops allowed to be
4198*62c56f98SSadaf Ebrahimi  *                              executed by an interruptible function in a
4199*62c56f98SSadaf Ebrahimi  *                              single call.
4200*62c56f98SSadaf Ebrahimi  */
4201*62c56f98SSadaf Ebrahimi uint32_t psa_interruptible_get_max_ops(void);
4202*62c56f98SSadaf Ebrahimi 
4203*62c56f98SSadaf Ebrahimi /**
4204*62c56f98SSadaf Ebrahimi  * \brief                       Get the number of ops that a hash signing
4205*62c56f98SSadaf Ebrahimi  *                              operation has taken so far. If the operation
4206*62c56f98SSadaf Ebrahimi  *                              has completed, then this will represent the
4207*62c56f98SSadaf Ebrahimi  *                              number of ops required for the entire
4208*62c56f98SSadaf Ebrahimi  *                              operation. After initialization or calling
4209*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_interruptible_abort() on
4210*62c56f98SSadaf Ebrahimi  *                              the operation, a value of 0 will be returned.
4211*62c56f98SSadaf Ebrahimi  *
4212*62c56f98SSadaf Ebrahimi  * \note                        This interface is guaranteed re-entrant and
4213*62c56f98SSadaf Ebrahimi  *                              thus may be called from driver code.
4214*62c56f98SSadaf Ebrahimi  *
4215*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4216*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4217*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4218*62c56f98SSadaf Ebrahimi  *
4219*62c56f98SSadaf Ebrahimi  *                              This is a helper provided to help you tune the
4220*62c56f98SSadaf Ebrahimi  *                              value passed to \c
4221*62c56f98SSadaf Ebrahimi  *                              psa_interruptible_set_max_ops().
4222*62c56f98SSadaf Ebrahimi  *
4223*62c56f98SSadaf Ebrahimi  * \param operation             The \c psa_sign_hash_interruptible_operation_t
4224*62c56f98SSadaf Ebrahimi  *                              to use. This must be initialized first.
4225*62c56f98SSadaf Ebrahimi  *
4226*62c56f98SSadaf Ebrahimi  * \return                      Number of ops that the operation has taken so
4227*62c56f98SSadaf Ebrahimi  *                              far.
4228*62c56f98SSadaf Ebrahimi  */
4229*62c56f98SSadaf Ebrahimi uint32_t psa_sign_hash_get_num_ops(
4230*62c56f98SSadaf Ebrahimi     const psa_sign_hash_interruptible_operation_t *operation);
4231*62c56f98SSadaf Ebrahimi 
4232*62c56f98SSadaf Ebrahimi /**
4233*62c56f98SSadaf Ebrahimi  * \brief                       Get the number of ops that a hash verification
4234*62c56f98SSadaf Ebrahimi  *                              operation has taken so far. If the operation
4235*62c56f98SSadaf Ebrahimi  *                              has completed, then this will represent the
4236*62c56f98SSadaf Ebrahimi  *                              number of ops required for the entire
4237*62c56f98SSadaf Ebrahimi  *                              operation. After initialization or calling \c
4238*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_interruptible_abort() on the
4239*62c56f98SSadaf Ebrahimi  *                              operation, a value of 0 will be returned.
4240*62c56f98SSadaf Ebrahimi  *
4241*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4242*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4243*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4244*62c56f98SSadaf Ebrahimi  *
4245*62c56f98SSadaf Ebrahimi  *                              This is a helper provided to help you tune the
4246*62c56f98SSadaf Ebrahimi  *                              value passed to \c
4247*62c56f98SSadaf Ebrahimi  *                              psa_interruptible_set_max_ops().
4248*62c56f98SSadaf Ebrahimi  *
4249*62c56f98SSadaf Ebrahimi  * \param operation             The \c
4250*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_interruptible_operation_t to
4251*62c56f98SSadaf Ebrahimi  *                              use. This must be initialized first.
4252*62c56f98SSadaf Ebrahimi  *
4253*62c56f98SSadaf Ebrahimi  * \return                      Number of ops that the operation has taken so
4254*62c56f98SSadaf Ebrahimi  *                              far.
4255*62c56f98SSadaf Ebrahimi  */
4256*62c56f98SSadaf Ebrahimi uint32_t psa_verify_hash_get_num_ops(
4257*62c56f98SSadaf Ebrahimi     const psa_verify_hash_interruptible_operation_t *operation);
4258*62c56f98SSadaf Ebrahimi 
4259*62c56f98SSadaf Ebrahimi /**
4260*62c56f98SSadaf Ebrahimi  * \brief                       Start signing a hash or short message with a
4261*62c56f98SSadaf Ebrahimi  *                              private key, in an interruptible manner.
4262*62c56f98SSadaf Ebrahimi  *
4263*62c56f98SSadaf Ebrahimi  * \see                         \c psa_sign_hash_complete()
4264*62c56f98SSadaf Ebrahimi  *
4265*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4266*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4267*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4268*62c56f98SSadaf Ebrahimi  *
4269*62c56f98SSadaf Ebrahimi  * \note                        This function combined with \c
4270*62c56f98SSadaf Ebrahimi  *                              psa_sign_hash_complete() is equivalent to
4271*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash() but
4272*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_complete() can return early and
4273*62c56f98SSadaf Ebrahimi  *                              resume according to the limit set with \c
4274*62c56f98SSadaf Ebrahimi  *                              psa_interruptible_set_max_ops() to reduce the
4275*62c56f98SSadaf Ebrahimi  *                              maximum time spent in a function call.
4276*62c56f98SSadaf Ebrahimi  *
4277*62c56f98SSadaf Ebrahimi  * \note                        Users should call \c psa_sign_hash_complete()
4278*62c56f98SSadaf Ebrahimi  *                              repeatedly on the same context after a
4279*62c56f98SSadaf Ebrahimi  *                              successful call to this function until \c
4280*62c56f98SSadaf Ebrahimi  *                              psa_sign_hash_complete() either returns 0 or an
4281*62c56f98SSadaf Ebrahimi  *                              error. \c psa_sign_hash_complete() will return
4282*62c56f98SSadaf Ebrahimi  *                              #PSA_OPERATION_INCOMPLETE if there is more work
4283*62c56f98SSadaf Ebrahimi  *                              to do. Alternatively users can call
4284*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_abort() at any point if they no
4285*62c56f98SSadaf Ebrahimi  *                              longer want the result.
4286*62c56f98SSadaf Ebrahimi  *
4287*62c56f98SSadaf Ebrahimi  * \note                        If this function returns an error status, the
4288*62c56f98SSadaf Ebrahimi  *                              operation enters an error state and must be
4289*62c56f98SSadaf Ebrahimi  *                              aborted by calling \c psa_sign_hash_abort().
4290*62c56f98SSadaf Ebrahimi  *
4291*62c56f98SSadaf Ebrahimi  * \param[in, out] operation    The \c psa_sign_hash_interruptible_operation_t
4292*62c56f98SSadaf Ebrahimi  *                              to use. This must be initialized first.
4293*62c56f98SSadaf Ebrahimi  *
4294*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
4295*62c56f98SSadaf Ebrahimi  *                              It must be an asymmetric key pair. The key must
4296*62c56f98SSadaf Ebrahimi  *                              allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4297*62c56f98SSadaf Ebrahimi  * \param alg                   A signature algorithm (\c PSA_ALG_XXX
4298*62c56f98SSadaf Ebrahimi  *                              value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4299*62c56f98SSadaf Ebrahimi  *                              is true), that is compatible with
4300*62c56f98SSadaf Ebrahimi  *                              the type of \p key.
4301*62c56f98SSadaf Ebrahimi  * \param[in] hash              The hash or message to sign.
4302*62c56f98SSadaf Ebrahimi  * \param hash_length           Size of the \p hash buffer in bytes.
4303*62c56f98SSadaf Ebrahimi  *
4304*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4305*62c56f98SSadaf Ebrahimi  *         The operation started successfully - call \c psa_sign_hash_complete()
4306*62c56f98SSadaf Ebrahimi  *         with the same context to complete the operation
4307*62c56f98SSadaf Ebrahimi  *
4308*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4309*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
4310*62c56f98SSadaf Ebrahimi  *         The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4311*62c56f98SSadaf Ebrahimi  *         not permit the requested algorithm.
4312*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4313*62c56f98SSadaf Ebrahimi  *         An operation has previously been started on this context, and is
4314*62c56f98SSadaf Ebrahimi  *         still in progress.
4315*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4316*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4317*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4318*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4319*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4320*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4321*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4322*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4323*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4324*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4325*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4326*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
4327*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
4328*62c56f98SSadaf Ebrahimi  *         results in this error code.
4329*62c56f98SSadaf Ebrahimi  */
4330*62c56f98SSadaf Ebrahimi psa_status_t psa_sign_hash_start(
4331*62c56f98SSadaf Ebrahimi     psa_sign_hash_interruptible_operation_t *operation,
4332*62c56f98SSadaf Ebrahimi     mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4333*62c56f98SSadaf Ebrahimi     const uint8_t *hash, size_t hash_length);
4334*62c56f98SSadaf Ebrahimi 
4335*62c56f98SSadaf Ebrahimi /**
4336*62c56f98SSadaf Ebrahimi  * \brief                       Continue and eventually complete the action of
4337*62c56f98SSadaf Ebrahimi  *                              signing a hash or short message with a private
4338*62c56f98SSadaf Ebrahimi  *                              key, in an interruptible manner.
4339*62c56f98SSadaf Ebrahimi  *
4340*62c56f98SSadaf Ebrahimi  * \see                         \c psa_sign_hash_start()
4341*62c56f98SSadaf Ebrahimi  *
4342*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4343*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4344*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4345*62c56f98SSadaf Ebrahimi  *
4346*62c56f98SSadaf Ebrahimi  * \note                        This function combined with \c
4347*62c56f98SSadaf Ebrahimi  *                              psa_sign_hash_start() is equivalent to
4348*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash() but this function can return
4349*62c56f98SSadaf Ebrahimi  *                              early and resume according to the limit set with
4350*62c56f98SSadaf Ebrahimi  *                              \c psa_interruptible_set_max_ops() to reduce the
4351*62c56f98SSadaf Ebrahimi  *                              maximum time spent in a function call.
4352*62c56f98SSadaf Ebrahimi  *
4353*62c56f98SSadaf Ebrahimi  * \note                        Users should call this function on the same
4354*62c56f98SSadaf Ebrahimi  *                              operation object repeatedly until it either
4355*62c56f98SSadaf Ebrahimi  *                              returns 0 or an error. This function will return
4356*62c56f98SSadaf Ebrahimi  *                              #PSA_OPERATION_INCOMPLETE if there is more work
4357*62c56f98SSadaf Ebrahimi  *                              to do. Alternatively users can call
4358*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_abort() at any point if they no
4359*62c56f98SSadaf Ebrahimi  *                              longer want the result.
4360*62c56f98SSadaf Ebrahimi  *
4361*62c56f98SSadaf Ebrahimi  * \note                        When this function returns successfully, the
4362*62c56f98SSadaf Ebrahimi  *                              operation becomes inactive. If this function
4363*62c56f98SSadaf Ebrahimi  *                              returns an error status, the operation enters an
4364*62c56f98SSadaf Ebrahimi  *                              error state and must be aborted by calling
4365*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_abort().
4366*62c56f98SSadaf Ebrahimi  *
4367*62c56f98SSadaf Ebrahimi  * \param[in, out] operation    The \c psa_sign_hash_interruptible_operation_t
4368*62c56f98SSadaf Ebrahimi  *                              to use. This must be initialized first, and have
4369*62c56f98SSadaf Ebrahimi  *                              had \c psa_sign_hash_start() called with it
4370*62c56f98SSadaf Ebrahimi  *                              first.
4371*62c56f98SSadaf Ebrahimi  *
4372*62c56f98SSadaf Ebrahimi  * \param[out] signature        Buffer where the signature is to be written.
4373*62c56f98SSadaf Ebrahimi  * \param signature_size        Size of the \p signature buffer in bytes. This
4374*62c56f98SSadaf Ebrahimi  *                              must be appropriate for the selected
4375*62c56f98SSadaf Ebrahimi  *                              algorithm and key:
4376*62c56f98SSadaf Ebrahimi  *                              - The required signature size is
4377*62c56f98SSadaf Ebrahimi  *                                #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4378*62c56f98SSadaf Ebrahimi  *                                key_bits, \c alg) where \c key_type and \c
4379*62c56f98SSadaf Ebrahimi  *                                key_bits are the type and bit-size
4380*62c56f98SSadaf Ebrahimi  *                                respectively of key.
4381*62c56f98SSadaf Ebrahimi  *                              - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4382*62c56f98SSadaf Ebrahimi  *                                maximum signature size of any supported
4383*62c56f98SSadaf Ebrahimi  *                                signature algorithm.
4384*62c56f98SSadaf Ebrahimi  * \param[out] signature_length On success, the number of bytes that make up
4385*62c56f98SSadaf Ebrahimi  *                              the returned signature value.
4386*62c56f98SSadaf Ebrahimi  *
4387*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4388*62c56f98SSadaf Ebrahimi  *         Operation completed successfully
4389*62c56f98SSadaf Ebrahimi  *
4390*62c56f98SSadaf Ebrahimi  * \retval #PSA_OPERATION_INCOMPLETE
4391*62c56f98SSadaf Ebrahimi  *         Operation was interrupted due to the setting of \c
4392*62c56f98SSadaf Ebrahimi  *         psa_interruptible_set_max_ops(). There is still work to be done.
4393*62c56f98SSadaf Ebrahimi  *         Call this function again with the same operation object.
4394*62c56f98SSadaf Ebrahimi  *
4395*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4396*62c56f98SSadaf Ebrahimi  *         The size of the \p signature buffer is too small. You can
4397*62c56f98SSadaf Ebrahimi  *         determine a sufficient buffer size by calling
4398*62c56f98SSadaf Ebrahimi  *         #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
4399*62c56f98SSadaf Ebrahimi  *         where \c key_type and \c key_bits are the type and bit-size
4400*62c56f98SSadaf Ebrahimi  *         respectively of \c key.
4401*62c56f98SSadaf Ebrahimi  *
4402*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4403*62c56f98SSadaf Ebrahimi  *         An operation was not previously started on this context via
4404*62c56f98SSadaf Ebrahimi  *         \c psa_sign_hash_start().
4405*62c56f98SSadaf Ebrahimi  *
4406*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4407*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4408*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4409*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4410*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4411*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4412*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4413*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4414*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4415*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4416*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4417*62c56f98SSadaf Ebrahimi  *         The library has either not been previously initialized by
4418*62c56f98SSadaf Ebrahimi  *         psa_crypto_init() or you did not previously call
4419*62c56f98SSadaf Ebrahimi  *         psa_sign_hash_start() with this operation object. It is
4420*62c56f98SSadaf Ebrahimi  *         implementation-dependent whether a failure to initialize results in
4421*62c56f98SSadaf Ebrahimi  *         this error code.
4422*62c56f98SSadaf Ebrahimi  */
4423*62c56f98SSadaf Ebrahimi psa_status_t psa_sign_hash_complete(
4424*62c56f98SSadaf Ebrahimi     psa_sign_hash_interruptible_operation_t *operation,
4425*62c56f98SSadaf Ebrahimi     uint8_t *signature, size_t signature_size,
4426*62c56f98SSadaf Ebrahimi     size_t *signature_length);
4427*62c56f98SSadaf Ebrahimi 
4428*62c56f98SSadaf Ebrahimi /**
4429*62c56f98SSadaf Ebrahimi  * \brief                       Abort a sign hash operation.
4430*62c56f98SSadaf Ebrahimi  *
4431*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4432*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4433*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4434*62c56f98SSadaf Ebrahimi  *
4435*62c56f98SSadaf Ebrahimi  * \note                        This function is the only function that clears
4436*62c56f98SSadaf Ebrahimi  *                              the number of ops completed as part of the
4437*62c56f98SSadaf Ebrahimi  *                              operation. Please ensure you copy this value via
4438*62c56f98SSadaf Ebrahimi  *                              \c psa_sign_hash_get_num_ops() if required
4439*62c56f98SSadaf Ebrahimi  *                              before calling.
4440*62c56f98SSadaf Ebrahimi  *
4441*62c56f98SSadaf Ebrahimi  * \note                        Aborting an operation frees all associated
4442*62c56f98SSadaf Ebrahimi  *                              resources except for the \p operation structure
4443*62c56f98SSadaf Ebrahimi  *                              itself. Once aborted, the operation object can
4444*62c56f98SSadaf Ebrahimi  *                              be reused for another operation by calling \c
4445*62c56f98SSadaf Ebrahimi  *                              psa_sign_hash_start() again.
4446*62c56f98SSadaf Ebrahimi  *
4447*62c56f98SSadaf Ebrahimi  * \note                        You may call this function any time after the
4448*62c56f98SSadaf Ebrahimi  *                              operation object has been initialized. In
4449*62c56f98SSadaf Ebrahimi  *                              particular, calling \c psa_sign_hash_abort()
4450*62c56f98SSadaf Ebrahimi  *                              after the operation has already been terminated
4451*62c56f98SSadaf Ebrahimi  *                              by a call to \c psa_sign_hash_abort() or
4452*62c56f98SSadaf Ebrahimi  *                              psa_sign_hash_complete() is safe.
4453*62c56f98SSadaf Ebrahimi  *
4454*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     Initialized sign hash operation.
4455*62c56f98SSadaf Ebrahimi  *
4456*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4457*62c56f98SSadaf Ebrahimi  *         The operation was aborted successfully.
4458*62c56f98SSadaf Ebrahimi  *
4459*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4460*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4461*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
4462*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
4463*62c56f98SSadaf Ebrahimi  *         results in this error code.
4464*62c56f98SSadaf Ebrahimi  */
4465*62c56f98SSadaf Ebrahimi psa_status_t psa_sign_hash_abort(
4466*62c56f98SSadaf Ebrahimi     psa_sign_hash_interruptible_operation_t *operation);
4467*62c56f98SSadaf Ebrahimi 
4468*62c56f98SSadaf Ebrahimi /**
4469*62c56f98SSadaf Ebrahimi  * \brief                       Start reading and verifying a hash or short
4470*62c56f98SSadaf Ebrahimi  *                              message, in an interruptible manner.
4471*62c56f98SSadaf Ebrahimi  *
4472*62c56f98SSadaf Ebrahimi  * \see                         \c psa_verify_hash_complete()
4473*62c56f98SSadaf Ebrahimi  *
4474*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4475*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4476*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4477*62c56f98SSadaf Ebrahimi  *
4478*62c56f98SSadaf Ebrahimi  * \note                        This function combined with \c
4479*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_complete() is equivalent to
4480*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_hash() but \c
4481*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_complete() can return early and
4482*62c56f98SSadaf Ebrahimi  *                              resume according to the limit set with \c
4483*62c56f98SSadaf Ebrahimi  *                              psa_interruptible_set_max_ops() to reduce the
4484*62c56f98SSadaf Ebrahimi  *                              maximum time spent in a function.
4485*62c56f98SSadaf Ebrahimi  *
4486*62c56f98SSadaf Ebrahimi  * \note                        Users should call \c psa_verify_hash_complete()
4487*62c56f98SSadaf Ebrahimi  *                              repeatedly on the same operation object after a
4488*62c56f98SSadaf Ebrahimi  *                              successful call to this function until \c
4489*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_complete() either returns 0 or
4490*62c56f98SSadaf Ebrahimi  *                              an error. \c psa_verify_hash_complete() will
4491*62c56f98SSadaf Ebrahimi  *                              return #PSA_OPERATION_INCOMPLETE if there is
4492*62c56f98SSadaf Ebrahimi  *                              more work to do. Alternatively users can call
4493*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_hash_abort() at any point if they
4494*62c56f98SSadaf Ebrahimi  *                              no longer want the result.
4495*62c56f98SSadaf Ebrahimi  *
4496*62c56f98SSadaf Ebrahimi  * \note                        If this function returns an error status, the
4497*62c56f98SSadaf Ebrahimi  *                              operation enters an error state and must be
4498*62c56f98SSadaf Ebrahimi  *                              aborted by calling \c psa_verify_hash_abort().
4499*62c56f98SSadaf Ebrahimi  *
4500*62c56f98SSadaf Ebrahimi  * \param[in, out] operation    The \c psa_verify_hash_interruptible_operation_t
4501*62c56f98SSadaf Ebrahimi  *                              to use. This must be initialized first.
4502*62c56f98SSadaf Ebrahimi  *
4503*62c56f98SSadaf Ebrahimi  * \param key                   Identifier of the key to use for the operation.
4504*62c56f98SSadaf Ebrahimi  *                              The key must allow the usage
4505*62c56f98SSadaf Ebrahimi  *                              #PSA_KEY_USAGE_VERIFY_HASH.
4506*62c56f98SSadaf Ebrahimi  * \param alg                   A signature algorithm (\c PSA_ALG_XXX
4507*62c56f98SSadaf Ebrahimi  *                              value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4508*62c56f98SSadaf Ebrahimi  *                              is true), that is compatible with
4509*62c56f98SSadaf Ebrahimi  *                              the type of \p key.
4510*62c56f98SSadaf Ebrahimi  * \param[in] hash              The hash whose signature is to be verified.
4511*62c56f98SSadaf Ebrahimi  * \param hash_length           Size of the \p hash buffer in bytes.
4512*62c56f98SSadaf Ebrahimi  * \param[in] signature         Buffer containing the signature to verify.
4513*62c56f98SSadaf Ebrahimi  * \param signature_length      Size of the \p signature buffer in bytes.
4514*62c56f98SSadaf Ebrahimi  *
4515*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4516*62c56f98SSadaf Ebrahimi  *         The operation started successfully - please call \c
4517*62c56f98SSadaf Ebrahimi  *         psa_verify_hash_complete() with the same context to complete the
4518*62c56f98SSadaf Ebrahimi  *         operation.
4519*62c56f98SSadaf Ebrahimi  *
4520*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4521*62c56f98SSadaf Ebrahimi  *         Another operation has already been started on this context, and is
4522*62c56f98SSadaf Ebrahimi  *         still in progress.
4523*62c56f98SSadaf Ebrahimi  *
4524*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
4525*62c56f98SSadaf Ebrahimi  *         The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4526*62c56f98SSadaf Ebrahimi  *         not permit the requested algorithm.
4527*62c56f98SSadaf Ebrahimi  *
4528*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4529*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4530*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4531*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4532*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4533*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4534*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4535*62c56f98SSadaf Ebrahimi  * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4536*62c56f98SSadaf Ebrahimi  * \retval PSA_ERROR_DATA_INVALID \emptydescription
4537*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4538*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
4539*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
4540*62c56f98SSadaf Ebrahimi  *         results in this error code.
4541*62c56f98SSadaf Ebrahimi  */
4542*62c56f98SSadaf Ebrahimi psa_status_t psa_verify_hash_start(
4543*62c56f98SSadaf Ebrahimi     psa_verify_hash_interruptible_operation_t *operation,
4544*62c56f98SSadaf Ebrahimi     mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4545*62c56f98SSadaf Ebrahimi     const uint8_t *hash, size_t hash_length,
4546*62c56f98SSadaf Ebrahimi     const uint8_t *signature, size_t signature_length);
4547*62c56f98SSadaf Ebrahimi 
4548*62c56f98SSadaf Ebrahimi /**
4549*62c56f98SSadaf Ebrahimi  * \brief                       Continue and eventually complete the action of
4550*62c56f98SSadaf Ebrahimi  *                              reading and verifying a hash or short message
4551*62c56f98SSadaf Ebrahimi  *                              signed with a private key, in an interruptible
4552*62c56f98SSadaf Ebrahimi  *                              manner.
4553*62c56f98SSadaf Ebrahimi  *
4554*62c56f98SSadaf Ebrahimi  * \see                         \c psa_verify_hash_start()
4555*62c56f98SSadaf Ebrahimi  *
4556*62c56f98SSadaf Ebrahimi  * \warning                     This is a beta API, and thus subject to change
4557*62c56f98SSadaf Ebrahimi  *                              at any point. It is not bound by the usual
4558*62c56f98SSadaf Ebrahimi  *                              interface stability promises.
4559*62c56f98SSadaf Ebrahimi  *
4560*62c56f98SSadaf Ebrahimi  * \note                        This function combined with \c
4561*62c56f98SSadaf Ebrahimi  *                              psa_verify_hash_start() is equivalent to
4562*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_hash() but this function can
4563*62c56f98SSadaf Ebrahimi  *                              return early and resume according to the limit
4564*62c56f98SSadaf Ebrahimi  *                              set with \c psa_interruptible_set_max_ops() to
4565*62c56f98SSadaf Ebrahimi  *                              reduce the maximum time spent in a function
4566*62c56f98SSadaf Ebrahimi  *                              call.
4567*62c56f98SSadaf Ebrahimi  *
4568*62c56f98SSadaf Ebrahimi  * \note                        Users should call this function on the same
4569*62c56f98SSadaf Ebrahimi  *                              operation object repeatedly until it either
4570*62c56f98SSadaf Ebrahimi  *                              returns 0 or an error. This function will return
4571*62c56f98SSadaf Ebrahimi  *                              #PSA_OPERATION_INCOMPLETE if there is more work
4572*62c56f98SSadaf Ebrahimi  *                              to do. Alternatively users can call
4573*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_hash_abort() at any point if they
4574*62c56f98SSadaf Ebrahimi  *                              no longer want the result.
4575*62c56f98SSadaf Ebrahimi  *
4576*62c56f98SSadaf Ebrahimi  * \note                        When this function returns successfully, the
4577*62c56f98SSadaf Ebrahimi  *                              operation becomes inactive. If this function
4578*62c56f98SSadaf Ebrahimi  *                              returns an error status, the operation enters an
4579*62c56f98SSadaf Ebrahimi  *                              error state and must be aborted by calling
4580*62c56f98SSadaf Ebrahimi  *                              \c psa_verify_hash_abort().
4581*62c56f98SSadaf Ebrahimi  *
4582*62c56f98SSadaf Ebrahimi  * \param[in, out] operation    The \c psa_verify_hash_interruptible_operation_t
4583*62c56f98SSadaf Ebrahimi  *                              to use. This must be initialized first, and have
4584*62c56f98SSadaf Ebrahimi  *                              had \c psa_verify_hash_start() called with it
4585*62c56f98SSadaf Ebrahimi  *                              first.
4586*62c56f98SSadaf Ebrahimi  *
4587*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4588*62c56f98SSadaf Ebrahimi  *         Operation completed successfully, and the passed signature is valid.
4589*62c56f98SSadaf Ebrahimi  *
4590*62c56f98SSadaf Ebrahimi  * \retval #PSA_OPERATION_INCOMPLETE
4591*62c56f98SSadaf Ebrahimi  *         Operation was interrupted due to the setting of \c
4592*62c56f98SSadaf Ebrahimi  *         psa_interruptible_set_max_ops(). There is still work to be done.
4593*62c56f98SSadaf Ebrahimi  *         Call this function again with the same operation object.
4594*62c56f98SSadaf Ebrahimi  *
4595*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4596*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
4597*62c56f98SSadaf Ebrahimi  *         The calculation was performed successfully, but the passed
4598*62c56f98SSadaf Ebrahimi  *         signature is not a valid signature.
4599*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4600*62c56f98SSadaf Ebrahimi  *         An operation was not previously started on this context via
4601*62c56f98SSadaf Ebrahimi  *         \c psa_verify_hash_start().
4602*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4603*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4604*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4605*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4606*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4607*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4608*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4609*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4610*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4611*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4612*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4613*62c56f98SSadaf Ebrahimi  *         The library has either not been previously initialized by
4614*62c56f98SSadaf Ebrahimi  *         psa_crypto_init() or you did not previously call
4615*62c56f98SSadaf Ebrahimi  *         psa_verify_hash_start() on this object. It is
4616*62c56f98SSadaf Ebrahimi  *         implementation-dependent whether a failure to initialize results in
4617*62c56f98SSadaf Ebrahimi  *         this error code.
4618*62c56f98SSadaf Ebrahimi  */
4619*62c56f98SSadaf Ebrahimi psa_status_t psa_verify_hash_complete(
4620*62c56f98SSadaf Ebrahimi     psa_verify_hash_interruptible_operation_t *operation);
4621*62c56f98SSadaf Ebrahimi 
4622*62c56f98SSadaf Ebrahimi /**
4623*62c56f98SSadaf Ebrahimi  * \brief                     Abort a verify hash operation.
4624*62c56f98SSadaf Ebrahimi  *
4625*62c56f98SSadaf Ebrahimi  * \warning                   This is a beta API, and thus subject to change at
4626*62c56f98SSadaf Ebrahimi  *                            any point. It is not bound by the usual interface
4627*62c56f98SSadaf Ebrahimi  *                            stability promises.
4628*62c56f98SSadaf Ebrahimi  *
4629*62c56f98SSadaf Ebrahimi  * \note                      This function is the only function that clears the
4630*62c56f98SSadaf Ebrahimi  *                            number of ops completed as part of the operation.
4631*62c56f98SSadaf Ebrahimi  *                            Please ensure you copy this value via
4632*62c56f98SSadaf Ebrahimi  *                            \c psa_verify_hash_get_num_ops() if required
4633*62c56f98SSadaf Ebrahimi  *                            before calling.
4634*62c56f98SSadaf Ebrahimi  *
4635*62c56f98SSadaf Ebrahimi  * \note                      Aborting an operation frees all associated
4636*62c56f98SSadaf Ebrahimi  *                            resources except for the operation structure
4637*62c56f98SSadaf Ebrahimi  *                            itself. Once aborted, the operation object can be
4638*62c56f98SSadaf Ebrahimi  *                            reused for another operation by calling \c
4639*62c56f98SSadaf Ebrahimi  *                            psa_verify_hash_start() again.
4640*62c56f98SSadaf Ebrahimi  *
4641*62c56f98SSadaf Ebrahimi  * \note                      You may call this function any time after the
4642*62c56f98SSadaf Ebrahimi  *                            operation object has been initialized.
4643*62c56f98SSadaf Ebrahimi  *                            In particular, calling \c psa_verify_hash_abort()
4644*62c56f98SSadaf Ebrahimi  *                            after the operation has already been terminated by
4645*62c56f98SSadaf Ebrahimi  *                            a call to \c psa_verify_hash_abort() or
4646*62c56f98SSadaf Ebrahimi  *                            psa_verify_hash_complete() is safe.
4647*62c56f98SSadaf Ebrahimi  *
4648*62c56f98SSadaf Ebrahimi  * \param[in,out] operation   Initialized verify hash operation.
4649*62c56f98SSadaf Ebrahimi  *
4650*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
4651*62c56f98SSadaf Ebrahimi  *         The operation was aborted successfully.
4652*62c56f98SSadaf Ebrahimi  *
4653*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4654*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
4655*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
4656*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
4657*62c56f98SSadaf Ebrahimi  *         results in this error code.
4658*62c56f98SSadaf Ebrahimi  */
4659*62c56f98SSadaf Ebrahimi psa_status_t psa_verify_hash_abort(
4660*62c56f98SSadaf Ebrahimi     psa_verify_hash_interruptible_operation_t *operation);
4661*62c56f98SSadaf Ebrahimi 
4662*62c56f98SSadaf Ebrahimi 
4663*62c56f98SSadaf Ebrahimi /**@}*/
4664*62c56f98SSadaf Ebrahimi 
4665*62c56f98SSadaf Ebrahimi #ifdef __cplusplus
4666*62c56f98SSadaf Ebrahimi }
4667*62c56f98SSadaf Ebrahimi #endif
4668*62c56f98SSadaf Ebrahimi 
4669*62c56f98SSadaf Ebrahimi /* The file "crypto_sizes.h" contains definitions for size calculation
4670*62c56f98SSadaf Ebrahimi  * macros whose definitions are implementation-specific. */
4671*62c56f98SSadaf Ebrahimi #include "crypto_sizes.h"
4672*62c56f98SSadaf Ebrahimi 
4673*62c56f98SSadaf Ebrahimi /* The file "crypto_struct.h" contains definitions for
4674*62c56f98SSadaf Ebrahimi  * implementation-specific structs that are declared above. */
4675*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
4676*62c56f98SSadaf Ebrahimi #include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
4677*62c56f98SSadaf Ebrahimi #else
4678*62c56f98SSadaf Ebrahimi #include "crypto_struct.h"
4679*62c56f98SSadaf Ebrahimi #endif
4680*62c56f98SSadaf Ebrahimi 
4681*62c56f98SSadaf Ebrahimi /* The file "crypto_extra.h" contains vendor-specific definitions. This
4682*62c56f98SSadaf Ebrahimi  * can include vendor-defined algorithms, extra functions, etc. */
4683*62c56f98SSadaf Ebrahimi #include "crypto_extra.h"
4684*62c56f98SSadaf Ebrahimi 
4685*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_H */
4686