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